class_udpserver.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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:`Reference<class_Reference>` **<** :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. ::
  16. # server.gd
  17. extends Node
  18. var server := UDPServer.new()
  19. var peers = []
  20. func _ready():
  21. server.listen(4242)
  22. func _process(delta):
  23. server.poll() # Important!
  24. if server.is_connection_available():
  25. var peer : PacketPeerUDP = server.take_connection()
  26. var pkt = peer.get_packet()
  27. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  28. print("Received data: %s" % [pkt.get_string_from_utf8()])
  29. # Reply so it knows we received the message.
  30. peer.put_packet(pkt)
  31. # Keep a reference so we can keep contacting the remote peer.
  32. peers.append(peer)
  33. for i in range(0, peers.size()):
  34. pass # Do something with the connected peers.
  35. ::
  36. # client.gd
  37. extends Node
  38. var udp := PacketPeerUDP.new()
  39. var connected = false
  40. func _ready():
  41. udp.connect_to_host("127.0.0.1", 4242)
  42. func _process(delta):
  43. if !connected:
  44. # Try to contact server
  45. udp.put_packet("The answer is... 42!".to_utf8())
  46. if udp.get_available_packet_count() > 0:
  47. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  48. connected = true
  49. Properties
  50. ----------
  51. +-----------------------+----------------------------------------------------------------------------------+--------+
  52. | :ref:`int<class_int>` | :ref:`max_pending_connections<class_UDPServer_property_max_pending_connections>` | ``16`` |
  53. +-----------------------+----------------------------------------------------------------------------------+--------+
  54. Methods
  55. -------
  56. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`bool<class_bool>` | :ref:`is_connection_available<class_UDPServer_method_is_connection_available>` **(** **)** |const| |
  58. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`bool<class_bool>` | :ref:`is_listening<class_UDPServer_method_is_listening>` **(** **)** |const| |
  60. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_UDPServer_method_listen>` **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)** |
  62. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`poll<class_UDPServer_method_poll>` **(** **)** |
  64. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`stop<class_UDPServer_method_stop>` **(** **)** |
  66. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`PacketPeerUDP<class_PacketPeerUDP>` | :ref:`take_connection<class_UDPServer_method_take_connection>` **(** **)** |
  68. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  69. Property Descriptions
  70. ---------------------
  71. .. _class_UDPServer_property_max_pending_connections:
  72. - :ref:`int<class_int>` **max_pending_connections**
  73. +-----------+------------------------------------+
  74. | *Default* | ``16`` |
  75. +-----------+------------------------------------+
  76. | *Setter* | set_max_pending_connections(value) |
  77. +-----------+------------------------------------+
  78. | *Getter* | get_max_pending_connections() |
  79. +-----------+------------------------------------+
  80. 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).
  81. Method Descriptions
  82. -------------------
  83. .. _class_UDPServer_method_is_connection_available:
  84. - :ref:`bool<class_bool>` **is_connection_available** **(** **)** |const|
  85. Returns ``true`` if a packet with a new address/port combination was received on the socket.
  86. ----
  87. .. _class_UDPServer_method_is_listening:
  88. - :ref:`bool<class_bool>` **is_listening** **(** **)** |const|
  89. Returns ``true`` if the socket is open and listening on a port.
  90. ----
  91. .. _class_UDPServer_method_listen:
  92. - :ref:`Error<enum_@GlobalScope_Error>` **listen** **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)**
  93. 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.listen<class_PacketPeerUDP_method_listen>`.
  94. ----
  95. .. _class_UDPServer_method_poll:
  96. - :ref:`Error<enum_@GlobalScope_Error>` **poll** **(** **)**
  97. 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>`.
  98. ----
  99. .. _class_UDPServer_method_stop:
  100. - void **stop** **(** **)**
  101. 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).
  102. ----
  103. .. _class_UDPServer_method_take_connection:
  104. - :ref:`PacketPeerUDP<class_PacketPeerUDP>` **take_connection** **(** **)**
  105. 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>`.
  106. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  107. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  108. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`