Sending data questions

Started by
10 comments, last by jeffreyp23 11 years, 10 months ago
Hey guys,

I have a question about sending data in UDP.
For example i have this structure :
[source]

structure NetworkPacket
{
char *m_data;
int m_id;
};

[/source]

And i fill it like this :

[source]
SomeStructure l_data;
...
NetworkPacket l_packet;
l_packet.m_data = (char*)(&l_data);
l_packet.m_id = 1;
[/source]

And send it / receive it like this
[source]

sendto(socket, (char*)(&l_packet), sizeof(NetworkPacket), 0, (sockaddr*)&address,
sizeof(sockaddr));

NetworkPacket l_packet;

auto l_bytes_received = 0;

l_bytes_received = recvfrom(socket, (char*)(&l_packet), sizeof(NetworkPacket), 0,
(sockaddr*)&l_incomingAddress, &l_size)
[/source]

Is it true that this will fail because in the structure NetworkPacket the char * is dynamic so it's size isn't known in the structure declaration?
Also i had an idea about solving it if the above is true.
What about just sending a packet with the size of the packet that will come next in it?
But this will fail if the packets got reordered somewhere. Does that happen often in UDP?
If so then this design isn't really good, and if it doesn't should i just ignore that and send the packets again that where lost?

Thanks in advance.
Advertisement
A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids

unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &sizetosend, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte :P
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

A pointer variable is really either a 32 bit or 64 bit number --depends on whether you build your application as a 32 bit or a 64 bit. So, when you send that structure --assuming a 32 bit build-- you are sending a total of 64 bits, 32 for m_data and 32 for m_id.

The number that the pointer holds is a memory address. If you want to send a variable length array, you can, but you need to do your own packing.

also , use unsigned shorts, which are 16 bits. You will likely not need 32 bit packet ids

unsigned short int packetid = 45;// best to use an enum for packet ids so you can reference them with words
std::string npcname = "big bad monster";
unsigned short int sizetosend = npcname.size() +sizeof(packetid) + sizeof(unsigned short int)*2;// 2 is for the size of the string 2 is for the m_id variable, 2 is for the size of the payload
char* data = new char[sizetosend];
memcpy(data, &packetid, sizeof(packetid));
memcpy(data+sizeof(packetid), &sizetosend, sizeof(sizetosend));// size of the payload so on the other side, you can copy the data correctly
memcpy(data + sizeof(packetid) +sizeof(sizetosend), npcname.c_str(), npcname.size());// there is no null terminator being copied in here to save a byte :P
sendto(socket, data, sizetosend, 0, (sockaddr*)&address, sizeof(sockaddr));



Oke thanks, but how will i handle this on the receiving end?
Because the information about the size is packed in the packet.
So i dont't know how big the packet is gonna be on the receiving end.
And i need that information in the recvfrom function.
Or am i missing something?
Or am i missing something? [/quote]

It sounds like you're missing a basic understanding of how data is represented and referenced in memory, and in external storage.
If you were to save this same data to a file, such that you could later read it back into memory, how would you do it?
In this sense, the payload of a network packet is like a file on disk. The question of how to format the data for basic transmission and retrieval is the same.
enum Bool { True, False, FileNotFound };

[background=rgb(250, 251, 252)]If you were to save this same data to a file, such that you could later read it back into memory, how would you do it?[/background]



[/quote]

I would probably read the data by each line or each char.
But i don't really get how i would apply that to a packet.
Can you explain a little more ?
Or do you know a better way of packing data(that allow structures)?

Thanks : )
Treat the data packet as a file. (It's a memory stream)
"Write" your packet to this memory stream.
Send the bytes of that memory stream as your packet.
On the other end, read the data back from a similar memory stream, initialized with the data of the packet.

Also, I highly recommend you practice the difference between pointers, arrays, and variables, and study how the stack and the heap work in C/C++. You should be able to read and follow the implementation of malloc()/free() and new/delete in your standard library (MSVC or GLIBC.) If you see things there that you don't understand, I recommend you study up on what those are!
Once you can do this, you'll have enough understanding of the bits-and-bytes of systems programming to start doing binary-level networking effectively.
enum Bool { True, False, FileNotFound };

I would probably read the data by each line or each char.
But i don't really get how i would apply that to a packet.
Can you explain a little more ?
Or do you know a better way of packing data(that allow structures)?

Thanks : )


I'm not sure if you're using C or C++. If you're using C, I can't help you, but if you're using C++, the C++ Middleware Writer could help you with the marshalling.
Your packet holds a pointer (which is an address) and an integer. It will send:
Address of l_data;
Integer id;

What you want to send is
l_data (including all its members like say position, name, rotation whatever online data)
Integer id;

To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data, ...sizeof(SomeStructure));
offset += sizeof(SomeStructure);
memcpy(&(Buffer[offset]), &id, 4);//integer is 4 bytes

SendTo(.....Buffer, offset);//sending "offset bytes" which is 4 + sizeof(SomeStructure)

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


What you want to send is
l_data (including all its members like say position, name, rotation whatever online data)
Integer id;

To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data, ...sizeof(SomeStructure));
offset += sizeof(SomeStructure);

Please don't do that.

If you did that in our studio you would be facing a VERY tough code review.

Proper serialization means allocating exactly the correct amount of space (rather than blindly allocating 1000 bytes), ensuring that only necessary fields are stored (instead of blindly copying the entire structure), and almost always it means implementing a simple interface (e.g. Serialize() and Deserialize()) so the object itself is responsible for reading itself in and writing itself out in optimal fashion.

As h+ pointed out, there is fundamentally no difference between serialization across a network and serialization to disk. In several games we have used disk-based networking where both sides would write to shared files; disk-based virtual networks are replayable and help with more complex networking issues like packet loss. All that maters from a game perspective is that you are hooked up to data streams.


Please to read many of the articles on serialization to learn how to do it properly.
I disagree that you need to allocate the exactly right number of bytes. Any stream-based output system is likely to use malloc() or a container like std::vector<> for implementation, and those will certainly pad most size requests up a bit.
If you have an upper limit to the size of a serialized packet, then that's a fine, fixed, size to use for your serialization buffer, assuming that your code does the right thing (i e, fails loudly) if you try to exceed that size.
The bonus of keeping some number of pre-allocated, fixed-size blocks around is that you don't have to worry about fragmentation.

The rest of the advice is fine!
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement