Massive restructuring.

This commit is contained in:
Jan Vidar Krey 2009-05-26 21:05:06 +02:00
parent 8167d79f5a
commit 860310caff
7 changed files with 86 additions and 42 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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)

View File

@ -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;
}

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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)
{