Designing a protocol

Started by
2 comments, last by wood_brian 16 years, 3 months ago
Im currently thinking about the network and event structure of my game (I read that event driven games make it very easy to make them multiplayer) but since Im new to these two topics, Id like some advice on how to implement it from you guys. My biggest problem at the moment is that my classes are full of pointers, which are invalid if I send them over the net, since the target machine doesnt have the same memory layout. For example, Id like to know how I could send the information stored in the map contained in this class (the map contains pointers to lists) over to another machine. I already figured out that I need a "mapinfo-event" or something like that, but I really got no idea on how to actually implement it so that I can transfer it via TCP.. And if someone knows some examples on networking with events, please tell me where I can find them (sourcecode would be good). In my books there are only vague and general examples of how to implement this, but I just lack the concept on how to design the events ...

namespace server
{
	class GalaxyMap
	{
		typedef std::map<Coordinate, std::list<Stellarobject*>, Coordcompare > Galaxy_map;
		typedef std::list<Stellarobject*> Stellar_list;

		typedef Galaxy_map::value_type map_type;

		public:
							GalaxyMap(GameInfo *new_info);
			virtual			~GalaxyMap();

			void 			set_size(int new_size_width, int new_size_height);
			void 			add_item(Stellarobject *new_stellar, int new_x_pos, int new_y_pos);
			void 			remove_item(Stellarobject *curr_stellar, int x_pos, int y_pos);
			void			create_starsystems(int number);
			void 			delete_starsystems();
			bool 			check_pos(int x_pos, int y_pos);

			Stellar_list 	&get_list(int x_pos, int y_pos);

		private:
			
			GameInfo		*curr_info;
			
			int 			size_width;
			int 			size_height;
			Galaxy_map 		curr_map;

			bool system_highlighted;
};

}

Advertisement
I'm no master of network programming, but i'd write a serialization interface that my network classes would implement. Something like:

class INetworkSerialization{    virtual void serialize(BYTE* &data, UINT &size) = 0;    virtual void unserialize(BYTE* data, UINT size) = 0;};


Each network class would implement these functions in their own way. Pack the data into a binary buffer that can be sent over the network. I think I actually used this scheme for some network game I wrote long ago.
You really don't want to send polymorphic classes over the network. It adds a ton of information that complicates things a lot. If you need to, assign a unique static ID to every class, and send that before each pointer, then serialized data after that.

Another problem with pointers you'll need to solve is memory management. A lot of things can go wrong, and unless you use some way to handle them reliably, you're begging for memory leaks. Using smart pointers will usually help with that.

Another problem are complex references, or in worst case - circular references. What happens if two StellarObjects reference each other. A naive serializer will go into an infinite loop.

A general overview of how to do this, although that approach is somewhat bulky for networking, which is better kept as lightweight as possible.

It also doesn't address the delta state propagation (sending only changes between two states, instead of full state on every frame), which will keep your network traffic down to managable size.

It's really not an easy topic that can be addressed in a few lines.
Quote:Original post by Antheus
>You really don't want to send polymorphic classes over the network.
>It adds a ton of information that complicates things a lot. If you
>need to, assign a unique static ID to every class, and send that
>before each pointer, then serialized data after that.

That is the approach I take here - www.webebenezer.net.
The unique IDs are generated as part of the process.

[snip]

>It also doesn't address the delta state propagation (sending only
>changes between two states, instead of full state on every frame),
>which will keep your network traffic down to managable size.

I think we have good support for that.

>It's really not an easy topic that can be addressed in a few lines.



Brian Wood
Ebenezer Enterprises

[Edited by - wood_brian on December 23, 2007 2:10:48 PM]

This topic is closed to new replies.

Advertisement