class_enetconnection.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/modules/enet/doc_classes/ENetConnection.xml.
  6. .. _class_ENetConnection:
  7. ENetConnection
  8. ==============
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A wrapper class for an `ENetHost <http://enet.bespin.org/group__host.html>`__.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).
  15. .. rst-class:: classref-introduction-group
  16. Tutorials
  17. ---------
  18. - `API documentation on the ENet website <http://enet.bespin.org/usergroup0.html>`__
  19. .. rst-class:: classref-reftable-group
  20. Methods
  21. -------
  22. .. table::
  23. :widths: auto
  24. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | void | :ref:`bandwidth_limit<class_ENetConnection_method_bandwidth_limit>` **(** :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)** |
  26. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | void | :ref:`broadcast<class_ENetConnection_method_broadcast>` **(** :ref:`int<class_int>` channel, :ref:`PackedByteArray<class_PackedByteArray>` packet, :ref:`int<class_int>` flags **)** |
  28. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | void | :ref:`channel_limit<class_ENetConnection_method_channel_limit>` **(** :ref:`int<class_int>` limit **)** |
  30. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`compress<class_ENetConnection_method_compress>` **(** :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` mode **)** |
  32. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`ENetPacketPeer<class_ENetPacketPeer>` | :ref:`connect_to_host<class_ENetConnection_method_connect_to_host>` **(** :ref:`String<class_String>` address, :ref:`int<class_int>` port, :ref:`int<class_int>` channels=0, :ref:`int<class_int>` data=0 **)** |
  34. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`create_host<class_ENetConnection_method_create_host>` **(** :ref:`int<class_int>` max_peers=32, :ref:`int<class_int>` max_channels=0, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)** |
  36. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`create_host_bound<class_ENetConnection_method_create_host_bound>` **(** :ref:`String<class_String>` bind_address, :ref:`int<class_int>` bind_port, :ref:`int<class_int>` max_peers=32, :ref:`int<class_int>` max_channels=0, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)** |
  38. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | void | :ref:`destroy<class_ENetConnection_method_destroy>` **(** **)** |
  40. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`dtls_client_setup<class_ENetConnection_method_dtls_client_setup>` **(** :ref:`String<class_String>` hostname, :ref:`TLSOptions<class_TLSOptions>` client_options=null **)** |
  42. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`dtls_server_setup<class_ENetConnection_method_dtls_server_setup>` **(** :ref:`TLSOptions<class_TLSOptions>` server_options **)** |
  44. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | void | :ref:`flush<class_ENetConnection_method_flush>` **(** **)** |
  46. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`int<class_int>` | :ref:`get_local_port<class_ENetConnection_method_get_local_port>` **(** **)** |const| |
  48. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`int<class_int>` | :ref:`get_max_channels<class_ENetConnection_method_get_max_channels>` **(** **)** |const| |
  50. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`ENetPacketPeer[]<class_ENetPacketPeer>` | :ref:`get_peers<class_ENetConnection_method_get_peers>` **(** **)** |
  52. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`float<class_float>` | :ref:`pop_statistic<class_ENetConnection_method_pop_statistic>` **(** :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` statistic **)** |
  54. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | void | :ref:`refuse_new_connections<class_ENetConnection_method_refuse_new_connections>` **(** :ref:`bool<class_bool>` refuse **)** |
  56. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Array<class_Array>` | :ref:`service<class_ENetConnection_method_service>` **(** :ref:`int<class_int>` timeout=0 **)** |
  58. +-----------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. .. rst-class:: classref-section-separator
  60. ----
  61. .. rst-class:: classref-descriptions-group
  62. Enumerations
  63. ------------
  64. .. _enum_ENetConnection_CompressionMode:
  65. .. rst-class:: classref-enumeration
  66. enum **CompressionMode**:
  67. .. _class_ENetConnection_constant_COMPRESS_NONE:
  68. .. rst-class:: classref-enumeration-constant
  69. :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` **COMPRESS_NONE** = ``0``
  70. No compression. This uses the most bandwidth, but has the upside of requiring the fewest CPU resources. This option may also be used to make network debugging using tools like Wireshark easier.
  71. .. _class_ENetConnection_constant_COMPRESS_RANGE_CODER:
  72. .. rst-class:: classref-enumeration-constant
  73. :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` **COMPRESS_RANGE_CODER** = ``1``
  74. ENet's built-in range encoding. Works well on small packets, but is not the most efficient algorithm on packets larger than 4 KB.
  75. .. _class_ENetConnection_constant_COMPRESS_FASTLZ:
  76. .. rst-class:: classref-enumeration-constant
  77. :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` **COMPRESS_FASTLZ** = ``2``
  78. `FastLZ <https://fastlz.org/>`__ compression. This option uses less CPU resources compared to :ref:`COMPRESS_ZLIB<class_ENetConnection_constant_COMPRESS_ZLIB>`, at the expense of using more bandwidth.
  79. .. _class_ENetConnection_constant_COMPRESS_ZLIB:
  80. .. rst-class:: classref-enumeration-constant
  81. :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` **COMPRESS_ZLIB** = ``3``
  82. `Zlib <https://www.zlib.net/>`__ compression. This option uses less bandwidth compared to :ref:`COMPRESS_FASTLZ<class_ENetConnection_constant_COMPRESS_FASTLZ>`, at the expense of using more CPU resources.
  83. .. _class_ENetConnection_constant_COMPRESS_ZSTD:
  84. .. rst-class:: classref-enumeration-constant
  85. :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` **COMPRESS_ZSTD** = ``4``
  86. `Zstandard <https://facebook.github.io/zstd/>`__ compression. Note that this algorithm is not very efficient on packets smaller than 4 KB. Therefore, it's recommended to use other compression algorithms in most cases.
  87. .. rst-class:: classref-item-separator
  88. ----
  89. .. _enum_ENetConnection_EventType:
  90. .. rst-class:: classref-enumeration
  91. enum **EventType**:
  92. .. _class_ENetConnection_constant_EVENT_ERROR:
  93. .. rst-class:: classref-enumeration-constant
  94. :ref:`EventType<enum_ENetConnection_EventType>` **EVENT_ERROR** = ``-1``
  95. An error occurred during :ref:`service<class_ENetConnection_method_service>`. You will likely need to :ref:`destroy<class_ENetConnection_method_destroy>` the host and recreate it.
  96. .. _class_ENetConnection_constant_EVENT_NONE:
  97. .. rst-class:: classref-enumeration-constant
  98. :ref:`EventType<enum_ENetConnection_EventType>` **EVENT_NONE** = ``0``
  99. No event occurred within the specified time limit.
  100. .. _class_ENetConnection_constant_EVENT_CONNECT:
  101. .. rst-class:: classref-enumeration-constant
  102. :ref:`EventType<enum_ENetConnection_EventType>` **EVENT_CONNECT** = ``1``
  103. A connection request initiated by enet_host_connect has completed. The array will contain the peer which successfully connected.
  104. .. _class_ENetConnection_constant_EVENT_DISCONNECT:
  105. .. rst-class:: classref-enumeration-constant
  106. :ref:`EventType<enum_ENetConnection_EventType>` **EVENT_DISCONNECT** = ``2``
  107. A peer has disconnected. This event is generated on a successful completion of a disconnect initiated by :ref:`ENetPacketPeer.peer_disconnect<class_ENetPacketPeer_method_peer_disconnect>`, if a peer has timed out, or if a connection request initialized by :ref:`connect_to_host<class_ENetConnection_method_connect_to_host>` has timed out. The array will contain the peer which disconnected. The data field contains user supplied data describing the disconnection, or 0, if none is available.
  108. .. _class_ENetConnection_constant_EVENT_RECEIVE:
  109. .. rst-class:: classref-enumeration-constant
  110. :ref:`EventType<enum_ENetConnection_EventType>` **EVENT_RECEIVE** = ``3``
  111. A packet has been received from a peer. The array will contain the peer which sent the packet, the channel number upon which the packet was received, and the received packet.
  112. .. rst-class:: classref-item-separator
  113. ----
  114. .. _enum_ENetConnection_HostStatistic:
  115. .. rst-class:: classref-enumeration
  116. enum **HostStatistic**:
  117. .. _class_ENetConnection_constant_HOST_TOTAL_SENT_DATA:
  118. .. rst-class:: classref-enumeration-constant
  119. :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` **HOST_TOTAL_SENT_DATA** = ``0``
  120. Total data sent.
  121. .. _class_ENetConnection_constant_HOST_TOTAL_SENT_PACKETS:
  122. .. rst-class:: classref-enumeration-constant
  123. :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` **HOST_TOTAL_SENT_PACKETS** = ``1``
  124. Total UDP packets sent.
  125. .. _class_ENetConnection_constant_HOST_TOTAL_RECEIVED_DATA:
  126. .. rst-class:: classref-enumeration-constant
  127. :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` **HOST_TOTAL_RECEIVED_DATA** = ``2``
  128. Total data received.
  129. .. _class_ENetConnection_constant_HOST_TOTAL_RECEIVED_PACKETS:
  130. .. rst-class:: classref-enumeration-constant
  131. :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` **HOST_TOTAL_RECEIVED_PACKETS** = ``3``
  132. Total UDP packets received.
  133. .. rst-class:: classref-section-separator
  134. ----
  135. .. rst-class:: classref-descriptions-group
  136. Method Descriptions
  137. -------------------
  138. .. _class_ENetConnection_method_bandwidth_limit:
  139. .. rst-class:: classref-method
  140. void **bandwidth_limit** **(** :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)**
  141. Adjusts the bandwidth limits of a host.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_ENetConnection_method_broadcast:
  145. .. rst-class:: classref-method
  146. void **broadcast** **(** :ref:`int<class_int>` channel, :ref:`PackedByteArray<class_PackedByteArray>` packet, :ref:`int<class_int>` flags **)**
  147. Queues a ``packet`` to be sent to all peers associated with the host over the specified ``channel``. See :ref:`ENetPacketPeer<class_ENetPacketPeer>` ``FLAG_*`` constants for available packet flags.
  148. .. rst-class:: classref-item-separator
  149. ----
  150. .. _class_ENetConnection_method_channel_limit:
  151. .. rst-class:: classref-method
  152. void **channel_limit** **(** :ref:`int<class_int>` limit **)**
  153. Limits the maximum allowed channels of future incoming connections.
  154. .. rst-class:: classref-item-separator
  155. ----
  156. .. _class_ENetConnection_method_compress:
  157. .. rst-class:: classref-method
  158. void **compress** **(** :ref:`CompressionMode<enum_ENetConnection_CompressionMode>` mode **)**
  159. Sets 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.
  160. \ **Note:** Most games' network design involve sending many small packets frequently (smaller than 4 KB each). If in doubt, it is recommended to keep the default compression algorithm as it works best on these small packets.
  161. \ **Note:** The compression mode must be set to the same value on both the server and all its clients. Clients will fail to connect if the compression mode set on the client differs from the one set on the server.
  162. .. rst-class:: classref-item-separator
  163. ----
  164. .. _class_ENetConnection_method_connect_to_host:
  165. .. rst-class:: classref-method
  166. :ref:`ENetPacketPeer<class_ENetPacketPeer>` **connect_to_host** **(** :ref:`String<class_String>` address, :ref:`int<class_int>` port, :ref:`int<class_int>` channels=0, :ref:`int<class_int>` data=0 **)**
  167. Initiates a connection to a foreign ``address`` using the specified ``port`` and allocating the requested ``channels``. Optional ``data`` can be passed during connection in the form of a 32 bit integer.
  168. \ **Note:** You must call either :ref:`create_host<class_ENetConnection_method_create_host>` or :ref:`create_host_bound<class_ENetConnection_method_create_host_bound>` before calling this method.
  169. .. rst-class:: classref-item-separator
  170. ----
  171. .. _class_ENetConnection_method_create_host:
  172. .. rst-class:: classref-method
  173. :ref:`Error<enum_@GlobalScope_Error>` **create_host** **(** :ref:`int<class_int>` max_peers=32, :ref:`int<class_int>` max_channels=0, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)**
  174. Create an ENetHost that will allow up to ``max_peers`` connected peers, each allocating up to ``max_channels`` channels, optionally limiting bandwidth to ``in_bandwidth`` and ``out_bandwidth``.
  175. .. rst-class:: classref-item-separator
  176. ----
  177. .. _class_ENetConnection_method_create_host_bound:
  178. .. rst-class:: classref-method
  179. :ref:`Error<enum_@GlobalScope_Error>` **create_host_bound** **(** :ref:`String<class_String>` bind_address, :ref:`int<class_int>` bind_port, :ref:`int<class_int>` max_peers=32, :ref:`int<class_int>` max_channels=0, :ref:`int<class_int>` in_bandwidth=0, :ref:`int<class_int>` out_bandwidth=0 **)**
  180. Create an ENetHost like :ref:`create_host<class_ENetConnection_method_create_host>` which is also bound to the given ``bind_address`` and ``bind_port``.
  181. .. rst-class:: classref-item-separator
  182. ----
  183. .. _class_ENetConnection_method_destroy:
  184. .. rst-class:: classref-method
  185. void **destroy** **(** **)**
  186. Destroys the host and all resources associated with it.
  187. .. rst-class:: classref-item-separator
  188. ----
  189. .. _class_ENetConnection_method_dtls_client_setup:
  190. .. rst-class:: classref-method
  191. :ref:`Error<enum_@GlobalScope_Error>` **dtls_client_setup** **(** :ref:`String<class_String>` hostname, :ref:`TLSOptions<class_TLSOptions>` client_options=null **)**
  192. Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before :ref:`connect_to_host<class_ENetConnection_method_connect_to_host>` to have ENet connect using DTLS validating the server certificate against ``hostname``. You can pass the optional ``client_options`` parameter to customize the trusted certification authorities, or disable the common name verification. See :ref:`TLSOptions.client<class_TLSOptions_method_client>` and :ref:`TLSOptions.client_unsafe<class_TLSOptions_method_client_unsafe>`.
  193. .. rst-class:: classref-item-separator
  194. ----
  195. .. _class_ENetConnection_method_dtls_server_setup:
  196. .. rst-class:: classref-method
  197. :ref:`Error<enum_@GlobalScope_Error>` **dtls_server_setup** **(** :ref:`TLSOptions<class_TLSOptions>` server_options **)**
  198. Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet servers. Call this right after :ref:`create_host_bound<class_ENetConnection_method_create_host_bound>` to have ENet expect peers to connect using DTLS. See :ref:`TLSOptions.server<class_TLSOptions_method_server>`.
  199. .. rst-class:: classref-item-separator
  200. ----
  201. .. _class_ENetConnection_method_flush:
  202. .. rst-class:: classref-method
  203. void **flush** **(** **)**
  204. Sends any queued packets on the host specified to its designated peers.
  205. .. rst-class:: classref-item-separator
  206. ----
  207. .. _class_ENetConnection_method_get_local_port:
  208. .. rst-class:: classref-method
  209. :ref:`int<class_int>` **get_local_port** **(** **)** |const|
  210. Returns the local port to which this peer is bound.
  211. .. rst-class:: classref-item-separator
  212. ----
  213. .. _class_ENetConnection_method_get_max_channels:
  214. .. rst-class:: classref-method
  215. :ref:`int<class_int>` **get_max_channels** **(** **)** |const|
  216. Returns the maximum number of channels allowed for connected peers.
  217. .. rst-class:: classref-item-separator
  218. ----
  219. .. _class_ENetConnection_method_get_peers:
  220. .. rst-class:: classref-method
  221. :ref:`ENetPacketPeer[]<class_ENetPacketPeer>` **get_peers** **(** **)**
  222. Returns the list of peers associated with this host.
  223. \ **Note:** This list might include some peers that are not fully connected or are still being disconnected.
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_ENetConnection_method_pop_statistic:
  227. .. rst-class:: classref-method
  228. :ref:`float<class_float>` **pop_statistic** **(** :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` statistic **)**
  229. Returns and resets host statistics. See :ref:`HostStatistic<enum_ENetConnection_HostStatistic>` for more info.
  230. .. rst-class:: classref-item-separator
  231. ----
  232. .. _class_ENetConnection_method_refuse_new_connections:
  233. .. rst-class:: classref-method
  234. void **refuse_new_connections** **(** :ref:`bool<class_bool>` refuse **)**
  235. Configures the DTLS server to automatically drop new connections.
  236. \ **Note:** This method is only relevant after calling :ref:`dtls_server_setup<class_ENetConnection_method_dtls_server_setup>`.
  237. .. rst-class:: classref-item-separator
  238. ----
  239. .. _class_ENetConnection_method_service:
  240. .. rst-class:: classref-method
  241. :ref:`Array<class_Array>` **service** **(** :ref:`int<class_int>` timeout=0 **)**
  242. Waits for events on the host specified and shuttles packets between the host and its peers. The returned :ref:`Array<class_Array>` will have 4 elements. An :ref:`EventType<enum_ENetConnection_EventType>`, the :ref:`ENetPacketPeer<class_ENetPacketPeer>` which generated the event, the event associated data (if any), the event associated channel (if any). If the generated event is :ref:`EVENT_RECEIVE<class_ENetConnection_constant_EVENT_RECEIVE>`, the received packet will be queued to the associated :ref:`ENetPacketPeer<class_ENetPacketPeer>`.
  243. Call this function regularly to handle connections, disconnections, and to receive new packets.
  244. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  245. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  246. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  247. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  248. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  249. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`