X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FClientSocket.c;h=a624a305498337f8f2ff690144e78df2005a51c1;hp=cb40cc7dc9eb1afe311919d685ca16de153846e7;hb=f6bdc9d0fc8db22c265918f3325e11177fd001b9;hpb=02e797baca7f15e306d70b3b7e2b4d83ff1cf44f diff --git a/src/ClientSocket.c b/src/ClientSocket.c index cb40cc7..a624a30 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,8 @@ #include "ConfigParser.h" #include "version.h" #include "IOHandler.h" +#include "IRCEvents.h" +#include "log.h" struct socket_list { struct ClientSocket *data; @@ -53,7 +55,7 @@ void init_sockets() { sockets = malloc(sizeof(*sockets)); if (!sockets) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return; } sockets->data = NULL; @@ -98,9 +100,10 @@ struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pas void connect_socket(struct ClientSocket *client) { client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback); client->iofd->data = client; + client->flags |= SOCKET_FLAG_RECONNECT; } -int close_socket(struct ClientSocket *client) { +static int _close_socket(struct ClientSocket *client) { if(client == NULL) return 0; if((client->flags & SOCKET_FLAG_CONNECTED)) { iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); @@ -120,6 +123,12 @@ int close_socket(struct ClientSocket *client) { return 1; } +int close_socket(struct ClientSocket *client) { //external call (don't reconnect) + if(client == NULL) return 0; + client->flags &= ~SOCKET_FLAG_RECONNECT; + return _close_socket(client); +} + int destroy_socket(struct ClientSocket *client) { if(client == NULL) return 0; close_socket(client); @@ -136,6 +145,7 @@ int destroy_socket(struct ClientSocket *client) { } else last_sock = sock; } + event_freeclient(client); free(client->host); if(client->bind) free(client->bind); @@ -154,9 +164,9 @@ 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 - putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg); + printf_log("main", LOG_IRCRAW, "[%d send %d] %s", getCurrentThreadID(), len, msg); #else - putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg); + printf_log("main", LOG_IRCRAW, "[send %d] %s", len, msg); #endif iohandler_send(client->iofd, msg, len); client->traffic_out += len; @@ -217,18 +227,22 @@ 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: - close_socket(client); + _close_socket(client); if(client->flags & SOCKET_FLAG_RECONNECT) { struct timeval timeout; gettimeofday(&timeout, NULL); @@ -245,6 +259,7 @@ static IOHANDLER_CALLBACK(socket_callback) { 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 +295,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; }