Networking: .NET MonoGame project
This project was a university multiplayer game module, given a basic MonoGame framework with a basic component system implemented. Working with this framework I worked towards creating a game that seamlessly serialises relevant data, and then is sent and read correctly using TCP and UDP to transport the data across a network, and then replicate the objects in the game based on transmitted data. Alongside sending this data I then implemented security for the game to prevent any breaches and external adjustments to data. This will be discussed further below.
TCP:
Utilising TCP and basic .json serialization we can change any valid data into a string that can be sent over a network using a packet library that was created. Within this packet library we have serialize/deserialize functions for the individual packet types. Instead of having a naïve world state even though it is a small game, opting for a larger packet library with different types allows me to add more complexity in the future without modifying the architecture. Having an IP that can be changed allows for a player to connect to the host device over a network, utilising TCP this way allows for the reading of data on both the client-side and also the host-side of the game. The connection is made when the clients login and the host is decided by who starts that game state first. Similar to an actual server sided-game, the host has the true game state and data is sent from the host predominantly for the client to update their game state. Besides from client-side inputs this is the basic structure of the network.
UDP:
Unlike TCP, UDP only needed the correct port number to start, I used UDP to pass over the clients and hosts RSA public keys to the correct destinations and then save them correctly for future encryption and decryption. Whilst no necessary, having a functional UDP sending and processing operations means that when UDP is needed for specific data or tasks it can be used. Utilising asynchronous tasks and threads in C# means that a function is always listening for data via UDP being transmitted to a certain port, whilst TCP knows its destination and expects a response UDP is utilised in my project for initialisation of data.
Security and Keys:
As mentioned briefly before, in this project I used RSA keys as my form of encryption and decryption. Having this security meant sending only the correct data to required clients so that no other clients had access to a key that they shouldn’t have. This encryption meant that it is harder and unlikely that any stream of data can be read outside of the clients games further preventing any possible security faults. Whilst I understand this is not a perfect security system and could potentially be more in depth and more secure, that is an aim for the project going forward.
GameStates and loops:
Whilst game states and the game loop can be easily overlooked it is vital to the flow of information. For example, on the login screen for my game(on the right), the host is decided on the client that first enters that state. When the second client logs in that is when both players are marked as logged in, and the state switches to logged in, instead of awaiting players. This “logged in” state is where the passing of vital information and handshakes take place, as mentioned before, the correct public keys are sent and processed at this stage and other information is grabbed such as remote endpoints, and client ID’s.
Enough of the networking for this section, the game works as you would expect pong to work, if the ball hits the opposing wall you gain a point, simple. All of this data is sent to the other client, such as the score incrementing, or the opposing client moving on an action based system, this is so that we are not constantly updating and sending things such as the score across the network every frame, and only when that valid collision occurs that packet is then filled with the necessary data and then sent across the network.
Extra notes and further steps:
Moving forward with this project I want to steer away from pong and maybe develop into another genre. A more complex game with more than two clients per game state will challenge my networking and programming skill, a multiplayer top down racing game will help me implement basic AI for filling in missing client gaps further expanding my knowledge and skill of this topic. Make sure to check back in from time to time if this progression interests you.
Back To Home

Game Preview

Handling the TCP packet, using recursion to check for an encrypted packet and feed it through again if so

Initial key handshake

Method I used to breakdown serialized packet data down into a byte stream and then encrypting or decrypting this data (this packet returned was then handled and down casted to its valid type)
