class_dtlsserver.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/DTLSServer.xml.
  6. .. _class_DTLSServer:
  7. DTLSServer
  8. ==========
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :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. ::
  17. # server.gd
  18. extends Node
  19. var dtls := DTLSServer.new()
  20. var server := UDPServer.new()
  21. var peers = []
  22. func _ready():
  23. server.listen(4242)
  24. var key = load("key.key") # Your private key.
  25. var cert = load("cert.crt") # Your X509 certificate.
  26. dtls.setup(key, cert)
  27. func _process(delta):
  28. while server.is_connection_available():
  29. var peer : PacketPeerUDP = server.take_connection()
  30. var dtls_peer : PacketPeerDTLS = dtls.take_connection(peer)
  31. if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
  32. continue # It is normal that 50% of the connections fails due to cookie exchange.
  33. print("Peer connected!")
  34. peers.append(dtls_peer)
  35. for p in peers:
  36. p.poll() # Must poll to update the state.
  37. if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  38. while p.get_available_packet_count() > 0:
  39. print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
  40. p.put_packet("Hello DTLS client".to_utf8())
  41. ::
  42. # client.gd
  43. extends Node
  44. var dtls := PacketPeerDTLS.new()
  45. var udp := PacketPeerUDP.new()
  46. var connected = false
  47. func _ready():
  48. udp.connect_to_host("127.0.0.1", 4242)
  49. dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!
  50. func _process(delta):
  51. dtls.poll()
  52. if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  53. if !connected:
  54. # Try to contact server
  55. dtls.put_packet("The answer is... 42!".to_utf8())
  56. while dtls.get_available_packet_count() > 0:
  57. print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
  58. connected = true
  59. .. rst-class:: classref-reftable-group
  60. Methods
  61. -------
  62. .. table::
  63. :widths: auto
  64. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`setup<class_DTLSServer_method_setup>` **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`X509Certificate<class_X509Certificate>` certificate, :ref:`X509Certificate<class_X509Certificate>` chain=null **)** |
  66. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` | :ref:`take_connection<class_DTLSServer_method_take_connection>` **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)** |
  68. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. .. rst-class:: classref-section-separator
  70. ----
  71. .. rst-class:: classref-descriptions-group
  72. Method Descriptions
  73. -------------------
  74. .. _class_DTLSServer_method_setup:
  75. .. rst-class:: classref-method
  76. :ref:`Error<enum_@GlobalScope_Error>` **setup** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`X509Certificate<class_X509Certificate>` certificate, :ref:`X509Certificate<class_X509Certificate>` chain=null **)**
  77. Setup the DTLS server to use the given ``private_key`` and provide the given ``certificate`` to clients. You can pass the optional ``chain`` parameter to provide additional CA chain information along with the certificate.
  78. .. rst-class:: classref-item-separator
  79. ----
  80. .. _class_DTLSServer_method_take_connection:
  81. .. rst-class:: classref-method
  82. :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` **take_connection** **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)**
  83. 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>`).
  84. \ **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.
  85. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  86. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  87. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  88. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`