--- trunk/reactos/services/tcpsvcs/skelserver.c 2005-11-18 22:32:44 UTC (rev 19331)
+++ trunk/reactos/services/tcpsvcs/skelserver.c 2005-11-18 22:55:30 UTC (rev 19332)
@@ -30,7 +30,7 @@
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;
}
@@ -94,8 +94,6 @@
INT TimeOut = 2000; // 2 seconds
//DebugBreak();
- /* monitor for incomming connections */
- FD_ZERO(&ReadFDS);
/* set timeout values */
TimeVal.tv_sec = TimeOut / 1000;
@@ -103,45 +101,52 @@
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;
}
+ 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);
- /* 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);
- 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);
+ }
- /* 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;
- }
- }
+ WaitForSingleObject(hThread, INFINITE);
+
+ CloseHandle(hThread);
+ }
+ else
+ {
+ LogEvent(_T("accept failed\n"), 0, TRUE);
+ return;
+ }
+ }
+ }
}
}
--- trunk/reactos/services/tcpsvcs/tcpsvcs.c 2005-11-18 22:32:44 UTC (rev 19331)
+++ trunk/reactos/services/tcpsvcs/tcpsvcs.c 2005-11-18 22:55:30 UTC (rev 19332)
@@ -75,25 +75,26 @@
VOID WINAPI
ServiceMain(DWORD argc, LPTSTR argv[])
{
- TCHAR LogFilePath[MAX_PATH];
+ TCHAR LogFilePath[MAX_PATH];
if(! GetSystemDirectory(LogFilePath, MAX_PATH))
return;
_tcscat(LogFilePath, LogFileName);
- hLogFile = fopen(LogFilePath, _T("w"));
+ hLogFile = fopen(LogFilePath, _T("a+"));
if (hLogFile == NULL)
{
TCHAR buf[50];
- _stprintf(buf, _T("Could not open log file: %s"), LogFilePath);
+ _stprintf(buf, _T("Could not open log file: %s\n"), LogFilePath);
MessageBox(NULL, buf, NULL, MB_OK);
return;
}
- LogEvent(_T("Entering ServiceMain"), 0, FALSE);
+ LogEvent(_T("Entering ServiceMain\n"), 0, FALSE);
+
hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
hServStatus.dwCurrentState = SERVICE_START_PENDING;
hServStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
@@ -107,9 +108,9 @@
if (hSStat == 0)
LogEvent(_T("Failed to register service\n"), -1, TRUE);
- LogEvent(_T("Control handler registered successfully"), 0, FALSE);
+ LogEvent(_T("Control handler registered successfully\n"), 0, FALSE);
SetServiceStatus (hSStat, &hServStatus);
- LogEvent(_T("Service status set to SERVICE_START_PENDING"), 0, FALSE);
+ LogEvent(_T("Service status set to SERVICE_START_PENDING\n"), 0, FALSE);
if (CreateServers() != 0)
{
@@ -119,13 +120,15 @@
return;
}
- LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status"), 0, FALSE);
+ LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status\n"), 0, FALSE);
/* We will only return here when the ServiceSpecific function
completes, indicating system shutdown. */
UpdateStatus (SERVICE_STOPPED, 0);
- LogEvent(_T("Service status set to SERVICE_STOPPED"), 0, FALSE);
- LogEvent(_T("Leaving ServiceMain"), 0, FALSE);
- fclose(hLogFile); /* Clean up everything, in general */
+ LogEvent(_T("Service status set to SERVICE_STOPPED\n"), 0, FALSE);
+ LogEvent(_T("Leaving ServiceMain\n"), 0, FALSE);
+
+ fclose(hLogFile);
+
return;
}
@@ -133,18 +136,23 @@
VOID WINAPI
ServerCtrlHandler(DWORD Control)
{
+ TCHAR buf[256];
+
switch (Control)
{
case SERVICE_CONTROL_SHUTDOWN: /* fall through */
case SERVICE_CONTROL_STOP:
- bShutDown = TRUE;
+ LogEvent(_T("stopping service\n"), 0, FALSE);
+ InterlockedExchange((LONG *)&bShutDown, TRUE);
UpdateStatus(SERVICE_STOP_PENDING, -1);
break;
- case SERVICE_CONTROL_PAUSE:
- bPause = TRUE;
+ case SERVICE_CONTROL_PAUSE: /* not yet implemented */
+ LogEvent(_T("pausing service\n"), 0, FALSE);
+ InterlockedExchange((LONG *)&bPause, TRUE);
break;
case SERVICE_CONTROL_CONTINUE:
- bPause = FALSE;
+ LogEvent(_T("continuing service\n"), 0, FALSE);
+ InterlockedExchange((LONG *)&bPause, FALSE);
break;
case SERVICE_CONTROL_INTERROGATE:
break;
@@ -169,7 +177,7 @@
hServStatus.dwCurrentState = NewStatus;
if (! SetServiceStatus (hSStat, &hServStatus))
- LogEvent(_T("Cannot set service status"), -1, TRUE);
+ LogEvent(_T("Cannot set service status\n"), -1, TRUE);
return;
}
@@ -180,7 +188,7 @@
DWORD dwThreadId[NUM_SERVICES];
HANDLE hThread[NUM_SERVICES];
WSADATA wsaData;
- TCHAR buf[256]; // temp for holding LogEvent text
+ TCHAR buf[256];
INT i;
DWORD RetVal;
@@ -251,6 +259,8 @@
LPTSTR lpvSysMsg;
TCHAR MessageBuffer[512];
+
+
if (PrintErrorMsg)
{
eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
@@ -258,13 +268,13 @@
ErrNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpvSysMsg, 0, NULL);
- _stprintf(MessageBuffer, _T("\n%s %s ErrNum = %lu. ExitCode = %lu."),
+ _stprintf(MessageBuffer, _T("%s %s ErrNum = %lu. ExitCode = %lu."),
UserMessage, lpvSysMsg, ErrNum, ExitCode);
HeapFree(GetProcessHeap (), 0, lpvSysMsg);
}
else
{
- _stprintf(MessageBuffer, _T("\n%s"), UserMessage);
+ _stprintf(MessageBuffer, _T("%s"), UserMessage);
}
fputs (MessageBuffer, hLogFile);