reckfellENGINE

DtlsServer

Helper class to implement a DTLS server.

On this page
class DtlsServerReckfellSharpInherits RefCounted

Helper class to implement a DTLS server.

This class is used to store the state of a DTLS server. Upon DtlsServer.Setup it converts connected PacketPeerUdp to PacketPeerDtls accepting them via DtlsServer.TakeConnection as DTLS clients. Under the hood, this class is used to store the DTLS state and cookies of the server. The reason of why the state and cookies are needed is outside of the scope of this documentation. Below a small example of how to use it:

C#
// ServerNode.cs
using Reckfell;

public partial class ServerNode : Entity
{
    private DtlsServer _dtls = new DtlsServer();
    private UdpServer _server = new UdpServer();
    private Reckfell.Collections.Array<PacketPeerDtls> _peers = [];

    public override void _Ready()
    {
        _server.Listen(4242);
        var key = ReckfellApi.Load<CryptoKey>("key.key"); // Your private key.
        var cert = ReckfellApi.Load<X509Certificate>("cert.crt"); // Your X509 certificate.
        _dtls.Setup(TlsOptions.Server(key, cert));
    }

    public override void _Process(double delta)
    {
        while (_server.IsConnectionAvailable())
        {
            PacketPeerUdp peer = _server.TakeConnection();
            PacketPeerDtls dtlsPeer = _dtls.TakeConnection(peer);
            if (dtlsPeer.GetStatus() != PacketPeerDtls.Status.Handshaking)
            {
                continue; // It is normal that 50% of the connections fails due to cookie exchange.
            }
            ReckfellApi.Print("Peer connected!");
            _peers.Add(dtlsPeer);
        }

        foreach (var p in _peers)
        {
            p.Poll(); // Must poll to update the state.
            if (p.GetStatus() == PacketPeerDtls.Status.Connected)
            {
                while (p.GetAvailablePacketCount() > 0)
                {
                    ReckfellApi.Print($"Received Message From Client: {p.GetPacket().GetStringFromUtf8()}");
                    p.PutPacket("Hello DTLS Client".ToUtf8Buffer());
                }
            }
        }
    }
}
C#
// ClientNode.cs
using Reckfell;
using System.Text;

public partial class ClientNode : Entity
{
    private PacketPeerDtls _dtls = new PacketPeerDtls();
    private PacketPeerUdp _udp = new PacketPeerUdp();
    private bool _connected = false;

    public override void _Ready()
    {
        _udp.ConnectToHost("127.0.0.1", 4242);
        _dtls.ConnectToPeer(_udp, validateCerts: false); // Use true in production for certificate validation!
    }

    public override void _Process(double delta)
    {
        _dtls.Poll();
        if (_dtls.GetStatus() == PacketPeerDtls.Status.Connected)
        {
            if (!_connected)
            {
                // Try to contact server
                _dtls.PutPacket("The Answer Is..42!".ToUtf8Buffer());
            }
            while (_dtls.GetAvailablePacketCount() > 0)
            {
                ReckfellApi.Print($"Connected: {_dtls.GetPacket().GetStringFromUtf8()}");
                _connected = true;
            }
        }
    }
}

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

DtlsServerConstructor
# DtlsServer
C#
public DtlsServer();

Methods2

SetupError
# Setup
C#
public Error Setup(TlsOptions serverOptions);

Setup the DTLS server to use the given server_options. See TlsOptions.Server.

Parameters
NameTypeDefault
serverOptionsTlsOptions—
Returns Error
TakeConnectionPacketPeerDtls
# TakeConnection
C#
public PacketPeerDtls TakeConnection(PacketPeerUdp udpPeer);

Try to initiate the DTLS handshake with the given udp_peer which must be already connected (see PacketPeerUdp.ConnectToHost). Note: You must check that the state of the return PacketPeerUDP is PacketPeerDtls.Status.Handshaking, as it is normal that 50% of the new connections will be invalid due to cookie exchange.

Parameters
NameTypeDefault
udpPeerPacketPeerUdp—
Returns PacketPeerDtls
Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.