class_udpserver.rst 12 KB

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