Sign in to follow this  
Weng

Raknet: How to receive bitstreams of different sizes?

Recommended Posts

I am using Raknet to develop the multiplayer component of an RPG game. Currently, when receiving packets, I treat all bitstream packets to be of the same size. e.g. p = server->Receive(); //p is a packet* type BitStream bs(p->data,(sizeof(int)+sizeof(int)+sizeof(int),false); bs.Read(message); if(message == NEW_POSITION) { playerStruct player; //a structure for storing position bs.Read(player.x); bs.Read(player.y); bs.Read(player.z); } How do I accept different bitstream packets of different sizes?

Share this post


Link to post
Share on other sites
You send the packet size with the packet (usually 2 bytes). It gets a little hard with TCP because the whole packet might not arrive in one go. If the whole packet doesn't arrive (check the packet size again how much bytes you received. If the bytes you received doesn't match the packet size then you still havn't received the whole packet yet) then you need to store the part of the packet that you did receive for the next time you call the Receive() function.

Here's sort of how i do it (this isn't the actual code):



class Player
{
Socket* socket;
ByteStream recvBuffer;
};

Player::ReceivePackets()
{
char buffer[0xFFFF];
int size = socket->Receive(buffer, sizeof(buffer));

//If we received anything, add it to end of our byte stream
if(size > 0)
recvBuffer.Write(buffer, size);

//While there is more than 2 bytes in the byte stream...
while(recvBuffer.size() > 2)
{
//Get the length of the packet (first 2 bytes)
size_t packetLen = (recvBuffer[0] << 8) + recvBuffer[1];

//Make sure we have the whole packet before trying to process it
if(packetLen + 2 <= recvBuffer.size())
{
//Copy the packet into a new ByteStream
ByteStream packet(recvBuffer, 2, packetLen);

//Remove the packet from the recvBuffer
recvBuffer.Remove(0, packetLen + 2);

//Do whatever you want with the packet...
} else break;

//^Make sure you break from the loop if we havn't receive the whole packet
}

}

Share this post


Link to post
Share on other sites
RakNet is UDP based, so no worries about sending size.

I'm a little rusty, so take this with a pinch of salt. But you should just write a Packet Identifier in the first byte(s)... from there, you already 'know' what that packet contains...

So, you should have constants such as 1= PLAYER_JOINED, 2=PLAYER_QUIT, 3=FOO...

Then, you write one of these 'packet ids' in the first byte(s).

On receiving end, only read out this first bytes, then check it's value, and read the rest of the data accordingly.

Share this post


Link to post
Share on other sites
My packet marshaling library does exactly that; it generates and remember the packet codes for you, and generates generic visitor functions that you can use to serialize, de-serialize, inspect or introspect the packets, using actors that come with the library, or you write yourself (say to generate a packet-to-text dump function).

It comes with explanatory articles and source code, so it's a good resource if you want to write your own, in addition to using it as-is.

Share this post


Link to post
Share on other sites
Good stuff hplus... wish I had this when I was still working with C++.

In C# it took a few hours to make the baddest ass little packet maker class that all my packet structures inherit from, which uses reflection to analyze its members and create ToByte() and FromByte() functions automagically... must have been a huge PITA to do in C++ without a code generator of some sort or preprocessor hacks... Gotta check out the source for curiosity sake.. unless you want to summarize how it works [grin].


EDIT: nevermind, I'm a dummy. checked it out and it's using code generation [grin]. sorry for the laziness.

[Edited by - graveyard filla on November 20, 2008 2:52:54 PM]

Share this post


Link to post
Share on other sites
Alright, i've gotten the network code to be able to receive bitstream packets of different sizes

What i did was to read the packet size before creating a bitstream instance.

Thanks all~

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this