class_udpserver.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/UDPServer.xml.
  6. .. _class_UDPServer:
  7. UDPServer
  8. =========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :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_private_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. .. tabs::
  18. .. code-tab:: gdscript
  19. # server_node.gd
  20. class_name ServerNode
  21. extends Node
  22. var server = UDPServer.new()
  23. var peers = []
  24. func _ready():
  25. server.listen(4242)
  26. func _process(delta):
  27. server.poll() # Important!
  28. if server.is_connection_available():
  29. var peer = server.take_connection()
  30. var packet = peer.get_packet()
  31. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  32. print("Received data: %s" % [packet.get_string_from_utf8()])
  33. # Reply so it knows we received the message.
  34. peer.put_packet(packet)
  35. # Keep a reference so we can keep contacting the remote peer.
  36. peers.append(peer)
  37. for i in range(0, peers.size()):
  38. pass # Do something with the connected peers.
  39. .. code-tab:: csharp
  40. // ServerNode.cs
  41. using Godot;
  42. using System.Collections.Generic;
  43. public partial class ServerNode : Node
  44. {
  45. private UdpServer _server = new UdpServer();
  46. private List<PacketPeerUdp> _peers = new List<PacketPeerUdp>();
  47. public override void _Ready()
  48. {
  49. _server.Listen(4242);
  50. }
  51. public override void _Process(double delta)
  52. {
  53. _server.Poll(); // Important!
  54. if (_server.IsConnectionAvailable())
  55. {
  56. PacketPeerUdp peer = _server.TakeConnection();
  57. byte[] packet = peer.GetPacket();
  58. GD.Print($"Accepted Peer: {peer.GetPacketIP()}:{peer.GetPacketPort()}");
  59. GD.Print($"Received Data: {packet.GetStringFromUtf8()}");
  60. // Reply so it knows we received the message.
  61. peer.PutPacket(packet);
  62. // Keep a reference so we can keep contacting the remote peer.
  63. _peers.Add(peer);
  64. }
  65. foreach (var peer in _peers)
  66. {
  67. // Do something with the peers.
  68. }
  69. }
  70. }
  71. .. tabs::
  72. .. code-tab:: gdscript
  73. # client_node.gd
  74. class_name ClientNode
  75. extends Node
  76. var udp = PacketPeerUDP.new()
  77. var connected = false
  78. func _ready():
  79. udp.connect_to_host("127.0.0.1", 4242)
  80. func _process(delta):
  81. if !connected:
  82. # Try to contact server
  83. udp.put_packet("The answer is... 42!".to_utf8_buffer())
  84. if udp.get_available_packet_count() > 0:
  85. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  86. connected = true
  87. .. code-tab:: csharp
  88. // ClientNode.cs
  89. using Godot;
  90. public partial class ClientNode : Node
  91. {
  92. private PacketPeerUdp _udp = new PacketPeerUdp();
  93. private bool _connected = false;
  94. public override void _Ready()
  95. {
  96. _udp.ConnectToHost("127.0.0.1", 4242);
  97. }
  98. public override void _Process(double delta)
  99. {
  100. if (!_connected)
  101. {
  102. // Try to contact server
  103. _udp.PutPacket("The Answer Is..42!".ToUtf8Buffer());
  104. }
  105. if (_udp.GetAvailablePacketCount() > 0)
  106. {
  107. GD.Print($"Connected: {_udp.GetPacket().GetStringFromUtf8()}");
  108. _connected = true;
  109. }
  110. }
  111. }
  112. .. rst-class:: classref-reftable-group
  113. Properties
  114. ----------
  115. .. table::
  116. :widths: auto
  117. +-----------------------+----------------------------------------------------------------------------------+--------+
  118. | :ref:`int<class_int>` | :ref:`max_pending_connections<class_UDPServer_property_max_pending_connections>` | ``16`` |
  119. +-----------------------+----------------------------------------------------------------------------------+--------+
  120. .. rst-class:: classref-reftable-group
  121. Methods
  122. -------
  123. .. table::
  124. :widths: auto
  125. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  126. | :ref:`int<class_int>` | :ref:`get_local_port<class_UDPServer_method_get_local_port>`\ (\ ) |const| |
  127. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  128. | :ref:`bool<class_bool>` | :ref:`is_connection_available<class_UDPServer_method_is_connection_available>`\ (\ ) |const| |
  129. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  130. | :ref:`bool<class_bool>` | :ref:`is_listening<class_UDPServer_method_is_listening>`\ (\ ) |const| |
  131. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  132. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_UDPServer_method_listen>`\ (\ port\: :ref:`int<class_int>`, bind_address\: :ref:`String<class_String>` = "*"\ ) |
  133. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  134. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`poll<class_UDPServer_method_poll>`\ (\ ) |
  135. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  136. | |void| | :ref:`stop<class_UDPServer_method_stop>`\ (\ ) |
  137. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  138. | :ref:`PacketPeerUDP<class_PacketPeerUDP>` | :ref:`take_connection<class_UDPServer_method_take_connection>`\ (\ ) |
  139. +-------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  140. .. rst-class:: classref-section-separator
  141. ----
  142. .. rst-class:: classref-descriptions-group
  143. Property Descriptions
  144. ---------------------
  145. .. _class_UDPServer_property_max_pending_connections:
  146. .. rst-class:: classref-property
  147. :ref:`int<class_int>` **max_pending_connections** = ``16`` :ref:`🔗<class_UDPServer_property_max_pending_connections>`
  148. .. rst-class:: classref-property-setget
  149. - |void| **set_max_pending_connections**\ (\ value\: :ref:`int<class_int>`\ )
  150. - :ref:`int<class_int>` **get_max_pending_connections**\ (\ )
  151. 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).
  152. .. rst-class:: classref-section-separator
  153. ----
  154. .. rst-class:: classref-descriptions-group
  155. Method Descriptions
  156. -------------------
  157. .. _class_UDPServer_method_get_local_port:
  158. .. rst-class:: classref-method
  159. :ref:`int<class_int>` **get_local_port**\ (\ ) |const| :ref:`🔗<class_UDPServer_method_get_local_port>`
  160. Returns the local port this server is listening to.
  161. .. rst-class:: classref-item-separator
  162. ----
  163. .. _class_UDPServer_method_is_connection_available:
  164. .. rst-class:: classref-method
  165. :ref:`bool<class_bool>` **is_connection_available**\ (\ ) |const| :ref:`🔗<class_UDPServer_method_is_connection_available>`
  166. Returns ``true`` if a packet with a new address/port combination was received on the socket.
  167. .. rst-class:: classref-item-separator
  168. ----
  169. .. _class_UDPServer_method_is_listening:
  170. .. rst-class:: classref-method
  171. :ref:`bool<class_bool>` **is_listening**\ (\ ) |const| :ref:`🔗<class_UDPServer_method_is_listening>`
  172. Returns ``true`` if the socket is open and listening on a port.
  173. .. rst-class:: classref-item-separator
  174. ----
  175. .. _class_UDPServer_method_listen:
  176. .. rst-class:: classref-method
  177. :ref:`Error<enum_@GlobalScope_Error>` **listen**\ (\ port\: :ref:`int<class_int>`, bind_address\: :ref:`String<class_String>` = "*"\ ) :ref:`🔗<class_UDPServer_method_listen>`
  178. 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.bind()<class_PacketPeerUDP_method_bind>`.
  179. .. rst-class:: classref-item-separator
  180. ----
  181. .. _class_UDPServer_method_poll:
  182. .. rst-class:: classref-method
  183. :ref:`Error<enum_@GlobalScope_Error>` **poll**\ (\ ) :ref:`🔗<class_UDPServer_method_poll>`
  184. Call this method at regular intervals (e.g. inside :ref:`Node._process()<class_Node_private_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>`.
  185. .. rst-class:: classref-item-separator
  186. ----
  187. .. _class_UDPServer_method_stop:
  188. .. rst-class:: classref-method
  189. |void| **stop**\ (\ ) :ref:`🔗<class_UDPServer_method_stop>`
  190. 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).
  191. .. rst-class:: classref-item-separator
  192. ----
  193. .. _class_UDPServer_method_take_connection:
  194. .. rst-class:: classref-method
  195. :ref:`PacketPeerUDP<class_PacketPeerUDP>` **take_connection**\ (\ ) :ref:`🔗<class_UDPServer_method_take_connection>`
  196. 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>`.
  197. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  198. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  199. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  200. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  201. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  202. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  203. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  204. .. |void| replace:: :abbr:`void (No return value.)`