edxTCPNetwork - Almost Done! !

posted in Old Stuff
Published October 04, 2008
Advertisement
I've been hard at work the past few days improving and bug testing my TCP network library. Right now it's going great! I've finally gotten it so it is nicely organized and does not make use of C-style callbacks. Everything is done through an OOP design, so it is very easy to use to add networking capabilities to a project. Stay tuned for a post in the networking forum soon asking for a critique on it! Here's a preview of a simple client and server using the library.

Server
#ifdef _DEBUG	#include "../debug/edxTCPNetwork.h"	#pragma comment(lib, "../debug/edxTCPNetwork_d.lib")#else	#include "../release/edxTCPNetwork.h"	#pragma comment(lib, "../release/edxTCPNetwork.lib")#endif#include #include //-----------------------------------------------------------------------------// This class represents the simplest server class you may have.class SkeletonTCPServer : public edx::TCPServer{public:	// Default ctor	SkeletonTCPServer()	{	}	// Default dtor	~SkeletonTCPServer()	{	}	// User function called when a client connects to the server	bool TCPOnConnect(edx::TCPClientInterface * client, const CHAR * ip, WORD port)	{		printf("[SkeletonTCPServer]Client [%X] connecting from %s:%i\n", PtrToUlong(client), ip, port);		return true;	}	// User function called when a client sends data to the server. Should return -1	// if the stream of data is not processable (needs more data) or a value > 0	// that represents how much data was processed.	int TCPOnProcessStream(edx::TCPClientInterface * client, WORD size, LPBYTE stream)	{		printf("[SkeletonTCPServer]Received %i bytes from client [%X].\n", size, PtrToUlong(client));		return -1;	}	// User function called when a client closes the connection	void TCPOnClose(edx::TCPClientInterface * client)	{		printf("[SkeletonTCPServer]Client [%X] disconnected.\n", PtrToUlong(client));	}	// User function called when a client encounters an error on the server 	// (not required, but a good idea)	void TCPOnError(edx::TCPClientInterface * client, CONST CHAR * file, CONST CHAR * function, CONST INT line, CONST TCHAR * error)	{		printf("[SkeletonTCPServer]Error generated on client [%X] - %s\n", PtrToUlong(client), error);	}};//-----------------------------------------------------------------------------// Entry pointint main(int argc, char * argv[]){	UNREFERENCED_PARAMETER(argc);	UNREFERENCED_PARAMETER(argv);	// Server object	SkeletonTCPServer server;	// Setup Winsock	if(edx::InitializeWinsock(2,2) == FALSE)		return 0;	// Try to create the server on port 16000	if(server.Create(16000) == false)	{		// Clean up the server		server.Destroy();		// Cleanup Winsock		edx::DeinitializeWinsock();		// Standard return		return 0;	}	// Just let the server run for 15 seconds so we can start a client	Sleep(15000);	// Clean up the server	server.Destroy();	// Cleanup Winsock	edx::DeinitializeWinsock();	// Keep output on screen	system("pause");	// Standard return	return 0;}//-----------------------------------------------------------------------------


Client
#ifdef _DEBUG	#include "../debug/edxTCPNetwork.h"	#pragma comment(lib, "../debug/edxTCPNetwork_d.lib")#else	#include "../release/edxTCPNetwork.h"	#pragma comment(lib, "../release/edxTCPNetwork.lib")#endif//-----------------------------------------------------------------------------// This class represents the simplest client class you may have.class SkeletonTCPClient : public edx::TCPClient{public:	// Default ctor	SkeletonTCPClient()	{	}	// Default dtor	~SkeletonTCPClient()	{	}	// User function called when a client connects to the server	bool TCPOnConnect(edx::TCPClientInterface * client, const CHAR * ip, WORD port)	{		printf("[SkeletonTCPClient]Client [%X] connecting to %s:%i\n", PtrToUlong(client), ip, port);		return true;	}	// User function called when a client sends data to the server. Should return -1	// if the stream of data is not processable (needs more data) or a value > 0	// that represents how much data was processed.	int TCPOnProcessStream(edx::TCPClientInterface * client, WORD size, LPBYTE stream)	{		printf("[SkeletonTCPClient]Received %i bytes from server [%X].\n", size, PtrToUlong(client));		return -1;	}	// User function called when a client closes the connection	void TCPOnClose(edx::TCPClientInterface * client)	{		printf("[SkeletonTCPClient]Client [%X] disconnected.\n", PtrToUlong(client));	}	// User function called when a client encounters an error on the server 	// (not required, but a good idea)	void TCPOnError(edx::TCPClientInterface * client, CONST CHAR * file, CONST CHAR * function, CONST INT line, CONST TCHAR * error)	{		printf("[SkeletonTCPClient]Error generated on client [%X] - %s\n", PtrToUlong(client), error);	}};//-----------------------------------------------------------------------------// Entry pointint main(int argc, char * argv[]){	UNREFERENCED_PARAMETER(argc);	UNREFERENCED_PARAMETER(argv);	// Client object	SkeletonTCPClient client;	// Setup Winsock	if(edx::InitializeWinsock(2,2) == FALSE)		return 0;	// Try to connect to our local server	if(client.Connect("127.0.0.1", 16000, false) == false)	{		// Clean up the client		client.Destroy();		// Cleanup Winsock		edx::DeinitializeWinsock();		// Standard return		return 0;	}	// Keep it connected for 5 seconds	Sleep(5000);	// Clean up the client	client.Destroy();	// Cleanup Winsock	edx::DeinitializeWinsock();	// Keep output on screen	system("pause");	// Standard return	return 0;}//-----------------------------------------------------------------------------


Pretty simple if I might say! What is not shown is any "packet" handling because there is no integrated handling to the library. The approach I took was to make a library that would do all of the hard work of using TCP, then expose a function that gives you data as it's received and then you can implement your own custom data handling from there without being tied to the network code specifically.

Right now I am working on my generic packet reader and packet builder classes and then a simple protocol to sit on top of the stream processing to show why I am proud of the design. Stay tuned!
Previous Entry Network Code Updated!
Next Entry Viva la revolucion!
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement