X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=eb11d88fc131558ff1bb698f883c37bd91144188;hb=a40e0df080a82db2b1150492a8952cdfe3559185;hp=5632de65e2fc07775d4ad8ab78ceb69fa257b036;hpb=c99b8d5951fe6429844c8cb5ff94213bb625ad95;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 5632de6..eb11d88 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -1,4 +1,4 @@ -/* ClientSocket.c - NeonServ v5.5 +/* ClientSocket.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - +#include "main.h" #include "ClientSocket.h" #include "IRCParser.h" #include "UserNode.h" @@ -24,6 +24,7 @@ #include "ConfigParser.h" #include "version.h" #include "IOHandler.h" +#include "IRCEvents.h" struct socket_list { struct ClientSocket *data; @@ -43,7 +44,6 @@ struct ParseOrder *parse_order = NULL; //the magic list :P static struct socket_list *sockets = NULL; -static char buffer[BUF_SIZ]; static IOHANDLER_CALLBACK(socket_callback); @@ -62,6 +62,8 @@ void init_sockets() { } struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) { + if(!sockets) + init_sockets(); struct ClientSocket *client = malloc(sizeof(*client)); if (!client) { return NULL; @@ -77,7 +79,6 @@ struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pas client->user = NULL; client->network_name = NULL; client->flags = 0; - client->bufferpos = 0; client->traffic_in = 0; client->traffic_out = 0; client->connection_time = 0; @@ -103,9 +104,7 @@ void connect_socket(struct ClientSocket *client) { int close_socket(struct ClientSocket *client) { if(client == NULL) return 0; if((client->flags & SOCKET_FLAG_CONNECTED)) { - char quitbuf[MAXLEN]; - int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); - iohandler_send(client, quitbuf, quitlen); + iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); bot_disconnect(client); @@ -138,6 +137,7 @@ int destroy_socket(struct ClientSocket *client) { } else last_sock = sock; } + event_freeclient(client); free(client->host); if(client->bind) free(client->bind); @@ -152,7 +152,7 @@ int destroy_socket(struct ClientSocket *client) { return 1; } -static int write_socket_force(struct ClientSocket *client, char* msg, int len) { +int write_socket_force(struct ClientSocket *client, char* msg, int len) { if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0; SYNCHRONIZE(synchronized); #ifdef HAVE_THREADS @@ -160,10 +160,10 @@ static int write_socket_force(struct ClientSocket *client, char* msg, int len) { #else putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg); #endif - iohandler_send(client->iofd, msg, len) + iohandler_send(client->iofd, msg, len); client->traffic_out += len; DESYNCHRONIZE(synchronized); - return ret; + return 1; } int write_socket(struct ClientSocket *client, char* msg, int len) { @@ -219,13 +219,18 @@ int clientsocket_parseorder_top(unsigned int tid) { static IOHANDLER_CALLBACK(socket_callback) { struct ClientSocket *client = event->iofd->data; + #ifdef HAVE_THREADS + unsigned int tid; + #endif + if(process_state.running == 0) + return; //just ignore the event (shutdown sequence) switch(event->type) { case IOEVENT_CONNECTED: client->flags |= SOCKET_FLAG_CONNECTED; if(client->pass && strcmp(client->pass, "")) - iohandler_printf(event->iofd, "PASS :%s", client->pass); - iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname); - iohandler_printf(event->iofd, "NICK %s", client->nick); + putsock(client, "PASS :%s", client->pass); + putsock(client, "USER %s 0 0 :%s", client->ident, client->realname); + putsock(client, "NICK %s", client->nick); break; case IOEVENT_NOTCONNECTED: case IOEVENT_CLOSED: @@ -243,8 +248,10 @@ static IOHANDLER_CALLBACK(socket_callback) { break; case IOEVENT_RECV: #ifdef HAVE_THREADS + tid = (unsigned int) pthread_self_tid(); clientsocket_start_of_recv(tid); #endif + client->traffic_in += strlen(event->data.recv_str); parse_line(client, event->data.recv_str); #ifdef HAVE_THREADS clientsocket_end_of_recv(tid); @@ -280,13 +287,19 @@ struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) { return NULL; } -void free_sockets() { +void free_sockets(int close_only) { if(!sockets) return; struct ClientSocket *client, *next; for (client = sockets->data; client; client = next) { next = client->next; - destroy_socket(client); + if(close_only) { + if((client->flags & SOCKET_FLAG_CONNECTED)) + iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel); + } else + destroy_socket(client); + } + if(!close_only) { + free(sockets); + sockets = NULL; } - free(sockets); - sockets = NULL; }