R-Type 0.1
Loading...
Searching...
No Matches
Network library

UDP transport, packet and message framing, sequence tracking and connection management (lib/Network). More...

Classes

class  Connection
 State the server keeps for one connected client. More...
 
struct  NetworkMetrics
 Traffic and latency counters of a network endpoint. More...
 
struct  HandshakeHeader
 Header of the datagrams exchanged before a connection exists. More...
 
struct  MessageHeader
 Header placed in front of every message inside a connected packet. More...
 
struct  ParsedMessage
 Message extracted from a connected packet. More...
 
class  MessageParser
 Splits the body of a connected packet into messages. More...
 
struct  PacketHeader
 Header that starts every packet sent on an open connection. More...
 
class  PacketReader
 Bounds checked reader of big-endian values over a byte buffer. More...
 
class  PacketWriter
 Growable buffer of big-endian values, capped at MAX_PAYLOAD_SIZE. More...
 
struct  PingMessage
 Payload of a Ping message, 4 bytes, sent by a client to measure latency. More...
 
struct  PongMessage
 Payload of a Pong message, 4 bytes, sent by the server in answer to a Ping. More...
 
class  NetworkServer
 UDP server that runs the handshake, tracks connections and dispatches messages. More...
 
class  PacketTracker
 Generates outgoing sequence numbers and tracks received ones. More...
 
class  UdpTransport
 Asynchronous UDP socket wrapper used by the server. More...
 

Typedefs

using MessageId = std::uint16_t
 Numeric identifier of a message type.
 

Enumerations

enum class  HandshakeType : std::uint8_t { HandshakeType::Hello = 1 , HandshakeType::Welcome = 2 , HandshakeType::Rejected = 3 }
 Kind of handshake datagram. More...
 
enum class  PacketReceiveRes { PacketReceiveRes::New , PacketReceiveRes::Dup , PacketReceiveRes::Out , PacketReceiveRes::TooOld }
 Classification of a received packet sequence number. More...
 

Functions

void updateRttMetrics (NetworkMetrics &metrics, float newRtt)
 Adds a round trip time sample and updates smoothing and jitter.
 
void updatePacketMetrics (NetworkMetrics &metrics)
 Recomputes the packet loss percentage.
 
void onPacketSend (NetworkMetrics &metrics, std::size_t packetSize)
 Counts a sent datagram.
 
void onPacketReceived (NetworkMetrics &metrics, std::size_t packetSize)
 Counts a received datagram.
 
void onPacketDuplicated (NetworkMetrics &metrics)
 Counts a duplicated datagram.
 
void onPacketOut (NetworkMetrics &metrics)
 Counts a datagram received out of order.
 
void onRetransmission (NetworkMetrics &metrics)
 Counts a retransmitted datagram.
 

Variables

constexpr std::size_t MAX_PACKET_SIZE = 576
 Largest IPv4 datagram every host is required to accept, in bytes.
 
constexpr std::size_t IP_HEADER_SIZE = 20
 Size of an IPv4 header without options, in bytes.
 
constexpr std::size_t UDP_HEADER_SIZE = 8
 Size of a UDP header, in bytes.
 
constexpr std::size_t MAX_PAYLOAD_SIZE
 Largest UDP payload sent or accepted by the network layer, in bytes.
 
constexpr std::size_t PACKET_HEADER_SIZE = 16
 Size of the PacketHeader that starts every connected packet, in bytes.
 
constexpr std::size_t MAX_MESSAGE_SIZE
 Room left for messages after the packet header, in bytes.
 
constexpr std::uint32_t MAGIC = 0x52545950
 Magic number that starts every handshake datagram, "RTYP" in ASCII.
 
constexpr std::uint8_t VERSION = 1
 Protocol version announced and expected during the handshake.
 
constexpr MessageId systemMessage::Ping = 0x03
 Latency probe sent by a client.
 
constexpr MessageId systemMessage::Disconnect = 0x04
 Client closes its connection.
 
constexpr MessageId systemMessage::Pong = 0x86
 Server answer to a Ping.
 

Detailed Description

UDP transport, packet and message framing, sequence tracking and connection management (lib/Network).

See Networking for an overview.

Enumeration Type Documentation

◆ HandshakeType

enum class HandshakeType : std::uint8_t
strong

Kind of handshake datagram.

Enumerator
Hello 

Client asks the server to open a connection.

Welcome 

Server accepts the client and gives its connection id.

Rejected 

Server refuses the connection.

◆ PacketReceiveRes

enum class PacketReceiveRes
strong

Classification of a received packet sequence number.

Enumerator
New 

Newer than every packet received so far.

Dup 

Already received.

Out 

Older than the latest one but inside the 32 packet window.

TooOld 

Older than the 32 packet window.

Function Documentation

◆ updateRttMetrics()

void updateRttMetrics ( NetworkMetrics & metrics,
float newRtt )

Adds a round trip time sample and updates smoothing and jitter.

Parameters
metricsCounters to update.
newRttNew round trip time sample.

◆ updatePacketMetrics()

void updatePacketMetrics ( NetworkMetrics & metrics)

Recomputes the packet loss percentage.

Parameters
metricsCounters to update.

◆ onPacketSend()

void onPacketSend ( NetworkMetrics & metrics,
std::size_t packetSize )

Counts a sent datagram.

Parameters
metricsCounters to update.
packetSizeSize of the datagram in bytes.

◆ onPacketReceived()

void onPacketReceived ( NetworkMetrics & metrics,
std::size_t packetSize )

Counts a received datagram.

Parameters
metricsCounters to update.
packetSizeSize of the datagram in bytes.

◆ onPacketDuplicated()

void onPacketDuplicated ( NetworkMetrics & metrics)

Counts a duplicated datagram.

Parameters
metricsCounters to update.

◆ onPacketOut()

void onPacketOut ( NetworkMetrics & metrics)

Counts a datagram received out of order.

Parameters
metricsCounters to update.

◆ onRetransmission()

void onRetransmission ( NetworkMetrics & metrics)

Counts a retransmitted datagram.

Parameters
metricsCounters to update.

Variable Documentation

◆ MAX_PAYLOAD_SIZE

std::size_t MAX_PAYLOAD_SIZE
inlineconstexpr
Initial value:
=
constexpr std::size_t MAX_PACKET_SIZE
Largest IPv4 datagram every host is required to accept, in bytes.
Definition NetworkConfig.hpp:11
constexpr std::size_t IP_HEADER_SIZE
Size of an IPv4 header without options, in bytes.
Definition NetworkConfig.hpp:13
constexpr std::size_t UDP_HEADER_SIZE
Size of a UDP header, in bytes.
Definition NetworkConfig.hpp:15

Largest UDP payload sent or accepted by the network layer, in bytes.

◆ MAX_MESSAGE_SIZE

std::size_t MAX_MESSAGE_SIZE
inlineconstexpr
Initial value:
=
constexpr std::size_t PACKET_HEADER_SIZE
Size of the PacketHeader that starts every connected packet, in bytes.
Definition NetworkConfig.hpp:20
constexpr std::size_t MAX_PAYLOAD_SIZE
Largest UDP payload sent or accepted by the network layer, in bytes.
Definition NetworkConfig.hpp:17

Room left for messages after the packet header, in bytes.