Class ENetConnection
A wrapper class for an ENetHost.
- Inheritance
-
ENetConnection
Remarks
ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).
See Also
Methods
bandwidth_limit(int, int)
Adjusts the bandwidth limits of a host.
void bandwidth_limit(int in_bandwidth, int out_bandwidth)
Parameters
broadcast(int, PackedByteArray, int)
Queues a packet
to be sent to all peers associated with the host over the specified channel
. See ENetPacketPeer FLAG_*
constants for available packet flags.
void broadcast(int channel, PackedByteArray packet, int flags)
Parameters
channel
intpacket
PackedByteArrayflags
int
channel_limit(int)
Limits the maximum allowed channels of future incoming connections.
void channel_limit(int limit)
Parameters
limit
int
compress(int)
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.
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.
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.
void compress(int mode)
Parameters
mode
int
connect_to_host(String, int, int, int)
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.
Note: You must call either ENetConnection.create_host or ENetConnection.create_host_bound on both ends before calling this method.
ENetPacketPeer connect_to_host(String address, int port, int channels, int data)
Parameters
create_host(int, int, int, int)
Creates an ENetHost that allows up to max_peers
connected peers, each allocating up to max_channels
channels, optionally limiting bandwidth to in_bandwidth
and out_bandwidth
(if greater than zero).
This method binds a random available dynamic UDP port on the host machine at the unspecified address. Use ENetConnection.create_host_bound to specify the address and port.
Note: It is necessary to create a host in both client and server in order to establish a connection.
int create_host(int max_peers, int max_channels, int in_bandwidth, int out_bandwidth)
Parameters
create_host_bound(String, int, int, int, int, int)
Creates an ENetHost bound to the given bind_address
and bind_port
that allows up to max_peers
connected peers, each allocating up to max_channels
channels, optionally limiting bandwidth to in_bandwidth
and out_bandwidth
(if greater than zero).
Note: It is necessary to create a host in both client and server in order to establish a connection.
int create_host_bound(String bind_address, int bind_port, int max_peers, int max_channels, int in_bandwidth, int out_bandwidth)
Parameters
destroy
Destroys the host and all resources associated with it.
void destroy
dtls_client_setup(String, TLSOptions)
Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before ENetConnection.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 TLSOptions.client and TLSOptions.client_unsafe.
int dtls_client_setup(String hostname, TLSOptions client_options)
Parameters
hostname
Stringclient_options
TLSOptions
dtls_server_setup(TLSOptions)
Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet servers. Call this right after ENetConnection.create_host_bound to have ENet expect peers to connect using DTLS. See TLSOptions.server.
int dtls_server_setup(TLSOptions server_options)
Parameters
server_options
TLSOptions
flush
Sends any queued packets on the host specified to its designated peers.
void flush
get_local_port
Qualifiers: const
Returns the local port to which this peer is bound.
int get_local_port
get_max_channels
Qualifiers: const
Returns the maximum number of channels allowed for connected peers.
int get_max_channels
get_peers
Returns the list of peers associated with this host.
Note: This list might include some peers that are not fully connected or are still being disconnected.
ENetPacketPeer[] get_peers
pop_statistic(int)
Returns and resets host statistics. See HostStatistic for more info.
float pop_statistic(int statistic)
Parameters
statistic
int
refuse_new_connections(bool)
Configures the DTLS server to automatically drop new connections.
Note: This method is only relevant after calling ENetConnection.dtls_server_setup.
void refuse_new_connections(bool refuse)
Parameters
refuse
bool
service(int)
Waits for events on this connection and shuttles packets between the host and its peers, with the given timeout
(in milliseconds). The returned Array will have 4 elements. An EventType, the ENetPacketPeer which generated the event, the event associated data (if any), the event associated channel (if any). If the generated event is ENetConnection.EVENT_RECEIVE, the received packet will be queued to the associated ENetPacketPeer.
Call this function regularly to handle connections, disconnections, and to receive new packets.
Note: This method must be called on both ends involved in the event (sending and receiving hosts).
Array service(int timeout)
Parameters
timeout
int
socket_send(String, int, PackedByteArray)
Sends a packet
toward a destination from the address and port currently bound by this ENetConnection instance.
This 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.
This 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 STUN 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.
void socket_send(String destination_address, int destination_port, PackedByteArray packet)
Parameters
destination_address
Stringdestination_port
intpacket
PackedByteArray