Jump to content

  • Log In with Google      Sign In   
  • Create Account


Generic Networking Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Xanather   Members   -  Reputation: 703

Like
0Likes
Like

Posted 05 June 2012 - 03:26 AM

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.

Sponsor:

#2 rip-off   Moderators   -  Reputation: 7633

Like
0Likes
Like

Posted 05 June 2012 - 08:24 AM

In reverse order:

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


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.

Ive also thought about sending serialized data over the network which is definately not available when using this stratergy.


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.

I have thought about using Unicode but Unicode is 2 bytes wide per character


Not quite.

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.

There are a few ways to do this. These can be broken into broad categories that I've just made up:
  • 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).
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. Q12 of the Multiplayer and Networking Forum FAQ should help there.

Edited by rip-off, 05 June 2012 - 08:30 AM.


#3 BeerNutts   Crossbones+   -  Reputation: 2522

Like
0Likes
Like

Posted 05 June 2012 - 09:09 AM

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:

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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#4 hplus0603   Moderators   -  Reputation: 4907

Like
0Likes
Like

Posted 05 June 2012 - 12:09 PM

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:

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;
    }
  }
}

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, 05 June 2012 - 12:11 PM.

enum Bool { True, False, FileNotFound };

#5 Xanather   Members   -  Reputation: 703

Like
0Likes
Like

Posted 06 June 2012 - 04:11 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS