Use bufferevents to handle control socket buffering.

This commit is contained in:
Guus Sliepen 2007-05-19 16:21:52 +00:00
parent 8c6131deda
commit 59108e4e4f

View file

@ -32,41 +32,35 @@ static struct event control_event;
static splay_tree_t *control_socket_tree; static splay_tree_t *control_socket_tree;
extern char *controlsocketname; extern char *controlsocketname;
static void handle_control_data(int fd, short events, void *event) { static void handle_control_data(struct bufferevent *event, void *data) {
char buf[MAXBUFSIZE]; char *line = evbuffer_readline(event->input);
size_t inlen; if(!line)
char *p; return;
inlen = read(fd, buf, sizeof buf); if(!strcasecmp(line, "stop")) {
if(inlen <= 0)
goto close;
p = memchr(buf, '\n', sizeof buf);
if(!p || p - buf + 1 != inlen)
goto malformed;
*p = 0;
if(!strcasecmp(buf, "stop")) {
logger(LOG_NOTICE, _("Got stop command")); logger(LOG_NOTICE, _("Got stop command"));
event_loopexit(NULL); event_loopexit(NULL);
return; return;
} }
malformed:
logger(LOG_DEBUG, _("Malformed control command received")); logger(LOG_DEBUG, _("Malformed control command received"));
close(event->ev_read.ev_fd);
close: splay_delete(control_socket_tree, event);
logger(LOG_DEBUG, _("Closing control socket")); }
event_del(event);
static void handle_control_error(struct bufferevent *event, short what, void *data) {
if(what & EVBUFFER_EOF)
logger(LOG_DEBUG, _("Control socket connection closed by peer"));
else
logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno));
close(event->ev_read.ev_fd);
splay_delete(control_socket_tree, event); splay_delete(control_socket_tree, event);
close(fd);
} }
static void handle_new_control_socket(int fd, short events, void *data) { static void handle_new_control_socket(int fd, short events, void *data) {
int newfd; int newfd;
struct event *ev; struct bufferevent *ev;
newfd = accept(fd, NULL, NULL); newfd = accept(fd, NULL, NULL);
@ -76,9 +70,14 @@ static void handle_new_control_socket(int fd, short events, void *data) {
return; return;
} }
ev = xmalloc(sizeof *ev); ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev); if(!ev) {
event_add(ev, NULL); logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno));
close(newfd);
return;
}
bufferevent_enable(ev, EV_READ);
splay_insert(control_socket_tree, ev); splay_insert(control_socket_tree, ev);
logger(LOG_DEBUG, _("Control socket connection accepted")); logger(LOG_DEBUG, _("Control socket connection accepted"));
@ -139,7 +138,7 @@ bool init_control() {
return false; return false;
} }
control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free); control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL); event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
event_add(&control_event, NULL); event_add(&control_event, NULL);