Network Protocols
This page documents the network communication protocols used in the R-Type project, including TCP and UDP. These protocols are essential for handling reliable and efficient communication between clients and the server.Network Functionality
Network Functionality
Lobby
The lobby operates exclusively using TCP to ensure a reliable and structured communication protocol. This guarantees that critical actions, such as joining or leaving lobbies, are securely handled.Game
The game utilizes a hybrid approach:- UDP: For real-time game data, such as player positions or actions.
- TCP: For critical information, such as game state synchronization or important updates.
Network Architecture
The server initializes a TCP port at startup. For every game instance, a new thread is launched with its own TCP and UDP ports to handle the game’s network communication.All networking logic is encapsulated within a static library, which includes:- Client and server interfaces: Facilitates the implementation of additional network protocols.
- Unified message handling: A wrapper for both TCP and UDP, providing a
SafeQueuemechanism that centralizes messages from both protocols into a single queue.
Serialization and Compression
Serialization and Compression
Serialization
The UDP protocol is serialized into binary, stored in data structures optimized for the size of the data. All messages include the following structure:Compression
All communications are compressed using the LZ4 algorithm. Packets include a header with the following structure:- Prepare the data structure.
- Compress the data.
- Add the header with the original size.
- Transmit the packet.
- Decompress the received data using the original size.
- Deserialize the data back into the appropriate structure.
Packet Reliability
Packet Reliability
Packet Duplication
To mitigate packet loss, the network library implements a message duplication mechanism:- Each packet is sent 10 times.
- Upon receipt, the
sequence_idis checked.- If the
sequence_idalready exists, the packet is ignored. - Otherwise, the corresponding command is executed.
- If the
TCP Protocol
Error Format
Error Format
Responses in case of errors follow this structure:
List of Errors
400: Invalid arguments401: Maximum number of players reached402: Room not found403: Player already in lobby404: Player is not in this lobby405: Invalid password406: Number of slots should be > 0 and < 5407: Lobby name already exists, please provide another408: Only the owner has the right409: Default lobby, you do not have the right410: Only admin can launch the game411: Some players are not ready412: Some parameters must be numbers413: Game already running
Commands and Responses
Commands and Responses
1. GET_ALL_LOBBY
1. GET_ALL_LOBBY
Command:Response:Notify: Clients that made the request.
2. JOIN
2. JOIN
Command:Response:Notify: All the clients in the joined lobby.
3. QUIT
3. QUIT
Command:Response:Notify: All the clients in the client’s lobby.
4. SET_NEW_LOBBY
4. SET_NEW_LOBBY
Command:Response:Notify: All connected clients.
5. DELETE_LOBBY
5. DELETE_LOBBY
Command:Response:Notify: All connected clients.
6. LAUNCH_GAME
6. LAUNCH_GAME
Command:Response:Notify: All the clients in the launched lobby.
7. SET_PLAYER_READY
7. SET_PLAYER_READY
Command:Response:Notify: All the clients in the client’s lobby.
8. UPDATE_LOBBY
8. UPDATE_LOBBY
Command:Response:Notify: All connected clients.
9. UPDATE_PERM
9. UPDATE_PERM
Command:Response:Notify: All the clients in the client’s lobby.
10. KICK_PLAYER
10. KICK_PLAYER
Command:Response:Notify: All the clients in the client’s lobby.
11. SEND_MSG
11. SEND_MSG
Command:Response:Notify: All the clients in the client’s lobby.
UDP Protocol
Header Format
Header Format
The UDP packet header is 7 bytes long and structured as follows:
Example of Header Structure:
Commands
Commands
LOGIN (0)
LOGIN (0)
Command Code: Description: Used for user login with a password.
0Structure:GET_POS (1)
GET_POS (1)
Command Code: Description: Retrieves the position of an entity.
1Structure:SHOOT (2)
SHOOT (2)
Command Code: Description: Command for shooting.
2Structure:SEND_POS (3)
SEND_POS (3)
Command Code: Description: Sends the position of an entity.
3Structure:SHOOT_ALLY (4)
SHOOT_ALLY (4)
Command Code: Description: Command for allies to shoot.
4Structure:SPAWN_PLANE (5)
SPAWN_PLANE (5)
Command Code: Description: Spawns a plane entity.
5Structure:SPAWN_CRAB (6)
SPAWN_CRAB (6)
Command Code: Description: Spawns a crab entity.
6Structure:SPAWN_ROBOT (7)
SPAWN_ROBOT (7)
Command Code: Description: Spawns a robot entity.
7Structure:SPAWN_BOSS (8)
SPAWN_BOSS (8)
Command Code: Description: Spawns a boss entity.
8Structure:KILL_MONSTER (9)
KILL_MONSTER (9)
Command Code: Description: Kills a monster entity.
9Structure:PLANE_SHOOT (10)
PLANE_SHOOT (10)
Command Code: Description: Plane shoots with a given direction.
10Structure:CRAB_NEW_DIR (11)
CRAB_NEW_DIR (11)
Command Code: Description: Changes the direction of a crab entity.
11Structure:ROBOT_NEW_DIR (12)
ROBOT_NEW_DIR (12)
Command Code: Description: Changes the direction of a robot entity.
12Structure:ROBOT_SHOOT (13)
ROBOT_SHOOT (13)
Command Code: Description: Robot shoots.
13Structure:BOSS_SHOOT (14)
BOSS_SHOOT (14)
Command Code: Description: Boss shoots.
14Structure:ALLY_UPDATE_HP (15)
ALLY_UPDATE_HP (15)
Command Code: Description: Updates the health points of an ally.
15Structure:UPDATE_HP (16)
UPDATE_HP (16)
Command Code: Description: Updates the health points of an entity.
16Structure:KILL_ALLY (17)
KILL_ALLY (17)
Command Code: Description: Kills an ally entity.
17Structure:KILL (18)
KILL (18)
Command Code: Description: Kills an entity by ID.
18Structure: