Advertisement

TCP stream breakup/merge

Started by August 18, 2005 05:17 PM
1 comment, last by hplus0603 19 years, 6 months ago
I've going to add networking into my game, but im confused on how to deal with the whole packet thing being split up or merged into multiple/one packet, respectivly. If it's a stream protocal, this means that it can split my data anywhere it wants. So what if I sent a few variables. How would I take the data, in parts, make them into 1 part, then pass them into the variable they belong into. I understand how to read the packets if they are split or merged, but how the hell do I parse it and merge split data, then make a variable= to my merged data...? Not looking for code, just theory/flow chart/step by step.
Black Sky A Star Control 2/Elite like game
Send: Write your packets to the socket (send(), etc.). TCP will buffer the data.

Receive: read data from the TCP socket (multiple reads are required; data will show up in (different sized) chunks through time). Your packets should contain a header that tells you what kind of packet it is which will tell you its size implicitly, or if the packet length is variable, the length is included in the header.

When you have read enough data from the socket to parse a header, read the header to determine how much more data is required before the packet is complete.

When enough data is available to dispatch the packet, cast the buffer to the appropriate packet type and process (implement inline (process directly from the buffer) or copy packet to another buffer if later processing is required (more expensive, as with a threaded implementation)).

The concept is exactly the same as reading and parsing data from a binary file on disk (serialization, etc.).
Advertisement
Step 0: see whether this is mentioned in the Forum FAQ.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement