Improve stopping control of the service
Modified: trunk/reactos/services/tcpsvcs/chargen.c
Modified: trunk/reactos/services/tcpsvcs/discard.c
Modified: trunk/reactos/services/tcpsvcs/echo.c
Modified: trunk/reactos/services/tcpsvcs/qotd.c
Modified: trunk/reactos/services/tcpsvcs/skelserver.c
Modified: trunk/reactos/services/tcpsvcs/tcpsvcs.c

Modified: trunk/reactos/services/tcpsvcs/chargen.c
--- trunk/reactos/services/tcpsvcs/chargen.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/chargen.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -17,6 +17,8 @@
 #include <tchar.h>
 #include "tcpsvcs.h"
 
+extern BOOL bShutDown;
+
 DWORD WINAPI ChargenHandler(VOID* Sock_)
 {
     DWORD RetVal = 0;
@@ -60,7 +62,7 @@
 
     /* where we will start output from */
     loopIndex = 0;
-    while (1)
+    while (! bShutDown)
     {
         /* if the loop index is equal to the last char,
          * start the loop again from the beginning */
@@ -82,14 +84,17 @@
         Line[LINESIZ - 2] = L'\r';
         Line[LINESIZ - 1] = L'\n';
 
-        if (!SendLine(Sock, Line))
+        if (! SendLine(Sock, Line))
             break;
 
         /* increment loop index to start printing from next char in ring */
         loopIndex++;
     }
-
-    return TRUE;
+    
+    if (bShutDown)
+        return FALSE;
+    else
+        return TRUE;
 }
 
 BOOL SendLine(SOCKET Sock, TCHAR* Line)
@@ -121,7 +126,7 @@
     }
     else
         LogEvent(_T("Chargen: unknown error\n"), 0, FALSE);
-        //WSAGetLastError()
+        // return FALSE;
 
     LogEvent(_T("Chargen: Connection closed by peer.\n"), 0, FALSE);
     return TRUE;

Modified: trunk/reactos/services/tcpsvcs/discard.c
--- trunk/reactos/services/tcpsvcs/discard.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/discard.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -14,6 +14,8 @@
 
 #include "tcpsvcs.h"
 
+extern BOOL bShutDown;
+
 DWORD WINAPI DiscardHandler(VOID* Sock_)
 {
     DWORD RetVal = 0;
@@ -43,7 +45,7 @@
 BOOL RecieveIncomingPackets(SOCKET Sock)
 {
     TCHAR ReadBuffer[BUF];
-    TCHAR temp[512]; // temp for holding LogEvent text
+    TCHAR buf[256];
     INT ReadBytes;
 
     do
@@ -51,17 +53,19 @@
         ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
         if (ReadBytes > 0)
         {
-            _stprintf(temp, _T("Received %d bytes from client\n"), ReadBytes);
-            LogEvent(temp, 0, FALSE);
+            _stprintf(buf, _T("Received %d bytes from client\n"), ReadBytes);
+            LogEvent(buf, 0, FALSE);
         }
         else if (ReadBytes == SOCKET_ERROR)
         {
-            _stprintf(temp, ("Socket Error: %d\n"), WSAGetLastError());
-            LogEvent(temp, 0, TRUE);
+            _stprintf(buf, ("Socket Error: %d\n"), WSAGetLastError());
+            LogEvent(buf, 0, TRUE);
             return FALSE;
         }
-    } while (ReadBytes > 0);
+    } while ((ReadBytes > 0) && (! bShutDown));
 
-    LogEvent(_T("Discard: Connection closed by peer.\n"), 0, FALSE);
+    if (! bShutDown)
+        LogEvent(_T("Discard: Connection closed by peer.\n"), 0, FALSE);
+
     return TRUE;
 }

Modified: trunk/reactos/services/tcpsvcs/echo.c
--- trunk/reactos/services/tcpsvcs/echo.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/echo.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -14,6 +14,8 @@
 
 #include "tcpsvcs.h"
 
+extern BOOL bShutDown;
+
 DWORD WINAPI EchoHandler(VOID* Sock_)
 {
     DWORD RetVal = 0;
@@ -43,7 +45,7 @@
 BOOL EchoIncomingPackets(SOCKET Sock)
 {
     TCHAR ReadBuffer[BUF];
-    TCHAR temp[512]; // temp for holding LogEvent text
+    TCHAR buf[256]; // temp for holding LogEvent text
     INT Temp;
     INT ReadBytes;
     INT SentBytes;
@@ -52,8 +54,8 @@
         ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
         if (ReadBytes > 0)
         {
-            _stprintf(temp, _T("Received %d bytes from client\n"), ReadBytes);
-            LogEvent(temp, 0, FALSE);
+            _stprintf(buf, _T("Received %d bytes from client\n"), ReadBytes);
+            LogEvent(buf, 0, FALSE);
 
             SentBytes = 0;
             while (SentBytes < ReadBytes)
@@ -62,8 +64,8 @@
                         ReadBytes - SentBytes, 0);
                 if (Temp > 0)
                 {
-                    _stprintf(temp, _T("Sent %d bytes back to client\n"), Temp);
-                    LogEvent(temp, 0, FALSE);
+                    _stprintf(buf, _T("Sent %d bytes back to client\n"), Temp);
+                    LogEvent(buf, 0, FALSE);
                     SentBytes += Temp;
                 }
                 else if (Temp == SOCKET_ERROR)
@@ -72,8 +74,8 @@
                 {
                     /* Client closed connection before we could reply to
                        all the data it sent, so quit early. */
-                    _stprintf(temp, _T("Peer unexpectedly dropped connection!\n"));
-                    LogEvent(temp, 0, FALSE);
+                    _stprintf(buf, _T("Peer unexpectedly dropped connection!\n"));
+                    LogEvent(buf, 0, FALSE);
                     return FALSE;
                 }
             }
@@ -81,8 +83,10 @@
         else if (ReadBytes == SOCKET_ERROR)
             return FALSE;
 
-    } while (ReadBytes != 0);
+    } while ((ReadBytes != 0) && (! bShutDown));
 
-    LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
+    if (! bShutDown)
+        LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
+
     return TRUE;
 }

Modified: trunk/reactos/services/tcpsvcs/qotd.c
--- trunk/reactos/services/tcpsvcs/qotd.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/qotd.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -44,10 +44,10 @@
     LogEvent(_T("QOTD: Opening quotes file\n"), 0, FALSE);
     if ((fp = _tfopen(Sys, "r")) == NULL)
     {
-		TCHAR temp[512];
+		TCHAR buf[256];
 
-		_stprintf(temp, _T("QOTD: Error opening quote file : %s\n"), Sys);
-        LogEvent(temp, 0, TRUE);
+		_stprintf(buf, _T("QOTD: Error opening quote file : %s\n"), Sys);
+        LogEvent(buf, 0, TRUE);
         LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
         ExitThread(-1);
     }

Modified: trunk/reactos/services/tcpsvcs/skelserver.c
--- trunk/reactos/services/tcpsvcs/skelserver.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/skelserver.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -14,18 +14,18 @@
 
 #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)
@@ -34,15 +34,14 @@
         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 @@
 /* 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,7 +90,7 @@
     FD_SET ReadFDS;
     INT nAddrSize = sizeof(Client);
     DWORD ThreadID;
-    TCHAR temp[512];
+    TCHAR buf[256];
     INT TimeOut = 2000; // 2 seconds
 
 //DebugBreak();
@@ -102,7 +101,7 @@
     TimeVal.tv_sec  = TimeOut / 1000;
     TimeVal.tv_usec = TimeOut % 1000;
 
-    while (! bShutDownFlag) // (i<MAX_CLIENTS && !bShutDownFlag)
+    while (! bShutDown) // (i<MAX_CLIENTS && !bShutDown)
     {
 		FD_SET(ListeningSocket, &ReadFDS);
         if (select(0, &ReadFDS, NULL, NULL, &TimeVal) == SOCKET_ERROR)
@@ -110,31 +109,32 @@
             LogEvent(_T("select failed\n"), 0, TRUE);
             return;
         }
-        
-        if (FD_ISSET(ListeningSocket, &ReadFDS))
+
+        /* 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(temp, _T("Accepted connection to %s server from %s:%d\n"),
+                _stprintf(buf, _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);
+                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. */ 
+                /* Check the return value for success. */
                 if (hThread == NULL)
                 {
-                    _stprintf(temp, _T("Failed to start worker thread for "
+                    _stprintf(buf, _T("Failed to start worker thread for "
                         "the %s server....\n"), Name);
-                    LogEvent(temp, 0, TRUE);
+                    LogEvent(buf, 0, TRUE);
                 }
 
-                //Do we need to wait, or just kill it?
                 WaitForSingleObject(hThread, INFINITE);
-
+                
+                CloseHandle(hThread);
             }
             else
             {
@@ -147,7 +147,7 @@
 
 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 +171,8 @@
             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);
         }
     }
 

Modified: trunk/reactos/services/tcpsvcs/tcpsvcs.c
--- trunk/reactos/services/tcpsvcs/tcpsvcs.c	2005-11-17 21:12:27 UTC (rev 19311)
+++ trunk/reactos/services/tcpsvcs/tcpsvcs.c	2005-11-17 21:35:56 UTC (rev 19312)
@@ -17,8 +17,8 @@
  * - write debugging function and print all dbg info via that.
  * - change 'temp' to something meaningfull
  */
- 
 
+
 #include "tcpsvcs.h"
 
 //#define NDEBUG
@@ -34,8 +34,8 @@
 static SERVICE_STATUS_HANDLE hSStat;
 
 FILE *hLogFile;
-BOOL bShutDownFlag = FALSE;
-BOOL bPauseFlag = FALSE;
+BOOL bShutDown = FALSE;
+BOOL bPause = FALSE;
 
 LPCTSTR LogFileName = "\\tcpsvcs_log.log";
 LPTSTR ServiceName = _T("Simp Tcp");
@@ -60,7 +60,7 @@
         {ServiceName, ServiceMain},
         {NULL, NULL}
     };
