class_dtlsserver.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/DTLSServer.xml.
  6. .. _class_DTLSServer:
  7. DTLSServer
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Helper class to implement a DTLS server.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class is used to store the state of a DTLS server. Upon :ref:`setup<class_DTLSServer_method_setup>` it converts connected :ref:`PacketPeerUDP<class_PacketPeerUDP>` to :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` accepting them via :ref:`take_connection<class_DTLSServer_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.
  15. Below a small example of how to use it:
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. # server_node.gd
  19. extends Node
  20. var dtls := DTLSServer.new()
  21. var server := UDPServer.new()
  22. var peers = []
  23. func _ready():
  24. server.listen(4242)
  25. var key = load("key.key") # Your private key.
  26. var cert = load("cert.crt") # Your X509 certificate.
  27. dtls.setup(key, cert)
  28. func _process(delta):
  29. while server.is_connection_available():
  30. var peer: PacketPeerUDP = server.take_connection()
  31. var dtls_peer: PacketPeerDTLS = dtls.take_connection(peer)
  32. if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
  33. continue # It is normal that 50% of the connections fails due to cookie exchange.
  34. print("Peer connected!")
  35. peers.append(dtls_peer)
  36. for p in peers:
  37. p.poll() # Must poll to update the state.
  38. if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  39. while p.get_available_packet_count() > 0:
  40. print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
  41. p.put_packet("Hello DTLS client".to_utf8_buffer())
  42. .. code-tab:: csharp
  43. // ServerNode.cs
  44. using Godot;
  45. public partial class ServerNode : Node
  46. {
  47. private DtlsServer _dtls = new DtlsServer();
  48. private UdpServer _server = new UdpServer();
  49. private Godot.Collections.Array<PacketPeerDTLS> _peers = new Godot.Collections.Array<PacketPeerDTLS>();
  50. public override void _Ready()
  51. {
  52. _server.Listen(4242);
  53. var key = GD.Load<CryptoKey>("key.key"); // Your private key.
  54. var cert = GD.Load<X509Certificate>("cert.crt"); // Your X509 certificate.
  55. _dtls.Setup(key, cert);
  56. }
  57. public override void _Process(double delta)
  58. {
  59. while (Server.IsConnectionAvailable())
  60. {
  61. PacketPeerUDP peer = _server.TakeConnection();
  62. PacketPeerDTLS dtlsPeer = _dtls.TakeConnection(peer);
  63. if (dtlsPeer.GetStatus() != PacketPeerDtls.Status.Handshaking)
  64. {
  65. continue; // It is normal that 50% of the connections fails due to cookie exchange.
  66. }
  67. GD.Print("Peer connected!");
  68. _peers.Add(dtlsPeer);
  69. }
  70. foreach (var p in _peers)
  71. {
  72. p.Poll(); // Must poll to update the state.
  73. if (p.GetStatus() == PacketPeerDtls.Status.Connected)
  74. {
  75. while (p.GetAvailablePacketCount() > 0)
  76. {
  77. GD.Print($"Received Message From Client: {p.GetPacket().GetStringFromUtf8()}");
  78. p.PutPacket("Hello DTLS Client".ToUtf8Buffer());
  79. }
  80. }
  81. }
  82. }
  83. }
  84. .. tabs::
  85. .. code-tab:: gdscript
  86. # client_node.gd
  87. extends Node
  88. var dtls := PacketPeerDTLS.new()
  89. var udp := PacketPeerUDP.new()
  90. var connected = false
  91. func _ready():
  92. udp.connect_to_host("127.0.0.1", 4242)
  93. dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!
  94. func _process(delta):
  95. dtls.poll()
  96. if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  97. if !connected:
  98. # Try to contact server
  99. dtls.put_packet("The answer is... 42!".to_utf8_buffer())
  100. while dtls.get_available_packet_count() > 0:
  101. print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
  102. connected = true
  103. .. code-tab:: csharp
  104. // ClientNode.cs
  105. using Godot;
  106. using System.Text;
  107. public partial class ClientNode : Node
  108. {
  109. private PacketPeerDtls _dtls = new PacketPeerDtls();
  110. private PacketPeerUdp _udp = new PacketPeerUdp();
  111. private bool _connected = false;
  112. public override void _Ready()
  113. {
  114. _udp.ConnectToHost("127.0.0.1", 4242);
  115. _dtls.ConnectToPeer(_udp, validateCerts: false); // Use true in production for certificate validation!
  116. }
  117. public override void _Process(double delta)
  118. {
  119. _dtls.Poll();
  120. if (_dtls.GetStatus() == PacketPeerDtls.Status.Connected)
  121. {
  122. if (!_connected)
  123. {
  124. // Try to contact server
  125. _dtls.PutPacket("The Answer Is..42!".ToUtf8Buffer());
  126. }
  127. while (_dtls.GetAvailablePacketCount() > 0)
  128. {
  129. GD.Print($"Connected: {_dtls.GetPacket().GetStringFromUtf8()}");
  130. _connected = true;
  131. }
  132. }
  133. }
  134. }
  135. .. rst-class:: classref-reftable-group
  136. Methods
  137. -------
  138. .. table::
  139. :widths: auto
  140. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
  141. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`setup<class_DTLSServer_method_setup>` **(** :ref:`TLSOptions<class_TLSOptions>` server_options **)** |
  142. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
  143. | :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` | :ref:`take_connection<class_DTLSServer_method_take_connection>` **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)** |
  144. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------+
  145. .. rst-class:: classref-section-separator
  146. ----
  147. .. rst-class:: classref-descriptions-group
  148. Method Descriptions
  149. -------------------
  150. .. _class_DTLSServer_method_setup:
  151. .. rst-class:: classref-method
  152. :ref:`Error<enum_@GlobalScope_Error>` **setup** **(** :ref:`TLSOptions<class_TLSOptions>` server_options **)**
  153. Setup the DTLS server to use the given ``server_options``. See :ref:`TLSOptions.server<class_TLSOptions_method_server>`.
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_DTLSServer_method_take_connection:
  157. .. rst-class:: classref-method
  158. :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` **take_connection** **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)**
  159. Try to initiate the DTLS handshake with the given ``udp_peer`` which must be already connected (see :ref:`PacketPeerUDP.connect_to_host<class_PacketPeerUDP_method_connect_to_host>`).
  160. \ **Note:** You must check that the state of the return PacketPeerUDP is :ref:`PacketPeerDTLS.STATUS_HANDSHAKING<class_PacketPeerDTLS_constant_STATUS_HANDSHAKING>`, as it is normal that 50% of the new connections will be invalid due to cookie exchange.
  161. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  162. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  163. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  164. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  165. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  166. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  167. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`