Class DtlsServer
- Namespace
- Godot
- Assembly
- GodotSharp.dll
This class is used to store the state of a DTLS server. Upon Setup(TlsOptions) it converts connected PacketPeerUdp to PacketPeerDtls accepting them via TakeConnection(PacketPeerUdp) 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:
// ServerNode.cs
using Godot;
public partial class ServerNode : Node
{
private DtlsServer _dtls = new DtlsServer();
private UdpServer _server = new UdpServer();
private Godot.Collections.Array<PacketPeerDtls> _peers = new Godot.Collections.Array<PacketPeerDtls>();
public override void _Ready()
{
_server.Listen(4242);
var key = GD.Load<CryptoKey>("key.key"); // Your private key.
var cert = GD.Load<X509Certificate>("cert.crt"); // Your X509 certificate.
_dtls.Setup(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.
}
GD.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)
{
GD.Print($"Received Message From Client: {p.GetPacket().GetStringFromUtf8()}");
p.PutPacket("Hello DTLS Client".ToUtf8Buffer());
}
}
}
}
}
// ClientNode.cs
using Godot;
using System.Text;
public partial class ClientNode : Node
{
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)
{
GD.Print($"Connected: {_dtls.GetPacket().GetStringFromUtf8()}");
_connected = true;
}
}
}
}
[GodotClassName("DTLSServer")]
public class DtlsServer : RefCounted, IDisposable
- Inheritance
-
DtlsServer
- Implements
- Inherited Members
Constructors
DtlsServer()
public DtlsServer()
Methods
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
Setup(TlsOptions)
Setup the DTLS server to use the given serverOptions
. See Server(CryptoKey, X509Certificate).
public Error Setup(TlsOptions serverOptions)
Parameters
serverOptions
TlsOptions
Returns
TakeConnection(PacketPeerUdp)
Try to initiate the DTLS handshake with the given udpPeer
which must be already connected (see ConnectToHost(string, int)).
Note: You must check that the state of the return PacketPeerUDP is Handshaking, as it is normal that 50% of the new connections will be invalid due to cookie exchange.
public PacketPeerDtls TakeConnection(PacketPeerUdp udpPeer)
Parameters
udpPeer
PacketPeerUdp