Do not accept new connections if the connection buffer is full.
[deb_shairplay.git] / src / lib / httpd.c
index 5779c0646e6501c8b012ba815bf547c891bf99d0..6ad28696b397c3edc3247a1899035f5ace652510 100644 (file)
@@ -1,3 +1,17 @@
+/**
+ *  Copyright (C) 2011-2012  Juho Vähä-Herttua
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library 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
+ *  Lesser General Public License for more details.
+ */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -25,6 +39,7 @@ struct httpd_s {
        int use_rtsp;
 
        int max_connections;
+       int open_connections;
        http_connection_t *connections;
 
        /* These variables only edited mutex locked */
@@ -101,6 +116,7 @@ httpd_add_connection(httpd_t *httpd, int fd, unsigned char *local, int local_len
                return;
        }
 
+       httpd->open_connections++;
        httpd->connections[i].socket_fd = fd;
        httpd->connections[i].connected = 1;
        httpd->connections[i].user_data = httpd->callbacks.conn_init(httpd->callbacks.opaque, local, local_len, remote, remote_len);
@@ -117,6 +133,7 @@ httpd_remove_connection(httpd_t *httpd, http_connection_t *connection)
        shutdown(connection->socket_fd, SHUT_WR);
        closesocket(connection->socket_fd);
        connection->connected = 0;
+       httpd->open_connections--;
 }
 
 static THREAD_RETVAL
@@ -147,8 +164,10 @@ httpd_thread(void *arg)
 
                /* Get the correct nfds value and set rfds */
                FD_ZERO(&rfds);
-               FD_SET(httpd->server_fd, &rfds);
-               nfds = httpd->server_fd+1;
+               if (httpd->open_connections < httpd->max_connections) {
+                       FD_SET(httpd->server_fd, &rfds);
+                       nfds = httpd->server_fd+1;
+               }
                for (i=0; i<httpd->max_connections; i++) {
                        int socket_fd;
                        if (!httpd->connections[i].connected) {