Startup¶
The first step to use MafiaNet is starting a peer. The modern API folds the
whole startup dance into a fluent builder — Peer::server() /
Peer::client() (see Startup Builders (Peer::server / Peer::client) below); the classic API calls
RakPeerInterface::Startup() directly.
Startup Builders (Peer::server / Peer::client)¶
MafiaNet::Peer::server() and MafiaNet::Peer::client() return fluent
builders that construct the SocketDescriptor, call Startup(), check the
result, and apply SetMaximumIncomingConnections() / Connect() in a
single chain. start() returns a MafiaNet::Result<Peer>:
#include "mafianet/mafianet.h"
auto server = MafiaNet::Peer::server()
.port(60000)
.max_connections(32)
.start(); // Startup + SetMaximumIncomingConnections
if (!server) {
printf("startup failed: %d\n", (int) server.error().startup);
return -1;
}
auto client = MafiaNet::Peer::client()
.max_connections(1)
.connect("127.0.0.1", 60000) // records the target
.start(); // Startup, then Connect
MafiaNet::Peer peer = std::move(*client); // Result<Peer> is move-only
On failure, result.error() preserves the underlying engine enum
(StartupResult or ConnectionAttemptResult) rather than collapsing it to
a bool. See Core API Reference for the full builder reference, including
passwords, bind addresses, and opt-in security.
The rest of this page documents the underlying Startup() call, which the
builders wrap and which remains fully supported.
Starting RakPeerInterface¶
StartupResult RakPeer::Startup(
unsigned short maxConnections,
SocketDescriptor *socketDescriptors,
unsigned socketDescriptorCount,
int threadPriority = -99999
);
Startup() will:
Generate
RakNetGUID, a unique identifier for this instanceAllocate an array of reliable connection slots (defined by
maxConnections)Create one or more sockets (described by
socketDescriptors)
Before calling Startup(), only raw UDP functions are available: Ping(), AdvertiseSystem(), and SendOutOfBand().
Parameters¶
maxConnections¶
MafiaNet preallocates memory for connections. Specify the maximum supported number of connections (both incoming and outgoing). If you want other systems to connect to you, also call SetMaximumIncomingConnections() with a value ≤ maxConnections.
socketDescriptors¶
In 95% of cases, use:
MafiaNet::SocketDescriptor sd(MY_LOCAL_PORT, 0);
For servers/peers: Set the port to your desired server port. This is the remotePort parameter clients will use with Connect().
For clients: Use port 0 to automatically pick a free port.
Note
On Linux, ports ≤ 1000 require admin rights.
Multiple Socket Bindings¶
For advanced users binding multiple network cards:
MafiaNet::SocketDescriptor sdArray[2];
sdArray[0].port = SERVER_PORT_1;
strcpy(sdArray[0].hostAddress, "192.168.0.1");
sdArray[1].port = SERVER_PORT_2;
strcpy(sdArray[1].hostAddress, "192.168.0.2");
if (rakPeer->Startup(32, sdArray, 2) == MafiaNet::RAKNET_STARTED) {
OnRakNetStarted();
}
IPv6 Support¶
IPv6 uses 16-byte addresses (e.g., fe80::7c:31f7:fec4:27de%14) instead of 4-byte IPv4 addresses. NAT Punchthrough is not needed with IPv6.
To enable IPv6:
socketDescriptor.socketFamily = AF_INET6;
Warning
IPv6 sockets can only connect to other IPv6 sockets. IPv4 sockets can only connect to IPv4.
threadPriority¶
Windows: Priority passed to
_beginthreadex(). Default (-99999) uses NORMAL_PRIORITY.Linux: Passed to
pthread_attr_setschedparam(). Default uses priority 1000.
Example: Basic Server Startup¶
#include "mafianet/peerinterface.h"
MafiaNet::RakPeerInterface* server = MafiaNet::RakPeerInterface::GetInstance();
MafiaNet::SocketDescriptor sd(60000, 0); // Port 60000
MafiaNet::StartupResult result = server->Startup(100, &sd, 1);
if (result != MafiaNet::RAKNET_STARTED) {
printf("Failed to start: %d\n", result);
return -1;
}
// Allow incoming connections
server->SetMaximumIncomingConnections(100);
Example: Basic Client Startup¶
MafiaNet::RakPeerInterface* client = MafiaNet::RakPeerInterface::GetInstance();
MafiaNet::SocketDescriptor sd; // Port 0 = auto-select
client->Startup(1, &sd, 1); // Only need 1 connection slot
Getting Your GUID¶
After startup, get your unique identifier:
MafiaNet::RakNetGUID myGuid = peer->GetGuidFromSystemAddress(
MafiaNet::UNASSIGNED_SYSTEM_ADDRESS
);
See Also¶
Connecting - How to connect to other systems
Building MafiaNet - Build configuration