Beginning network game programming

Started by
5 comments, last by wood_brian 14 years, 2 months ago
Hi All, I am really keen on getting started with making a multiplayer component to my game. I've looked online and found the boost::asio library, since I'm already using boost (and want to use it as much as possible) I figure it's the way I should go. So my questions are basically, is boost::asio the way to go? And what is the best way to write game multiplayer code? Is it difficult adding in dedicated server support? how doe sit all really come together? I'm really just afraid of network programming, terms such as packet loss coupled with what seems like a huge task involving the synching of physics and player input/output not to mention AI over a network just scares the crap out of me. Then there's the whole UDP vs. TCP/IP where TCP/IP is apparently easier to program for but UDP is faster, yet I hear most games these days use TCP/IP anyway because we have the badnwidth and it's easier to code for, but then I hear other opinions saying UDP is better because it's faster which makes me think that maybe TCP/IP is crazy slow compared to UDP? I don't know, it's all just so much to take in... I mean obviously I'll start small but by the end of this I would sincerely like to have dozens of rigid objects being thrown around and 16 - 32 player multiplayer (more even maybe, just for the technical fun of it)... Are there any tutorials out there I could read (preferably using boost::asio if that is a good choice for network API) to cushion the shock? I'm equally worried about the design hurdles as I am trying to figure out how to code it. I'm particularly curious as to how I would go about adding networking support to my existing game engine and integrating it with the physics and rendering systems... Generelly I have a lot of experience with physics and graphics programming but for network programming I have 0 experience... I've always put off learning it because to me it seems enourmously complex and radically different to everything else I've ever done...
Advertisement
One of the network architectures I've looked at basically divides messages into important and unimportant. Important will use TCP, unimportant, UDP.

Assuming a client/server model, the server is responsible for maintaining the 'true' game world, clients will push their movements/updates to the server, the server will push its true 'vision' back to the client.

The clients will be rendering their 'picture' of the true game world to the user, but due to lag will usually involve some prediction (you can get away without, but you will notice lag quicker), the periodic server messages will correct the client if the prediction gets too far away from the 'true world'.

Now the server->client messages are important, and should be TCP, client->server messages can be a mix, things the server _must_ know should be sent on reliable TCP channels.

To distribute data and bandwidth there is also the potential of using client<->client messages as well (usually positional updates), these would usually be UDP as they are 'unimportant' optional update information, which could improve the clients prediction without requiring server bandwidth, but its no great loss if they get dropped as they might be wrong (compared to the server updates).

Don't know about the boost library, but Beej's guide is good for network socket knowledge, might be worth a skim at least. I also remember some good articles about the HalfLife 2 network system, but that might just scare you even more ;)

Best of luck,

Alex

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

Quote:Original post by chillypacman

So my questions are basically, is boost::asio the way to go? And what is the best way to write game multiplayer code? Is it difficult adding in dedicated server support? how doe sit all really come together?

asio is network API wrapper, nothing more. It can send and receive data over sockets.

Here is the basics of how that data is actually used.

Quote:I'm really just afraid of network programming, terms such as packet loss coupled with what seems like a huge task involving the synching of physics and player input/output not to mention AI over a network just scares the crap out of me.

Yes, it's tedious (not hard, just takes up a lot of testing and time). You could also look into one of existing libraries which takes care of that, such as RakNet or similar.
It's not that complex, if you have a message system and something that manages connections and network traffic for you, you're half-way there.

If you use a third-party library, such as Raknet or Enet, you can have a mix and match of reliable and unreliable deliveries.

For example, creating a player could be a one-off reliable message sent to all the clients.

Then further updates would be unreliable serialisations of the player state, stuff in the player that changed (like position, orientation, animation state, health points...).

Enet and Raknet have an internal reliable delivery system on top of UDP, in fact, the UDP / TCP network transport is abstract and not really important as far as the user is concerned.

All the user needs to do is handle messages and network events (new connection established, connection closed, new message received, send message to a node, broadcast message to all nodes, ...).

So in fact, the interface to the network layer can be made simple.

Transmitting object states, object creation, object destruction requires serialisation, all you need really is serialise() / deserialise() methods for the objects you want to network, as well as some form of unique ID and type ID to uniquely address your objects (player #1, player #8, car #654, badger #92, ...).

Managing bandwidth is another problem, for example, if your player stops moving, or a pile of box is static, sending position updates would be redundant. There are methods, such as delta compression, but that can be quite tricky to manage.

Then you can worry about client side prediction and interpolation later.

If you are not interested in the network guts but more about making a game, a third party library will help you and save you tremendous time. But I am no expert on boost::asio (I kinda write my own).

Here is a series of articles regarding the design of a networked multiplayer game. It's not complete, but when it will be, it will be pretty much all the steps you need to take to design a multiplayer game.

Everything is better with Metal.

Quote:asio is network API wrapper, nothing more.


Pedantically: ASIO is a general aynchronous worker library; it supports asynchronization of various blocking operations, including networking and other I/O.

enum Bool { True, False, FileNotFound };
I highly suggest RakNet. It abstracts away all of the details so that you can focus on actually sending messages back and forth, rather than *how* to send messages back and forth. It also has an extremely effective BitStream utility which is very useful when serializing networked objects to and from packets.
Quote:Original post by chillypacman
Hi All,

I am really keen on getting started with making a multiplayer component to my game. I've looked online and found the boost::asio library, since I'm already using boost (and want to use it as much as possible) I figure it's the way I should go.


The C++ Middleware Writer is on line alternative to C++ serialization libraries.

Brian Wood
http://webEbenezer.net
(651) 251-9384

This topic is closed to new replies.

Advertisement