• 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
Xanather

Generic Networking Question

4 posts in this topic

When using Tcp, or actually any protocol in that matter, how would you make sense of messages received, and how to generate/send messages accordingly. So far Ive just used ASCII (each character is one byte) and kept reading bytes and converting it to a ASCII character from a stream until hitting a delimiter and then processing that piece of string. I have thought about using Unicode but Unicode is 2 bytes wide per character, what if you receive half a character (i.e. 1 byte) and try converting that piece of string, how could you prevent this? Ive also thought about sending serialized data over the network which is definately not available when using this stratergy.

tl;dr: What is the best way to make sense of data from within a protocol (tcp stream).

Thanks, Xanather.
0

Share this post


Link to post
Share on other sites
In reverse order:
[quote]
[left][background=rgb(250, 251, 252)]What is the best way to make sense of data from within a protocol (tcp stream).[/background][/left]

[/quote]
There is no absolute best. It is highly dependant on the application's requirements. Action oriented games typically care about low latency more than anything else, so they try to use minimal representations of the data that is needed.

In some cases they care so much about fresh data that they don't want reliablitiy - if a message doesn't make it immediately then there is no point resending it - it will be too stale to be useful. Such games might use UDP instead of TCP - TCP will appear to "stall" when a packet it dropped, because it delivers it's byte stream in-order and reliably. New data might be waiting in your local OS buffer until the remote peer to resends some earlier data.

Other games care about reliability and stability more than anything, an example is a lockstep RTS game. They cannot proceed to process out of order, as each peer must simulate the exact same conditions to maintain the synchronisation. Such a game would choose TCP.

[quote]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]Ive also thought about sending serialized data over the network which is definately not available when using this stratergy.[/background][/size][/font][/color][/left]

[/quote]
Not with a naive delimiter. You could use an "escape" byte, much like C strings use the \ character. If the payload contains that byte, duplicate it during send. The receiving end knows that duplicate escape bytes should be merged and included in the payload. If the escape byte is paired with a distinct "delimiter" byte, you can now have delimited data.

You would also have a space of 254 other byte pairs that will never occur in the payload, if you could think of a use for them. For example - in a simple chat protocol you could use that to encode simple text formatting like colours, bold, italics or fonts.

I'm not recommending this, I'm just pointing out that it is possible.
[quote]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]I have thought about using Unicode but Unicode is 2 bytes wide per character[/background][/size][/font][/color][/left]

[/quote]
[url="http://www.joelonsoftware.com/articles/Unicode.html"]Not quite[/url].

[quote]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]When using Tcp, or actually any protocol in that matter, how would you make sense of messages received, and how to generate/send messages accordingly.[/background][/size][/font][/color][/left]
[/quote]
There are a few ways to do this. These can be broken into broad categories that I've just made up:[list]
[*]Implicit structure. There is no hint of the message structure in the messages, the message is 100% payload. E.g., a really simple game (Pong?) might consist of the server sending gamestate message to the client, and the client sending input messages. The size and structure of these messages could be decided in advance.
[*]Inferred structure. Each message begins with an identifier describing the "type" of packet. The size and layout of the message can be inferred from this value. Such a protocol might have lots of arbitrary limits in it, strings might be 100 bytes, there might be a limit of 50 active objects in the game, etc.
[*]Delimited structure. Each message ends with a particular sequence of bytes.
[*]Header prefixed. Each message starts with a complex header, containing information about the length of the message and possibly identifying the message's contents, perhaps even going to include a digital signature to prevent tampering. It might also contain information for ordering and acknowledgement (if the underlying layers are unreliable).
[/list]
Most real protocols go for the last one. In addition, real game protocols will pack as many messages as possible into one "packet".

That is an overview. I think the next place to start researching is what real games do. [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]Q12 of the Multiplayer and Networking Forum FAQ[/url] should help there. Edited by rip-off
0

Share this post


Link to post
Share on other sites
FWIW, I always assign a header to the data I'm sending across, and the header defines the type of message and length of the payload. In C++, it'd look something like this:

[code]
struct tMsgStructure {
uint16_t MsgType;
uint16_t PayloadLength; // Defines data's payload length, for more complex messages
uint8_t Payload[0]; // empty array, can be filled with any data
};

// Example of a Sending a Chat message

void SendChat(std::string ChatString)
{
tMsgStructure *ChatMessage = new ChatMessage[sizeof(tMsgStructure ) + ChatString.size()];

ChatMessage->MsgType = CHAT_MESSAGE;
ChatMessage->PayloadLength = ChatString.size();
memcpy(ChatMessage->Payload, static_cast<uint8_t*>(ChatString.c_str()), ChatString.size());

DataSend(ChatMessage); // this sends the data based on packet header size and payload size
[/code]
0

Share this post


Link to post
Share on other sites
You need to keep the raw receive buffer separate from the parsed data stream, and you need to make sure that the raw receive buffer is retained across loops of your main loop.
The logic looks something like this:

[code]
void network_tick() {
if (socket has data) {
int n = recv(socket, end of buffer, size remaining in buffer, 0);
if (n > 0) {
end of buffer += n;
}
}
while (enough in buffer for a header) {
header hdr = peek at header();
if (enough in buffer for header and data) {
packet = remove header and packet data from buffer();
dispatch_incoming_packet(packet);
}
else {
break;
}
}
}
[/code]

If you use a delimited protocol, then instead of "decode header," you look for whether there is a delimiter within the data in the buffer, and if so, remove/process the part that is up to the delimiter, and then go back and look again.
This re-buffering, and an efficient implementation of the remove/extend buffer (typically, a cyclic buffer,) are crucial keys to good game networking. Networking/sockets wrapper libraries that don't help with this are, in my opinion, no better than just banging the sockets directly.
Edited by hplus0603
0

Share this post


Link to post
Share on other sites
Thanks for the replies everyone, I know understand :)

Ill go for the "Delimited structure. Each message ends with a particular sequence of bytes."

which is what i was doing in the first place, also done in the way that hplus mentioned. Thanks everyone :D
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