Add comprehensive server architecture diagrams and documentation

Co-authored-by: aronwk-aaron <26027722+aronwk-aaron@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-08-31 04:05:35 +00:00
parent 93354af834
commit d5822e2d24
4 changed files with 688 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
# DarkflameServer Implementation Notes
## Key Implementation Files by Server Type
### Master Server (`dMasterServer/`)
- **MasterServer.cpp**: Main server loop and packet handling
- **InstanceManager.cpp**: Manages zone instances and player distribution
- **PersistentIDManager.cpp**: Generates unique object IDs across servers
- **Start.cpp**: Utility functions for starting other server types
### Authentication Server (`dAuthServer/`)
- **AuthServer.cpp**: Handles login, account creation, session management
- **AuthPackets.cpp**: Authentication protocol implementation
### Chat Server (`dChatServer/`)
- **ChatServer.cpp**: Main chat server with social features
- **ChatPacketHandler.cpp**: Routes chat and social messages
- **PlayerContainer.cpp**: Manages online player registry
- **TeamContainer.cpp**: Handles team/group functionality
### World Server (`dWorldServer/`)
- **WorldServer.cpp**: Game world simulation and entity management
- **PerformanceManager.cpp**: Monitors server performance metrics
### Networking Layer (`dNet/`)
- **dServer.cpp/h**: Base server class with master communication
- **MasterPackets.cpp/h**: Master server protocol implementation
- **AuthPackets.cpp/h**: Authentication protocol
- **ChatPackets.cpp/h**: Chat and social protocols
- **WorldPackets.cpp/h**: World server protocols
## Message Flow Implementation
### Packet Structure
```cpp
// All packets use this basic structure:
BitStreamUtils::WriteHeader(bitStream, ServiceType, MessageType);
// Followed by message-specific data
```
### Service Types (from `ServiceType.h`)
```cpp
enum class ServiceType : uint16_t {
COMMON = 0,
AUTH, // Authentication Server
CHAT, // Chat Server
WORLD = 4, // World Server
CLIENT, // Client messages
MASTER, // Master Server
UNKNOWN
};
```
### Critical Communication Patterns
#### 1. Server-to-Master Registration
```cpp
// All servers connect to master on startup
void dServer::ConnectToMaster() {
mMasterPeer->Connect(mMasterIP.c_str(), mMasterPort,
mMasterPassword.c_str(), mMasterPassword.size());
}
// On connection, servers send their info
case ID_CONNECTION_REQUEST_ACCEPTED: {
MasterPackets::SendServerInfo(this, packet);
break;
}
```
#### 2. Zone Transfer Protocol
```cpp
// World Server requests transfer
MasterPackets::SendZoneTransferRequest(server, requestID, mythranShift, zoneID, cloneID);
// Master finds/creates instance and responds
void HandleZoneTransferRequest() {
auto instance = Game::im->GetInstance(zoneID, false, zoneClone);
// Send response with target server info
}
```
#### 3. Chat Message Routing
```cpp
// World Server forwards chat to Chat Server
void HandleChatMessage() {
// Route through Chat Server for processing
Game::chatServer->Send(bitStream, chatServerAddr, false);
}
// Chat Server routes to all relevant World Servers
void RouteMessage() {
for (auto& worldServer : connectedWorldServers) {
Send(message, worldServer.address, false);
}
}
```
## Database Architecture
### Core Tables
- **accounts**: User account information
- **charinfo**: Character data and statistics
- **friends**: Friend relationships
- **mail**: In-game mail system
- **properties**: Player property data
- **leaderboards**: Competition scores
- **servers**: Server configuration
### Connection Management
```cpp
// Shared database connection across all servers
Database::Connect(); // Uses connection pooling
auto result = Database::Get()->Query(sql);
```
## Scalability Design
### Load Distribution
- Master Server acts as load balancer
- Multiple World Servers can run different zones
- Instance Manager distributes players across zones
- Chat Server handles all social features centrally
### Fault Tolerance
- All servers maintain heartbeat with Master
- Automatic reconnection on connection loss
- Player state persisted in database
- Graceful degradation when servers unavailable
### Performance Optimizations
- Entity serialization only to relevant players (ghosting)
- Spatial partitioning for efficient updates
- Message batching for network efficiency
- Database query optimization with prepared statements
## Security Features
### Network Security
- RakNet encryption for client connections
- Server-to-server authentication via passwords
- Session key validation across servers
- Packet validation and sanitization
### Game Security
- Server-side validation of all game actions
- Anti-cheat through server authority
- Rate limiting on client requests
- Database injection prevention
## Development Notes
### Build System
- CMake-based build with modular components
- Cross-platform support (Windows, Linux, macOS)
- Dependency management through git submodules
- Automated testing framework
### Configuration
- INI-based configuration files per server type
- Environment variable support
- Runtime configuration updates
- Database-driven server settings
### Debugging Features
- Comprehensive logging system
- Performance metrics collection
- Packet capture and analysis tools
- Real-time server monitoring
This implementation successfully recreates the LEGO Universe MMO experience through a robust, scalable architecture that can support thousands of concurrent players across multiple game zones while maintaining the original game's functionality and performance characteristics.

