SystemAddress¶
SystemAddress uniquely identifies a network endpoint by IP address and port.
Structure¶
struct SystemAddress {
// Binary IP address (network byte order)
union {
unsigned char addr4[4]; // IPv4
unsigned char addr6[16]; // IPv6
};
unsigned short port; // Port (host byte order)
// ...
};
Creating SystemAddress¶
From String¶
MafiaNet::SystemAddress addr;
addr.FromString("192.168.1.100:60000");
// Or separately
addr.FromString("192.168.1.100");
addr.SetPortHostOrder(60000);
From IP and Port¶
MafiaNet::SystemAddress addr("192.168.1.100", 60000);
// Or using SetBinaryAddress
MafiaNet::SystemAddress addr;
addr.SetBinaryAddress("192.168.1.100");
addr.SetPortHostOrder(60000);
Special Values¶
// Unassigned (used for broadcast)
MafiaNet::UNASSIGNED_SYSTEM_ADDRESS
// Localhost
MafiaNet::SystemAddress localhost("127.0.0.1", 60000);
Converting to String¶
MafiaNet::SystemAddress addr;
// Full address with port
const char* str = addr.ToString(true); // "192.168.1.100:60000"
// IP only
const char* ip = addr.ToString(false); // "192.168.1.100"
// With buffer (thread-safe)
char buffer[64];
addr.ToString(true, buffer);
Getting Components¶
// Get port
unsigned short port = addr.GetPort();
// Get IP as string
const char* ip = addr.ToString(false);
// Check if valid
if (addr != MafiaNet::UNASSIGNED_SYSTEM_ADDRESS) {
// Address is valid
}
Comparison¶
MafiaNet::SystemAddress addr1("192.168.1.100", 60000);
MafiaNet::SystemAddress addr2("192.168.1.100", 60000);
if (addr1 == addr2) {
// Same address
}
if (addr1 != MafiaNet::UNASSIGNED_SYSTEM_ADDRESS) {
// Valid address
}
SystemAddress vs RakNetGUID¶
MafiaNet provides two ways to identify peers:
SystemAddress:
IP + Port combination
Changes if client reconnects from different IP/port
Used for network operations
RakNetGUID:
Unique 64-bit identifier generated at startup
Survives reconnections (if you track it)
Better for player identification
// Get GUID from SystemAddress
MafiaNet::RakNetGUID guid = peer->GetGuidFromSystemAddress(addr);
// Get SystemAddress from GUID
MafiaNet::SystemAddress addr = peer->GetSystemAddressFromGuid(guid);
PeerGuid: type-safe peer identity¶
A RakNetGUID carries its value in a bare uint64_t (RakNetGUID::g), and
NetworkID (used by NetworkIDObject) is itself a uint64_t. Because
both collapse to the same primitive, the compiler lets you pass an object id where
a peer guid is expected, or vice-versa — a common source of silent bugs in
ReplicaManager3 glue and void(uint64_t) callbacks.
PeerGuid is a strong type that names a peer’s guid value distinctly:
enum class PeerGuid : uint64_t {};
Because it is an enum class with a uint64_t underlying type, it is
trivially copyable and exactly 8 bytes, so it serializes byte-identically to
the raw uint64_t through BitStream (and therefore VariableDeltaSerializer,
which writes through BitStream) — it is fully wire-compatible and needs no
netcode change. What it adds is type safety: a PeerGuid will not implicitly
convert to or from NetworkID.
// Convert between RakNetGUID and PeerGuid
MafiaNet::PeerGuid pg = MafiaNet::ToPeerGuid(packet->guid);
MafiaNet::RakNetGUID guid = MafiaNet::ToGuid(pg);
// Unassigned sentinel, mirroring UNASSIGNED_RAKNET_GUID
if (pg != MafiaNet::UNASSIGNED_PEER_GUID) {
// Valid peer guid
}
Use PeerGuid in your own signatures and callbacks to stop peer ids and object
ids from being mixed up, while keeping the wire format unchanged.
Using with Send¶
You can use either SystemAddress or GUID with Send:
// Using SystemAddress
peer->Send(&bs, MafiaNet::Priority::High, MafiaNet::Reliability::Reliable, 0, systemAddress, false);
// Using GUID (AddressOrGUID accepts both)
peer->Send(&bs, MafiaNet::Priority::High, MafiaNet::Reliability::Reliable, 0, guid, false);
Getting Your Own Address¶
// Get your external address (as seen by a specific peer)
MafiaNet::SystemAddress myAddr = peer->GetExternalID(remoteAddr);
// Get your local/internal address
MafiaNet::SystemAddress localAddr = peer->GetInternalID();
IPv6¶
For IPv6 addresses:
MafiaNet::SystemAddress addr6;
addr6.FromString("fe80::7c:31f7:fec4:27de%14");
addr6.SetPortHostOrder(60000);
// Check if IPv6
if (addr6.IsIPV6()) {
// Handle IPv6
}
See Also¶
Connecting - Using addresses for connections
Core Concepts - Core networking concepts