|
-
October 27th, 2005, 03:02 PM
#14
Re: WinsockUDP + recv() + MSG_PEEK = WSAEMSGSIZE
Here's another example of packet-type identification, which is based on a union. I found the example at http://www.gamedev.net/community/for...sp?forum_id=15. In general, if you have not been there already, you really should visit and search over at the www.gamedev.net site, since you're writing a game:
Code:
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;
...
}
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|