Skip to content
0.2.0-dev Download ZIP
168 lines ·5.5 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="DTLSServer" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Helper class to implement a DTLS server.
5 </brief_description>
6 <description>
7 This class is used to store the state of a DTLS server. Upon [method setup] it converts connected [PacketPeerUDP] to [PacketPeerDTLS] accepting them via [method 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.
8 Below a small example of how to use it:
9 [codeblocks]
10 [gdscript]
11 # server_node.gd
12 extends Node
13
14 var dtls = DTLSServer.new()
15 var server = UDPServer.new()
16 var peers = []
17
18 func _ready():
19 server.listen(4242)
20 var key = load("key.key") # Your private key.
21 var cert = load("cert.crt") # Your X509 certificate.
22 dtls.setup(TlsOptions.server(key, cert))
23
24 func _process(delta):
25 while server.is_connection_available():
26 var peer = server.take_connection()
27 var dtls_peer = dtls.take_connection(peer)
28 if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
29 continue # It is normal that 50% of the connections fails due to cookie exchange.
30 print("Peer connected!")
31 peers.append(dtls_peer)
32
33 for p in peers:
34 p.poll() # Must poll to update the state.
35 if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
36 while p.get_available_packet_count() &gt; 0:
37 print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
38 p.put_packet("Hello DTLS client".to_utf8_buffer())
39 [/gdscript]
40 [csharp]
41 // ServerNode.cs
42 using Godot;
43
44 public partial class ServerNode : Node
45 {
46 private DtlsServer _dtls = new DtlsServer();
47 private UdpServer _server = new UdpServer();
48 private Godot.Collections.Array&lt;PacketPeerDtls&gt; _peers = [];
49
50 public override void _Ready()
51 {
52 _server.Listen(4242);
53 var key = GD.Load&lt;CryptoKey&gt;("key.key"); // Your private key.
54 var cert = GD.Load&lt;X509Certificate&gt;("cert.crt"); // Your X509 certificate.
55 _dtls.Setup(TlsOptions.Server(key, cert));
56 }
57
58 public override void _Process(double delta)
59 {
60 while (_server.IsConnectionAvailable())
61 {
62 PacketPeerUdp peer = _server.TakeConnection();
63 PacketPeerDtls dtlsPeer = _dtls.TakeConnection(peer);
64 if (dtlsPeer.GetStatus() != PacketPeerDtls.Status.Handshaking)
65 {
66 continue; // It is normal that 50% of the connections fails due to cookie exchange.
67 }
68 GD.Print("Peer connected!");
69 _peers.Add(dtlsPeer);
70 }
71
72 foreach (var p in _peers)
73 {
74 p.Poll(); // Must poll to update the state.
75 if (p.GetStatus() == PacketPeerDtls.Status.Connected)
76 {
77 while (p.GetAvailablePacketCount() &gt; 0)
78 {
79 GD.Print($"Received Message From Client: {p.GetPacket().GetStringFromUtf8()}");
80 p.PutPacket("Hello DTLS Client".ToUtf8Buffer());
81 }
82 }
83 }
84 }
85 }
86 [/csharp]
87 [/codeblocks]
88 [codeblocks]
89 [gdscript]
90 # client_node.gd
91 extends Node
92
93 var dtls = PacketPeerDTLS.new()
94 var udp = PacketPeerUDP.new()
95 var connected = false
96
97 func _ready():
98 udp.connect_to_host("127.0.0.1", 4242)
99 dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!
100
101 func _process(delta):
102 dtls.poll()
103 if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
104 if !connected:
105 # Try to contact server
106 dtls.put_packet("The answer is... 42!".to_utf8_buffer())
107 while dtls.get_available_packet_count() &gt; 0:
108 print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
109 connected = true
110 [/gdscript]
111 [csharp]
112 // ClientNode.cs
113 using Godot;
114 using System.Text;
115
116 public partial class ClientNode : Node
117 {
118 private PacketPeerDtls _dtls = new PacketPeerDtls();
119 private PacketPeerUdp _udp = new PacketPeerUdp();
120 private bool _connected = false;
121
122 public override void _Ready()
123 {
124 _udp.ConnectToHost("127.0.0.1", 4242);
125 _dtls.ConnectToPeer(_udp, validateCerts: false); // Use true in production for certificate validation!
126 }
127
128 public override void _Process(double delta)
129 {
130 _dtls.Poll();
131 if (_dtls.GetStatus() == PacketPeerDtls.Status.Connected)
132 {
133 if (!_connected)
134 {
135 // Try to contact server
136 _dtls.PutPacket("The Answer Is..42!".ToUtf8Buffer());
137 }
138 while (_dtls.GetAvailablePacketCount() &gt; 0)
139 {
140 GD.Print($"Connected: {_dtls.GetPacket().GetStringFromUtf8()}");
141 _connected = true;
142 }
143 }
144 }
145 }
146 [/csharp]
147 [/codeblocks]
148 </description>
149 <tutorials>
150 </tutorials>
151 <methods>
152 <method name="setup">
153 <return type="int" enum="Error" />
154 <param index="0" name="server_options" type="TLSOptions" />
155 <description>
156 Setup the DTLS server to use the given [param server_options]. See [method TLSOptions.server].
157 </description>
158 </method>
159 <method name="take_connection">
160 <return type="PacketPeerDTLS" />
161 <param index="0" name="udp_peer" type="PacketPeerUDP" />
162 <description>
163 Try to initiate the DTLS handshake with the given [param udp_peer] which must be already connected (see [method PacketPeerUDP.connect_to_host]).
164 [b]Note:[/b] You must check that the state of the return PacketPeerUDP is [constant PacketPeerDTLS.STATUS_HANDSHAKING], as it is normal that 50% of the new connections will be invalid due to cookie exchange.
165 </description>
166 </method>
167 </methods>
168</class>