Jocomm - Client-Host Game Server Implementation
Unity Strategy Game Port
Ported this same game into unity using C# instead of TS.
Three.js Strategy Game
Started a project with Typescript, Vue.js and Three.js. Idea is to build a civilization type game.
Why?
I wanted to get into application programmin using C++, and having done some network programming before this peaked my interest. I could also use this server for the game I have been writing with C#/Typescript.
Methods
I started by finding out what exactly I should do. I did some research into TCP and UDP sockets, and had this idea of using TCP sockets for player input, and then using UDP to broadcast updates to the game state. The application could be split to two different server implementations. The application would also have to have some basic game implementation.
This resulted in an architecture where I would have: a client binary, a server binary. The client binary would have to be able to send input to server and request game states and render them. The server would have to handle connections, read inputs and sync them between state updates, and broadcast state via UDP.
Testing
To test this contraption, i use docker containers which have a volume attached to them which functions as a dns, because I dont want to write that. DUring startup the server writes its IP to the volume and the clients mount that volume and connect to the server. I looked into this and usually Kubernetes or Docker or other virtualization software is used to do this so I think this is state of the art.
The code quickly got complicated after finding osme code to do a simple TCP server using sockets. The main features I want to implement is: Be able to connect, rejoin if connection lost, rejoins dont disrupt game updates, game updates. And its rather difficult to get it to work. Perhaps developing some tests would help.
Thread Safety
So while planning the architecture for this thing. I found this cool post describing such things as mutex, condition_variable and queue. The previous threads I had done were stupidly not using any measures to enable thread safety. So I started looking into that.
Condition Variable
A way to inform other threads with complex conditions of locks and releases
Mutex
An object that allows locking a thread
std::lock_guard
A way of locking the local scope, lock is released when scope deleted.
std::unique_lock
A bit unclear how this differs from regular mutex, allows manual lock/release and some fancy stuff Im not quite grasping.
Architecture
So the software solution consists of following parts:
- GameServer
- Game
- TCPServer
Game
Classes that implement Game Logic
TCPServer
- ConnectionHandler
- InputHandler Spawns two threads: ConnectionHandler, InputHandler. Takes care of new & returning connections and reads their sockets for input.
ConnectionHandler
Handles new connections, reconnections and kicks.
InputHandler
Reads Client Sockets, and upkeeps Queue In addition to this Im going to have an UDPServer, which broadcasts the new calculated Game State to Clients.