Congestion Control

MafiaNet implements adaptive congestion control to maximize throughput while avoiding network congestion. Understanding and tuning these settings is essential for optimal game networking performance.

Overview

The congestion control system:

  • Monitors packet loss and round-trip times

  • Adjusts send rate dynamically

  • Prioritizes reliable over unreliable traffic

  • Supports different algorithms for different use cases

Basic Configuration

Setting bandwidth limits:

MafiaNet::RakPeerInterface* peer = MafiaNet::RakPeerInterface::GetInstance();

// Limit outgoing bandwidth (bytes per second)
peer->SetPerConnectionOutgoingBandwidthLimit(50000);  // 50 KB/s

// Set unlimited (default)
peer->SetPerConnectionOutgoingBandwidthLimit(0);

Checking connection quality:

MafiaNet::RakNetStatistics stats;
peer->GetStatistics(remoteAddress, &stats);

printf("RTT: %d ms\n", peer->GetAveragePing(remoteAddress));
printf("Packet loss: %.2f%%\n", stats.packetlossLastSecond * 100.0f);
printf("Bandwidth: %d bytes/sec\n", stats.BPSLimitByCongestionControl);

Algorithm Selection

Configure in RakNetDefinesOverrides.h:

// Sliding window (TCP-like, recommended for most games)
#define USE_SLIDING_WINDOW_CONGESTION_CONTROL 1

// Fixed rate (no congestion control)
#define USE_SLIDING_WINDOW_CONGESTION_CONTROL 0

Priority and Reliability

Message priorities:

// Immediate send (highest priority)
peer->Send(&bs, IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, addr, false);

// High priority (game state)
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, 0, addr, false);

// Medium priority (player updates)
peer->Send(&bs, MEDIUM_PRIORITY, UNRELIABLE_SEQUENCED, 0, addr, false);

// Low priority (chat, non-critical)
peer->Send(&bs, LOW_PRIORITY, RELIABLE, 0, addr, false);

Reliability types:

  • RELIABLE - Guaranteed delivery, any order

  • RELIABLE_ORDERED - Guaranteed delivery, in order

  • RELIABLE_SEQUENCED - Guaranteed, newest only

  • UNRELIABLE - No guarantee, lowest overhead

  • UNRELIABLE_SEQUENCED - No guarantee, newest only

Tuning Parameters

// MTU discovery (finds optimal packet size)
peer->SetMTUSize(1492);  // Default, good for most networks

// Timeout settings
peer->SetTimeoutTime(10000, remoteAddress);  // 10 seconds

// Packet ordering channels (0-31)
// Use different channels for independent data streams
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, 0, addr, false);  // Channel 0
peer->Send(&bs2, HIGH_PRIORITY, RELIABLE_ORDERED, 1, addr, false); // Channel 1

Statistics Monitoring

MafiaNet::RakNetStatistics stats;
peer->GetStatistics(addr, &stats);

// Key metrics
stats.messageInSendBuffer;        // Pending messages
stats.bytesInSendBuffer;          // Pending bytes
stats.packetlossLastSecond;       // Recent packet loss
stats.BPSLimitByCongestionControl; // Current bandwidth limit

See Also