From 860310caff46499ce449ddb253533cf42cd3caee Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Tue, 26 May 2009 21:05:06 +0200 Subject: [PATCH] Massive restructuring. --- src/hub.c | 14 ++++++++- src/hub.h | 3 ++ src/hubio.c | 8 ++--- src/message.c | 1 + src/netevent.c | 80 +++++++++++++++++++++++++++++--------------------- src/route.c | 5 ++-- src/user.c | 17 +++++++++++ 7 files changed, 86 insertions(+), 42 deletions(-) diff --git a/src/hub.c b/src/hub.c index 8fbe006..05420d9 100644 --- a/src/hub.c +++ b/src/hub.c @@ -514,8 +514,20 @@ struct hub_info* hub_start_service(struct hub_config* config) return 0; } + hub->recvbuf = hub_malloc(MAX_RECV_BUF); + hub->sendbuf = hub_malloc(MAX_SEND_BUF); + if (!hub->recvbuf || !hub->sendbuf) + { + hub_free(hub->recvbuf); + hub_free(hub->sendbuf); + uman_shutdown(hub); + hub_free(hub); + net_close(server_tcp); + return 0; + } + hub->status = hub_status_running; - + g_hub = hub; return hub; } diff --git a/src/hub.h b/src/hub.h index 18c684b..3d3e17a 100644 --- a/src/hub.h +++ b/src/hub.h @@ -96,6 +96,9 @@ struct hub_info struct adc_message* command_banner; /* The default welcome message */ time_t tm_started; int status; + char* recvbuf; /* Global receive buffer */ + char* sendbuf; /* Global send buffer */ + #ifdef SSL_SUPPORT SSL_METHOD* ssl_method; SSL_CTX* ssl_ctx; diff --git a/src/hubio.c b/src/hubio.c index a17847e..5a5be3e 100644 --- a/src/hubio.c +++ b/src/hubio.c @@ -24,8 +24,6 @@ struct hub_recvq* hub_recvq_create() { struct hub_recvq* q = hub_malloc_zero(sizeof(struct hub_recvq)); - q->buf = hub_malloc(MAX_RECV_BUF); - return q; } @@ -55,15 +53,15 @@ size_t hub_recvq_get(struct hub_recvq* q, void* buf, size_t bufsize) size_t hub_recvq_set(struct hub_recvq* q, void* buf, size_t bufsize) { - if (!bufsize) - return 0; - if (q->buf) { hub_free(q->buf); q->buf = 0; q->size = 0; } + + if (!bufsize) + return 0; q->buf = hub_malloc(bufsize); if (!q->buf) diff --git a/src/message.c b/src/message.c index c794856..46d03f1 100644 --- a/src/message.c +++ b/src/message.c @@ -244,6 +244,7 @@ struct adc_message* adc_msg_parse_verify(struct user* u, const char* line, size_ if (command->source && (!u || command->source != u->id.sid)) { + hub_log(log_debug, "Command does not match user's SID (command->source=%d, user->id.sid=%d)", command->source, (u ? u->id.sid : 0)); adc_msg_free(command); return 0; } diff --git a/src/netevent.c b/src/netevent.c index 419fa3b..dc12912 100644 --- a/src/netevent.c +++ b/src/netevent.c @@ -23,11 +23,19 @@ /* FIXME: This should not be needed! */ extern struct hub_info* g_hub; +#define DEBUG_SENDQ 1 + int net_user_send(void* ptr, const void* buf, size_t len) { struct user* user = (struct user*) ptr; int ret = net_send(user->net.sd, buf, len, UHUB_SEND_SIGNAL); - +#ifdef DEBUG_SENDQ + printf("net_user_send: sd=%d, %d/%d bytes\n", user->net.sd, ret, (int) len); + if (ret == -1) + { + printf(" errno: %d - %s\n", errno, strerror(errno)); + } +#endif if (ret > 0) { user->net.tm_last_write = time(NULL); @@ -47,10 +55,10 @@ int net_user_send(void* ptr, const void* buf, size_t len) int net_user_recv(void* ptr, void* buf, size_t len) { struct user* user = (struct user*) ptr; - int ret = net_recv(user->net.sd, buf, len, 0); - - hub_log(log_debug, "net_user_recv: sd=%d, len=%d/%d", user->net.sd, ret, (int) len); - + int ret = net_recv(user->net.sd, buf, len, 0); +/* + hub_log(log_trace, "net_user_recv: sd=%d, len=%d/%d", user->net.sd, ret, (int) len); +*/ if (ret > 0) { user->net.tm_last_read = time(NULL); @@ -63,34 +71,18 @@ int net_user_recv(void* ptr, void* buf, size_t len) { printf(" errno: %d - %s\n", errno, strerror(errno)); } + + if (ret > 0) + { + char* data = hub_malloc_zero(ret + 1); + memcpy(data, buf, ret); + printf("RECV: \"%s\"\n", data); + hub_free(data); + } #endif return ret; } -/** - * @param buf buffer to extract line from - * @param bufsize size of buffer - * @param offset (in/out) offset into buffer - * @param len (out) length of the line returned - * @param max_size maximum length of line, if line is longer, it is discarded. - * - * @return line from buffer, or NULL if no line can be returned. - */ -static char* extract_line(char* buf, size_t bufsize, size_t* offset, size_t* len, size_t max_size) -{ - size_t x = *offset; - char* pos = memchr(&buf[x], '\n', (bufsize - x)); - if (pos) - { - *len = &pos[0] - &buf[x]; - pos[0] = '\0'; - pos = &buf[x]; - (*offset) += (*len + 1); - } - return pos; -} - - void net_on_read(int fd, short ev, void *arg) { static char buf[MAX_RECV_BUF]; @@ -141,18 +133,38 @@ void net_on_read(int fd, short ev, void *arg) { size_t offset = 0; size_t length; - char* line = 0; - - while ((line = extract_line(buf, buf_size, &offset, &length, g_hub->config->max_recv_buffer))) + char* start = buf; + char* pos = 0; + while ((pos = memchr(start, '\n', (buf_size - offset)))) { - puts(line); + char* line = start; + length = pos - start; + pos[0] = '\0'; + +#ifdef DEBUG_SENDQ + printf("PROC: \"%s\" (%d)\n", line, (int) length); +#endif + if (hub_handle_message(g_hub, user, line, length) == -1) { flag_close = quit_protocol_error; break; } + + start = pos; + start++; + offset += length; } - hub_recvq_set(q, buf+offset, buf_size); + + if (start < buf + buf_size) + { + hub_recvq_set(q, buf+offset, buf_size); + } + else + { + hub_recvq_set(q, 0, 0); + } + } } diff --git a/src/route.c b/src/route.c index 2f2a404..616bee6 100644 --- a/src/route.c +++ b/src/route.c @@ -18,6 +18,7 @@ */ #include "uhub.h" +#define DEBUG_SENDQ 1 int route_message(struct hub_info* hub, struct user* u, struct adc_message* msg) { @@ -96,9 +97,9 @@ static int check_send_queue(struct user* user, struct adc_message* msg) int route_to_user(struct hub_info* hub, struct user* user, struct adc_message* msg) { -#if LOG_SEND_MESSAGES_WHEN_ROUTED +#ifdef DEBUG_SENDQ char* data = strndup(msg->cache, msg->length-1); - hub_log(log_protocol, "send %s: %s", sid_to_string(user->sid), data); + hub_log(log_protocol, "send %s: \"%s\"", sid_to_string(user->id.sid), data); free(data); #endif diff --git a/src/user.c b/src/user.c index b48f32c..3715e86 100644 --- a/src/user.c +++ b/src/user.c @@ -19,6 +19,20 @@ #include "uhub.h" +static const char* user_log_str(struct user* user) +{ + static char buf[128]; + if (user) + { + snprintf(buf, 128, "user={ %p, \"%s\", %s/%s}", user, user->id.nick, sid_to_string(user->id.sid), user->id.cid); + } + else + { + snprintf(buf, 128, "user={ %p }", user); + } + return buf; +} + struct user* user_create(struct hub_info* hub, int sd) { struct user* user = NULL; @@ -319,6 +333,7 @@ int user_is_registered(struct user* user) void user_want_write(struct user* user) { + hub_log(log_trace, "user_want_write: %s", user_log_str(user)); if (user && user->net.ev_write) { event_add(user->net.ev_write, 0); @@ -327,6 +342,8 @@ void user_want_write(struct user* user) void user_want_read(struct user* user, int timeout_s) { + hub_log(log_trace, "user_want_read: %s", user_log_str(user)); + struct timeval timeout = { timeout_s, 0 }; if (user && user->net.ev_read) {