ReactOS  0.4.11-dev-946-g431643b
echo.c File Reference
#include "tcpsvcs.h"
Include dependency graph for echo.c:

Go to the source code of this file.

Macros

#define RECV_BUF   1024
 

Functions

static BOOL EchoIncomingPackets (SOCKET sock)
 
DWORD WINAPI EchoHandler (VOID *sock_)
 

Macro Definition Documentation

◆ RECV_BUF

#define RECV_BUF   1024

Definition at line 12 of file echo.c.

Referenced by EchoIncomingPackets().

Function Documentation

◆ EchoHandler()

DWORD WINAPI EchoHandler ( VOID sock_)

Definition at line 69 of file echo.c.

70 {
71  DWORD retVal = 0;
72  SOCKET sock = (SOCKET)sock_;
73 
74  if (!EchoIncomingPackets(sock))
75  {
76  LogEvent(L"Echo: EchoIncomingPackets failed", 0, 0, LOG_FILE);
77  retVal = 1;
78  }
79 
80  LogEvent(L"Echo: Shutting connection down", 0, 0, LOG_FILE);
81 
82  if (ShutdownConnection(sock, TRUE))
83  {
84  LogEvent(L"Echo: Connection is down", 0, 0, LOG_FILE);
85  }
86  else
87  {
88  LogEvent(L"Echo: Connection shutdown failed", 0, 0, LOG_FILE);
89  retVal = 1;
90  }
91 
92  LogEvent(L"Echo: Terminating thread", 0, 0, LOG_FILE);
93  ExitThread(retVal);
94 }
#define TRUE
Definition: types.h:120
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
static BOOL EchoIncomingPackets(SOCKET sock)
Definition: echo.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47

◆ EchoIncomingPackets()

static BOOL EchoIncomingPackets ( SOCKET  sock)
static

Definition at line 15 of file echo.c.

Referenced by EchoHandler().

16 {
17  CHAR readBuffer[RECV_BUF];
18  WCHAR logBuf[256];
19  INT totalSentBytes;
20  INT readBytes;
21  INT retVal;
22 
23  do
24  {
25  readBytes = recv(sock, readBuffer, RECV_BUF, 0);
26  if (readBytes > 0)
27  {
28  swprintf(logBuf, L"Received %d bytes from client", readBytes);
29  LogEvent(logBuf, 0, 0, LOG_FILE);
30 
31  totalSentBytes = 0;
32  while (!bShutdown && totalSentBytes < readBytes)
33  {
34  retVal = send(sock, readBuffer + totalSentBytes, readBytes - totalSentBytes, 0);
35  if (retVal > 0)
36  {
37  swprintf(logBuf, L"Sent %d bytes back to client", retVal);
38  LogEvent(logBuf, 0, 0, LOG_FILE);
39  totalSentBytes += retVal;
40  }
41  else if (retVal == SOCKET_ERROR)
42  {
43  LogEvent(L"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR);
44  return FALSE;
45  }
46  else
47  {
48  /* Client closed connection before we could reply to
49  all the data it sent, so quit early. */
50  LogEvent(L"Peer unexpectedly dropped connection!", 0, 0, LOG_FILE);
51  return FALSE;
52  }
53  }
54  }
55  else if (readBytes == SOCKET_ERROR)
56  {
57  LogEvent(L"Echo: socket error", WSAGetLastError(), 0, LOG_ERROR);
58  return FALSE;
59  }
60  } while ((readBytes != 0) && (!bShutdown));
61 
62  if (!bShutdown)
63  LogEvent(L"Echo: Connection closed by peer", 0, 0, LOG_FILE);
64 
65  return TRUE;
66 }
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
#define SOCKET_ERROR
Definition: winsock.h:333
#define TRUE
Definition: types.h:120
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175
int32_t INT
Definition: typedefs.h:56
#define RECV_BUF
Definition: echo.c:12
#define LOG_ERROR
Definition: tcpsvcs.h:16
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
#define swprintf(buf, format,...)
Definition: sprintf.c:56
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23