Пример #1
0
int
main(int argc, char **argv)
{
	struct event *signal_int;
	struct event_base* base;
#ifdef _WIN32
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD(2, 2);

	(void) WSAStartup(wVersionRequested, &wsaData);
#endif

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());

	event_add(signal_int, NULL);

	event_base_dispatch(base);
	event_free(signal_int);
	event_base_free(base);

	return (0);
}
Пример #2
0
int main(){
	//делаем соект
	static evutil_socket_t master_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(-1==master_socket)
		std::cout << "socket():\n"<<errno<<std::endl;
	else{
		//делаем структуру
		struct sockaddr_in sa;
		sa.sin_family=AF_INET;
		sa.sin_port=htons(12345);
		sa.sin_addr.s_addr=htonl(INADDR_ANY);
		//биндим
		if(bind(master_socket,(sockaddr*)&sa,sizeof(sa))==-1)
			std::cout << "bind:\n"<<errno<<std::endl;
		else if(listen(master_socket,SOMAXCONN)==-1)
			std::cout << "listen:\n"<<errno<<std::endl;
		else{
			//повторное использование мастер сокета
			int optval = 1;
			setsockopt(master_socket,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));
			//не  блокирующий
			set_non_block(master_socket);
			                                                               
			//делаем ядро
			struct event_base * base=event_base_new();
	
			//регистрируем события
			struct event * evn=event_new(base,master_socket,EV_READ|EV_PERSIST,cb_master,event_self_cbarg());
			event_add(evn,NULL);

			//запускаем цикл
			event_base_dispatch(base);
			
			//очищаем
			event_base_free(base);
			event_free(evn);
			//закрываем
			shutdown(master_socket,SHUT_RDWR);
			close(master_socket);
	
		}
	}
	return 0;
}
Пример #3
0
int
main(int argc, char **argv)
{
	struct event_base *base;
	struct event_config *cfg;
	struct event *ev;
	const char *test = "test string";
	evutil_socket_t pair[2];

	/* Initialize the library and check if the backend
	   supports EV_FEATURE_EARLY_CLOSE
	*/
	cfg = event_config_new();
	event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
	base = event_base_new_with_config(cfg);
	event_config_free(cfg);
	if (!base) {
		/* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
		return 0;
	}

	/* Create a pair of sockets */
	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
		return (1);

	/* Send some data on socket 0 and immediately close it */
	if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
		return (1);
	shutdown(pair[0], SHUT_WR);

	/* Dispatch */
	ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
	event_add(ev, &timeout);
	event_base_dispatch(base);

	/* Finalize library */
	event_base_free(base);
	return 0;
}
Пример #4
0
int main(int argc, char **argv)
{
    struct context ctx = {0};
    struct event *signal_int;
    struct evhttp_bound_socket *handle;
    char listen_addr[256];

    if (argc != 2)
    {
        printf("usage: statsrv STATS\n");
        return -1;
    }

    if (stats_cl_create(&ctx.cl) != S_OK)
    {
        printf("Failed to allocate stats counter list\n");
        return ERROR_FAIL;
    }

    if (stats_sample_create(&ctx.sample) != S_OK)
    {
        printf("Failed to allocate stats sample\n");
        return ERROR_FAIL;
    }

    if (stats_sample_create(&ctx.prev_sample) != S_OK)
    {
        printf("Failed to allocate stats sample\n");
        return ERROR_FAIL;
    }

    ctx.stats = open_stats(argv[1]);
    if (!ctx.stats)
    {
        printf("Failed to open stats %s\n", argv[1]);
        return ERROR_FAIL;
    }

    ctx.base = event_base_new();
    if (!ctx.base)
    {
        printf("Could not allocate event base\n");
        return 1;
    }

    /* add a handler for SIGINT */
    signal_int = evsignal_new(ctx.base, SIGINT, sigint_cb, event_self_cbarg());
    evsignal_add(signal_int,0);

    /* Create a new evhttp object to handle requests. */
    ctx.http = evhttp_new(ctx.base);
    if (!ctx.http)
    {
        printf("could not create evhttp.\n");
        return ERROR_FAIL;
    }

    evhttp_set_gencb(ctx.http, http_request_cb, &ctx);

    /* Now we tell the evhttp what port to listen on */
    handle = evhttp_bind_socket_with_handle(ctx.http, "0.0.0.0", 8080);
    if (!handle)
    {
        printf("couldn't bind to http port %d.\n", (int)8080);
        return ERROR_FAIL;
    }

    if (http_get_address(handle, listen_addr, sizeof(listen_addr)) == S_OK)
        printf("http: listening at %s\n", listen_addr);

    event_base_dispatch(ctx.base);

    event_free(signal_int);

#if 0
    start_time = current_time();

    while (!signal_received)
    {
        err = stats_get_sample(stats,cl,sample);
        if (err != S_OK)
        {
            printf("Error %08x (%s) getting sample\n",err,error_message(err));
        }

        clear();

        sample_time = TIME_DELTA_TO_NANOS(start_time, sample->sample_time);

        mvprintw(0,0,"SAMPLE @ %6lld.%03llds  SEQ:%d\n", sample_time / 1000000000ll, (sample->sample_time % 1000000000ll) / 1000000ll, sample->sample_seq_no);

        n = 1;
        maxy = getmaxy(stdscr);
        col = 0;
        for (j = 0; j < cl->cl_count; j++)
        {
            counter_get_key(cl->cl_ctr[j],counter_name,MAX_COUNTER_KEY_LENGTH+1);
            mvprintw(n,col+0,"%s", counter_name);
            mvprintw(n,col+29,"%15lld", stats_sample_get_value(sample,j));
            mvprintw(n,col+46,"%15lld", stats_sample_get_delta(sample,prev_sample,j));
            if (++n == maxy)
            {
                col += 66;
                n = 1;
            }
        }
        refresh();

        tmp = prev_sample;
        prev_sample = sample;
        sample = tmp;

        FD_ZERO(&fds);
        FD_SET(0,&fds);

        now = current_time();

        tv.tv_sec = 0;
        tv.tv_usec = 1000000 - (now % 1000000000) / 1000;

        ret = select(1, &fds, NULL, NULL, &tv);
        if (ret == 1)
        {
            ch = getch();
            if (ch == 'c' || ch == 'C')
            {
                stats_reset_counters(stats);
            }
        }
    }

    close_screen();
#endif

    if (ctx.base)
        event_base_free(ctx.base);

    if (ctx.http)
        evhttp_free(ctx.http);

    if (ctx.stats)
    {
        stats_close(ctx.stats);
        stats_free(ctx.stats);
    }

    if (ctx.cl)
        stats_cl_free(ctx.cl);

    if (ctx.sample)
        stats_sample_free(ctx.sample);

    if (ctx.prev_sample)
        stats_sample_free(ctx.prev_sample);

    return 0;
}
Пример #5
0
int
main(int argc, char **argv)
{
	struct event *evfifo;
	struct event_base* base;
#ifdef _WIN32
	HANDLE socket;
	/* Open a file. */
	socket = CreateFileA("test.txt",	/* open File */
			GENERIC_READ,		/* open for reading */
			0,			/* do not share */
			NULL,			/* no security */
			OPEN_EXISTING,		/* existing file only */
			FILE_ATTRIBUTE_NORMAL,	/* normal file */
			NULL);			/* no attr. template */

	if (socket == INVALID_HANDLE_VALUE)
		return 1;

#else
	struct event *signal_int;
	struct stat st;
	const char *fifo = "event.fifo";
	int socket;

	if (lstat(fifo, &st) == 0) {
		if ((st.st_mode & S_IFMT) == S_IFREG) {
			errno = EEXIST;
			perror("lstat");
			exit(1);
		}
	}

	unlink(fifo);
	if (mkfifo(fifo, 0600) == -1) {
		perror("mkfifo");
		exit(1);
	}

	socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);

	if (socket == -1) {
		perror("open");
		exit(1);
	}

	fprintf(stderr, "Write data to %s\n", fifo);
#endif
	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
#ifdef _WIN32
	evfifo = event_new(base, (evutil_socket_t)socket, EV_READ|EV_PERSIST, fifo_read,
                           event_self_cbarg());
#else
	/* catch SIGINT so that event.fifo can be cleaned up */
	//如果收到SIGINT,则退出eventloop循环
	signal_int = evsignal_new(base, SIGINT, signal_cb, base);
	event_add(signal_int, NULL);//将事件加入

	//处理socket的收事件
	evfifo = event_new(base, socket, EV_READ|EV_PERSIST, fifo_read,
                           event_self_cbarg());
#endif

	/* Add it to the active events, without a timeout */
	event_add(evfifo, NULL);//将此读事件加入

	event_base_dispatch(base);//事件循环
	//开始销毁
	event_base_free(base);
#ifdef _WIN32
	CloseHandle(socket);
#else
	close(socket);
	unlink(fifo);
#endif
	libevent_global_shutdown();
	return (0);
}
Пример #6
0
void cb_master(evutil_socket_t fd, short what, void *ptr){
	struct event * evn=(event *)ptr;
	evutil_socket_t slave_socket=accept(fd,NULL,NULL);
	if(slave_socket==-1)
		std::cout<<"accept:\n"<<errno<<std::endl;
	else{
		//регистрируем события
		struct event * evn_new=event_new(evn->ev_base,slave_socket,EV_READ|EV_PERSIST,cb_slave,event_self_cbarg());
		event_add(evn_new,NULL);
		std::cout<<"Hi\n";
	}
}