System Overview¶
This page provides a high-level overview of MafiaNet’s architecture.
Core Components¶
┌─────────────────────────────────────────────────────────┐
│ Your Application │
├─────────────────────────────────────────────────────────┤
│ Plugin Layer │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │Replica │ │NAT Punch │ │File │ │ RPC4 │ │
│ │Manager3 │ │through │ │Transfer │ │ │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
├─────────────────────────────────────────────────────────┤
│ RakPeerInterface │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Connection Management | Reliability Layer │ │
│ │ BitStream | Security │ │
│ └──────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────┤
│ Socket Layer │
│ ┌──────────────────────────────────────────────────┐ │
│ │ UDP Sockets │ │
│ └──────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
RakPeerInterface¶
The main entry point for all networking. Handles:
Socket creation and management
Connection establishment and maintenance
Packet sending and receiving
Plugin management
Statistics tracking
Reliability Layer¶
Provides reliable delivery over UDP:
Sequencing: Packets arrive in order
Acknowledgments: Confirms packet delivery
Retransmission: Resends lost packets
Congestion control: Adapts to network conditions
BitStream¶
Binary serialization utility:
Write any data type
Bit-level precision
Endian handling
Compression helpers
Plugin System¶
Modular extensions that hook into the network layer:
Process packets before/after your application
Add features without modifying core code
Enable/disable as needed
Data Flow¶
Sending¶
Your App → BitStream → RakPeerInterface → Reliability Layer → Socket → Network
Create data with BitStream
Call
Send()with priority and reliabilityReliability layer queues and manages delivery
Socket sends UDP packets
Receiving¶
Network → Socket → Reliability Layer → Plugins → RakPeerInterface → Your App
Socket receives UDP packets
Reliability layer reassembles and orders
Plugins process (optional)
Receive()returns packet to your app
Threading Model¶
MafiaNet uses a background thread for:
Socket I/O
Reliability processing
Keep-alive pings
Your application:
Calls
Receive()from any threadCalls
Send()from any threadBoth are thread-safe
Memory Management¶
Packets: Allocated by MafiaNet, freed with
DeallocatePacket()BitStream: You manage (stack or heap)
Plugins: You manage lifetime
RakPeerInterface: Create with
GetInstance(), destroy withDestroyInstance()
Namespaces¶
MafiaNet::- Primary namespace for all classesMNet- Preprocessor macro shorthand that expands toMafiaNet(defined inmafianet/defines.h)DataStructures::- Container classes (DS_List, DS_Queue, etc.)
Common Patterns¶
Client-Server¶
One authoritative server, multiple clients:
Client A ─────┐
│
Client B ─────┼───── Server
│
Client C ─────┘
Peer-to-Peer¶
All peers connect to each other:
Peer A ◄────► Peer B
│ ╲ ╱ │
│ ╲ ╱ │
│ ╲ ╱ │
│ ╲╱ │
│ ╱╲ │
│ ╱ ╲ │
│ ╱ ╲ │
▼ ╱ ╲ ▼
Peer C ◄────► Peer D
Hybrid¶
Central server for matchmaking, P2P for gameplay:
Phase 1: Matchmaking Phase 2: Gameplay
Client A ──┐ Client A ◄──► Client B
│
Client B ──┼── Lobby Server (direct connection)
│
Client C ──┘
See Also¶
Quick Start - Get started coding
Startup - Startup details
Core Concepts - Core concepts explained