[ create a new paste ] login | about

Link: http://codepad.org/L9krv6od    [ 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
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);
}

static void
connection_cb(uv_stream_t *server, int status)
{

	fprintf(stderr, "Hit connection cb.\n");
}

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_pipe_t server = {0};
        uv_pipe_init(uv_default_loop(), &server, 0);

        if (uv_pipe_bind(&server, "test.sock") < 0) {
                fprintf(stderr, "error - failed to bind socket.\n");
                return -1;
        }

        if (uv_listen((uv_stream_t *)&server, 128, connection_cb) < 0) {
                fprintf(stderr, "error - failed to listen to heartbeat socket.\n");
                return -1;
        }

	uv_run(uv_default_loop(), UV_RUN_DEFAULT);

	return 0;
}


Create a new paste based on this one


Comments: