Skip to content

Instantly share code, notes, and snippets.

@minaguib
Created March 27, 2013 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minaguib/5250939 to your computer and use it in GitHub Desktop.
Save minaguib/5250939 to your computer and use it in GitHub Desktop.
libevent test - missing error callback after read high watermark reached
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <event.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#define PORT 5001
#define BUFFLEN 4
#define NODRAIN
static void handle_client_buffer_in(struct bufferevent *bev, void *hint) {
#ifdef NODRAIN
printf("Got data from client - ignoring it\n");
#else
printf("Got data from client - draining it\n");
struct evbuffer *in = bufferevent_get_input(bev);
evbuffer_drain(in, evbuffer_get_length(in));
#endif
}
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