-    
+
     //DPRINT("Starting tcpsvcs service. See \system32%s for logs\n", LogFileName);
 
     if (! StartServiceCtrlDispatcher(ServiceTable))
@@ -79,19 +79,19 @@
 
     if(! GetSystemDirectory(LogFilePath, MAX_PATH))
         return;
-    
+
     _tcscat(LogFilePath, LogFileName);
 
 	hLogFile = fopen(LogFilePath, _T("w"));
     if (hLogFile == NULL)
     {
-        TCHAR *temp = NULL;
-        
-        _stprintf(temp, _T("Could not open log file: %s"), LogFilePath);
-        MessageBox(NULL, temp, NULL, MB_OK);
+        TCHAR buf[50];
+
+        _stprintf(buf, _T("Could not open log file: %s"), LogFilePath);
+        MessageBox(NULL, buf, NULL, MB_OK);
         return;
     }
-      
+
     LogEvent(_T("Entering ServiceMain"), 0, FALSE);
 
     hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
@@ -102,7 +102,7 @@
     hServStatus.dwServiceSpecificExitCode = NO_ERROR;
     hServStatus.dwCheckPoint = 0;
     hServStatus.dwWaitHint = 2*CS_TIMEOUT;
-    
+
     hSStat = RegisterServiceCtrlHandler(ServiceName, ServerCtrlHandler);
     if (hSStat == 0)
         LogEvent(_T("Failed to register service\n"), -1, TRUE);
@@ -118,7 +118,7 @@
         SetServiceStatus(hSStat, &hServStatus);
         return;
     }
-    
+
 	LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status"), 0, FALSE);
 	/*  We will only return here when the ServiceSpecific function
 		completes, indicating system shutdown. */
@@ -137,14 +137,14 @@
     {
         case SERVICE_CONTROL_SHUTDOWN: /* fall through */
         case SERVICE_CONTROL_STOP:
-            bShutDownFlag = TRUE;
+            bShutDown = TRUE;
             UpdateStatus(SERVICE_STOP_PENDING, -1);
             break;
         case SERVICE_CONTROL_PAUSE:
-            bPauseFlag = TRUE;
+            bPause = TRUE;
             break;
         case SERVICE_CONTROL_CONTINUE:
-            bPauseFlag = FALSE;
+            bPause = FALSE;
             break;
         case SERVICE_CONTROL_INTERROGATE:
             break;
@@ -164,13 +164,13 @@
         hServStatus.dwCheckPoint++;
 	else
         hServStatus.dwCheckPoint = Check;
-        
+
 	if (NewStatus >= 0)
         hServStatus.dwCurrentState = NewStatus;
-	
+
 	if (! SetServiceStatus (hSStat, &hServStatus))
 		LogEvent(_T("Cannot set service status"), -1, TRUE);
-		
+
 	return;
 }
 
@@ -180,17 +180,17 @@
     DWORD dwThreadId[NUM_SERVICES];
     HANDLE hThread[NUM_SERVICES];
 	WSADATA wsaData;
-	TCHAR temp[512]; // temp for holding LogEvent text
+	TCHAR buf[256]; // temp for holding LogEvent text
     INT i;
     DWORD RetVal;
-    
+
     if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
     {
-        _stprintf(temp, _T("WSAStartup() failed : %lu\n"), RetVal);
-        LogEvent(temp, RetVal, TRUE);
+        _stprintf(buf, _T("WSAStartup() failed : %lu\n"), RetVal);
+        LogEvent(buf, RetVal, TRUE);
         return -1;
     }
-    
+
     UpdateStatus(-1, -1); /* increment checkpoint */
 
     LogEvent(_T("Creating server Threads\n"), 0, FALSE);
@@ -198,8 +198,8 @@
     /* Create MAX_THREADS worker threads. */
     for( i=0; i<NUM_SERVICES; i++ )
     {
-        _stprintf(temp, _T("Starting %s server....\n"), Services[i].Name);
-        LogEvent(temp, 0, FALSE);
+        _stprintf(buf, _T("Starting %s server....\n"), Services[i].Name);
+        LogEvent(buf, 0, FALSE);
 
         hThread[i] = CreateThread(
             NULL,              // default security attributes
@@ -212,14 +212,14 @@
         /* Check the return value for success. */
         if (hThread[i] == NULL)
         {
-            _stprintf(temp, _T("Failed to start %s server....\n"), Services[i].Name);
+            _stprintf(buf, _T("Failed to start %s server....\n"), Services[i].Name);
             /* don't exit process via LogEvent. We want to exit via the server
              * which failed to start, which could mean i=0 */
-            LogEvent(temp, 0, TRUE);
+            LogEvent(buf, 0, TRUE);
             ExitProcess(i);
         }
     }
-    
+
     LogEvent(_T("setting service status to running\n"), 0, FALSE);
 
     UpdateStatus(SERVICE_RUNNING, 0);
@@ -232,6 +232,10 @@
     {
         CloseHandle(hThread[i]);
     }
+    
+    LogEvent(_T("Detaching Winsock2...\n"), 0, FALSE);
+    WSACleanup();
+    
     return 0;
 }