Don't display exit code for none fatal info
[reactos.git] / reactos / services / tcpsvcs / echo.c
1 /*
2 * ReactOS Services
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * LICENCE: GPL - See COPYING in the top level directory
6 * PROJECT: ReactOS simple TCP/IP services
7 * FILE: apps/utils/net/tcpsvcs/echo.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
10 * REVISIONS:
11 * GM 04/10/05 Created
12 *
13 */
14
15 #include "tcpsvcs.h"
16
17 DWORD WINAPI EchoHandler(VOID* Sock_)
18 {
19 DWORD RetVal = 0;
20 SOCKET Sock = (SOCKET)Sock_;
21
22 if (!EchoIncomingPackets(Sock)) {
23 LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE);
24 RetVal = -1;
25 }
26
27 LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE);
28
29 if (ShutdownConnection(Sock, TRUE))
30 LogEvent(_T("Echo: Connection is down\n"), 0, FALSE);
31 else
32 {
33 LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE);
34 RetVal = -1;
35 }
36
37 LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE);
38 ExitThread(RetVal);
39 }
40
41
42
43 BOOL EchoIncomingPackets(SOCKET Sock)
44 {
45 TCHAR ReadBuffer[BUF];
46 TCHAR temp[512]; // temp for holding LogEvent text
47 INT Temp;
48 INT ReadBytes;
49 INT SentBytes;
50
51 do {
52 ReadBytes = recv(Sock, ReadBuffer, BUF, 0);
53 if (ReadBytes > 0)
54 {
55 _stprintf(temp, _T("Received %d bytes from client\n"), ReadBytes);
56 LogEvent(temp, 0, FALSE);
57
58 SentBytes = 0;
59 while (SentBytes < ReadBytes)
60 {
61 Temp = send(Sock, ReadBuffer + SentBytes,
62 ReadBytes - SentBytes, 0);
63 if (Temp > 0)
64 {
65 _stprintf(temp, _T("Sent %d bytes back to client\n"), Temp);
66 LogEvent(temp, 0, FALSE);
67 SentBytes += Temp;
68 }
69 else if (Temp == SOCKET_ERROR)
70 return FALSE;
71 else
72 {
73 /* Client closed connection before we could reply to
74 all the data it sent, so quit early. */
75 _stprintf(temp, _T("Peer unexpectedly dropped connection!\n"));
76 LogEvent(temp, 0, FALSE);
77 return FALSE;
78 }
79 }
80 }
81 else if (ReadBytes == SOCKET_ERROR)
82 return FALSE;
83
84 } while (ReadBytes != 0);
85
86 LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
87 return TRUE;
88 }