X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=647a63396bff25ebca2c4f54b1782edc47fea9f0;hb=55831bf424312a6908ca07a904f288fba0919a9a;hp=ec8432aaa48e910b57553f6dff97a0afbcbc77bb;hpb=44436a96352a38631237978c9fd431cef3d85cfb;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index ec8432a..647a633 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -1,5 +1,5 @@ /* ClientSocket.c - NeonServ v5.3 - * Copyright (C) 2011 Philipp Kreil (pk910) + * 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,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; @@ -269,34 +285,9 @@ 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); - } - if(client->flags & SOCKET_FLAG_HAVE_SSL) - ssl_disconnect(client); - struct ClientSocket *sock, *last_sock = NULL; - for (sock = sockets->data; sock; sock = sock->next) { - if(sock == client) { - if(last_sock) - last_sock->next = sock->next; - else - sockets->data = sock->next; - sockets->count--; - } else - last_sock = sock; } - if(client->queue) - queue_destroy(client); - if(client->whoqueue_first) - clear_whoqueue(client); - if(client->handleinfo_first) - clear_handleinfoqueue(client); - free(client->host); - if(client->bind) - free(client->bind); - if(client->pass) - free(client->pass); - free(client); + client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT); + client->flags |= SOCKET_FLAG_QUITTED | SOCKET_FLAG_DEAD; return 1; } @@ -306,6 +297,15 @@ int disconnect_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); + } + client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT); + client->flags |= SOCKET_FLAG_QUITTED; + return 1; +} + +static void destroy_socket(struct ClientSocket *client, int free_socket) { + SYNCHRONIZE(synchronized); + if((client->flags & SOCKET_FLAG_CONNECTED)) { close(client->sock); bot_disconnect(client); } @@ -317,11 +317,35 @@ int disconnect_socket(struct ClientSocket *client) { clear_whoqueue(client); if(client->handleinfo_first) clear_handleinfoqueue(client); - client->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT); - return 1; + client->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY | SOCKET_FLAG_HAVE_SSL); + if(free_socket) { + struct ClientSocket *sock, *last_sock = NULL; + for (sock = sockets->data; sock; sock = sock->next) { + if(sock == client) { + if(last_sock) + last_sock->next = sock->next; + else + sockets->data = sock->next; + sockets->count--; + break; + } else + last_sock = sock; + } + free(client->host); + if(client->bind) + free(client->bind); + if(client->pass) + free(client->pass); + 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) { + SYNCHRONIZE(synchronized); printf("[send %d] %s", len, msg); if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) { #ifdef WIN32 @@ -331,6 +355,7 @@ int write_socket_force(struct ClientSocket *client, char* msg, int len) { #endif } client->traffic_out += len; + DESYNCHRONIZE(synchronized); return 1; } @@ -344,9 +369,11 @@ 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; + struct ClientSocket *sock, *next; int ret = 0, bytes, i; FD_ZERO(&fds); @@ -359,9 +386,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) { + 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)) { if(sock->bufferpos != 0) { if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) { #ifdef WIN32 @@ -390,19 +421,11 @@ void socket_loop(int timeout_seconds) { } if(bytes <= 0) { //error - sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY); - bot_disconnect(sock); - if(sock->queue) - queue_destroy(sock); - close(sock->sock); - if(sock->flags & SOCKET_FLAG_HAVE_SSL) - ssl_disconnect(sock); - if(sock->whoqueue_first) - clear_whoqueue(sock); - if(sock->handleinfo_first) - clear_handleinfoqueue(sock); + 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 @@ -413,12 +436,22 @@ 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) && (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); } } + if((sock->flags & SOCKET_FLAG_QUITTED)) { + sock->flags &= ~SOCKET_FLAG_QUITTED; + destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD)); + } + } + if(is_synchronized) { + DESYNCHRONIZE(synchronized); } }