Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

DTLSServer

Inherits: RefCounted < Object

Helper class to implement a DTLS server.

Description

This class is used to store the state of a DTLS server. Upon setup it converts connected PacketPeerUDP to PacketPeerDTLS accepting them via take_connection 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:

GDScriptC#

  1. # server_node.gd
  2. extends Node
  3. var dtls := DTLSServer.new()
  4. var server := UDPServer.new()
  5. var peers = []
  6. func _ready():
  7. server.listen(4242)
  8. var key = load("key.key") # Your private key.
  9. var cert = load("cert.crt") # Your X509 certificate.
  10. dtls.setup(key, cert)
  11. func _process(delta):
  12. while server.is_connection_available():
  13. var peer: PacketPeerUDP = server.take_connection()
  14. var dtls_peer: PacketPeerDTLS = dtls.take_connection(peer)
  15. if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
  16. continue # It is normal that 50% of the connections fails due to cookie exchange.
  17. print("Peer connected!")
  18. peers.append(dtls_peer)
  19. for p in peers:
  20. p.poll() # Must poll to update the state.
  21. if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  22. while p.get_available_packet_count() > 0:
  23. print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
  24. p.put_packet("Hello DTLS client".to_utf8_buffer())
  1. // ServerNode.cs
  2. using Godot;
  3. public partial class ServerNode : Node
  4. {
  5. private DtlsServer _dtls = new DtlsServer();
  6. private UdpServer _server = new UdpServer();
  7. private Godot.Collections.Array<PacketPeerDTLS> _peers = new Godot.Collections.Array<PacketPeerDTLS>();
  8. public override void _Ready()
  9. {
  10. _server.Listen(4242);
  11. var key = GD.Load<CryptoKey>("key.key"); // Your private key.
  12. var cert = GD.Load<X509Certificate>("cert.crt"); // Your X509 certificate.
  13. _dtls.Setup(key, cert);
  14. }
  15. public override void _Process(double delta)
  16. {
  17. while (Server.IsConnectionAvailable())
  18. {
  19. PacketPeerUDP peer = _server.TakeConnection();
  20. PacketPeerDTLS dtlsPeer = _dtls.TakeConnection(peer);
  21. if (dtlsPeer.GetStatus() != PacketPeerDtls.Status.Handshaking)
  22. {
  23. continue; // It is normal that 50% of the connections fails due to cookie exchange.
  24. }
  25. GD.Print("Peer connected!");
  26. _peers.Add(dtlsPeer);
  27. }
  28. foreach (var p in _peers)
  29. {
  30. p.Poll(); // Must poll to update the state.
  31. if (p.GetStatus() == PacketPeerDtls.Status.Connected)
  32. {
  33. while (p.GetAvailablePacketCount() > 0)
  34. {
  35. GD.Print($"Received Message From Client: {p.GetPacket().GetStringFromUtf8()}");
  36. p.PutPacket("Hello DTLS Client".ToUtf8Buffer());
  37. }
  38. }
  39. }
  40. }
  41. }

GDScriptC#

  1. # client_node.gd
  2. extends Node
  3. var dtls := PacketPeerDTLS.new()
  4. var udp := PacketPeerUDP.new()
  5. var connected = false
  6. func _ready():
  7. udp.connect_to_host("127.0.0.1", 4242)
  8. dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!
  9. func _process(delta):
  10. dtls.poll()
  11. if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  12. if !connected:
  13. # Try to contact server
  14. dtls.put_packet("The answer is... 42!".to_utf8_buffer())
  15. while dtls.get_available_packet_count() > 0:
  16. print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
  17. connected = true
  1. // ClientNode.cs
  2. using Godot;
  3. using System.Text;
  4. public partial class ClientNode : Node
  5. {
  6. private PacketPeerDtls _dtls = new PacketPeerDtls();
  7. private PacketPeerUdp _udp = new PacketPeerUdp();
  8. private bool _connected = false;
  9. public override void _Ready()
  10. {
  11. _udp.ConnectToHost("127.0.0.1", 4242);
  12. _dtls.ConnectToPeer(_udp, validateCerts: false); // Use true in production for certificate validation!
  13. }
  14. public override void _Process(double delta)
  15. {
  16. _dtls.Poll();
  17. if (_dtls.GetStatus() == PacketPeerDtls.Status.Connected)
  18. {
  19. if (!_connected)
  20. {
  21. // Try to contact server
  22. _dtls.PutPacket("The Answer Is..42!".ToUtf8Buffer());
  23. }
  24. while (_dtls.GetAvailablePacketCount() > 0)
  25. {
  26. GD.Print($"Connected: {_dtls.GetPacket().GetStringFromUtf8()}");
  27. _connected = true;
  28. }
  29. }
  30. }
  31. }

Methods

Error

setup ( TLSOptions server_options )

PacketPeerDTLS

take_connection ( PacketPeerUDP udp_peer )


Method Descriptions

Error setup ( TLSOptions server_options )

Setup the DTLS server to use the given server_options. See TLSOptions.server.


PacketPeerDTLS take_connection ( PacketPeerUDP udp_peer )

Try to initiate the DTLS handshake with the given udp_peer which must be already connected (see PacketPeerUDP.connect_to_host).

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.