class_udpserver.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/UDPServer.xml.
  6. .. _class_UDPServer:
  7. UDPServer
  8. =========
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. Helper class to implement a UDP server.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. A simple server that opens a UDP socket and returns connected :ref:`PacketPeerUDP<class_PacketPeerUDP>` upon receiving new packets. See also :ref:`PacketPeerUDP.connect_to_host<class_PacketPeerUDP_method_connect_to_host>`.
  15. After starting the server (:ref:`listen<class_UDPServer_method_listen>`), you will need to :ref:`poll<class_UDPServer_method_poll>` it at regular intervals (e.g. inside :ref:`Node._process<class_Node_method__process>`) for it to process new packets, delivering them to the appropriate :ref:`PacketPeerUDP<class_PacketPeerUDP>`, and taking new connections.
  16. Below a small example of how it can be used:
  17. ::
  18. # server.gd
  19. extends Node
  20. var server := UDPServer.new()
  21. var peers = []
  22. func _ready():
  23. server.listen(4242)
  24. func _process(delta):
  25. server.poll() # Important!
  26. if server.is_connection_available():
  27. var peer : PacketPeerUDP = server.take_connection()
  28. var pkt = peer.get_packet()
  29. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  30. print("Received data: %s" % [pkt.get_string_from_utf8()])
  31. # Reply so it knows we received the message.
  32. peer.put_packet(pkt)
  33. # Keep a reference so we can keep contacting the remote peer.
  34. peers.append(peer)
  35. for i in range(0, peers.size()):
  36. pass # Do something with the connected peers.
  37. ::
  38. # client.gd
  39. extends Node
  40. var udp := PacketPeerUDP.new()
  41. var connected = false
  42. func _ready():
  43. udp.connect_to_host("127.0.0.1", 4242)
  44. func _process(delta):
  45. if !connected:
  46. # Try to contact server
  47. udp.put_packet("The answer is... 42!".to_utf8())
  48. if udp.get_available_packet_count() > 0:
  49. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  50. connected = true
  51. .. rst-class:: classref-reftable-group
  52. Properties
  53. ----------
  54. .. table::
  55. :widths: auto
  56. +-----------------------+----------------------------------------------------------------------------------+--------+
  57. | :ref:`int<class_int>` | :ref:`max_pending_connections<class_UDPServer_property_max_pending_connections>` | ``16`` |
  58. +-----------------------+----------------------------------------------------------------------------------+--------+
  59. .. rst-class:: classref-reftable-group
  60. Methods
  61. -------
  62. .. table::
  63. :widths: auto
  64. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`is_connection_available<class_UDPServer_method_is_connection_available>` **(** **)** |const| |
  66. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`is_listening<class_UDPServer_method_is_listening>` **(** **)** |const| |
  68. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_UDPServer_method_listen>` **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)** |
  70. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`poll<class_UDPServer_method_poll>` **(** **)** |
  72. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  73. | void | :ref:`stop<class_UDPServer_method_stop>` **(** **)** |
  74. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`PacketPeerUDP<class_PacketPeerUDP>` | :ref:`take_connection<class_UDPServer_method_take_connection>` **(** **)** |
  76. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  77. .. rst-class:: classref-section-separator
  78. ----
  79. .. rst-class:: classref-descriptions-group
  80. Property Descriptions
  81. ---------------------
  82. .. _class_UDPServer_property_max_pending_connections:
  83. .. rst-class:: classref-property
  84. :ref:`int<class_int>` **max_pending_connections** = ``16``
  85. .. rst-class:: classref-property-setget
  86. - void **set_max_pending_connections** **(** :ref:`int<class_int>` value **)**
  87. - :ref:`int<class_int>` **get_max_pending_connections** **(** **)**
  88. Define the maximum number of pending connections, during :ref:`poll<class_UDPServer_method_poll>`, any new pending connection exceeding that value will be automatically dropped. Setting this value to ``0`` effectively prevents any new pending connection to be accepted (e.g. when all your players have connected).
  89. .. rst-class:: classref-section-separator
  90. ----
  91. .. rst-class:: classref-descriptions-group
  92. Method Descriptions
  93. -------------------
  94. .. _class_UDPServer_method_is_connection_available:
  95. .. rst-class:: classref-method
  96. :ref:`bool<class_bool>` **is_connection_available** **(** **)** |const|
  97. Returns ``true`` if a packet with a new address/port combination was received on the socket.
  98. .. rst-class:: classref-item-separator
  99. ----
  100. .. _class_UDPServer_method_is_listening:
  101. .. rst-class:: classref-method
  102. :ref:`bool<class_bool>` **is_listening** **(** **)** |const|
  103. Returns ``true`` if the socket is open and listening on a port.
  104. .. rst-class:: classref-item-separator
  105. ----
  106. .. _class_UDPServer_method_listen:
  107. .. rst-class:: classref-method
  108. :ref:`Error<enum_@GlobalScope_Error>` **listen** **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)**
  109. Starts the server by opening a UDP socket listening on the given port. You can optionally specify a ``bind_address`` to only listen for packets sent to that address. See also :ref:`PacketPeerUDP.listen<class_PacketPeerUDP_method_listen>`.
  110. .. rst-class:: classref-item-separator
  111. ----
  112. .. _class_UDPServer_method_poll:
  113. .. rst-class:: classref-method
  114. :ref:`Error<enum_@GlobalScope_Error>` **poll** **(** **)**
  115. Call this method at regular intervals (e.g. inside :ref:`Node._process<class_Node_method__process>`) to process new packets. And packet from known address/port pair will be delivered to the appropriate :ref:`PacketPeerUDP<class_PacketPeerUDP>`, any packet received from an unknown address/port pair will be added as a pending connection (see :ref:`is_connection_available<class_UDPServer_method_is_connection_available>`, :ref:`take_connection<class_UDPServer_method_take_connection>`). The maximum number of pending connection is defined via :ref:`max_pending_connections<class_UDPServer_property_max_pending_connections>`.
  116. .. rst-class:: classref-item-separator
  117. ----
  118. .. _class_UDPServer_method_stop:
  119. .. rst-class:: classref-method
  120. void **stop** **(** **)**
  121. Stops the server, closing the UDP socket if open. Will close all connected :ref:`PacketPeerUDP<class_PacketPeerUDP>` accepted via :ref:`take_connection<class_UDPServer_method_take_connection>` (remote peers will not be notified).
  122. .. rst-class:: classref-item-separator
  123. ----
  124. .. _class_UDPServer_method_take_connection:
  125. .. rst-class:: classref-method
  126. :ref:`PacketPeerUDP<class_PacketPeerUDP>` **take_connection** **(** **)**
  127. Returns the first pending connection (connected to the appropriate address/port). Will return ``null`` if no new connection is available. See also :ref:`is_connection_available<class_UDPServer_method_is_connection_available>`, :ref:`PacketPeerUDP.connect_to_host<class_PacketPeerUDP_method_connect_to_host>`.
  128. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  129. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  130. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  131. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`