Skip to content

Shared State Between Clients

wowus edited this page Sep 14, 2010 · 1 revision

Introduction

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?”.

Introducing… the ClientFactory!

Lucky for us, the cockpit library makes accommodations for such a construct. Instead of instantiating Clients 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!

Caveats

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.