Sign in to follow this  
tom_mai78101

Concept of a basic custom packet in use for sending data to server and client, back and forth

Recommended Posts

Please assume me as a guy with a background in Java programming.

Is there a basic packet design in use for sending custom data, preferably game related information, to servers and/or clients? Any data you can think of is absolutely helpful. My purpose is to understand what a fundamental packet would look like for a packet sent from a game to some place, that allows multiplayers and stuffs. And what a packet does need as a prerequisite.

Does it involves implementing a Runnable? Does it require that it implements Serializable? Will a packet contains a few ArrayLists as member fields? I'm just looking for an easy, generic design. I know the question itself is vague, but that's my point. I just wanted to see what it looks like in the first place.

Thanks in advance. Edited by tom_mai78101

Share this post


Link to post
Share on other sites
This is indeed a vague area, one that I think is impossible to answer definitively without context. A networked death match FPS game will have a completely different solution to something like a chess game. A browser game might be written in terms of HTTP requests, possibly encoded in something like Json or XML, while a desktop client might use a custom binary protocol. A MUD could be designed where the client is a dump terminal, with a simple line based textual protocol.

Have you looked at the [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]Forum FAQ[/url], in particular Q12.

One point I can make is that many games distinguish "packets" from "messages". A message might be something like "a new explosion was created at X,Y,Z" or "object PLAYER_4 has position P and heading H" or "actor PLAYER_4 has died". A packet will generally include all the messages that have occurred since the last packet was sent. A packet will often have a header which contains some metadata about the packet, for example it might contain information related to reliability, ordering, acknowledgement, ping timings and even flow control. The header complexity tends to be greater in a game that uses a lossy datagram style rather than a game that uses reliable streams. Time sensitive and action oriented games tend to use the former style, while turn based games tend towards the latter.

It is possible to design a "general" API, but there must be a few more constraints decided first. Raknet, for example, is a general game networking API but it places emphasis on low latency action oriented games (at least at the time I last used it).

[quote]
Does it involves implementing a Runnable?
[/quote]
This is a completely implementation specific question. No, there is no necessity for a "packet" to be runnable. However, this might be part of a solution to move networking logic to a separate thread, if desired.

[quote]
Does it require that it implements Serializable?
[/quote]
No. Using the built in Java serialization might be a great way to bootstrap your game, and for certain types of games (e.g. our aforementioned chess example) might be all you need to do. However, Java serialization has a number of draw backs. The biggest one, from a game programmer's point of view, is that the overhead tends to be very large. Another issue is handling protocol version changes - though this isn't an easy one to do in general.

[quote]
Will a packet contains a few ArrayLists as member fields?
[/quote]
It is conceivable that someone would write a class "Packet" like that. However, a packet tends to be the lowest stage of the networking, at which point you are generally talking about arrays of bytes rather than higher level constructs. Some designs might not even model packets using a class - they might serialise messages to a byte buffer which is wrapped in a datagram or written to the stream.

Share this post


Link to post
Share on other sites
Packets are just byte arrays. Serializable is generally terrible, because it adds all kind of reflection and type information that bloats your packets for no good reason. You already know what the bytes mean.

A typical game packet will look something like this, at the byte level (assuming UDP):

[a] [b] -- protocol version bytes / magic header
[c] -- cyclic sequence number of this packet
[d] [e] -- lowest two bytes of timestamp when this packet was sent
[f] -- last sequence number received by the other end
[g] [h] -- lowest two bytes of timestamp provided by the other end for the packet with id [g]
[i] [j] -- duration of processing between receiving packet [f] and sending out this packet [c]
[k] -- source of the packet (player id, or server)
[l] -- destination for the packet (player id, or server)
[m] -- number of messages in this packet

followed by, for each message:
[a] -- size of message (after size, channel, and type)
[b] -- logical channel for message
[c] -- type of message
[[a] bytes] -- payload according to [a]

finally followed by:
[x bytes] -- checksum, CRC, or signature of packet

Each message type [c] maps to a different layout of the packets, and should be interpreted differently.

There is also lots of variation.
Sometimes, the destination isn't included, because the destination is implicit in where you send the packet.
Sometimes, the timing information is not included, if calculating accurate round-trip-time or server-time-offset isn't needed.
Sometimes, there's not multiple logical channels in the stream, so the channel id isn't needed.
Sometimes, payloads bigger than 255 bytes per message is needed, and larger size fields are used.
Sometimes, integers are encoded using variable length (for example, if high bit is set, use the lowest 7 bits, shift left 7 bits, and then attach whatever comes after it, repeat until get a byte with high bit clear.)
Sometimes, more acknowledgement information is included, to support advanced forms of selective re-transmit, packet scheduling, etc.
Sometimes, the destination values are bigger, because you can target any particular object or entity within the system, and there are many of those.

There's a lot more variation, even within commonly accepted best practices. Compression, bit stuffing, implicit length by type, the list goes on. There are also games that don't use best practices, and send data in text form instead of binary, or send one packet per message, or do all kinds of other unnecessary things. However, if you start with this packet structure (again, assuming UDP,) then you're doing fine.

If you're using TCP, you have to additionally prefix each packet with a length field; typically 2 bytes (as you don't want to send more than 64kB of data at a time anyway.) You do not need a "magic" header/signature for TCP, as the protocol version stays the same for the duration of the connection. You may not be as concerned about timing with TCP, because it will deliver everything in order with larger amounts of jitter than you'll see with UDP. You do not need acknowledgement information for TCP, because it takes care of re-transmitting. The list goes on :-)

Share this post


Link to post
Share on other sites
Thanks rip-off and hplus0603!

[quote name='rip-off' timestamp='1353179547' post='5001814']
Have you looked at the [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]Forum FAQ[/url], in particular Q12.
[/quote]

Yes, but it narrowly answers a part of my question. At least, from that, I am able to understand what a network packet's content may want to have, depending on the game that uses the packet.

[quote name='rip-off' timestamp='1353179547' post='5001814']
This is a completely implementation specific question. No, there is no necessity for a "packet" to be runnable. However, this might be part of a solution to move networking logic to a separate thread, if desired.
[/quote]

Oh I see. I tend to see them as together, but I don't know why it has to be together, until now. Moving network logic to a separate thread seems to be a very reasonable answer.

[quote name='rip-off' timestamp='1353179547' post='5001814']
No. Using the built in Java serialization might be a great way to bootstrap your game, and for certain types of games (e.g. our aforementioned chess example) might be all you need to do. However, Java serialization has a number of draw backs. The biggest one, from a game programmer's point of view, is that the overhead tends to be very large. Another issue is handling protocol version changes - though this isn't an easy one to do in general.
[/quote]

Since I haven't had much experiences in Java serialization, before, I was thinking if a packet were to be thought as a binary file, wouldn't sending a binary file, in the order of a standard network packet, be easier to read/write? Now, this is my first taste of knowing that there would be drawbacks, something I never thought it would be that severe before. I'll keep my head up regarding this.

[quote name='rip-off' timestamp='1353179547' post='5001814']It is conceivable that someone would write a class "Packet" like that. However, a packet tends to be the lowest stage of the networking, at which point you are generally talking about arrays of bytes rather than higher level constructs. Some designs might not even model packets using a class - they might serialise messages to a byte buffer which is wrapped in a datagram or written to the stream.
[/quote]

I was worried that I have to write a few packets entirely in bytes. What a relief. Edited by tom_mai78101

Share this post


Link to post
Share on other sites
[quote name='tom_mai78101' timestamp='1353244643' post='5002026']
I was worried that I have to write a few packets entirely in bytes. What a relief.
[/quote]

I don't think that's a bad thing. You can model the contents of packets as structs/classes, and then actually generate the reading/writing to/from byte arrays as functions, perhaps on a general interface that each of your messages implement.

reading/writing fields of a message isn't that hard to do manually, and gives you great control over how your protocol grows.

[code]
class WhisperMessage extends Message {
public int toPlayer;
public string text;
private static const int code = 10; // should not conflict with any other message class
private static const registration = new MessageRegistration<WhisperMessage>(code); // for finding class on receipt
public WhisperMessage(int p, string t) {
super(code);
toPlyaer = p;
text = t;
}
protected void writeData(ByteStream strm) {
strm.writeInt(toPlayer);
strm.writeString(text);
}
protected void readData(ByteStream strm) {
toPlayer = strm.readInt();
text = strm.readString();
}
};
[/code]

Class Message would have public read/write functions that take care of also adding the type code and length fields to the output stream, as well as static functions to read message instances from a byte stream (you need to register the code with some dipatcher).

Also, please apologize if my Java is not quite syntactically correct; it's been several years since I last wrote any.

Share this post


Link to post
Share on other sites
[quote name='hplus0603' timestamp='1353287539' post='5002185']
[quote name='tom_mai78101' timestamp='1353244643' post='5002026']
I was worried that I have to write a few packets entirely in bytes. What a relief.
[/quote]

I don't think that's a bad thing. You can model the contents of packets as structs/classes, and then actually generate the reading/writing to/from byte arrays as functions, perhaps on a general interface that each of your messages implement.

reading/writing fields of a message isn't that hard to do manually, and gives you great control over how your protocol grows.

[code]
class WhisperMessage extends Message {
public int toPlayer;
public string text;
private static const int code = 10; // should not conflict with any other message class
private static const registration = new MessageRegistration<WhisperMessage>(code); // for finding class on receipt
public WhisperMessage(int p, string t) {
super(code);
toPlyaer = p;
text = t;
}
protected void writeData(ByteStream strm) {
strm.writeInt(toPlayer);
strm.writeString(text);
}
protected void readData(ByteStream strm) {
toPlayer = strm.readInt();
text = strm.readString();
}
};
[/code]

Class Message would have public read/write functions that take care of also adding the type code and length fields to the output stream, as well as static functions to read message instances from a byte stream (you need to register the code with some dipatcher).

Also, please apologize if my Java is not quite syntactically correct; it's been several years since I last wrote any.
[/quote]

Better yet, make the Message class realize the Externalizable interface, which provide those public read and write methods and is seamless pluggable to Java's default serialization framework.

[url="http://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html"]http://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html[/url]

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