This project was an exercise in networking for games: using RakNet, there is a dedicated server that clients can connect to. Concepts like packet loss, differences between TCP/UDP, and different ways to handle network lag and syncing was covered over the course of the project.
PongBreak uses an authoritative server which simulates the game and sends the states to its clients to display to the players. This rids the need of temporal delay. However, there is interpolation to account for the time difference between the clients and the server.
The server contains a 2D array of client pairs, giving it the capability of running multiple games at the same time. It has an update loop, where it updates each game individually, and sends the new game state (a struct called
GameInfo) to each client. Upon initialization, it creates all of the
Game objects, as well as opens up connection ports to listen on. Upon a game ending, it waits for confirmation from a client to restart, or closes the connections and frees up the client slots if the clients disconnect.
The clients and server send packets back and forth a couple times each second. In order to reduce the amount of information flowing over the network, the info stored in each packet is as concise as possible, saving space. Here’s the method that reads the packets’ information:
The magic lies in
GetPacketIdentifier, which gets the header of the packet. This allows us to understand what type of packet it is (and what we should do with its contents). For us, the packet identifiers are a simple enum.
update() is finished, it sends the game info to all of the clients, simply sending a packet to each client:
The client simply responds to user input, and draws the updated
GameInfo sent from the server. Upon receiving input from the player, it converts that to data based on the most recent info. For instance, here’s how a client would send updated paddle info:
Retrospection and Postmortem
This was my first experience in networking, and I am thankful that it was using an industry standard like RakNet. It gave me experience in separating server logic from game logic, as well as introduce me to topics like interpolation, replication, and ways to handle network lag like temporal delay. The game itself is simple, but it does a good job in creating an environment where a good amount of data is needed to be sent back and forth.