class_networkedmultiplayerenet.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the NetworkedMultiplayerENet.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_NetworkedMultiplayerENet:
  6. NetworkedMultiplayerENet
  7. ========================
  8. **Inherits:** :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. PacketPeer implementation using the `ENet <http://enet.bespin.org/index.html>`_ library.
  10. Description
  11. -----------
  12. A PacketPeer implementation that should be passed to :ref:`SceneTree.network_peer<class_SceneTree_property_network_peer>` after being initialized as either a client or server. Events can then be handled by connecting to :ref:`SceneTree<class_SceneTree>` signals.
  13. Tutorials
  14. ---------
  15. - :doc:`../tutorials/networking/high_level_multiplayer`
  16. - `http://enet.bespin.org/usergroup0.html <http://enet.bespin.org/usergroup0.html>`_
  17. Properties
  18. ----------
  19. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  20. | :ref:`bool<class_bool>` | :ref:`always_ordered<class_NetworkedMultiplayerENet_property_always_ordered>` | ``false`` |
  21. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  22. | :ref:`int<class_int>` | :ref:`channel_count<class_NetworkedMultiplayerENet_property_channel_count>` | ``3`` |
  23. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  24. | :ref:`CompressionMode<enum_NetworkedMultiplayerENet_CompressionMode>` | :ref:`compression_mode<class_NetworkedMultiplayerENet_property_compression_mode>` | ``0`` |
  25. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  26. | :ref:`bool<class_bool>` | :ref:`dtls_verify<class_NetworkedMultiplayerENet_property_dtls_verify>` | ``true`` |
  27. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  28. | :ref:`bool<class_bool>` | refuse_new_connections | ``false`` *(parent override)* |
  29. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  30. | :ref:`bool<class_bool>` | :ref:`server_relay<class_NetworkedMultiplayerENet_property_server_relay>` | ``true`` |
  31. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  32. | :ref:`int<class_int>` | :ref:`transfer_channel<class_NetworkedMultiplayerENet_property_transfer_channel>` | ``-1`` |
  33. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  34. | :ref:`TransferMode<enum_NetworkedMultiplayerPeer_TransferMode>` | transfer_mode | ``2`` *(parent override)* |
  35. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  36. | :ref:`bool<class_bool>` | :ref:`use_dtls<class_NetworkedMultiplayerENet_property_use_dtls>` | ``false`` |
  37. +-----------------------------------------------------------------------+-----------------------------------------------------------------------------------+-------------------------------+
  38. Methods
  39. -------
  40. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | void | :ref:`close_connection<class_NetworkedMultiplayerENet_method_close_connection>` **(** :ref:`int<class_int>` wait_usec=100 **)** |
  42. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`create_client<class_NetworkedMultiplayerENet_method_create_client>` **(** :ref:`String<class_String>` address, :ref:`int<class_int>` port, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0, :ref:`int<class_int>` client_port=0 **)** |
  44. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`create_server<class_NetworkedMultiplayerENet_method_create_server>` **(** :ref:`int<class_int>` port, :ref:`int<class_int>` max_clients=32, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)** |
  46. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | void | :ref:`disconnect_peer<class_NetworkedMultiplayerENet_method_disconnect_peer>` **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` now=false **)** |
  48. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`int<class_int>` | :ref:`get_last_packet_channel<class_NetworkedMultiplayerENet_method_get_last_packet_channel>` **(** **)** |const| |
  50. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`int<class_int>` | :ref:`get_packet_channel<class_NetworkedMultiplayerENet_method_get_packet_channel>` **(** **)** |const| |
  52. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`String<class_String>` | :ref:`get_peer_address<class_NetworkedMultiplayerENet_method_get_peer_address>` **(** :ref:`int<class_int>` id **)** |const| |
  54. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`int<class_int>` | :ref:`get_peer_port<class_NetworkedMultiplayerENet_method_get_peer_port>` **(** :ref:`int<class_int>` id **)** |const| |
  56. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | void | :ref:`set_bind_ip<class_NetworkedMultiplayerENet_method_set_bind_ip>` **(** :ref:`String<class_String>` ip **)** |
  58. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | void | :ref:`set_dtls_certificate<class_NetworkedMultiplayerENet_method_set_dtls_certificate>` **(** :ref:`X509Certificate<class_X509Certificate>` certificate **)** |
  60. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`set_dtls_key<class_NetworkedMultiplayerENet_method_set_dtls_key>` **(** :ref:`CryptoKey<class_CryptoKey>` key **)** |
  62. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. Enumerations
  64. ------------
  65. .. _enum_NetworkedMultiplayerENet_CompressionMode:
  66. .. _class_NetworkedMultiplayerENet_constant_COMPRESS_NONE:
  67. .. _class_NetworkedMultiplayerENet_constant_COMPRESS_RANGE_CODER:
  68. .. _class_NetworkedMultiplayerENet_constant_COMPRESS_FASTLZ:
  69. .. _class_NetworkedMultiplayerENet_constant_COMPRESS_ZLIB:
  70. .. _class_NetworkedMultiplayerENet_constant_COMPRESS_ZSTD:
  71. enum **CompressionMode**:
  72. - **COMPRESS_NONE** = **0** --- No compression. This uses the most bandwidth, but has the upside of requiring the fewest CPU resources.
  73. - **COMPRESS_RANGE_CODER** = **1** --- ENet's built-in range encoding.
  74. - **COMPRESS_FASTLZ** = **2** --- `FastLZ <http://fastlz.org/>`_ compression. This option uses less CPU resources compared to :ref:`COMPRESS_ZLIB<class_NetworkedMultiplayerENet_constant_COMPRESS_ZLIB>`, at the expense of using more bandwidth.
  75. - **COMPRESS_ZLIB** = **3** --- `Zlib <https://www.zlib.net/>`_ compression. This option uses less bandwidth compared to :ref:`COMPRESS_FASTLZ<class_NetworkedMultiplayerENet_constant_COMPRESS_FASTLZ>`, at the expense of using more CPU resources.
  76. - **COMPRESS_ZSTD** = **4** --- `Zstandard <https://facebook.github.io/zstd/>`_ compression.
  77. Property Descriptions
  78. ---------------------
  79. .. _class_NetworkedMultiplayerENet_property_always_ordered:
  80. - :ref:`bool<class_bool>` **always_ordered**
  81. +-----------+---------------------------+
  82. | *Default* | ``false`` |
  83. +-----------+---------------------------+
  84. | *Setter* | set_always_ordered(value) |
  85. +-----------+---------------------------+
  86. | *Getter* | is_always_ordered() |
  87. +-----------+---------------------------+
  88. Enforce ordered packets when using :ref:`NetworkedMultiplayerPeer.TRANSFER_MODE_UNRELIABLE<class_NetworkedMultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE>` (thus behaving similarly to :ref:`NetworkedMultiplayerPeer.TRANSFER_MODE_UNRELIABLE_ORDERED<class_NetworkedMultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE_ORDERED>`). This is the only way to use ordering with the RPC system.
  89. ----
  90. .. _class_NetworkedMultiplayerENet_property_channel_count:
  91. - :ref:`int<class_int>` **channel_count**
  92. +-----------+--------------------------+
  93. | *Default* | ``3`` |
  94. +-----------+--------------------------+
  95. | *Setter* | set_channel_count(value) |
  96. +-----------+--------------------------+
  97. | *Getter* | get_channel_count() |
  98. +-----------+--------------------------+
  99. The number of channels to be used by ENet. Channels are used to separate different kinds of data. In reliable or ordered mode, for example, the packet delivery order is ensured on a per-channel basis. This is done to combat latency and reduces ordering restrictions on packets. The delivery status of a packet in one channel won't stall the delivery of other packets in another channel.
  100. ----
  101. .. _class_NetworkedMultiplayerENet_property_compression_mode:
  102. - :ref:`CompressionMode<enum_NetworkedMultiplayerENet_CompressionMode>` **compression_mode**
  103. +-----------+-----------------------------+
  104. | *Default* | ``0`` |
  105. +-----------+-----------------------------+
  106. | *Setter* | set_compression_mode(value) |
  107. +-----------+-----------------------------+
  108. | *Getter* | get_compression_mode() |
  109. +-----------+-----------------------------+
  110. The compression method used for network packets. These have different tradeoffs of compression speed versus bandwidth, you may need to test which one works best for your use case if you use compression at all.
  111. ----
  112. .. _class_NetworkedMultiplayerENet_property_dtls_verify:
  113. - :ref:`bool<class_bool>` **dtls_verify**
  114. +-----------+--------------------------------+
  115. | *Default* | ``true`` |
  116. +-----------+--------------------------------+
  117. | *Setter* | set_dtls_verify_enabled(value) |
  118. +-----------+--------------------------------+
  119. | *Getter* | is_dtls_verify_enabled() |
  120. +-----------+--------------------------------+
  121. Enable or disable certiticate verification when :ref:`use_dtls<class_NetworkedMultiplayerENet_property_use_dtls>` ``true``.
  122. ----
  123. .. _class_NetworkedMultiplayerENet_property_server_relay:
  124. - :ref:`bool<class_bool>` **server_relay**
  125. +-----------+---------------------------------+
  126. | *Default* | ``true`` |
  127. +-----------+---------------------------------+
  128. | *Setter* | set_server_relay_enabled(value) |
  129. +-----------+---------------------------------+
  130. | *Getter* | is_server_relay_enabled() |
  131. +-----------+---------------------------------+
  132. Enable or disable the server feature that notifies clients of other peers' connection/disconnection, and relays messages between them. When this option is ``false``, clients won't be automatically notified of other peers and won't be able to send them packets through the server.
  133. ----
  134. .. _class_NetworkedMultiplayerENet_property_transfer_channel:
  135. - :ref:`int<class_int>` **transfer_channel**
  136. +-----------+-----------------------------+
  137. | *Default* | ``-1`` |
  138. +-----------+-----------------------------+
  139. | *Setter* | set_transfer_channel(value) |
  140. +-----------+-----------------------------+
  141. | *Getter* | get_transfer_channel() |
  142. +-----------+-----------------------------+
  143. Set the default channel to be used to transfer data. By default, this value is ``-1`` which means that ENet will only use 2 channels: one for reliable packets, and one for unreliable packets. The channel ``0`` is reserved and cannot be used. Setting this member to any value between ``0`` and :ref:`channel_count<class_NetworkedMultiplayerENet_property_channel_count>` (excluded) will force ENet to use that channel for sending data. See :ref:`channel_count<class_NetworkedMultiplayerENet_property_channel_count>` for more information about ENet channels.
  144. ----
  145. .. _class_NetworkedMultiplayerENet_property_use_dtls:
  146. - :ref:`bool<class_bool>` **use_dtls**
  147. +-----------+-------------------------+
  148. | *Default* | ``false`` |
  149. +-----------+-------------------------+
  150. | *Setter* | set_dtls_enabled(value) |
  151. +-----------+-------------------------+
  152. | *Getter* | is_dtls_enabled() |
  153. +-----------+-------------------------+
  154. When enabled, the client or server created by this peer, will use :ref:`PacketPeerDTLS<class_PacketPeerDTLS>` instead of raw UDP sockets for communicating with the remote peer. This will make the communication encrypted with DTLS at the cost of higher resource usage and potentially larger packet size.
  155. Note: When creating a DTLS server, make sure you setup the key/certificate pair via :ref:`set_dtls_key<class_NetworkedMultiplayerENet_method_set_dtls_key>` and :ref:`set_dtls_certificate<class_NetworkedMultiplayerENet_method_set_dtls_certificate>`. For DTLS clients, have a look at the :ref:`dtls_verify<class_NetworkedMultiplayerENet_property_dtls_verify>` option, and configure the certificate accordingly via :ref:`set_dtls_certificate<class_NetworkedMultiplayerENet_method_set_dtls_certificate>`.
  156. Method Descriptions
  157. -------------------
  158. .. _class_NetworkedMultiplayerENet_method_close_connection:
  159. - void **close_connection** **(** :ref:`int<class_int>` wait_usec=100 **)**
  160. Closes the connection. Ignored if no connection is currently established. If this is a server it tries to notify all clients before forcibly disconnecting them. If this is a client it simply closes the connection to the server.
  161. ----
  162. .. _class_NetworkedMultiplayerENet_method_create_client:
  163. - :ref:`Error<enum_@GlobalScope_Error>` **create_client** **(** :ref:`String<class_String>` address, :ref:`int<class_int>` port, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0, :ref:`int<class_int>` client_port=0 **)**
  164. Create client that connects to a server at ``address`` using specified ``port``. The given address needs to be either a fully qualified domain name (e.g. ``"www.example.com"``) or an IP address in IPv4 or IPv6 format (e.g. ``"192.168.1.1"``). The ``port`` is the port the server is listening on. The ``in_bandwidth`` and ``out_bandwidth`` parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if a client was created, :ref:`@GlobalScope.ERR_ALREADY_IN_USE<class_@GlobalScope_constant_ERR_ALREADY_IN_USE>` if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call :ref:`close_connection<class_NetworkedMultiplayerENet_method_close_connection>` first) or :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` if the client could not be created. If ``client_port`` is specified, the client will also listen to the given port; this is useful for some NAT traversal techniques.
  165. ----
  166. .. _class_NetworkedMultiplayerENet_method_create_server:
  167. - :ref:`Error<enum_@GlobalScope_Error>` **create_server** **(** :ref:`int<class_int>` port, :ref:`int<class_int>` max_clients=32, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)**
  168. Create server that listens to connections via ``port``. The port needs to be an available, unused port between 0 and 65535. Note that ports below 1024 are privileged and may require elevated permissions depending on the platform. To change the interface the server listens on, use :ref:`set_bind_ip<class_NetworkedMultiplayerENet_method_set_bind_ip>`. The default IP is the wildcard ``"*"``, which listens on all available interfaces. ``max_clients`` is the maximum number of clients that are allowed at once, any number up to 4095 may be used, although the achievable number of simultaneous clients may be far lower and depends on the application. For additional details on the bandwidth parameters, see :ref:`create_client<class_NetworkedMultiplayerENet_method_create_client>`. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if a server was created, :ref:`@GlobalScope.ERR_ALREADY_IN_USE<class_@GlobalScope_constant_ERR_ALREADY_IN_USE>` if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call :ref:`close_connection<class_NetworkedMultiplayerENet_method_close_connection>` first) or :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` if the server could not be created.
  169. ----
  170. .. _class_NetworkedMultiplayerENet_method_disconnect_peer:
  171. - void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` now=false **)**
  172. Disconnect the given peer. If "now" is set to ``true``, the connection will be closed immediately without flushing queued messages.
  173. ----
  174. .. _class_NetworkedMultiplayerENet_method_get_last_packet_channel:
  175. - :ref:`int<class_int>` **get_last_packet_channel** **(** **)** |const|
  176. Returns the channel of the last packet fetched via :ref:`PacketPeer.get_packet<class_PacketPeer_method_get_packet>`.
  177. ----
  178. .. _class_NetworkedMultiplayerENet_method_get_packet_channel:
  179. - :ref:`int<class_int>` **get_packet_channel** **(** **)** |const|
  180. Returns the channel of the next packet that will be retrieved via :ref:`PacketPeer.get_packet<class_PacketPeer_method_get_packet>`.
  181. ----
  182. .. _class_NetworkedMultiplayerENet_method_get_peer_address:
  183. - :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** |const|
  184. Returns the IP address of the given peer.
  185. ----
  186. .. _class_NetworkedMultiplayerENet_method_get_peer_port:
  187. - :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** |const|
  188. Returns the remote port of the given peer.
  189. ----
  190. .. _class_NetworkedMultiplayerENet_method_set_bind_ip:
  191. - void **set_bind_ip** **(** :ref:`String<class_String>` ip **)**
  192. The IP used when creating a server. This is set to the wildcard ``"*"`` by default, which binds to all available interfaces. The given IP needs to be in IPv4 or IPv6 address format, for example: ``"192.168.1.1"``.
  193. ----
  194. .. _class_NetworkedMultiplayerENet_method_set_dtls_certificate:
  195. - void **set_dtls_certificate** **(** :ref:`X509Certificate<class_X509Certificate>` certificate **)**
  196. Configure the :ref:`X509Certificate<class_X509Certificate>` to use when :ref:`use_dtls<class_NetworkedMultiplayerENet_property_use_dtls>` is ``true``. For servers, you must also setup the :ref:`CryptoKey<class_CryptoKey>` via :ref:`set_dtls_key<class_NetworkedMultiplayerENet_method_set_dtls_key>`.
  197. ----
  198. .. _class_NetworkedMultiplayerENet_method_set_dtls_key:
  199. - void **set_dtls_key** **(** :ref:`CryptoKey<class_CryptoKey>` key **)**
  200. Configure the :ref:`CryptoKey<class_CryptoKey>` to use when :ref:`use_dtls<class_NetworkedMultiplayerENet_property_use_dtls>` is ``true``. Remember to also call :ref:`set_dtls_certificate<class_NetworkedMultiplayerENet_method_set_dtls_certificate>` to setup your :ref:`X509Certificate<class_X509Certificate>`.
  201. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  202. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  203. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`