Skip to main content

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.

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 SafeQueue mechanism that centralizes messages from both protocols into a single queue.

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:
The serialization process:
  1. Prepare the data structure.
  2. Compress the data.
  3. Add the header with the original size.
  4. Transmit the packet.
The deserialization process:
  1. Decompress the received data using the original size.
  2. Deserialize the data back into the appropriate structure.

Packet Duplication

To mitigate packet loss, the network library implements a message duplication mechanism:
  • Each packet is sent 10 times.
  • Upon receipt, the sequence_id is checked.
    • If the sequence_id already exists, the packet is ignored.
    • Otherwise, the corresponding command is executed.
This ensures reliable communication even in environments with high packet loss.

TCP Protocol

Responses in case of errors follow this structure:

List of Errors

  • 400: Invalid arguments
  • 401: Maximum number of players reached
  • 402: Room not found
  • 403: Player already in lobby
  • 404: Player is not in this lobby
  • 405: Invalid password
  • 406: Number of slots should be > 0 and < 5
  • 407: Lobby name already exists, please provide another
  • 408: Only the owner has the right
  • 409: Default lobby, you do not have the right
  • 410: Only admin can launch the game
  • 411: Some players are not ready
  • 412: Some parameters must be numbers
  • 413: Game already running
Command:
Response:
Notify: Clients that made the request.
Command:
Response:
Notify: All the clients in the joined lobby.
Command:
Response:
Notify: All the clients in the client’s lobby.
Command:
Response:
Notify: All connected clients.
Command:
Response:
Notify: All connected clients.
Command:
Response:
Notify: All the clients in the launched lobby.
Command:
Response:
Notify: All the clients in the client’s lobby.
Command:
Response:
Notify: All connected clients.
Command:
Response:
Notify: All the clients in the client’s lobby.
Command:
Response:
Notify: All the clients in the client’s lobby.
Command:
Response:
Notify: All the clients in the client’s lobby.

UDP Protocol

The UDP packet header is 7 bytes long and structured as follows:Example of Header Structure:
Command Code: 0Structure:
Description: Used for user login with a password.
Command Code: 1Structure:
Description: Retrieves the position of an entity.
Command Code: 2Structure:
Description: Command for shooting.
Command Code: 3Structure:
Description: Sends the position of an entity.
Command Code: 4Structure:
Description: Command for allies to shoot.
Command Code: 5Structure:
Description: Spawns a plane entity.
Command Code: 6Structure:
Description: Spawns a crab entity.
Command Code: 7Structure:
Description: Spawns a robot entity.
Command Code: 8Structure:
Description: Spawns a boss entity.
Command Code: 9Structure:
Description: Kills a monster entity.
Command Code: 10Structure:
Description: Plane shoots with a given direction.
Command Code: 11Structure:
Description: Changes the direction of a crab entity.
Command Code: 12Structure:
Description: Changes the direction of a robot entity.
Command Code: 13Structure:
Description: Robot shoots.
Command Code: 14Structure:
Description: Boss shoots.
Command Code: 15Structure:
Description: Updates the health points of an ally.
Command Code: 16Structure:
Description: Updates the health points of an entity.
Command Code: 17Structure:
Description: Kills an ally entity.
Command Code: 18Structure:
Description: Kills an entity by ID.

Summary Table

TCP Commands

UDP Commands


Conclusion

The TCP Protocol ensures reliable communication for operations such as joining lobbies, retrieving information, and launching games. The UDP Protocol is optimized for speed and lightweight packet delivery, making it ideal for real-time updates. Together, these protocols provide a robust and efficient foundation for network communication in the R-Type project.