]> Nishi Git Mirror - tewi.git/commitdiff
use poll for select by default
authorNishi <nishi@nishi.boats>
Sat, 21 Sep 2024 09:03:27 +0000 (09:03 +0000)
committerNishi <nishi@nishi.boats>
Sat, 21 Sep 2024 09:03:27 +0000 (09:03 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@118 8739d7e6-ffea-ec47-b151-bdff447c6205

Server/http.c
Server/server.c
Server/tw_version.h
config.h.tmpl

index a63ea67158149f1640eafe230a4daa3be5c5c923..5cf108dcd26cd5a7225df95854fbbbba158ffb21 100644 (file)
 #ifdef __MINGW32__
 #include <winsock2.h>
 #else
+#ifdef USE_POLL
+#include <poll.h>
+#else
 #include <sys/select.h>
 #endif
+#endif
 
 void tw_free_request(struct tw_http_request* req) {
        if(req->method != NULL) free(req->method);
@@ -45,7 +49,14 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
        char buffer[512];
        char cbuf[2];
        int phase = 0;
+
+#ifdef USE_POLL
+       struct pollfd pollfds[1];
+       pollfds[0].fd = sock;
+       pollfds[0].events = POLLIN | POLLPRI;
+#else
        fd_set fds;
+#endif
 
        bool bad = false;
 
@@ -63,18 +74,24 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
        int nl = 0;
 
        while(1) {
+#ifndef USE_POLL
                FD_ZERO(&fds);
                FD_SET(sock, &fds);
                struct timeval tv;
                tv.tv_sec = 5;
                tv.tv_usec = 0;
+#endif
 #ifndef NO_SSL
                if(ssl == NULL || !SSL_has_pending(ssl)) {
 #endif
+#ifdef USE_POLL
+                       int n = poll(pollfds, 1, 5000);
+#else
 #ifdef __HAIKU__
-                       int n = select(32, &fds, NULL, NULL, &tv);
+               int n = select(32, &fds, NULL, NULL, &tv);
 #else
                int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
+#endif
 #endif
                        if(n <= 0) {
                                cm_log("HTTP", "Timeout, disconncting");
index 2525ea3d9cf48de24ee06bd9374d7e9a12fbc377..1a25db653717dcafcebf794b0e048967aefa361d 100644 (file)
 
 #include "strptime.h"
 #else
+#ifdef USE_POLL
+#include <poll.h>
+#else
 #include <sys/select.h>
+#endif
 #include <sys/socket.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
@@ -53,7 +57,6 @@
 extern struct tw_config config;
 extern char tw_server[];
 
-fd_set fdset;
 int sockcount = 0;
 
 SOCKADDR addresses[MAX_PORTS];
@@ -801,25 +804,38 @@ struct thread_entry {
 #endif
 
 void tw_server_loop(void) {
-       struct timeval tv;
        int i;
 #if defined(__MINGW32__) || defined(__HAIKU__)
        struct thread_entry threads[2048];
        for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
                threads[i].used = false;
        }
+#endif
+#ifdef USE_POLL
+       struct pollfd pollfds[sockcount];
+       for(i = 0; i < sockcount; i++) {
+               pollfds[i].fd = sockets[i];
+               pollfds[i].events = POLLIN | POLLPRI;
+       }
+#else
+               fd_set fdset;
+               struct timeval tv;
 #endif
        while(1) {
-               FD_ZERO(&fdset);
-               for(i = 0; i < sockcount; i++) {
-                       FD_SET(sockets[i], &fdset);
-               }
-               tv.tv_sec = 1;
-               tv.tv_usec = 0;
+#ifdef USE_POLL
+               int ret = poll(pollfds, sockcount, 1000);
+#else
+                       FD_ZERO(&fdset);
+                       for(i = 0; i < sockcount; i++) {
+                               FD_SET(sockets[i], &fdset);
+                       }
+                       tv.tv_sec = 1;
+                       tv.tv_usec = 0;
 #ifdef __HAIKU__
-               int ret = select(32, &fdset, NULL, NULL, &tv);
+                       int ret = select(32, &fdset, NULL, NULL, &tv);
 #else
                        int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
+#endif
 #endif
                if(ret == -1) {
 #ifndef __MINGW32__
@@ -836,7 +852,13 @@ void tw_server_loop(void) {
                        /* connection */
                        int i;
                        for(i = 0; i < sockcount; i++) {
-                               if(FD_ISSET(sockets[i], &fdset)) {
+                               bool cond;
+#ifdef USE_POLL
+                               cond = pollfds[i].revents & POLLIN;
+#else
+                                       cond = FD_ISSET(sockets[i], &fdset);
+#endif
+                               if(cond) {
                                        SOCKADDR claddr;
                                        int clen = sizeof(claddr);
                                        int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
index 24beee15b4b976ad04050f292259dda3f49639c2..0eedbf7b9a3da408828633319775304a5ba6ee2f 100644 (file)
@@ -3,7 +3,7 @@
 #ifndef __TW_VERSION_H__
 #define __TW_VERSION_H__
 
-#define TW_VERSION "1.01\0"
+#define TW_VERSION "1.02\0"
 
 const char* tw_get_version(void);
 const char* tw_get_platform(void);
index c00b7a885392ba44ec4f8fd0a9e0a1252f6ddc52..c22dabca7efdfd16824acc5b7d6f7855d7f037db 100644 (file)
@@ -4,6 +4,7 @@
 #define __CONFIG_H__
 
 #undef NO_SSL
+#define USE_POLL
 
 /* DO NOT EDIT BELOW THIS LINE */
 
 #define SSL void
 #endif
 
+#if defined(__MINGW32__) && defined(USE_POLL)
+#undef USE_POLL
+/* Force select(2) for Windows */
+#endif
+
 #endif
 
 /*