Path: blob/master/modules/enet/doc_classes/ENetConnection.xml
10278 views
<?xml version="1.0" encoding="UTF-8" ?>1<class name="ENetConnection" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">2<brief_description>3A wrapper class for an [url=http://enet.bespin.org/group__host.html]ENetHost[/url].4</brief_description>5<description>6ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).7</description>8<tutorials>9<link title="API documentation on the ENet website">http://enet.bespin.org/usergroup0.html</link>10</tutorials>11<methods>12<method name="bandwidth_limit">13<return type="void" />14<param index="0" name="in_bandwidth" type="int" default="0" />15<param index="1" name="out_bandwidth" type="int" default="0" />16<description>17Adjusts the bandwidth limits of a host.18</description>19</method>20<method name="broadcast">21<return type="void" />22<param index="0" name="channel" type="int" />23<param index="1" name="packet" type="PackedByteArray" />24<param index="2" name="flags" type="int" />25<description>26Queues a [param packet] to be sent to all peers associated with the host over the specified [param channel]. See [ENetPacketPeer] [code]FLAG_*[/code] constants for available packet flags.27</description>28</method>29<method name="channel_limit">30<return type="void" />31<param index="0" name="limit" type="int" />32<description>33Limits the maximum allowed channels of future incoming connections.34</description>35</method>36<method name="compress">37<return type="void" />38<param index="0" name="mode" type="int" enum="ENetConnection.CompressionMode" />39<description>40Sets 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.41[b]Note:[/b] 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.42[b]Note:[/b] 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.43</description>44</method>45<method name="connect_to_host">46<return type="ENetPacketPeer" />47<param index="0" name="address" type="String" />48<param index="1" name="port" type="int" />49<param index="2" name="channels" type="int" default="0" />50<param index="3" name="data" type="int" default="0" />51<description>52Initiates a connection to a foreign [param address] using the specified [param port] and allocating the requested [param channels]. Optional [param data] can be passed during connection in the form of a 32 bit integer.53[b]Note:[/b] You must call either [method create_host] or [method create_host_bound] on both ends before calling this method.54</description>55</method>56<method name="create_host">57<return type="int" enum="Error" />58<param index="0" name="max_peers" type="int" default="32" />59<param index="1" name="max_channels" type="int" default="0" />60<param index="2" name="in_bandwidth" type="int" default="0" />61<param index="3" name="out_bandwidth" type="int" default="0" />62<description>63Creates an ENetHost that allows up to [param max_peers] connected peers, each allocating up to [param max_channels] channels, optionally limiting bandwidth to [param in_bandwidth] and [param out_bandwidth] (if greater than zero).64This method binds a random available dynamic UDP port on the host machine at the [i]unspecified[/i] address. Use [method create_host_bound] to specify the address and port.65[b]Note:[/b] It is necessary to create a host in both client and server in order to establish a connection.66</description>67</method>68<method name="create_host_bound">69<return type="int" enum="Error" />70<param index="0" name="bind_address" type="String" />71<param index="1" name="bind_port" type="int" />72<param index="2" name="max_peers" type="int" default="32" />73<param index="3" name="max_channels" type="int" default="0" />74<param index="4" name="in_bandwidth" type="int" default="0" />75<param index="5" name="out_bandwidth" type="int" default="0" />76<description>77Creates an ENetHost bound to the given [param bind_address] and [param bind_port] that allows up to [param max_peers] connected peers, each allocating up to [param max_channels] channels, optionally limiting bandwidth to [param in_bandwidth] and [param out_bandwidth] (if greater than zero).78[b]Note:[/b] It is necessary to create a host in both client and server in order to establish a connection.79</description>80</method>81<method name="destroy">82<return type="void" />83<description>84Destroys the host and all resources associated with it.85</description>86</method>87<method name="dtls_client_setup">88<return type="int" enum="Error" />89<param index="0" name="hostname" type="String" />90<param index="1" name="client_options" type="TLSOptions" default="null" />91<description>92Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before [method connect_to_host] to have ENet connect using DTLS validating the server certificate against [param hostname]. You can pass the optional [param client_options] parameter to customize the trusted certification authorities, or disable the common name verification. See [method TLSOptions.client] and [method TLSOptions.client_unsafe].93</description>94</method>95<method name="dtls_server_setup">96<return type="int" enum="Error" />97<param index="0" name="server_options" type="TLSOptions" />98<description>99Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet servers. Call this right after [method create_host_bound] to have ENet expect peers to connect using DTLS. See [method TLSOptions.server].100</description>101</method>102<method name="flush">103<return type="void" />104<description>105Sends any queued packets on the host specified to its designated peers.106</description>107</method>108<method name="get_local_port" qualifiers="const">109<return type="int" />110<description>111Returns the local port to which this peer is bound.112</description>113</method>114<method name="get_max_channels" qualifiers="const">115<return type="int" />116<description>117Returns the maximum number of channels allowed for connected peers.118</description>119</method>120<method name="get_peers">121<return type="ENetPacketPeer[]" />122<description>123Returns the list of peers associated with this host.124[b]Note:[/b] This list might include some peers that are not fully connected or are still being disconnected.125</description>126</method>127<method name="pop_statistic">128<return type="float" />129<param index="0" name="statistic" type="int" enum="ENetConnection.HostStatistic" />130<description>131Returns and resets host statistics.132</description>133</method>134<method name="refuse_new_connections">135<return type="void" />136<param index="0" name="refuse" type="bool" />137<description>138Configures the DTLS server to automatically drop new connections.139[b]Note:[/b] This method is only relevant after calling [method dtls_server_setup].140</description>141</method>142<method name="service">143<return type="Array" />144<param index="0" name="timeout" type="int" default="0" />145<description>146Waits for events on this connection and shuttles packets between the host and its peers, with the given [param timeout] (in milliseconds). The returned [Array] will have 4 elements. An [enum EventType], the [ENetPacketPeer] which generated the event, the event associated data (if any), the event associated channel (if any). If the generated event is [constant EVENT_RECEIVE], the received packet will be queued to the associated [ENetPacketPeer].147Call this function regularly to handle connections, disconnections, and to receive new packets.148[b]Note:[/b] This method must be called on both ends involved in the event (sending and receiving hosts).149</description>150</method>151<method name="socket_send">152<return type="void" />153<param index="0" name="destination_address" type="String" />154<param index="1" name="destination_port" type="int" />155<param index="2" name="packet" type="PackedByteArray" />156<description>157Sends a [param packet] toward a destination from the address and port currently bound by this ENetConnection instance.158This is useful as it serves to establish entries in NAT routing tables on all devices between this bound instance and the public facing internet, allowing a prospective client's connection packets to be routed backward through the NAT device(s) between the public internet and this host.159This requires forward knowledge of a prospective client's address and communication port as seen by the public internet - after any NAT devices have handled their connection request. This information can be obtained by a [url=https://en.wikipedia.org/wiki/STUN]STUN[/url] service, and must be handed off to your host by an entity that is not the prospective client. This will never work for a client behind a Symmetric NAT due to the nature of the Symmetric NAT routing algorithm, as their IP and Port cannot be known beforehand.160</description>161</method>162</methods>163<constants>164<constant name="COMPRESS_NONE" value="0" enum="CompressionMode">165No 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.166</constant>167<constant name="COMPRESS_RANGE_CODER" value="1" enum="CompressionMode">168ENet's built-in range encoding. Works well on small packets, but is not the most efficient algorithm on packets larger than 4 KB.169</constant>170<constant name="COMPRESS_FASTLZ" value="2" enum="CompressionMode">171[url=https://fastlz.org/]FastLZ[/url] compression. This option uses less CPU resources compared to [constant COMPRESS_ZLIB], at the expense of using more bandwidth.172</constant>173<constant name="COMPRESS_ZLIB" value="3" enum="CompressionMode">174[url=https://www.zlib.net/]Zlib[/url] compression. This option uses less bandwidth compared to [constant COMPRESS_FASTLZ], at the expense of using more CPU resources.175</constant>176<constant name="COMPRESS_ZSTD" value="4" enum="CompressionMode">177[url=https://facebook.github.io/zstd/]Zstandard[/url] 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.178</constant>179<constant name="EVENT_ERROR" value="-1" enum="EventType">180An error occurred during [method service]. You will likely need to [method destroy] the host and recreate it.181</constant>182<constant name="EVENT_NONE" value="0" enum="EventType">183No event occurred within the specified time limit.184</constant>185<constant name="EVENT_CONNECT" value="1" enum="EventType">186A connection request initiated by enet_host_connect has completed. The array will contain the peer which successfully connected.187</constant>188<constant name="EVENT_DISCONNECT" value="2" enum="EventType">189A peer has disconnected. This event is generated on a successful completion of a disconnect initiated by [method ENetPacketPeer.peer_disconnect], if a peer has timed out, or if a connection request initialized by [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.190</constant>191<constant name="EVENT_RECEIVE" value="3" enum="EventType">192A packet has been received from a peer. The array will contain the peer which sent the packet and the channel number upon which the packet was received. The received packet will be queued to the associated [ENetPacketPeer].193</constant>194<constant name="HOST_TOTAL_SENT_DATA" value="0" enum="HostStatistic">195Total data sent.196</constant>197<constant name="HOST_TOTAL_SENT_PACKETS" value="1" enum="HostStatistic">198Total UDP packets sent.199</constant>200<constant name="HOST_TOTAL_RECEIVED_DATA" value="2" enum="HostStatistic">201Total data received.202</constant>203<constant name="HOST_TOTAL_RECEIVED_PACKETS" value="3" enum="HostStatistic">204Total UDP packets received.205</constant>206</constants>207</class>208209210