X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=2c77b080207e1eb607bda3eafbc241c8f6e672d0;hb=c575e458c6257e75b97884847143b20965a5dfda;hp=f4288f291ec6360aa1e0b532aad62746de386eab;hpb=8942135758dbad0f07add7c1e2567867d4f53d6b;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index f4288f2..2c77b08 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -1,7 +1,24 @@ +/* ClientSocket.c - NeonServ v5.2 + * Copyright (C) 2011 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "ClientSocket.h" #include "IRCParser.h" #include "UserNode.h" +#include "IRCQueue.h" struct socket_list { struct ClientSocket *data; @@ -33,7 +50,7 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User } client->host = strdup(host); client->port = port; - printf("Connect: %s:%d", client->host, client->port); + printf("Connect: %s:%d\n", client->host, client->port); client->pass = (pass == NULL ? NULL : strdup(pass)); client->user = user; client->flags = 0; @@ -43,6 +60,7 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User client->connection_time = 0; client->botid = 0; client->clientid = 0; + client->queue = NULL; client->next = sockets->data; sockets->data = client; return client; @@ -80,14 +98,14 @@ int connect_socket(struct ClientSocket *client) { } client->sock = sock; - client->flags |= SOCKET_FLAG_CONNECTED; + client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT; client->connection_time = time(0); //send the IRC Headers char sendBuf[512]; int len; - if(client->pass) { + if(client->pass && strcmp(client->pass, "")) { len = sprintf(sendBuf, "PASS :%s\n", client->pass); write_socket(client, sendBuf, len); } @@ -133,14 +151,14 @@ int connect_socket(struct ClientSocket *client) { } client->sock = sock; - client->flags |= SOCKET_FLAG_CONNECTED; + client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT; client->connection_time = time(0); //send the IRC Headers char sendBuf[512]; int len; - if(client->pass) { + if(client->pass && strcmp(client->pass, "")) { len = sprintf(sendBuf, "PASS :%s\n", client->pass); write_socket(client, sendBuf, len); } @@ -169,20 +187,33 @@ int close_socket(struct ClientSocket *client) { } else last_sock = sock; } + if(client->queue) + queue_destroy(client); free(client->host); free(client->pass); free(client); return 1; } -int write_socket(struct ClientSocket *client, char* msg, int len) { - if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0; +int write_socket_force(struct ClientSocket *client, char* msg, int len) { printf("[send %d] %s", len, msg); + #ifdef WIN32 + send(client->sock, msg, len, 0); + #else write(client->sock, msg, len); + #endif client->traffic_out += len; return 1; } +int write_socket(struct ClientSocket *client, char* msg, int len) { + if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0; + if(client->flags & SOCKET_FLAG_USE_QUEUE) + return queue_add(client, msg, len); + else + return write_socket_force(client, msg, len); +} + void socket_loop(int timeout_seconds) { if(sockets == NULL) return; fd_set fds; @@ -204,7 +235,11 @@ void socket_loop(int timeout_seconds) { for (sock = sockets->data; sock; sock = sock->next) { if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) { if(sock->bufferpos != 0) { + #ifdef WIN32 + bytes = recv(sock->sock, buffer, sizeof(buffer), 0); + #else bytes = read(sock->sock, buffer, sizeof(buffer)); + #endif if(bytes > 0) { for(i = 0; i < bytes; i++) { if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow @@ -213,7 +248,11 @@ void socket_loop(int timeout_seconds) { sock->bufferpos += i; } } else { + #ifdef WIN32 + bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0); + #else bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer)); + #endif if(bytes > 0) sock->bufferpos = bytes; } @@ -221,6 +260,8 @@ void socket_loop(int timeout_seconds) { //error sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY); bot_disconnect(sock); + if(sock->queue) + queue_destroy(sock); } else { sock->traffic_in += bytes; int used = parse_lines(sock, sock->buffer, sock->bufferpos); @@ -234,6 +275,10 @@ void socket_loop(int timeout_seconds) { sock->bufferpos -= used; } } + } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) { + if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) { + connect_socket(sock); + } } } } @@ -272,6 +317,8 @@ void free_sockets() { next = client->next; if((client->flags & SOCKET_FLAG_CONNECTED)) close(client->sock); + if(client->queue) + queue_destroy(client); free(client->host); free(client->pass); free(client);