*
*/
-#include <stdio.h>
-#include <winsock2.h>
-#include <tchar.h>
#include "tcpsvcs.h"
+extern BOOL bShutDown;
+
DWORD WINAPI EchoHandler(VOID* Sock_)
{
DWORD RetVal = 0;
SOCKET Sock = (SOCKET)Sock_;
if (!EchoIncomingPackets(Sock)) {
- _tprintf(_T("Echo incoming packets failed\n"));
- RetVal = -1;
+ LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE);
+ RetVal = 1;
}
- _tprintf(_T("Shutting connection down...\n"));
- if (ShutdownConnection(Sock, TRUE)) {
- _tprintf(_T("Connection is down.\n"));
- }
+ LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE);
+
+ if (ShutdownConnection(Sock, TRUE))
+ LogEvent(_T("Echo: Connection is down\n"), 0, FALSE);
else
{
- _tprintf(_T("Connection shutdown failed\n"));
- RetVal = -1;
+ LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE);
+ RetVal = 1;
}
- _tprintf(_T("Terminating echo thread\n"));
+ LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE);
ExitThread(RetVal);
}
BOOL EchoIncomingPackets(SOCKET Sock)
{
TCHAR ReadBuffer[BUF];
+ TCHAR buf[256]; // temp for holding LogEvent text
INT Temp;
INT ReadBytes;
INT SentBytes;
ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
if (ReadBytes > 0)
{
- _tprintf(_T("Received %d bytes from client\n"), ReadBytes);
+ _stprintf(buf, _T("Received %d bytes from client\n"), ReadBytes);
+ LogEvent(buf, 0, FALSE);
SentBytes = 0;
while (SentBytes < ReadBytes)
ReadBytes - SentBytes, 0);
if (Temp > 0)
{
- _tprintf(_T("Sent %d bytes back to client\n"), Temp);
+ _stprintf(buf, _T("Sent %d bytes back to client\n"), Temp);
+ LogEvent(buf, 0, FALSE);
SentBytes += Temp;
}
else if (Temp == SOCKET_ERROR)
{
/* Client closed connection before we could reply to
all the data it sent, so quit early. */
- _tprintf(_T("Peer unexpectedly dropped connection!\n"));
+ _stprintf(buf, _T("Peer unexpectedly dropped connection!\n"));
+ LogEvent(buf, 0, FALSE);
return FALSE;
}
}
else if (ReadBytes == SOCKET_ERROR)
return FALSE;
- } while (ReadBytes != 0);
+ } while ((ReadBytes != 0) && (! bShutDown));
+
+ if (! bShutDown)
+ LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
+
+ if (bShutDown)
+ LogEvent(_T("Echo: thread recieved shutdown signal\n"), 0, FALSE);
- _tprintf(("Connection closed by peer.\n"));
return TRUE;
}