View File

@@ -0,0 +1,129 @@
# DarkflameServer Architecture - ASCII Diagram
```
DARKFLAME SERVER ARCHITECTURE
===============================
┌─────────────────────────────────────────────────────────────────────────────────────────┐
│ CLIENT LAYER │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │LEGO Universe│ │LEGO Universe│ │LEGO Universe│ │LEGO Universe│ │
│ │ Client │ │ Client │ │ Client │ │ Client │ │
│ │ (Player) │ │ (Player) │ │ (Player) │ │ (Player) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │ │
└─────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┘
│ │ │ │
│ │ │ │
┌─────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┐
│ │ SERVER COMMUNICATION LAYER │ │ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │AUTH SERVER │ │CHAT SERVER │ │WORLD SERVER │ │WORLD SERVER │ │
│ │ │ │ │ │ │ │ │ │
│ │Port: 1001 │ │Port: 1501 │ │Port: Dynamic│ │Port: Dynamic│ │
│ │ │ │ │ │ │ │ │ │
│ │• Login │ │• Chat Msgs │ │• Game Logic │ │• Game Logic │ │
│ │• Accounts │ │• Teams │ │• Entities │ │• Entities │ │
│ │• Sessions │ │• Guilds │ │• Physics │ │• Physics │ │
│ │• Keys │ │• Friends │ │• Zone Mgmt │ │• Zone Mgmt │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │ │
│ └───────────────────┼───────────────────┼───────────────────┘ │
│ │ │ │
│ │ │ │
│ ┌───────────────────┼───────────────────┼───────────────────┐ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────────┐ │
│ │ MASTER SERVER (Port: 1500) │ │
│ │ │ │
│ │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ │
│ │ │ Instance Manager │ │Persistent ID Mgr │ │ Session Manager │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │• Zone Instances │ │• Object ID Gen │ │• Player Sessions │ │ │
│ │ │• Load Balance │ │• Unique IDs │ │• Server Registry │ │ │
│ │ │• Player Transfer │ │• Cross-Server │ │• Coordination │ │ │
│ │ └──────────────────┘ └──────────────────┘ └──────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────────────┘ │
│ │ │
└────────────────────────────────────────┼────────────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ DATABASE LAYER │
│ │
│ ┌─────────────────────┐ │
│ │ MySQL Database │ │
│ │ │ │
│ │ • Player Data │ │
│ │ • Characters │ │
│ │ • Friends/Mail │ │
│ │ • Properties │ │
│ │ • Leaderboards │ │
│ │ • Configuration │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────┘
MESSAGE FLOW OVERVIEW
====================
Auth Messages Chat Messages World Messages
============= ============= ==============
LOGIN_REQUEST GENERAL_CHAT_MESSAGE CHARACTER_LIST_REQUEST
CREATE_ACCOUNT PRIVATE_CHAT_MESSAGE GAME_MSG (1000+ types)
SESSION_KEY TEAM_INVITE LEVEL_LOAD_COMPLETE
AUTH_RESPONSE ADD_FRIEND POSITION_UPDATE
GUILD_OPERATIONS ZONE_TRANSFER_REQUEST
Master Coordination Messages
============================
SERVER_INFO REQUEST_PERSISTENT_ID WORLD_READY
PLAYER_ADDED/REMOVED REQUEST_ZONE_TRANSFER SHUTDOWN
SET_SESSION_KEY AFFIRM_TRANSFER NEW_SESSION_ALERT
COMMUNICATION PATTERNS
======================
1. Client Authentication Flow:
Client → Auth Server → Master Server → World Server → Chat Server
2. Zone Transfer Flow:
World Server → Master Server → Target World Server → Client
3. Chat Message Flow:
Client → World Server → Chat Server → Target World Server → Target Client
4. Entity Updates Flow:
World Server → EntityManager → ReplicaManager → All Clients in Zone
5. Master Coordination:
All Servers ↔ Master Server (Heartbeat, Registration, Coordination)
NETWORK SPECIFICATIONS
======================
Protocol: RakNet (UDP-based)
Encryption: Enabled for client connections
Packet Structure: [RakNet Header][Message Type][Service Type][Message Data]
Service Type Codes:
- MASTER (5): Server coordination
- AUTH (1): Authentication
- CHAT (2): Social features
- WORLD (4): Game simulation
- CLIENT (4): Client-specific
Key Features:
- Distributed architecture for scalability
- Fault tolerance through master coordination
- Real-time entity replication
- Cross-server player transfers
- Persistent world state
```

