- 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
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"));
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++)
/* 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<LINESIZ; i++)
+ for (i=0; i < LINESIZ - 2; i++)
{
- /* FIXME: Should send lines instead of chars to improve efficiency
- * TCP will wait until it fills a packet anway before putting on
- * the wire, so it's pointless to keep polling send */
- if (!SendChar(Sock, ring[charIndex]))
- {
- return FALSE;
- }
- /* if current char equal last char, reset */
+ Line[i] = ring[charIndex];
+
if (ring[charIndex] == *endring)
charIndex = 0;
else
charIndex++;
}
- if (bLoop)
- if ((!SendChar(Sock, L'\r')) || (!SendChar(Sock, L'\n')))
- return FALSE;
+ Line[LINESIZ - 2] = L'\r';
+ Line[LINESIZ - 1] = L'\n';
+
+ if (!SendLine(Sock, Line))
+ break;
/* increment loop index to start printing from next char in ring */
loopIndex++;
return TRUE;
}
-BOOL SendChar(SOCKET Sock, TCHAR c)
+BOOL SendLine(SOCKET Sock, TCHAR* Line)
{
INT RetVal;
INT SentBytes;
+ INT LineSize;
+
+ LineSize = sizeof(TCHAR) * LINESIZ;
SentBytes = 0;
- RetVal = send(Sock, &c, sizeof(TCHAR), 0);
+ RetVal = send(Sock, Line, LineSize, 0);
/*FIXME: need to establish if peer closes connection,
not just report a socket error */
if (RetVal > 0)
{
+ if (RetVal != LineSize)
+ {
+ _tprintf(("Not sent enough\n"));
+ return FALSE;
+ }
SentBytes += RetVal;
return TRUE;
}
--- /dev/null
+#include <stdio.h>\r
+#include <winsock2.h>\r
+#include <tchar.h>\r
+#include "tcpsvcs.h"\r
+\r
+DWORD WINAPI DiscardHandler(VOID* Sock_)\r
+{\r
+ DWORD Retval = 0;\r
+ SOCKET Sock = (SOCKET)Sock_;\r
+\r
+ if (!RecieveIncomingPackets(Sock))\r
+ {\r
+ _tprintf(_T("RecieveIncomingPackets failed\n"));\r
+ Retval = 3;\r
+ }\r
+\r
+ _tprintf(_T("Shutting connection down...\n"));\r
+ if (ShutdownConnection(Sock, TRUE))\r
+ {\r
+ _tprintf(_T("Connection is down.\n"));\r
+ }\r
+ else\r
+ {\r
+ _tprintf(_T("Connection shutdown failed\n"));\r
+ Retval = 3;\r
+ }\r
+ _tprintf(_T("Terminating thread\n"));\r
+ ExitThread(0);\r
+\r
+ return Retval;\r
+}\r
+\r
+\r
+\r
+BOOL RecieveIncomingPackets(SOCKET Sock)\r
+{\r
+ TCHAR ReadBuffer[BUF];\r
+ INT ReadBytes;\r
+\r
+ do\r
+ {\r
+ ReadBytes = recv(Sock, ReadBuffer, BUF, 0);\r
+ if (ReadBytes > 0)\r
+ _tprintf(_T("Received %d bytes from client\n"), ReadBytes);\r
+ else if (ReadBytes == SOCKET_ERROR)\r
+ {\r
+ _tprintf(("Socket Error: %d\n"), WSAGetLastError());\r
+ return FALSE;\r
+ }\r
+ } while (ReadBytes > 0);\r
+\r
+ _tprintf(("Connection closed by peer.\n"));\r
+ return TRUE;\r
+}\r
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"));
}
}
}
- else if (ReadBytes == SOCKET_ERROR) {
+ else if (ReadBytes == SOCKET_ERROR)
return FALSE;
- }
+
} while (ReadBytes != 0);
_tprintf(("Connection closed by peer.\n"));
#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_)
#include <tchar.h>
#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];
+ HANDLE hThread[NUM_SERVICES];
+ INT i;
+
+ /* Create MAX_THREADS worker threads. */
+ for( i=0; i<NUM_SERVICES; i++ )
+ {
+ /* 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; i<NUM_SERVICES; i++)
+ {
+ CloseHandle(hThread[i]);
+ }
+ return 0;
+}
+
+#if 0
+static VOID CALLBACK
+ServiceMain(DWORD argc, LPTSTR *argv)
{
PSERVICES pServices[NUM_SERVICES];
DWORD dwThreadId[NUM_SERVICES];
{
/* Allocate memory for thread data. */
pServices[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SERVICES));
-
+
if( pServices[i] == NULL )
ExitProcess(2);
CloseHandle(hThread[i]);
}
+}
+
+int
+main(int argc, char *argv[])
+{
+ DPRINT("tcpsvcs: main() started\n");
+
+ StartServiceCtrlDispatcher(ServiceTable);
+
+ DPRINT("Umpnpmgr: main() done\n");
+
+ ExitThread(0);
+
return 0;
}
+#endif
/* default port numbers */
#define ECHO_PORT 7
-#define CHARGEN_PORT 19
-#define DAYTIME_PORT 13
#define DISCARD_PORT 9
+#define DAYTIME_PORT 13
#define QOTD_PORT 17
+#define CHARGEN_PORT 19
#define NUM_SERVICES 6
#define BUF_SIZE 255
#define END 126
/* number of chars to put on a line */
-#define LINESIZ 72
+#define LINESIZ 74 // 72 + /r and /n
/* data structure to pass to threads */
typedef struct _Services {
LPTHREAD_START_ROUTINE Service;
} SERVICES, *PSERVICES;
+/* tcpsvcs functions */
+//static VOID CALLBACK ServiceMain(DWORD argc, LPTSTR *argv);
+
/* skelserver functions */
DWORD WINAPI StartServer(LPVOID lpParam);
SOCKET SetUpListener(const char* ServAddr, int Port);
/* chargen functions */
DWORD WINAPI ChargenHandler(VOID* Sock_);
BOOL GenerateChars(SOCKET Sock);
-BOOL SendChar(SOCKET Sock, CHAR c);
+BOOL SendLine(SOCKET Sock, TCHAR* Line);
/* daytime functions */
DWORD WINAPI DaytimeHandler(VOID* Sock_);
BOOL EchoIncomingPackets(SOCKET Sock);
/* discard functions */
-
+DWORD WINAPI DiscardHandler(VOID* Sock_);
+BOOL RecieveIncomingPackets(SOCKET Sock);
/* qotd functions */
DWORD WINAPI QotdHandler(VOID* Sock_);
<file>tcpsvcs.c</file>
<file>skelserver.c</file>
<file>echo.c</file>
- <file>chargen.c</file>
+ <file>discard.c</file>
<file>daytime.c</file>
<file>qotd.c</file>
+ <file>chargen.c</file>
+ <file>tcpsvcs.rc</file>
</module>
-