added simple anti flood system
[NeonServV5.git] / src / ClientSocket.c
index 470fa505432a16980d343ac6c5f35b6d0d771f73..cda3ee76adeb93b82501db4b305fc05322ef7aa1 100644 (file)
@@ -18,6 +18,7 @@
 #include "ClientSocket.h"
 #include "IRCParser.h"
 #include "UserNode.h"
+#include "IRCQueue.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -59,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;
@@ -185,14 +187,15 @@ 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);
@@ -203,6 +206,14 @@ int write_socket(struct ClientSocket *client, char* msg, int 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;
@@ -249,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);
@@ -300,6 +313,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);