Decoupling Network from Game/UI logic

Started by
4 comments, last by CS_ 11 years, 4 months ago

I am wondering what design strategies or approaches others have used to decouple networking from game and UI logic in a game client.

I considered the notion where everything that would be sent to the server endpoint would be some form of an event. Various points in the game logic code, specific events would get dispatched to an event queue. The network layer components would register during initialization for these types of events and upon receipt, perform the network action. Once the network action completes, a network layer completion handler that got invoked that would fire an event back to the event queue to be distributed the next time the event dispatcher was ticked.

One thing I like about the above is that the event dispatcher is the conduit by which any module can speak with the network layer. In fact, my design has been hinged around this being the way any subsystem in the framework can talk to one another.

Using boost asio with an io service worker thread, I can have the service event loop continually respond to read/write operations as fast as possible and update an internal queue of message packets received from the server. During the main game loop I can simply lock the queue, make a clone, clear, and unlock then process the cloned list. This is so I can maintain some order of state update during the game loop. I don't think it would be ideal to allow the io service thread to manipulate state since it has no idea where the main game loop may be during the simulation for example.

I'm curious whether there is a better approach to interfacing the network layer in the game loop besides the above? The world simulation will have a large number of various message types sent to/from the server and I'd like to find a good way to handle sending/receiving these messages effectively with minimal overhead and decent decoupling too.

EDIT: Also keep in mind, that if a proposed solution is not to use an event queue like the above, the game client maintains at most three server connections simultaneously during game play, namely the authentication service connection, chat service connection, and world simulation service connection. So I'd need to have a way to distinguish between multiple server-side endpoints.

Advertisement
Generally the good implementations I've seen follow that basic pattern: there's a "game layer" and a "network layer" and they communicate via some form of message queueing. The details are going to vary by engine and other design considerations, but it sounds like you're on the right track.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


Generally the good implementations I've seen follow that basic pattern: there's a "game layer" and a "network layer" and they communicate via some form of message queueing. The details are going to vary by engine and other design considerations, but it sounds like you're on the right track.


A centralized queue would work when you're speaking about specific events like GameObjectUpdateEvent or ChatMessageEvent for example. Since I am only ever connected to a single world simulation server or chat server that is responsible for sending data to my client this makes sense.

But how would you handle network-specific event notifications such as connected, timeout, disconnected, etc? Using the event dispatch queue would be nice to keep things nicely decoupled but I also think it really breaks down when you are dealing with multiple connections unless the events have some identifier that specify which endpoint the network event is related to.

Another option would be to use some simple INetworkEventSink interface that could be registered for each object interested in network-specific events. But this would mean that some game layer objects would need access to network layer objects to register such a notification sink.

That is where I am having a hard time trying to connect the dots between the two layers.
What's wrong with identifying the connection channels in a way that both layers understand?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I think you need to be careful that you don't try and over-develop the abstraction / decoupling.
There comes a point whereby you try and reduce the awareness of either component so much that you have inefficient and hard to maintain code.

Using boost asio with an io service worker thread, I can have the service event loop continually respond to read/write operations as fast as possible and update an internal queue of message packets received from the server. During the main game loop I can simply lock the queue, make a clone, clear, and unlock then process the cloned list.


Double buffer your queue. Add two of them, one for receiving events, one for processing. When the processing queue is, well, processed, throw your lock on the manager and switch the two queues around. Much shorter lock time, I expect.

This topic is closed to new replies.

Advertisement