UDPServer.xml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="UDPServer" inherits="Reference" version="3.4">
  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. [codeblock]
  11. # server.gd
  12. extends Node
  13. var server := UDPServer.new()
  14. var peers = []
  15. func _ready():
  16. server.listen(4242)
  17. func _process(delta):
  18. server.poll() # Important!
  19. if server.is_connection_available():
  20. var peer : PacketPeerUDP = server.take_connection()
  21. var pkt = peer.get_packet()
  22. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  23. print("Received data: %s" % [pkt.get_string_from_utf8()])
  24. # Reply so it knows we received the message.
  25. peer.put_packet(pkt)
  26. # Keep a reference so we can keep contacting the remote peer.
  27. peers.append(peer)
  28. for i in range(0, peers.size()):
  29. pass # Do something with the connected peers.
  30. [/codeblock]
  31. [codeblock]
  32. # client.gd
  33. extends Node
  34. var udp := PacketPeerUDP.new()
  35. var connected = false
  36. func _ready():
  37. udp.connect_to_host("127.0.0.1", 4242)
  38. func _process(delta):
  39. if !connected:
  40. # Try to contact server
  41. udp.put_packet("The answer is... 42!".to_utf8())
  42. if udp.get_available_packet_count() &gt; 0:
  43. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  44. connected = true
  45. [/codeblock]
  46. </description>
  47. <tutorials>
  48. </tutorials>
  49. <methods>
  50. <method name="is_connection_available" qualifiers="const">
  51. <return type="bool">
  52. </return>
  53. <description>
  54. Returns [code]true[/code] if a packet with a new address/port combination was received on the socket.
  55. </description>
  56. </method>
  57. <method name="is_listening" qualifiers="const">
  58. <return type="bool">
  59. </return>
  60. <description>
  61. Returns [code]true[/code] if the socket is open and listening on a port.
  62. </description>
  63. </method>
  64. <method name="listen">
  65. <return type="int" enum="Error">
  66. </return>
  67. <argument index="0" name="port" type="int">
  68. </argument>
  69. <argument index="1" name="bind_address" type="String" default="&quot;*&quot;">
  70. </argument>
  71. <description>
  72. 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.listen].
  73. </description>
  74. </method>
  75. <method name="poll">
  76. <return type="int" enum="Error">
  77. </return>
  78. <description>
  79. 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].
  80. </description>
  81. </method>
  82. <method name="stop">
  83. <return type="void">
  84. </return>
  85. <description>
  86. 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).
  87. </description>
  88. </method>
  89. <method name="take_connection">
  90. <return type="PacketPeerUDP">
  91. </return>
  92. <description>
  93. 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].
  94. </description>
  95. </method>
  96. </methods>
  97. <members>
  98. <member name="max_pending_connections" type="int" setter="set_max_pending_connections" getter="get_max_pending_connections" default="16">
  99. 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).
  100. </member>
  101. </members>
  102. <constants>
  103. </constants>
  104. </class>