UDPServer.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="UDPServer" inherits="RefCounted" version="4.0">
  3. <brief_description>
  4. Helper class to implement a UDP server.
  5. </brief_description>
  6. <description>
  7. A simple server that opens a UDP socket and returns connected [PacketPeerUDP] upon receiving new packets. See also [method PacketPeerUDP.connect_to_host].
  8. After starting the server ([method listen]), you will need to [method poll] it at regular intervals (e.g. inside [method Node._process]) for it to process new packets, delivering them to the appropriate [PacketPeerUDP], and taking new connections.
  9. Below a small example of how it can be used:
  10. [codeblocks]
  11. [gdscript]
  12. class_name Server
  13. extends Node
  14. var server := UDPServer.new()
  15. var peers = []
  16. func _ready():
  17. server.listen(4242)
  18. func _process(delta):
  19. server.poll() # Important!
  20. if server.is_connection_available():
  21. var peer : PacketPeerUDP = server.take_connection()
  22. var packet = peer.get_packet()
  23. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  24. print("Received data: %s" % [packet.get_string_from_utf8()])
  25. # Reply so it knows we received the message.
  26. peer.put_packet(packet)
  27. # Keep a reference so we can keep contacting the remote peer.
  28. peers.append(peer)
  29. for i in range(0, peers.size()):
  30. pass # Do something with the connected peers.
  31. [/gdscript]
  32. [csharp]
  33. using Godot;
  34. using System;
  35. using System.Collections.Generic;
  36. public class Server : Node
  37. {
  38. public UDPServer Server = new UDPServer();
  39. public List&lt;PacketPeerUDP&gt; Peers = new List&lt;PacketPeerUDP&gt;();
  40. public override void _Ready()
  41. {
  42. Server.Listen(4242);
  43. }
  44. public override void _Process(float delta)
  45. {
  46. Server.Poll(); // Important!
  47. if (Server.IsConnectionAvailable())
  48. {
  49. PacketPeerUDP peer = Server.TakeConnection();
  50. byte[] packet = peer.GetPacket();
  51. GD.Print($"Accepted Peer: {peer.GetPacketIp()}:{peer.GetPacketPort()}");
  52. GD.Print($"Received Data: {packet.GetStringFromUTF8()}");
  53. // Reply so it knows we received the message.
  54. peer.PutPacket(packet);
  55. // Keep a reference so we can keep contacting the remote peer.
  56. Peers.Add(peer);
  57. }
  58. foreach (var peer in Peers)
  59. {
  60. // Do something with the peers.
  61. }
  62. }
  63. }
  64. [/csharp]
  65. [/codeblocks]
  66. [codeblocks]
  67. [gdscript]
  68. class_name Client
  69. extends Node
  70. var udp := PacketPeerUDP.new()
  71. var connected = false
  72. func _ready():
  73. udp.connect_to_host("127.0.0.1", 4242)
  74. func _process(delta):
  75. if !connected:
  76. # Try to contact server
  77. udp.put_packet("The answer is... 42!".to_utf8())
  78. if udp.get_available_packet_count() &gt; 0:
  79. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  80. connected = true
  81. [/gdscript]
  82. [csharp]
  83. using Godot;
  84. using System;
  85. public class Client : Node
  86. {
  87. public PacketPeerUDP Udp = new PacketPeerUDP();
  88. public bool Connected = false;
  89. public override void _Ready()
  90. {
  91. Udp.ConnectToHost("127.0.0.1", 4242);
  92. }
  93. public override void _Process(float delta)
  94. {
  95. if (!Connected)
  96. {
  97. // Try to contact server
  98. Udp.PutPacket("The Answer Is..42!".ToUTF8());
  99. }
  100. if (Udp.GetAvailablePacketCount() &gt; 0)
  101. {
  102. GD.Print($"Connected: {Udp.GetPacket().GetStringFromUTF8()}");
  103. Connected = true;
  104. }
  105. }
  106. }
  107. [/csharp]
  108. [/codeblocks]
  109. </description>
  110. <tutorials>
  111. </tutorials>
  112. <methods>
  113. <method name="get_local_port" qualifiers="const">
  114. <return type="int" />
  115. <description>
  116. Returns the local port this server is listening to.
  117. </description>
  118. </method>
  119. <method name="is_connection_available" qualifiers="const">
  120. <return type="bool" />
  121. <description>
  122. Returns [code]true[/code] if a packet with a new address/port combination was received on the socket.
  123. </description>
  124. </method>
  125. <method name="is_listening" qualifiers="const">
  126. <return type="bool" />
  127. <description>
  128. Returns [code]true[/code] if the socket is open and listening on a port.
  129. </description>
  130. </method>
  131. <method name="listen">
  132. <return type="int" enum="Error" />
  133. <argument index="0" name="port" type="int" />
  134. <argument index="1" name="bind_address" type="String" default="&quot;*&quot;" />
  135. <description>
  136. Starts the server by opening a UDP socket listening on the given port. You can optionally specify a [code]bind_address[/code] to only listen for packets sent to that address. See also [method PacketPeerUDP.bind].
  137. </description>
  138. </method>
  139. <method name="poll">
  140. <return type="int" enum="Error" />
  141. <description>
  142. Call this method at regular intervals (e.g. inside [method Node._process]) to process new packets. And packet from known address/port pair will be delivered to the appropriate [PacketPeerUDP], any packet received from an unknown address/port pair will be added as a pending connection (see [method is_connection_available], [method take_connection]). The maximum number of pending connection is defined via [member max_pending_connections].
  143. </description>
  144. </method>
  145. <method name="stop">
  146. <return type="void" />
  147. <description>
  148. Stops the server, closing the UDP socket if open. Will close all connected [PacketPeerUDP] accepted via [method take_connection] (remote peers will not be notified).
  149. </description>
  150. </method>
  151. <method name="take_connection">
  152. <return type="PacketPeerUDP" />
  153. <description>
  154. Returns the first pending connection (connected to the appropriate address/port). Will return [code]null[/code] if no new connection is available. See also [method is_connection_available], [method PacketPeerUDP.connect_to_host].
  155. </description>
  156. </method>
  157. </methods>
  158. <members>
  159. <member name="max_pending_connections" type="int" setter="set_max_pending_connections" getter="get_max_pending_connections" default="16">
  160. Define the maximum number of pending connections, during [method poll], any new pending connection exceeding that value will be automatically dropped. Setting this value to [code]0[/code] effectively prevents any new pending connection to be accepted (e.g. when all your players have connected).
  161. </member>
  162. </members>
  163. </class>