Original Post
Hello everyone, I'm writing a little multiuser game and i ran into a problem. I am useing Structures to communicate and i am useing the solution thats given in the FAQ like this: The problem i ran into is the following: Sometimes the recv-method isnt fast enough and so are more than one packages in the buffer. since i take things out of the buffer and then check for its size i get an error because the package is now twice the size. Since i am useing diffrent packeges i cant just divide tham since i never know which ones are in the buffer. Can anyone point me to a solution for this problem? Or anywhere i could read more on this? Thx a lot in advance!
struct PlayerPosPacket {
unsigned short code;
unsigned short player;
float pos_x, pos_y, pos_z;
float vel_x, vel_y, vel_z;
float heading;
};
PlayerPosPacket packet;
packet.code = PLAYER_POS;
packet.player = thePlayerId;
...
int r = sendto( sock, (char const *)&packet, sizeof(packet), 0, (sockaddr *)&dst_addr, sizeof(dst_addr) );
if( r < 0 ) { error(sock); }
union {
PlayerPosPacket playerPos;
SomeOtherKindOfPacket someOther;
} recvPacket;
int r = recvfrom( sock, (char *)&recvPacket, sizeof( recvPacket ), 0, (sockaddr *)&src_addr, sizeof(src_addr) );
if( r < 0 ) { error(sock); }
/* assume all packet types start with unsigned short code; */
switch( recvPacket.playerPos.code ) {
case PLAYER_POS:
if( r != sizeof(recvPacket.playerPos) ) { formatError(); }
do_playerPos( recvPacket.playerPos, src_addr );
break;
case SOME_OTHER:
if( r != sizeof(recvPacket.someOther) ) { formatError(); }
do_someOther( recvPacket.someOther, src_addr );
break;
...
}