ReactOS 0.4.16-dev-329-g9223134
echo.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS simple TCP/IP services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/tcpsvcs/echo.c
5 * PURPOSE: Returns whatever input the client sends
6 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10#include "tcpsvcs.h"
11
12#define RECV_BUF 1024
13
14static BOOL
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}
67
70{
71 DWORD retVal = 0;
72 SOCKET sock = (SOCKET)sock_;
73
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
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}
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:365
#define swprintf
Definition: precomp.h:40
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define L(x)
Definition: ntvdm.h:50
static BOOL EchoIncomingPackets(SOCKET sock)
Definition: echo.c:15
#define RECV_BUF
Definition: echo.c:12
DWORD WINAPI EchoHandler(VOID *sock_)
Definition: echo.c:69
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
Definition: tcpcore.h:1455
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
#define LOG_ERROR
Definition: tcpsvcs.h:16
int32_t INT
Definition: typedefs.h:58
#define WINAPI
Definition: msvc.h:6
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:112
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCKET_ERROR
Definition: winsock.h:333
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175