From af01d7edd088283036530a26bebe55d6cd8e2d36 Mon Sep 17 00:00:00 2001 From: Ged Murphy Date: Thu, 29 Sep 2005 20:41:12 +0000 Subject: [PATCH] discard.c - implemented discard. chargen.c - improve efficiency sending full line instead of singular chars. TCP will generally wait until the packet is full anyway, so it's pointless to keep calling send for each char. tcpsvcs.c - implement the beginnings of the code to start tcpsvcs as a service. #if 0'd out at the moment. Fix bug from calling non existant DiscardHandler. Surely this is enough info to satisfy lkcl this time :p svn path=/trunk/; revision=18159 --- reactos/apps/utils/net/tcpsvcs/chargen.c | 53 ++++++------ reactos/apps/utils/net/tcpsvcs/discard.c | 54 ++++++++++++ reactos/apps/utils/net/tcpsvcs/echo.c | 19 +++-- reactos/apps/utils/net/tcpsvcs/qotd.c | 4 +- reactos/apps/utils/net/tcpsvcs/tcpsvcs.c | 96 +++++++++++++++++++--- reactos/apps/utils/net/tcpsvcs/tcpsvcs.h | 14 ++-- reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml | 5 +- 7 files changed, 194 insertions(+), 51 deletions(-) create mode 100644 reactos/apps/utils/net/tcpsvcs/discard.c diff --git a/reactos/apps/utils/net/tcpsvcs/chargen.c b/reactos/apps/utils/net/tcpsvcs/chargen.c index 47ba87c6a25..8ce58252f4c 100644 --- a/reactos/apps/utils/net/tcpsvcs/chargen.c +++ b/reactos/apps/utils/net/tcpsvcs/chargen.c @@ -8,15 +8,15 @@ DWORD WINAPI ChargenHandler(VOID* Sock_) DWORD Retval = 0; SOCKET Sock = (SOCKET)Sock_; - if (!GenerateChars(Sock)) { + if (!GenerateChars(Sock)) + { _tprintf(_T("Char generation failed\n")); Retval = 3; } _tprintf(_T("Shutting connection down...\n")); - if (ShutdownConnection(Sock, FALSE)) { + if (ShutdownConnection(Sock, FALSE)) _tprintf(_T("Connection is down.\n")); - } else { _tprintf(_T("Connection shutdown failed\n")); @@ -31,12 +31,12 @@ DWORD WINAPI ChargenHandler(VOID* Sock_) BOOL GenerateChars(SOCKET Sock) { - int i, - charIndex, /* internal loop */ - loopIndex; /* line loop */ + int i; + int charIndex; /* internal loop */ + int loopIndex; /* line loop */ char ring[END-START]; char *endring; - BOOL bLoop = TRUE; + char Line[LINESIZ]; /* fill ring with printable characters */ for (charIndex=0, i=START; i<=END; charIndex++, i++) @@ -46,35 +46,30 @@ BOOL GenerateChars(SOCKET Sock) /* where we will start output from */ loopIndex = 0; - - while (bLoop) + while (1) { - /* if the loop index is equal to number of chars previously - * printed, start the loop from the beginning */ + /* if the loop index is equal to the last char, + * start the loop again from the beginning */ if (loopIndex == END-START) loopIndex = 0; /* start printing from char controled by loopIndex */ charIndex = loopIndex; - for (i=0; i 0) { + if (RetVal != LineSize) + { + _tprintf(("Not sent enough\n")); + return FALSE; + } SentBytes += RetVal; return TRUE; } diff --git a/reactos/apps/utils/net/tcpsvcs/discard.c b/reactos/apps/utils/net/tcpsvcs/discard.c new file mode 100644 index 00000000000..aa2ff5fb0b6 --- /dev/null +++ b/reactos/apps/utils/net/tcpsvcs/discard.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include "tcpsvcs.h" + +DWORD WINAPI DiscardHandler(VOID* Sock_) +{ + DWORD Retval = 0; + SOCKET Sock = (SOCKET)Sock_; + + if (!RecieveIncomingPackets(Sock)) + { + _tprintf(_T("RecieveIncomingPackets failed\n")); + Retval = 3; + } + + _tprintf(_T("Shutting connection down...\n")); + if (ShutdownConnection(Sock, TRUE)) + { + _tprintf(_T("Connection is down.\n")); + } + else + { + _tprintf(_T("Connection shutdown failed\n")); + Retval = 3; + } + _tprintf(_T("Terminating thread\n")); + ExitThread(0); + + return Retval; +} + + + +BOOL RecieveIncomingPackets(SOCKET Sock) +{ + TCHAR ReadBuffer[BUF]; + INT ReadBytes; + + do + { + ReadBytes = recv(Sock, ReadBuffer, BUF, 0); + if (ReadBytes > 0) + _tprintf(_T("Received %d bytes from client\n"), ReadBytes); + else if (ReadBytes == SOCKET_ERROR) + { + _tprintf(("Socket Error: %d\n"), WSAGetLastError()); + return FALSE; + } + } while (ReadBytes > 0); + + _tprintf(("Connection closed by peer.\n")); + return TRUE; +} diff --git a/reactos/apps/utils/net/tcpsvcs/echo.c b/reactos/apps/utils/net/tcpsvcs/echo.c index 3fc51fd358b..1acc731b97e 100644 --- a/reactos/apps/utils/net/tcpsvcs/echo.c +++ b/reactos/apps/utils/net/tcpsvcs/echo.c @@ -39,21 +39,24 @@ BOOL EchoIncomingPackets(SOCKET Sock) do { ReadBytes = recv(Sock, ReadBuffer, BUF, 0); - if (ReadBytes > 0) { + if (ReadBytes > 0) + { _tprintf(_T("Received %d bytes from client\n"), ReadBytes); SentBytes = 0; - while (SentBytes < ReadBytes) { + while (SentBytes < ReadBytes) + { Temp = send(Sock, ReadBuffer + SentBytes, ReadBytes - SentBytes, 0); - if (Temp > 0) { + if (Temp > 0) + { _tprintf(_T("Sent %d bytes back to client\n"), Temp); SentBytes += Temp; } - else if (Temp == SOCKET_ERROR) { + else if (Temp == SOCKET_ERROR) return FALSE; - } - else { + else + { /* Client closed connection before we could reply to all the data it sent, so quit early. */ _tprintf(_T("Peer unexpectedly dropped connection!\n")); @@ -61,9 +64,9 @@ BOOL EchoIncomingPackets(SOCKET Sock) } } } - else if (ReadBytes == SOCKET_ERROR) { + else if (ReadBytes == SOCKET_ERROR) return FALSE; - } + } while (ReadBytes != 0); _tprintf(("Connection closed by peer.\n")); diff --git a/reactos/apps/utils/net/tcpsvcs/qotd.c b/reactos/apps/utils/net/tcpsvcs/qotd.c index 8eb222312f1..aa4c5ba1598 100644 --- a/reactos/apps/utils/net/tcpsvcs/qotd.c +++ b/reactos/apps/utils/net/tcpsvcs/qotd.c @@ -5,13 +5,15 @@ #include "tcpsvcs.h" //these need putting in an RC file. -TCHAR Quotes[][MAX_QUOTE_BUF] = { +TCHAR Quotes[][MAX_QUOTE_BUF] = +{ _T("\"I have a penchant for mischief, property damage, stalking and cheesecake, of course\" - kjk hyperion"), _T("\"Wow! I fixed a setmenu bug.\" - jimtabor"), _T("\"if the code is broken though, your free to call it ur own\" - Alex Ionescu"), _T("\"i don't know about any bug; none exist; ReactOS is prefect\" - filip2307"), _T("\"if you were kernel code, cutler would rewrite you.\" - Alex Ionescu"), _T("\"Looks like Hartmut is cleaning out his WC. working copy, that is\" - WaxDragon") + _T("\"don't question it ... it's clearly an optimization\" - arty") }; DWORD WINAPI QotdHandler(VOID* Sock_) diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c index 9dbb9e028d0..3db7aadf6cc 100644 --- a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c +++ b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c @@ -3,24 +3,38 @@ #include #include "tcpsvcs.h" +static LPTHREAD_START_ROUTINE -ServiceHandler[NUM_SERVICES] = { +ServiceHandler[NUM_SERVICES] = +{ EchoHandler, - ChargenHandler, + DiscardHandler, DaytimeHandler, - NULL, - QotdHandler + QotdHandler, + ChargenHandler }; -INT ServicePort[NUM_SERVICES] = { +static int +ServicePort[NUM_SERVICES] = +{ ECHO_PORT, - CHARGEN_PORT, - DAYTIME_PORT, DISCARD_PORT, - QOTD_PORT + DAYTIME_PORT, + QOTD_PORT, + CHARGEN_PORT }; -int main(int argc, char *argv[]) +#if 0 +static +SERVICE_TABLE_ENTRY +ServiceTable[2] = +{ + {TEXT("tcpsvcs"), ServiceMain}, + {NULL, NULL} +}; +#endif + +int main(void) { PSERVICES pServices[NUM_SERVICES]; DWORD dwThreadId[NUM_SERVICES]; @@ -32,7 +46,55 @@ int main(int argc, char *argv[]) { /* Allocate memory for thread data. */ pServices[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICES)); - + + if( pServices[i] == NULL ) + ExitProcess(2); + + /* Generate unique data for each thread. */ + pServices[i]->Service = ServiceHandler[i]; + pServices[i]->Port = ServicePort[i]; + + hThread[i] = CreateThread( + NULL, // default security attributes + 0, // use default stack size + StartServer, // thread function + pServices[i], // argument to thread function + 0, // use default creation flags + &dwThreadId[i]); // returns the thread identifier + + /* Check the return value for success. */ + if (hThread[i] == NULL) + { + ExitProcess(i); + } + } + + /* Wait until all threads have terminated. */ + WaitForMultipleObjects(NUM_SERVICES, hThread, TRUE, INFINITE); + + /* Close all thread handles upon completion. */ + for(i=0; itcpsvcs.c skelserver.c echo.c - chargen.c + discard.c daytime.c qotd.c + chargen.c + tcpsvcs.rc -