Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MindWipe

Network packet handling problems..

This topic is 5658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on my network class using DPlay. And I've been working on the packet handling system. Well for you who don't know DPlay it's not that confusing: I've got: struct DPN_BUFFER_DESC { DWORD dwBufferSize; BYTE* pBufferData; } okey... so this is my idea: I've got two DWORDS as the files header. My use for it doesn't really matter except the first DWORD specifies the type of data found in the buffer. One packet looks like this: DWORD | DWORD | ...(dynamic mem) So I've got my: sturct PACKET { DWORD dwType; DWORD dwSendRecv; (don't worry about this now) BYTE *pbyData; } Okey.. well, when I want to send some player info to a client I send a: struct PLAYER { DWORD dwPlayer; char szName[64]; }; in the dynamic mem(in this case it has a set size) and set the dwType to MSGID_PLAYERINFO.. mmmkey?
quote:
bool CNetwork::Server_SendPlayerInfo( DWORD dwPlayerID, ... ) { DPN_BUFFER_DESC dpnBuffer; PLAYER player; PACKET datapacket; Player.dwPlayer = m_Player[ dwPlayerID ].dwPlayer; strcpy( player.szName, m_Player[ dwPlayerID ].szName ); datapacket.dwType = MSGID_PLAYERINFO; datapacket.dwSendRecv = ... datapacket.pbyData; = new unsigned char[sizeof(PLAYER)]; memcpy( datapacket.*pbyData, &player, sizeof(PLAYER) ); dpnBuffer.pBufferData = (unsigned char*)&datapacket; dpnBuffer.dwBufferSize = 8 + sizeof(PLAYER); ... }
then when I receive it and check the *(DWORD*) ReceivedBuffer == MSGID_PLAYINFO I know if it includes player info. Well, then shouldn't it work to have: PLAYER *pPlayer; pPlayer = (PLAYER*)(((DWORD*)ReceivedBuffer)+2); shouldn't that work? Somehow I'm doing something wrong!? One other question. Should this work: PACKET *pPacket; pPacket = (PACKET*)ReceivedBuffer; pPlacyer = pPacket->pbyData; Shouldn't all this work!? But somehow the header is fine (the two dwords) but the rest is screwed up /MindWipe [edited by - MindWipe on December 2, 2002 10:49:05 AM]

Share this post


Link to post
Share on other sites
Advertisement
Generally, when people make a packet structure, it is all one big contiguous block of memory. Your structure is 2 DWORDS followed by a pointer that possibly references some other memory somewhere on the heap. Not to say you can''t do it that way. You can. You just need to be more careful in sending.


I''m not sure if this is your problem but it may be. When you allocate space for the player object, it does not immediately follow the 2 DWORDs in your packet structure. The OS finds some free heap space, marks it as used, and returns the address of it which is stored in your pData. This means that send() calls of length 2 + sizeof(Player) produce gibberish since the packet is only 9 bytes long (actually more like 12 after the padding for word alignment is added). The data that follows is just whatever happened to be allocated in that space. Most likely nothing you want.


The call you make:

memcpy( datapacket.*pbyData, &player, sizeof(PLAYER) );

also seems kinda suspect. I''m not actually sure what happens with that or even if it would compile. I would normally write:

memcpy( datapacket.pbyData, &player, sizeof(PLAYER) );

to accomplish copying the player data into the newly allocated memory.


On the receiving side, you can do those things calls that you mentioned but in order for them to work, the code must''ve been compiled with the same compiler generally. Otherwise, differences in padding the structures can cause problems. Also, make sure that the data you use to construct the objects doesn''t get deallocated immediately like in the case:


Player* getPlayer()
{
char buffer[256];
Player *player;

...

ReceiveInfo(buffer);
player = (Player*) &buffer[2];

...

return player;
}



The line:

pPlayer = pPacket->pbyData;

is completely wrong though since pbyData holds a memory address instead of the actual data...and that memory address is certain to be wrong on the receiving computer.


My advice to you would be to go over pointers and memory first. It doesn''t seem to me like you have a completely firm grasp on them. And use the debugger to determine what exactly is in your packet and is being sent/received on both the sender and receiver side.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!