[ create a new paste ] login | about

Link: http://codepad.org/9gkA4qiG    [ raw code | fork ]

C, pasted on Jun 8:
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <uv.h>

#define KEEPALIVE_INTERVAL 3600000

static int timer_counter;

static void
keepalive_cb(uv_timer_t *handle)
{

      /* Intentionally empty. */
}

static void
timer2_cb(uv_timer_t *handle)
{
	fprintf(stderr, "timer cb 2\n");

	++timer_counter;
}

static void
timer_cb(uv_timer_t *handle)
{
	fprintf(stderr, "timer cb 1\n");

	++timer_counter;
}

static void
catch(int signo)
{
	/*
	 * Not safe to call from a signal handler, but
	 * it's fine for this example.
	 */
	fprintf(stderr, "caught signal %d; starting timers...\n", signo);

	uv_timer_t timer = {0};
	uv_timer_init(uv_default_loop(), &timer);
	uv_timer_start(&timer, timer_cb, 0, 0);

	uv_timer_t timer2 = {0};
	uv_timer_init(uv_default_loop(), &timer2);
	uv_timer_start(&timer2, timer2_cb, 5000, 0);
}

int
main(int argc, char *argv[])
{
	fprintf(stderr, "Timer before: %d\n", timer_counter);

	struct sigaction sa;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = catch;
	if (sigaction(SIGINT, &sa, NULL) < 0) {
		fprintf(stderr, "Failed to register signal handler for SIGINT.\n");
		return 1;
	}

	uv_timer_t keepalive_timer = {0};
	uv_timer_init(uv_default_loop(), &keepalive_timer);
	uv_timer_start(&keepalive_timer, keepalive_cb,
	  KEEPALIVE_INTERVAL, KEEPALIVE_INTERVAL);

	uv_run(uv_default_loop(), UV_RUN_DEFAULT);

	return 0;
}


Create a new paste based on this one


Comments: