X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FClientSocket.c;h=d1f80ff8773358a09a9c557b216afbea9c8490c8;hp=16ad26d4b9a0d7979f3678826bfc239fe84684dd;hb=406c308308e4d131475a692cd425cb156e0776f1;hpb=37a4c120f59e3a65d401d23e9469958e4181fcf4 diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 16ad26d..d1f80ff 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -1,5 +1,5 @@ -/* ClientSocket.c - NeonServ v5.3 - * Copyright (C) 2011 Philipp Kreil (pk910) +/* ClientSocket.c - NeonServ v5.4 + * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,11 +30,25 @@ struct socket_list { unsigned count; }; +#ifdef HAVE_THREADS +static pthread_mutex_t synchronized; +static pthread_mutex_t synchronized_recv; + +struct ParseOrder { + unsigned int tid; + struct ParseOrder *next; +}; +struct ParseOrder *parse_order = NULL; +#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 +60,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) { @@ -61,6 +74,7 @@ struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pas client->ident = strdup(ident); client->realname = strdup(realname); client->user = NULL; + client->network_name = NULL; client->flags = 0; client->bufferpos = 0; client->traffic_in = 0; @@ -73,13 +87,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 +231,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; @@ -269,8 +294,6 @@ int close_socket(struct ClientSocket *client) { char quitbuf[MAXLEN]; int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); write_socket_force(client, quitbuf, quitlen); - close(client->sock); - bot_disconnect(client); } client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT); client->flags |= SOCKET_FLAG_QUITTED | SOCKET_FLAG_DEAD; @@ -290,6 +313,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); @@ -321,15 +345,23 @@ static void destroy_socket(struct ClientSocket *client, int free_socket) { free(client->bind); if(client->pass) free(client->pass); + if(client->network_name) + free(client->network_name); free(client); } else if(client->flags & SOCKET_FLAG_FAST_JUMP) { client->flags &= ~SOCKET_FLAG_FAST_JUMP; connect_socket(client); } + DESYNCHRONIZE(synchronized); } int write_socket_force(struct ClientSocket *client, char* msg, int len) { - printf("[send %d] %s", len, msg); + SYNCHRONIZE(synchronized); + #ifdef HAVE_THREADS + putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg); + #else + putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg); + #endif if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) { #ifdef WIN32 send(client->sock, msg, len, 0); @@ -338,6 +370,7 @@ int write_socket_force(struct ClientSocket *client, char* msg, int len) { #endif } client->traffic_out += len; + DESYNCHRONIZE(synchronized); return 1; } @@ -349,11 +382,56 @@ int write_socket(struct ClientSocket *client, char* msg, int len) { return write_socket_force(client, msg, len); } -void socket_loop(int timeout_seconds) { - if(sockets == NULL) return; +#if HAVE_THREADS +static void clientsocket_start_of_recv(unsigned int tid) { + SYNCHRONIZE(whohandler_sync); + struct ParseOrder *entry, *last; + for(last = parse_order; last; last = last->next) { + if(last->next == NULL) + break; + } + entry = malloc(sizeof(*entry)); + entry->tid = tid; + entry->next = NULL; + if(last) + last->next = entry; + else + parse_order = entry; + DESYNCHRONIZE(whohandler_sync); +} + +static void clientsocket_end_of_recv(unsigned int tid) { + SYNCHRONIZE(whohandler_sync); + struct ParseOrder *entry, *last = NULL; + for(entry = parse_order; entry; entry = entry->next) { + if(entry->tid == tid) { + if(last) + last->next = entry->next; + else + parse_order = entry->next; + free(entry); + break; + } else + last = entry; + } + DESYNCHRONIZE(whohandler_sync); +} + +int clientsocket_parseorder_top(unsigned int tid) { + if(parse_order && parse_order->tid == tid) + return 1; + else + return 0; +} +#endif + +int socket_loop(int timeout_seconds) { + if(sockets == NULL) return 0; + int is_synchronized = 1; + SYNCHRONIZE(synchronized_recv); fd_set fds; struct timeval timeout; - struct ClientSocket *sock; + struct ClientSocket *sock, *next; int ret = 0, bytes, i; FD_ZERO(&fds); @@ -366,9 +444,13 @@ 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; - for (sock = sockets->data; sock; sock = sock->next) { - if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) { + if(ret == 0) { + DESYNCHRONIZE(synchronized_recv); + return 1; + } + 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)) { if(sock->bufferpos != 0) { if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) { #ifdef WIN32 @@ -400,6 +482,31 @@ 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; + unsigned int tid = (unsigned int) pthread_self_tid(); + clientsocket_start_of_recv(tid); + DESYNCHRONIZE(synchronized_recv); + parse_lines(sock, linesbuf, used); + clientsocket_end_of_recv(tid); + #else int used = parse_lines(sock, sock->buffer, sock->bufferpos); if(used == sock->bufferpos + 1) { //used all bytes so just reset the bufferpos @@ -410,8 +517,14 @@ 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) && (sock->flags & SOCKET_FLAG_RECONNECT)) { + } else if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT)) == SOCKET_FLAG_RECONNECT) { if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) { connect_socket(sock); } @@ -421,6 +534,10 @@ void socket_loop(int timeout_seconds) { destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD)); } } + if(is_synchronized) { + DESYNCHRONIZE(synchronized_recv); + } + return (ret + 1); } void @@ -466,6 +583,8 @@ void free_sockets() { free(client->bind); if(client->pass) free(client->pass); + if(client->network_name) + free(client->network_name); free(client); } free(sockets);