View File

@@ -0,0 +1,382 @@
# DarkflameServer Architecture and Communication Diagram
This document provides a comprehensive overview of the server architecture, inter-server communication patterns, and message flows in the DarkflameServer LEGO Universe emulator.
## Server Architecture Overview
```mermaid
graph TB
subgraph "Client Layer"
Client1[LEGO Universe Client]
Client2[LEGO Universe Client]
Client3[LEGO Universe Client]
end
subgraph "DarkflameServer Infrastructure"
subgraph "Master Server (Port 1500)"
MasterServer[Master Server<br/>- Zone Management<br/>- Instance Coordination<br/>- Session Management<br/>- Server Registration]
IM[Instance Manager<br/>- Zone Instances<br/>- Player Transfers<br/>- Load Balancing]
PIM[Persistent ID Manager<br/>- Object ID Generation<br/>- Unique Identifiers]
end
subgraph "Authentication Server (Port 1001)"
AuthServer[Auth Server<br/>- Login Processing<br/>- Account Management<br/>- Session Keys<br/>- Player Authentication]
end
subgraph "Chat Server (Port 1501)"
ChatServer[Chat Server<br/>- General Chat<br/>- Private Messages<br/>- Team Management<br/>- Guild System<br/>- Friend Lists<br/>- Mail System]
PC[Player Container<br/>- Online Players<br/>- Chat Routing]
end
subgraph "World Servers (Dynamic Ports)"
WorldServer1[World Server 1<br/>- Game Logic<br/>- Entity Management<br/>- Physics Simulation<br/>- Zone: Avant Gardens]
WorldServer2[World Server 2<br/>- Game Logic<br/>- Entity Management<br/>- Physics Simulation<br/>- Zone: Nimbus Station]
WorldServer3[World Server N<br/>- Game Logic<br/>- Entity Management<br/>- Physics Simulation<br/>- Zone: Custom]
EM1[Entity Manager]
EM2[Entity Manager]
EM3[Entity Manager]
WorldServer1 --- EM1
WorldServer2 --- EM2
WorldServer3 --- EM3
end
end
subgraph "Database Layer"
Database[(MySQL Database<br/>- Player Data<br/>- Characters<br/>- Properties<br/>- Leaderboards<br/>- Mail<br/>- Friends)]
end
%% Client Connections
Client1 -.->|1. Authentication| AuthServer
Client1 -.->|2. Character/Zone Data| WorldServer1
Client1 -.->|3. Chat Messages| ChatServer
Client2 -.->|Authentication| AuthServer
Client2 -.->|Character/Zone Data| WorldServer2
Client2 -.->|Chat Messages| ChatServer
Client3 -.->|Authentication| AuthServer
Client3 -.->|Character/Zone Data| WorldServer3
Client3 -.->|Chat Messages| ChatServer
%% Server-to-Master Connections (All servers connect to master)
AuthServer <-.->|SERVER_INFO<br/>SESSION_KEY<br/>PLAYER_ADDED/REMOVED| MasterServer
ChatServer <-.->|SERVER_INFO<br/>PLAYER_ADDED/REMOVED<br/>HEARTBEAT| MasterServer
WorldServer1 <-.->|ZONE_TRANSFER<br/>PERSISTENT_ID<br/>WORLD_READY<br/>SERVER_INFO| MasterServer
WorldServer2 <-.->|ZONE_TRANSFER<br/>PERSISTENT_ID<br/>WORLD_READY<br/>SERVER_INFO| MasterServer
WorldServer3 <-.->|ZONE_TRANSFER<br/>PERSISTENT_ID<br/>WORLD_READY<br/>SERVER_INFO| MasterServer
%% Master Server Internal Communication
MasterServer --- IM
MasterServer --- PIM
%% Database Connections
AuthServer <--> Database
ChatServer <--> Database
WorldServer1 <--> Database
WorldServer2 <--> Database
WorldServer3 <--> Database
MasterServer <--> Database
%% Inter-World Server Communication (through Chat Server)
WorldServer1 <-.->|Player Messages<br/>Zone Chat<br/>Team Invites| ChatServer
WorldServer2 <-.->|Player Messages<br/>Zone Chat<br/>Team Invites| ChatServer
WorldServer3 <-.->|Player Messages<br/>Zone Chat<br/>Team Invites| ChatServer
style MasterServer fill:#ff9999
style AuthServer fill:#99ccff
style ChatServer fill:#99ff99
style WorldServer1 fill:#ffcc99
style WorldServer2 fill:#ffcc99
style WorldServer3 fill:#ffcc99
style Database fill:#cc99ff
```
## Message Type Breakdown
### Master Server Messages (`MessageType::Master`)
```mermaid
graph LR
subgraph "Master Server Communication"
Master[Master Server]
subgraph "To/From Other Servers"
M1[REQUEST_PERSISTENT_ID<br/>↔ World Servers]
M2[REQUEST_ZONE_TRANSFER<br/>↔ World Servers]
M3[SERVER_INFO<br/>← All Servers]
M4[SET_SESSION_KEY<br/>↔ Auth Server]
M5[PLAYER_ADDED/REMOVED<br/>← All Servers]
M6[WORLD_READY<br/>← World Servers]
M7[SHUTDOWN<br/>→ All Servers]
end
Master -.-> M1
Master -.-> M2
Master -.-> M3
Master -.-> M4
Master -.-> M5
Master -.-> M6
Master -.-> M7
end
```
### Authentication Server Messages (`MessageType::Auth`)
```mermaid
graph LR
subgraph "Auth Server Communication"
Auth[Auth Server]
subgraph "To/From Clients"
A1[LOGIN_REQUEST<br/>← Clients]
A2[CREATE_NEW_ACCOUNT_REQUEST<br/>← Clients]
A3[LEGOINTERFACE_AUTH_RESPONSE<br/>→ Clients]
end
subgraph "To/From Master"
A4[SESSION_KEY<br/>→ Master]
A5[PLAYER_NOTIFICATION<br/>→ Master]
end
Auth -.-> A1
Auth -.-> A2
Auth -.-> A3
Auth -.-> A4
Auth -.-> A5
end
```
### Chat Server Messages (`MessageType::Chat`)
```mermaid
graph LR
subgraph "Chat Server Communication"
Chat[Chat Server]
subgraph "Player Social Features"
C1[GENERAL_CHAT_MESSAGE<br/>↔ All Players]
C2[PRIVATE_CHAT_MESSAGE<br/>↔ Players]
C3[TEAM_INVITE/RESPONSE<br/>↔ Players]
C4[ADD_FRIEND_REQUEST<br/>↔ Players]
C5[GUILD_OPERATIONS<br/>↔ Players]
C6[MAIL_SYSTEM<br/>↔ Players]
end
subgraph "World Server Integration"
C7[WORLD_ROUTE_PACKET<br/>↔ World Servers]
C8[PLAYER_READY<br/>← World Servers]
C9[HEARTBEAT_REQUEST<br/>↔ World Servers]
end
Chat -.-> C1
Chat -.-> C2
Chat -.-> C3
Chat -.-> C4
Chat -.-> C5
Chat -.-> C6
Chat -.-> C7
Chat -.-> C8
Chat -.-> C9
end
```
### World Server Messages (`MessageType::World` & `MessageType::Game`)
```mermaid
graph TB
subgraph "World Server Communication"
World[World Server]
subgraph "Client Game Messages"
W1[CHARACTER_LIST_REQUEST<br/>← Clients]
W2[CHARACTER_CREATE_REQUEST<br/>← Clients]
W3[LOGIN_REQUEST<br/>← Clients]
W4[GAME_MSG<br/>↔ Clients]
W5[LEVEL_LOAD_COMPLETE<br/>← Clients]
W6[POSITION_UPDATE<br/>↔ Clients]
end
subgraph "Entity & Game Logic (1000+ Game Messages)"
G1[Entity Serialization<br/>Player/NPC Updates]
G2[Combat System<br/>Skills & Damage]
G3[Building System<br/>Property Management]
G4[Mission System<br/>Quest Management]
G5[Inventory System<br/>Item Management]
G6[Racing System<br/>Vehicle Physics]
end
subgraph "Master Server Communication"
W7[ZONE_TRANSFER_REQUEST<br/>→ Master]
W8[PERSISTENT_ID_REQUEST<br/>→ Master]
W9[WORLD_READY<br/>→ Master]
end
subgraph "Chat Server Communication"
W10[Chat Messages<br/>↔ Chat Server]
W11[Team Operations<br/>↔ Chat Server]
end
World -.-> W1
World -.-> W2
World -.-> W3
World -.-> W4
World -.-> W5
World -.-> W6
World -.-> W7
World -.-> W8
World -.-> W9
World -.-> W10
World -.-> W11
World --- G1
World --- G2
World --- G3
World --- G4
World --- G5
World --- G6
end
```
## Communication Flow Diagrams
### Player Login Flow
```mermaid
sequenceDiagram
participant C as Client
participant A as Auth Server
participant M as Master Server
participant W as World Server
participant Ch as Chat Server
participant DB as Database
C->>A: LOGIN_REQUEST
A->>DB: Validate credentials
DB-->>A: User data
A->>M: SET_SESSION_KEY
A->>C: LEGOINTERFACE_AUTH_RESPONSE
C->>W: CHARACTER_LIST_REQUEST
W->>DB: Get characters
DB-->>W: Character data
W->>C: Character list
C->>W: LOGIN_REQUEST (character selected)
W->>M: PLAYER_ADDED
W->>Ch: LOGIN_SESSION_NOTIFY
W->>C: Zone data & enter world
```
### Zone Transfer Flow
```mermaid
sequenceDiagram
participant C as Client
participant W1 as Source World Server
participant M as Master Server
participant W2 as Target World Server
participant Ch as Chat Server
C->>W1: Transfer request (rocket, door, etc.)
W1->>M: REQUEST_ZONE_TRANSFER
M->>M: Find/create zone instance
M-->>W1: ZONE_TRANSFER_RESPONSE
W1->>C: TRANSFER_TO_ZONE
C->>W2: Connect to new world server
W2->>M: Confirm player transfer
W2->>Ch: Update player location
W1->>M: PLAYER_REMOVED
W2->>M: PLAYER_ADDED
```
### Chat Message Flow
```mermaid
sequenceDiagram
participant C1 as Client 1
participant W1 as World Server 1
participant Ch as Chat Server
participant W2 as World Server 2
participant C2 as Client 2
C1->>W1: Chat message
W1->>Ch: GENERAL_CHAT_MESSAGE
Ch->>Ch: Process & route message
Ch->>W2: WORLD_ROUTE_PACKET
W2->>C2: Display message
Ch->>W1: Message confirmation
W1->>C1: Message sent confirmation
```
## Technical Implementation Details
### Network Architecture
- **Protocol**: RakNet UDP-based networking
- **Security**: Encryption enabled for external connections
- **Port Configuration**:
- Master Server: 1500 (default)
- Auth Server: 1001 (hardcoded in client)
- Chat Server: 1501 (configurable)
- World Servers: Dynamic ports assigned by Master
### Server-to-Server Communication
```cpp
// All servers maintain connection to Master
class dServer {
Packet* ReceiveFromMaster();
void SendToMaster(RakNet::BitStream& bitStream);
bool ConnectToMaster();
void SetupForMasterConnection();
};
```
### Key Classes and Components
#### Master Server Components
- `InstanceManager`: Manages zone instances and player distribution
- `PersistentIDManager`: Generates unique object IDs across all servers
- `MasterPackets`: Handles inter-server message protocols
#### World Server Components
- `EntityManager`: Manages all game entities and their serialization
- `UserManager`: Tracks connected players and their sessions
- `ReplicaManager`: Handles object replication to clients
- `GameMessages`: Processes 1000+ different game mechanic messages
#### Chat Server Components
- `PlayerContainer`: Maintains online player registry
- `TeamContainer`: Manages team/group functionality
- `ChatPacketHandler`: Routes all chat and social messages
### Database Integration
- All servers connect to shared MySQL database
- Player data, characters, mail, friends, properties stored centrally
- Concurrent access managed through connection pooling
- Migration system for database schema updates
### Scalability Features
- Multiple World Server instances for different zones
- Load balancing through Master Server's Instance Manager
- Dynamic server spawning capability
- Horizontal scaling support for high player counts
## Message Protocol Specifications
### Packet Structure
```
[RakNet Header][Message Type][Service Type][Message Data]
```
### Service Types
- `MASTER (5)`: Master server coordination
- `AUTH (1)`: Authentication services
- `CHAT (2)`: Chat and social features
- `WORLD (4)`: Game world simulation
- `CLIENT (4)`: Client-specific messages
### Critical Message Types by Frequency
1. **GAME_MSG** (~60% of traffic): Real-time game mechanics
2. **POSITION_UPDATE** (~20% of traffic): Player movement
3. **Chat Messages** (~10% of traffic): Social communication
4. **Entity Serialization** (~8% of traffic): World state sync
5. **Administrative** (~2% of traffic): Server coordination
This architecture enables the DarkflameServer to faithfully recreate the LEGO Universe MMO experience through a distributed, scalable server infrastructure that can handle thousands of concurrent players across multiple game zones.