-
Notifications
You must be signed in to change notification settings - Fork 1
Shared State Between Clients
If only it were possible to run GoGo as a stateless server. It would be so nice. Unfortunately, we require shared state (at least, unless we switch languages altogether, but bear with me here). Now the question becomes “How do we share data between clients?”.
Lucky for us, the cockpit library makes accommodations for such a construct. Instead of instantiating Client
s as new connections come in, cockpit::MatchServer
will call a factory function for the construction. This function is implemented in gogo/src/GoGoFactory.[h|cpp]
. This has a very interesting consequence: All shared state between clients goes in GoGoFactory. When a client needs access to the state, GoGoFactory passes a pointer to this state in the Client’s constructor. And voila, the client has access!
Clients can be thought of as each running in their own thread. It’s a bit more complex than that, but this works as a good model of their behavior. Therefore, all shared state must be protected by mutexes! Race conditions are the ugliest of all bugs, and it’s extremely hard to track them down without specialized tools (Intel V-Tune, anybody?). Therefore, keep any class that can be shared between multiple clients totally reentrant, even if that means expensive synchronization.