X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=27896187b19385b48d17fbb1af3145c9c828ba54;hb=f90d21daf31f8d69e24406678be696afa8cae962;hp=2897d9d8ca722f6b5dceada83a71e792baf5082d;hpb=bb5692b9cbff069abbf9573c81e86c3cd2061ceb;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 2897d9d..2789618 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -30,11 +30,19 @@ struct socket_list { unsigned count; }; +#ifdef HAVE_THREADS +static pthread_mutex_t synchronized; +static pthread_mutex_t synchronized_recv; +#endif + //the magic list :P static struct socket_list *sockets = NULL; static char buffer[BUF_SIZ]; -static void init_sockets() { +void init_sockets() { + THREAD_MUTEX_INIT(synchronized); + THREAD_MUTEX_INIT(synchronized_recv); + sockets = malloc(sizeof(*sockets)); if (!sockets) { @@ -46,7 +54,6 @@ static void init_sockets() { } struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) { - if(sockets == NULL) init_sockets(); struct ClientSocket *client = malloc(sizeof(*client)); if (!client) { @@ -73,13 +80,24 @@ struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pas client->whoqueue_last = NULL; client->handleinfo_first = NULL; client->handleinfo_last = NULL; + SYNCHRONIZE(synchronized); client->next = sockets->data; sockets->data = client; + DESYNCHRONIZE(synchronized); return client; } -#ifndef WIN32 +static int _connect_socket(struct ClientSocket *client); + int connect_socket(struct ClientSocket *client) { + SYNCHRONIZE(synchronized); + int ret = _connect_socket(client); + DESYNCHRONIZE(synchronized); + return ret; +} + +#ifndef WIN32 +static int _connect_socket(struct ClientSocket *client) { if((client->flags & SOCKET_FLAG_CONNECTED)) return 1; int sock; @@ -206,7 +224,7 @@ int connect_socket(struct ClientSocket *client) { return 1; } #else -int connect_socket(struct ClientSocket *client) { +static int connect_socket(struct ClientSocket *client) { if((client->flags & SOCKET_FLAG_CONNECTED)) return 1; struct hostent *host; struct sockaddr_in addr; @@ -288,6 +306,7 @@ int disconnect_socket(struct ClientSocket *client) { } static void destroy_socket(struct ClientSocket *client, int free_socket) { + SYNCHRONIZE(synchronized); if((client->flags & SOCKET_FLAG_CONNECTED)) { close(client->sock); bot_disconnect(client); @@ -324,9 +343,11 @@ static void destroy_socket(struct ClientSocket *client, int free_socket) { client->flags &= ~SOCKET_FLAG_FAST_JUMP; connect_socket(client); } + DESYNCHRONIZE(synchronized); } int write_socket_force(struct ClientSocket *client, char* msg, int len) { + SYNCHRONIZE(synchronized); printf("[send %d] %s", len, msg); if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) { #ifdef WIN32 @@ -336,6 +357,7 @@ int write_socket_force(struct ClientSocket *client, char* msg, int len) { #endif } client->traffic_out += len; + DESYNCHRONIZE(synchronized); return 1; } @@ -349,6 +371,8 @@ int write_socket(struct ClientSocket *client, char* msg, int len) { void socket_loop(int timeout_seconds) { if(sockets == NULL) return; + int is_synchronized = 1; + SYNCHRONIZE(synchronized_recv); fd_set fds; struct timeval timeout; struct ClientSocket *sock, *next; @@ -364,7 +388,10 @@ void socket_loop(int timeout_seconds) { timeout.tv_sec = timeout_seconds; timeout.tv_usec = 0; ret = select(ret + 1, &fds, NULL, NULL, &timeout); - if(ret == 0) return; + if(ret == 0) { + DESYNCHRONIZE(synchronized_recv); + return; + } for (sock = sockets->data; sock; sock = next) { next = sock->next; if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_QUITTED)) == SOCKET_FLAG_CONNECTED && FD_ISSET(sock->sock, &fds)) { @@ -399,6 +426,28 @@ void socket_loop(int timeout_seconds) { sock->flags |= SOCKET_FLAG_QUITTED; } else { sock->traffic_in += bytes; + #ifdef HAVE_THREADS + char linesbuf[BUF_SIZ*2]; + strcpy(linesbuf, sock->buffer); + int used = 0; + for(i = 0; i < sock->bufferpos; i++) { + if(sock->buffer[i] == '\n') { + used = i+1; + } + } + if(used == sock->bufferpos + 1) { + //used all bytes so just reset the bufferpos + sock->bufferpos = 0; + } else { + for(i = 0; i < sock->bufferpos - used; i++) { + sock->buffer[i] = sock->buffer[i+used]; + } + sock->bufferpos -= used; + } + is_synchronized = 0; + DESYNCHRONIZE(synchronized_recv); + parse_lines(sock, linesbuf, used); + #else int used = parse_lines(sock, sock->buffer, sock->bufferpos); if(used == sock->bufferpos + 1) { //used all bytes so just reset the bufferpos @@ -409,6 +458,12 @@ void socket_loop(int timeout_seconds) { } sock->bufferpos -= used; } + is_synchronized = 0; + DESYNCHRONIZE(synchronized_recv); + #endif + #ifdef HAVE_THREADS + FD_ZERO(&fds); //zero out all other pending sockets here (we have other threads receiving from them) + #endif } } else if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT)) == SOCKET_FLAG_RECONNECT) { if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) { @@ -420,6 +475,9 @@ void socket_loop(int timeout_seconds) { destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD)); } } + if(is_synchronized) { + DESYNCHRONIZE(synchronized_recv); + } } void