class_udpserver.rst 11 KB

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