class_dtlsserver.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the DTLSServer.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_DTLSServer:
  6. DTLSServer
  7. ==========
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. Helper class to implement a DTLS server.
  10. Description
  11. -----------
  12. 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.
  13. Below a small example of how to use it:
  14. ::
  15. # server.gd
  16. extends Node
  17. var dtls := DTLSServer.new()
  18. var server := UDPServer.new()
  19. var peers = []
  20. func _ready():
  21. server.listen(4242)
  22. var key = load("key.key") # Your private key.
  23. var cert = load("cert.crt") # Your X509 certificate.
  24. dtls.setup(key, cert)
  25. func _process(delta):
  26. while server.is_connection_available():
  27. var peer : PacketPeerUDP = server.take_connection()
  28. var dtls_peer : PacketPeerDTLS = dtls.take_connection(peer)
  29. if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
  30. continue # It is normal that 50% of the connections fails due to cookie exchange.
  31. print("Peer connected!")
  32. peers.append(dtls_peer)
  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() > 0:
  37. print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
  38. p.put_packet("Hello DTLS client".to_utf8())
  39. ::
  40. # client.gd
  41. extends Node
  42. var dtls := PacketPeerDTLS.new()
  43. var udp := PacketPeerUDP.new()
  44. var connected = false
  45. func _ready():
  46. udp.connect_to_host("127.0.0.1", 4242)
  47. dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!
  48. func _process(delta):
  49. dtls.poll()
  50. if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
  51. if !connected:
  52. # Try to contact server
  53. dtls.put_packet("The answer is... 42!".to_utf8())
  54. while dtls.get_available_packet_count() > 0:
  55. print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
  56. connected = true
  57. Methods
  58. -------
  59. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :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 **)** |
  61. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` | :ref:`take_connection<class_DTLSServer_method_take_connection>` **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)** |
  63. +---------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. Method Descriptions
  65. -------------------
  66. .. _class_DTLSServer_method_setup:
  67. - :ref:`Error<enum_@GlobalScope_Error>` **setup** **(** :ref:`CryptoKey<class_CryptoKey>` key, :ref:`X509Certificate<class_X509Certificate>` certificate, :ref:`X509Certificate<class_X509Certificate>` chain=null **)**
  68. 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.
  69. ----
  70. .. _class_DTLSServer_method_take_connection:
  71. - :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` **take_connection** **(** :ref:`PacketPeerUDP<class_PacketPeerUDP>` udp_peer **)**
  72. 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>`).
  73. **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.
  74. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  75. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  76. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`