Multiplayer, multithreading, and an actor model in C++
I’m currently writing a 4X strategy game in C++ with no engine. I already talked a little about it in my first devlog.
During previous gamedev experiments, I learned that if you do not plan early for multiplayer, you’ll have a hard time refactoring the code. This is why networking was on my top priority list. Especially since I’m making the game from scratch, with no help from Unity, or Unreal Engine, or whatever. Battle-tested engines provide lots of tools to ease the pain of making games, but hey! where would be the fun? 🙂
Dedicated server or builtin?
Those are basically the 2 options you have. Your server can either be its own executable, separated from your game, which can then be run on a dedicated server (baremetal, in the cloud, …). Or you can embed it in your game, which will act both as a server and a client.
I went with the second option because it fits into my goal of distributing everything in a single executable.
Even with the second option, there is two ways to achieve this, either with a process, or with a thread.
A thread allows me to share memory between the server thread and the main thread, this enables the introduction of synchronisation points through std::promise<T>
. This is the solution I went with.
The server is responsible for creating and managing the game state, and relay changes to it to the clients via a TCP socket…