Jump to content

  • Log In with Google      Sign In   
  • Create Account

Decoupling Network from Game/UI logic


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 30 November 2012 - 01:52 PM

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.



Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 16414

Like
0Likes
Like

Posted 30 November 2012 - 02:38 PM

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.

#3 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 30 November 2012 - 06:12 PM

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, 30 November 2012 - 08:42 PM.


#4 ApochPiQ   Moderators   -  Reputation: 16414

Like
0Likes
Like

Posted 01 December 2012 - 03:29 AM

What's wrong with identifying the connection channels in a way that both layers understand?

#5 Angus Hollands   Members   -  Reputation: 722

Like
0Likes
Like

Posted 01 December 2012 - 12:32 PM

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.

#6 CS_   Members   -  Reputation: 225

Like
0Likes
Like

Posted 04 December 2012 - 09:50 PM

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_, 04 December 2012 - 09:51 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS