Jump to content
  • Advertisement
Sign in to follow this  
crancran

Decoupling Network from Game/UI logic

This topic is 2142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. Edited by crancran

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. Edited by CS_

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!