• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jeffreyp23

Sending data questions

11 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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
[code]
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));
[/code] Edited by smasherprog
1

Share this post


Link to post
Share on other sites
[quote name='smasherprog' timestamp='1339688250' post='4949179']
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
[code]
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));
[/code]
[/quote]

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?
0

Share this post


Link to post
Share on other sites
[quote]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.
1

Share this post


Link to post
Share on other sites
[quote]
[left][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][/left]

[/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 : ) Edited by Jeffreyp
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
[quote name='Jeffreyp' timestamp='1339705448' post='4949270']
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 : )
[/quote]

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[url="http://webEbenezer.net"] C++ Middleware Writer[/url] could help you with the marshalling.
0

Share this post


Link to post
Share on other sites
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)
-1

Share this post


Link to post
Share on other sites
[quote name='dpadam450' timestamp='1339733572' post='4949421']
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);
[/quote]
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.
1

Share this post


Link to post
Share on other sites
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! Edited by hplus0603
0

Share this post


Link to post
Share on other sites
To the OP: what I meant was:
To do this create a char Buffer[1000];
int offset = 0;
memcpy(&(Buffer[offset]), l_data.m_data, ...sizeof(YOUR_M_DATA));
offset += sizeof(YOUR_M_DATA);
memcpy(&(Buffer[offset]), &l_data.m_id, 4);//integer is 4 bytes

Basically you have to copy what is actually in the pointer itself was what I was getting at. Edited by dpadam450
1

Share this post


Link to post
Share on other sites
Thanks for all the advice : ). I already got something up and running with memcpy.
I also found something else(what hplus mentioned) here : [url="http://stackoverflow.com/questions/3261227/c-how-to-serialize-an-object-to-send-over-network"]http://stackoverflow...nd-over-network[/url] (scroll down a bit for the code). Would this be a better solution?
Also i couldn't find to much information about
[left][background=rgb(250, 251, 252)]serialization(found 1 tutorial).[/background][/left]

[left][size=4][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)]If anybody got some useful links post them : )[/background][/font][/color][/size][/left]


[left][size=4][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)]Thanks[/background][/font][/color][/size][/left] Edited by Jeffreyp
0

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  
Followers 0