R-Type Network Architecture Documentation
This document provides a detailed explanation of the R-Type network implementation. It describes the structure, threading model, client management, and mechanisms for handling communication between the server and clients using TCP and UDP protocols.
Overview
The R-Type network is designed to handle real-time multiplayer interactions with a balance between reliability and performance. The server uses a dual-protocol approach:
- TCP: Ensures reliable delivery of critical operations such as player creation and key press events.
- UDP: Optimized for real-time updates like player positions, bullet movements, and obstacle updates, where occasional packet loss is acceptable.
Architecture and Structure
Server Components
-
Main Server Class (
Server)- Orchestrates the overall network flow.
- Initializes both TCP and UDP sockets.
- Starts separate threads for handling TCP and UDP operations.
-
TCP Socket (
TcpSocket)- Manages reliable client-server communication.
- Handles client connections, disconnections, and data reception.
- Binds each client connection to a specific player.
-
UDP Socket (
UdpSocket)- Manages real-time communication.
- Broadcasts updates to all clients.
- Operates three main loops:
- Read Loop: Reads incoming UDP packets.
- Update Loop: Processes game logic.
- Send Loop: Sends updates to clients.
-
SmartBuffer
- Handles serialization and deserialization of data.
- Used to encode and decode protocol-compliant messages.
Threading Model
The server uses a multi-threaded approach to handle TCP and UDP operations concurrently:
Threads
-
TCP Thread:
- Handles client connections and disconnections via the
TcpSocket. - Reads and processes incoming data using a dedicated thread per client.
- Handles client connections and disconnections via the
-
UDP Threads:
- Read Thread: Continuously receives packets from clients and processes them.
- Update Thread: Runs the game logic, updating player positions, bullets, and obstacles.
- Send Thread: Sends game state updates to all clients.
Thread Synchronization
- Mutexes: Used to protect shared resources such as the list of connected clients and game entities.
- Locks: Ensures that no two threads modify the same data simultaneously.
Client Management
Storing Clients
Clients are stored in separate lists for TCP and UDP communications:
-
TCP Clients:
- Maintained as a vector of socket file descriptors.
- Each client is associated with a player ID, stored in the
PlayerManager.
-
UDP Clients:
- Stored as a vector of
sockaddr_instructures. - Each client’s address is added when a UDP packet is received.
- Stored as a vector of
Mapping Players to Clients
- When a new player is created via TCP, their
playerIdis mapped to their TCP socket. - The same
playerIdis used for UDP communications to identify the player.
TCP Workflow
Connection Handling
-
Initialization:
- The TCP socket is created and bound to a port.
- The socket listens for incoming connections.
-
Accepting Connections:
- New connections are accepted in a loop.
- Each accepted client socket is passed to a dedicated thread for communication.
Data Handling
-
Receiving Data:
- Each client thread reads data from its associated socket.
- Data is passed to the
Protocolclass for decoding and handling.
-
Sending Data:
- Responses are serialized using
SmartBufferand sent back to the client. - Broadcasts are handled by iterating over the list of clients.
- Responses are serialized using
UDP Workflow
Initialization
- The UDP socket is created and bound to a port.
- Unlike TCP, there is no need to establish connections.
Read Loop
- Continuously reads incoming packets from any client.
- Decodes messages using
Protocoland updates the game state.
Update Loop
- Executes game logic, such as updating player positions, bullets, and obstacles.
- Uses mutexes to synchronize shared resources.
Send Loop
- Serializes the current game state into protocol-compliant messages.
- Sends updates to all connected clients using their stored addresses.
Key Classes and Responsibilities
1. Server
- Entry point of the application.
- Initializes
TcpSocketandUdpSocket. - Manages threads for TCP and UDP workflows.
2. TcpSocket
- Handles client connections and reliable communication.
- Maintains a list of connected clients.
- Maps client sockets to player IDs.
3. UdpSocket
- Handles real-time communication.
- Maintains a list of client addresses.
- Reads, updates, and sends game state in separate threads.
4. Protocol
- Decodes incoming messages and routes them to the appropriate handler.
- Encodes outgoing messages to ensure protocol compliance.
5. SmartBuffer
- Provides serialization and deserialization utilities.
- Ensures efficient handling of binary data.
Communication Flow
-
Player Creation:
- A client sends a
CREATE_PLAYERrequest via TCP. - The server creates a player and sends back a
CREATE_PLAYER_CALLBACKresponse. - The server broadcasts a
CREATE_PLAYER_BROADCASTmessage via UDP.
- A client sends a
-
Game Updates:
- The server’s update thread processes the game state (e.g., player movements, bullets).
- The send thread broadcasts updates via UDP to all clients.
-
Hotkey Presses:
- A client sends a
HOTKEY_PRESSEDmessage via TCP. - The server updates the player’s state based on the hotkey.
- A client sends a
Summary
The R-Type network implementation leverages the strengths of both TCP and UDP protocols to deliver reliable and efficient communication. The architecture ensures scalability and real-time performance while maintaining a clear separation of responsibilities between components. Mutexes and threads ensure thread safety, enabling smooth operation in a multiplayer environment.