- use InterlockedExchange for setting shutdown flag
[reactos.git] / reactos / services / tcpsvcs / skelserver.c
index 10616d6..99a6aaa 100644 (file)
 
 #include "tcpsvcs.h"
 
-extern BOOL bShutDownFlag;
-extern BOOL bPauseFlag;
+extern BOOL bShutDown;
+extern BOOL bPause;
 
 DWORD WINAPI StartServer(LPVOID lpParam)
 {
        SOCKET ListeningSocket;
        PSERVICES pServices;
     const TCHAR* HostIP = "127.0.0.1";
-    TCHAR temp[512];
+    TCHAR buf[256];
 
     pServices = (PSERVICES)lpParam;
-       
+
 //DebugBreak();
     ListeningSocket = SetUpListener(HostIP, htons(pServices->Port));
     if (ListeningSocket == INVALID_SOCKET)
     {
-               LogEvent("Socket error when setting up listener", 0, TRUE);
+               LogEvent("Socket error when setting up listener\n", 0, TRUE);
         return 3;
     }
 
-    _stprintf(temp, _T("%s is waiting for connections on port %d...\n"),
+    _stprintf(buf, _T("%s is waiting for connections on port %d...\n"),
         pServices->Name, pServices->Port);
-    LogEvent(temp, 0, FALSE);
+    LogEvent(buf, 0, FALSE);
 
-       AcceptConnections(ListeningSocket, pServices->Service, pServices->Name);
+    if (! bShutDown)
+       AcceptConnections(ListeningSocket, pServices->Service, pServices->Name);
 
-    LogEvent(_T("Detaching Winsock2...\n"), 0, FALSE);
-    WSACleanup();
-    return 0;
+    ExitThread(0);
 }
 
 
@@ -72,7 +71,7 @@ SOCKET SetUpListener(const char* ServAddr, int Port)
 /* note: consider allowing a maximum number of connections
  * A number of threads can be allocated and worker threads will
  * only be spawned if a free space is available
+
 typedef struct _WORKER_THREAD {
     DWORD num;
     BOOL available;
@@ -91,63 +90,69 @@ VOID AcceptConnections(SOCKET ListeningSocket,
     FD_SET ReadFDS;
     INT nAddrSize = sizeof(Client);
     DWORD ThreadID;
-    TCHAR temp[512];
+    TCHAR buf[256];
     INT TimeOut = 2000; // 2 seconds
 
 //DebugBreak();
-    /* monitor for incomming connections */
-    FD_ZERO(&ReadFDS);
 
     /* set timeout values */
     TimeVal.tv_sec  = TimeOut / 1000;
     TimeVal.tv_usec = TimeOut % 1000;
 
-    while (! bShutDownFlag) // (i<MAX_CLIENTS && !bShutDownFlag)
+    while (! bShutDown) // (i<MAX_CLIENTS && !bShutDown)
     {
+               INT SelRet = 0;
+
+               FD_ZERO(&ReadFDS);
                FD_SET(ListeningSocket, &ReadFDS);
-        if (select(0, &ReadFDS, NULL, NULL, &TimeVal) == SOCKET_ERROR)
+
+               SelRet = select(0, &ReadFDS, NULL, NULL, &TimeVal);
+        if (SelRet == SOCKET_ERROR)
         {
             LogEvent(_T("select failed\n"), 0, TRUE);
             return;
         }
-        
-        if (FD_ISSET(ListeningSocket, &ReadFDS))
-        {
-            Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);
-            if (Sock != INVALID_SOCKET)
-            {
-                _stprintf(temp, _T("Accepted connection to %s server from %s:%d\n"),
-                    Name, inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
-                LogEvent(temp, 0, FALSE);
-                _stprintf(temp, _T("Creating new thread for %s\n"), Name);
-                LogEvent(temp, 0, FALSE);
-
-                hThread = CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);
-
-                /* Check the return value for success. */ 
-                if (hThread == NULL)
-                {
-                    _stprintf(temp, _T("Failed to start worker thread for "
-                        "the %s server....\n"), Name);
-                    LogEvent(temp, 0, TRUE);
-                }
-
-                //Do we need to wait, or just kill it?
-                WaitForSingleObject(hThread, INFINITE);
-
-            }
-            else
-            {
-                LogEvent(_T("accept failed\n"), 0, TRUE);
-                return;
-            }
-        }
+               else if (SelRet > 0)
+               {
+                       /* don't call FD_ISSET if bShutDown flag is set */
+                       if ((! bShutDown) || (FD_ISSET(ListeningSocket, &ReadFDS)))
+                       {
+                               Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);
+                               if (Sock != INVALID_SOCKET)
+                               {
+                                       _stprintf(buf, _T("Accepted connection to %s server from %s:%d\n"),
+                                               Name, inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
+                                       LogEvent(buf, 0, FALSE);
+                                       _stprintf(buf, _T("Creating new thread for %s\n"), Name);
+                                       LogEvent(buf, 0, FALSE);
+
+                                       hThread = CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);
+
+                                       /* Check the return value for success. */
+                                       if (hThread == NULL)
+                                       {
+                                               _stprintf(buf, _T("Failed to start worker thread for "
+                                                       "the %s server....\n"), Name);
+                                               LogEvent(buf, 0, TRUE);
+                                       }
+
+                                       WaitForSingleObject(hThread, INFINITE);
+                       
+                                       CloseHandle(hThread);
+                               }
+                               else
+                               {
+                                       LogEvent(_T("accept failed\n"), 0, TRUE);
+                                       return;
+                               }
+                       }
+               }
     }
 }
 
 BOOL ShutdownConnection(SOCKET Sock, BOOL bRec)
 {
-    TCHAR temp[512];
+    TCHAR buf[256];
 
     /* Disallow any further data sends.  This will tell the other side
        that we want to go away now.  If we skip this step, we don't
@@ -171,8 +176,8 @@ BOOL ShutdownConnection(SOCKET Sock, BOOL bRec)
             return FALSE;
         else if (NewBytes != 0)
         {
-            _stprintf(temp, _T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes);
-            LogEvent(temp, 0, FALSE);
+            _stprintf(buf, _T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes);
+            LogEvent(buf, 0, FALSE);
         }
     }