Note that there are some explanatory texts on larger screens.

plurals
  1. POCommunication pattern for child objects to parents
    text
    copied!<p>I am actually developping a multithreaded game server in C#. I am trying to create it as unit-testable as possible.</p> <p>As of now, the pattern I started using resemble to something like this : </p> <p>A <strong>Server</strong> class is instanciated:</p> <ul> <li>Serves to start and shutdown the server </li> </ul> <p>OnStart the Server instanciate the <strong>ConnnectionManager</strong> : </p> <ul> <li>Contains an instance of the class <strong>ConnectionPool</strong> </li> <li>Serves to accept asynchronously connections (via TCPListener.BeginAcceptSocket) </li> <li>When a socket is accepted an instance of <strong>ClientConnection</strong> is created ans added to the list in <strong>ConnectionsPool</strong> </li> </ul> <p>The class <strong>ConnectionsPool</strong> : </p> <ul> <li>Responsible to add and remove active connections to the pool, managed as a list </li> </ul> <p>The class <strong>ClientConnection</strong> : </p> <ul> <li>Responsible to receive the data via Socket.BeginReceive </li> <li>When all is received, an instance of <strong>SocketHandler</strong> is created (basically parse the socket content and run actions)</li> </ul> <p>The schema should look like this :<br> Server -> ConnectionManager -> ConnectionsPool -> ClientConnection -> SocketHandler</p> <p><strong>The problem</strong> :<br> When I am inside the SocketHandler, how can I affect another player ? (for example, player A hits player B: I need to get the instance of the player B inside ConnectionsPool and update his HP property) or even update the server itself (say call the shutdown method on the Server class)</p> <p>I assume to have 3 choices : </p> <ul> <li>Transform all important classes (Server + ConnectionsPool) into static classes > drawback: can't be unit tested</li> <li>Transform all important classes into Singleton classes > drawback : difficult to test </li> <li>Inject inside child constructor the instances of important classes > drawback : less readable code since a lot of information is passed</li> </ul> <p>The goal here is to make this all unit testable using best practices while keeping a simple approach.</p> <p>I received a suggestion to inject a delegate to childs which is similar to the 3rd method, but I am unsure about unit testing this and the real effect since everything is multithreaded of course.</p> <p>What would be the best architecture choice here ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload