From: Nishi Date: Sat, 21 Sep 2024 09:03:27 +0000 (+0000) Subject: use poll for select by default X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=c6120af3460bd9af47be177de073b129a01736b2;p=tewi.git use poll for select by default git-svn-id: file:///raid/svn-personal/tewi/trunk@118 8739d7e6-ffea-ec47-b151-bdff447c6205 --- diff --git a/Server/http.c b/Server/http.c index a63ea67..5cf108d 100644 --- a/Server/http.c +++ b/Server/http.c @@ -18,8 +18,12 @@ #ifdef __MINGW32__ #include #else +#ifdef USE_POLL +#include +#else #include #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"); diff --git a/Server/server.c b/Server/server.c index 2525ea3..1a25db6 100644 --- a/Server/server.c +++ b/Server/server.c @@ -38,7 +38,11 @@ #include "strptime.h" #else +#ifdef USE_POLL +#include +#else #include +#endif #include #include #include @@ -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); diff --git a/Server/tw_version.h b/Server/tw_version.h index 24beee1..0eedbf7 100644 --- a/Server/tw_version.h +++ b/Server/tw_version.h @@ -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); diff --git a/config.h.tmpl b/config.h.tmpl index c00b7a8..c22dabc 100644 --- a/config.h.tmpl +++ b/config.h.tmpl @@ -4,6 +4,7 @@ #define __CONFIG_H__ #undef NO_SSL +#define USE_POLL /* DO NOT EDIT BELOW THIS LINE */ @@ -11,6 +12,11 @@ #define SSL void #endif +#if defined(__MINGW32__) && defined(USE_POLL) +#undef USE_POLL +/* Force select(2) for Windows */ +#endif + #endif /*