Created
March 27, 2013 13:16
-
-
Save minaguib/5254078 to your computer and use it in GitHub Desktop.
libevent test - missing error callback after read high watermark reached - with line draining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <event.h> | |
#include <event2/bufferevent.h> | |
#include <event2/listener.h> | |
#include <event2/buffer.h> | |
#define PORT 5001 | |
#define BUFFLEN 4 | |
static void handle_client_buffer_in(struct bufferevent *bev, void *hint) { | |
struct evbuffer *in = bufferevent_get_input(bev); | |
int num_lines = 0; | |
char *line; | |
while ((line = evbuffer_readln(in, NULL, EVBUFFER_EOL_LF)) != NULL) { | |
num_lines++; | |
free(line); | |
} | |
printf("Got data from client - drained %d line(s)\n", num_lines); | |
} | |
static void handle_client_buffer_evt(struct bufferevent *bev, short revents, void *hint) { | |
if (revents & BEV_EVENT_ERROR) { | |
printf("Client error: %s\n", strerror(errno)); | |
} | |
else if (revents & BEV_EVENT_TIMEOUT) { | |
printf("Client timed out\n"); | |
} | |
else if (revents & BEV_EVENT_EOF) { | |
printf("Client disconnected\n"); | |
} | |
else { | |
printf("Spurious event from client: %d", revents); | |
} | |
} | |
static | |
void | |
handle_server_newclient(struct evconnlistener *listener, evutil_socket_t sock, struct sockaddr *addr, int len, void *ptr) { | |
struct bufferevent * bufferevent; | |
printf("New client\n"); | |
bufferevent = bufferevent_socket_new( evconnlistener_get_base(listener), sock, BEV_OPT_CLOSE_ON_FREE); | |
bufferevent_setcb(bufferevent, handle_client_buffer_in, NULL, handle_client_buffer_evt, NULL); | |
bufferevent_setwatermark(bufferevent, EV_READ, 0, BUFFLEN); | |
bufferevent_enable(bufferevent, EV_READ); | |
} | |
int main(int argc, char ** argv) { | |
struct event_base *eb; | |
struct sockaddr_in sa; | |
eb = event_base_new(); | |
memset(&sa, 0, sizeof(sa)); | |
sa.sin_family = AF_INET; | |
sa.sin_addr.s_addr = htonl(INADDR_ANY); | |
sa.sin_port = htons(PORT); | |
evconnlistener_new_bind( | |
eb, | |
handle_server_newclient, | |
NULL, | |
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, | |
-1, | |
(struct sockaddr *)&sa, | |
sizeof(sa) | |
); | |
event_base_dispatch(eb); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment