My UDP Protocol Wrapper

I’m working on a reusable network layer for java games, and I have some questions about UDP. TCP/IP support is finished, and now I’m just trying to design the UDP support before I start coding it.

I read that you shouldn’t send UDP packets that contain small amounts of data in the “The Internet Sucks” article at:
http://www.gamasutra.com/features/19990903/lincroft_01.htm
because of uncompressed packet headers. But that seems doesn’t sound right… It seems like with many real-time multiplayer games, ‘streaming’ data is required. Data consisting of small packets containing actions the other player(s) have taken, some sort of GameState, etc… Whatever the method of updating the other player(s), the updates usually need to be quite frequent. Any thoughts on his comment?

Also, I’m trying to design a method to ensure the arrival of packets when using UDP. The only way I can think of is to send a response back when a packet is received. But then you don’t know if that response packet was received, therefore the response itself would require a response, and that response would require a response, and so on. Also, this seems like it would create quite a bit of network latency.

Here’s my project if you want to check it out:
http://gaming.cyntaks.com/projects/viewproject.php?project=RJNL

Breadstick

That article sucks.

Anyway, the statement is not intrinsically wrong - you will hose bandwidth with small packets if you’re not careful. A UDP packet has 22 bytes overhead, and if you send a single 4 byte number (java int) in each packet then you’re wasting 3/4 of your b/w!

This is trivial to fix, though, you just batch your messages up to a configurable threshold. Set up a heuristic e.g.

"Whilst:

  • I have less than 100 bytes of data to send
  • the first byte of data came in less than 5 milliseconds ago
    batch the data and don’t send it yet"

This 5 ms wait would be invisible to players, on their minimum 30ms latency even for an almost directly-connected fast broadband connection - but it would spare you some b/w. Very significant if you have a lot of traffic.

But…it depends from game to game whether it matters enough to bother going to that length!

Welcome to the pain of why I strongly advise no-one to write their own protocol on top of UDP! :wink:

Short answer: go read the internet RFC’s that describe how TCP works. That includes all the detail you ever need on hwo to do retransmit.

Especially do NOT try and muck about thinking you’re cleverer than the desingers of TCP - take it from me, you’re not. Just implement what they did. Chances are, if you try and cut a corner, it’ll come back to bite you on the ass some time in the future…

Finally, a warning: sometimes all the above advice is wrong. I had to make some assumptions about your project, and if I assumed wrong, then I could be telling you the wrong stuff. C.f. SWP who had a very unusual (as far as games are concerned ;)) protocol, and several standard assumptions no longer applied.

That’s interesting… It seems that the main point people use when claiming that UDP is faster than TCP/IP is the large overhead in TCP/IP because of how it ensures that lost packets are retransmited, and that packets arrive in the correct order. If you must implement a system similar to TCP on top of UDP to ensure all your data is sent, can UDP really be faster when proper data transmission is important? :stuck_out_tongue:

Only fools claim that UDP is faster than TCP.

[quote]Only fools claim that UDP is faster than TCP.
[/quote]
Well said :).

Breadstick, if you’re not sure on this stuff, PLEASE read the massive great post that I went to the trouble of making sticky ;). I assure you it’s worth reading!

[quote]Finally, a warning: sometimes all the above advice is wrong. I had to make some assumptions about your project, and if I assumed wrong, then I could be telling you the wrong stuff. C.f. SWP who had a very unusual (as far as games are concerned ;)) protocol, and several standard assumptions no longer applied.
[/quote]
Our media distribution tool “Copper” is now available based on this fast bulk data transfer using FEC and UDP. The technique I was looking at is not suitable for the highly interactive game data, but would be suitable for streaming out things like models, level data, or game updates from the game server.

With a UDP-based FEC transmission protocol we are able to get much better utilization of the available bandwidth than TCP-based solutions such as FTP.

http://www.digital-rapids.com/Products_Copper.html

If you need reliable in-order delivery - that’s what TCP already is, re-implementing it with UDP would be a complete waste. Not to mention that all the routers between the game server and the clients already “know” TCP so they don’t just blindly move IP packets… they take TCP-specific stuff into consideration.

edit - I should say -“some” of the routers and equipment between the nodes would be TCP aware and do appropriate buffering and re-transmission without propagating the need to re-transmit back to the original sender.

hint: TCP needs ACK, UDP not

depends on what you need, you have to decide wether TCP is better for your game or UDP fits in your game.