]> Git repositories of Nishi - tewi.git/commitdiff
close threads
authorNishi <nishi@nishi.boats>
Wed, 18 Sep 2024 21:28:33 +0000 (21:28 +0000)
committerNishi <nishi@nishi.boats>
Wed, 18 Sep 2024 21:28:33 +0000 (21:28 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@65 8739d7e6-ffea-ec47-b151-bdff447c6205

Server/server.c

index 58a1ee5e97247a745234628cf79abe30b66ebcf1..965600648f6c3a0e869198feae73f95f5673f5b0 100644 (file)
@@ -704,11 +704,24 @@ extern SERVICE_STATUS status;
 extern SERVICE_STATUS_HANDLE status_handle;
 #endif
 
+#ifdef __MINGW32__
+struct thread_entry {
+       HANDLE handle;
+       bool used;
+};
+#endif
+
 void tw_server_loop(void) {
        struct timeval tv;
+#ifdef __MINGW32__
+       struct thread_entry threads[2048];
+       int i;
+       for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++){
+               threads[i].used = false;
+       }
+#endif
        while(1) {
                FD_ZERO(&fdset);
-               int i;
                for(i = 0; i < sockcount; i++) {
                        FD_SET(sockets[i], &fdset);
                }
@@ -718,6 +731,16 @@ void tw_server_loop(void) {
                if(ret == -1) {
                        break;
                }else if(ret == 0){
+                       for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++){
+                               if(threads[i].used){
+                                       DWORD ex;
+                                       GetExitCodeThread(threads[i].handle, &ex);
+                                       if(ex != STILL_ACTIVE){
+                                               CloseHandle(threads[i].handle);
+                                               threads[i].used = false;
+                                       }
+                               }
+                       }
 #ifdef SERVICE
                        if(status.dwCurrentState == SERVICE_STOP_PENDING){
                                break;
@@ -733,13 +756,29 @@ void tw_server_loop(void) {
                                        int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
                                        cm_log("Server", "New connection accepted");
 #ifdef __MINGW32__
-                                       HANDLE thread;
                                        struct pass_entry* e = malloc(sizeof(*e));
                                        e->sock = sock;
                                        e->ssl = config.ports[i] & (1ULL << 32);
                                        e->port = config.ports[i];
                                        e->addr = claddr;
-                                       thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
+                                       int j;
+                                       for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++){
+                                               if(threads[j].used){
+                                                       DWORD ex;
+                                                       GetExitCodeThread(threads[j].handle, &ex);
+                                                       if(ex != STILL_ACTIVE){
+                                                               CloseHandle(threads[j].handle);
+                                                               threads[j].used = false;
+                                                       }
+                                               }
+                                       }
+                                       for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++){
+                                               if(!threads[j].used){
+                                                       threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
+                                                       threads[j].used = true;
+                                                       break;
+                                               }
+                                       }
 #else
                                        pid_t pid = fork();
                                        if(pid == 0) {