added first command requesting asynchronous data (adduser)
[NeonServV5.git] / ClientSocket.c
index 2f80c71bbeee31e9bff8f7dfdd7672a26cf319ae..59f272a2fbd11a3fbe92db7825c24cc86f6ba0ec 100644 (file)
@@ -1,6 +1,7 @@
 
 #include "ClientSocket.h"
 #include "IRCParser.h"
+#include "UserNode.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -11,7 +12,7 @@ struct socket_list {
 static struct socket_list *sockets = NULL;
 static char buffer[BUF_SIZ];
 
-static void init() {
+static void init_sockets() {
     sockets = malloc(sizeof(*sockets));
     if (!sockets)
     {
@@ -23,7 +24,7 @@ static void init() {
 }
 
 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
-    if(sockets == NULL) init();
+    if(sockets == NULL) init_sockets();
     struct ClientSocket *client = malloc(sizeof(*client));
     if (!client)
     {
@@ -32,10 +33,13 @@ 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);
     client->pass = (pass == NULL ? NULL : strdup(pass));
     client->user = user;
     client->flags = 0;
     client->bufferpos = 0;
+       client->botid = 0;
+    client->clientid = 0;
     client->next = sockets->data;
     sockets->data = client;
     return client;
@@ -156,6 +160,7 @@ void socket_loop(int timeout_seconds) {
             if(bytes <= 0) {
                 //error
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
+                bot_disconnect(sock);
             } else {
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
                 if(used == sock->bufferpos + 1) {
@@ -198,3 +203,18 @@ struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
     }
     return NULL;
 }
+
+void free_sockets() {
+    if(!sockets) return;
+    struct ClientSocket *client, *next;
+    for (client = sockets->data; client; client = next) {
+        next = client->next;
+        if((client->flags & SOCKET_FLAG_CONNECTED))
+            close(client->sock);
+        free(client->host);
+        free(client->pass);
+        free(client);
+    }
+    free(sockets);
+    sockets = NULL;
+}