class_udpserver.rst 11 KB

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