Advertisement

Uh... Clean network?

Started by December 21, 2003 02:16 AM
2 comments, last by shadowman13131 21 years, 1 month ago
Hey, I ALWAYS have problems with my network code. It clutters everything, with all of those did-I-receive-this checks and intermittent sending and AHHHHHH! How does one make good network code? That looks neat? And integrates into programs in general??? Anyone? Walt
quote:
Original post by shadowman13131
Hey,
I ALWAYS have problems with my network code. It clutters everything, with all of those did-I-receive-this checks and intermittent sending and AHHHHHH! How does one make good network code? That looks neat? And integrates into programs in general??? Anyone?

Walt


Wrap it all up into a class that does all that crap "Behind the scenes", so your app only needs to worry about a few things, like:

class Socket_C{private: unsigned char SocketState; //Stores the state of our socket//Function pointer of a function to call when data arrives! void (*OnDataArrival)(char *Data, unsigned long Length);public: unsigned char CheckState(void) { return SocketState; }; Socket_C(void (*ptrFunc)(char*,unsigned long)); void Connect(char *IP, unsigned short Port); void Listen(unsigned short Port); void SendData(void *data, unsigned long length); void Disconnect(void); ~Socket_C(void);};


You would write all the code behind this, and when a send or receive is called, attach a packet size to it, this way you know how large each packet is for receiving. When you receive a packet, you first check it''s length, then you read an entire packet when available, and call whatever function the user passed in the constructor. The function only has to worry about when a FULL packet has arrived, and it doesn''t have to do anything fancy. Also, you can check the state by calling CheckState, this variable should be set whenever anything happens (good or bad), so the user can determine what''s going on (connection, connected, disconnected, listening, etc). This would then be highly reusable in anything, you''d just drop this + source into your project, create a new Socket_C with whatever packet handler function you want, and the rest is as easy as a few function calls.
Advertisement
You could try to write something like ReplicaNet from www.replicanet.com
What this does is have a clever automatic class member variable update database so when you change variables in one class the other classes on other machines get the same data. It is all automatic so when you''re trying to write a multiplayer game you don''t have to worry about the network.
Keep it Object Oriented, as the above poster said.

This topic is closed to new replies.

Advertisement