added experimental multi thread support
[NeonServV5.git] / src / ClientSocket.c
index 2897d9d8ca722f6b5dceada83a71e792baf5082d..647a63396bff25ebca2c4f54b1782edc47fea9f0 100644 (file)
@@ -30,11 +30,17 @@ struct socket_list {
     unsigned count;
 };
 
+#ifdef HAVE_THREADS
+static pthread_mutex_t synchronized;
+#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);
+    
     sockets = malloc(sizeof(*sockets));
     if (!sockets)
     {
@@ -46,7 +52,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 +78,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 +222,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 +304,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 +341,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 +355,7 @@ int write_socket_force(struct ClientSocket *client, char* msg, int len) {
         #endif
     }
     client->traffic_out += len;
+    DESYNCHRONIZE(synchronized);
     return 1;
 }
 
@@ -349,6 +369,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);
     fd_set fds;
     struct timeval timeout;
     struct ClientSocket *sock, *next;
@@ -364,7 +386,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) {
+        DEDESYNCHRONIZE(synchronized);
+        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 +424,8 @@ void socket_loop(int timeout_seconds) {
                 sock->flags |= SOCKET_FLAG_QUITTED;
             } else {
                 sock->traffic_in += bytes;
+                is_synchronized = 0;
+                DESYNCHRONIZE(synchronized);
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
                 if(used == sock->bufferpos + 1) {
                     //used all bytes so just reset the bufferpos
@@ -409,6 +436,9 @@ void socket_loop(int timeout_seconds) {
                     }
                     sock->bufferpos -= used;
                 }
+                #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 +450,9 @@ void socket_loop(int timeout_seconds) {
             destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD));
         }
     }
+    if(is_synchronized) {
+        DESYNCHRONIZE(synchronized);
+    }
 }
 
 void