Class ENetConnection
- Namespace
- Godot
- Assembly
- GodotSharp.dll
ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).
public class ENetConnection : RefCounted, IDisposable
- Inheritance
-
ENetConnection
- Implements
- Inherited Members
Constructors
ENetConnection()
public ENetConnection()
Methods
BandwidthLimit(int, int)
Adjusts the bandwidth limits of a host.
public void BandwidthLimit(int inBandwidth = 0, int outBandwidth = 0)
Parameters
Broadcast(int, byte[], int)
Queues a packet
to be sent to all peers associated with the host over the specified channel
. See ENetPacketPeerFLAG_*
constants for available packet flags.
public void Broadcast(int channel, byte[] packet, int flags)
Parameters
ChannelLimit(int)
Limits the maximum allowed channels of future incoming connections.
public void ChannelLimit(int limit)
Parameters
limit
int
Compress(CompressionMode)
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.
public void Compress(ENetConnection.CompressionMode mode)
Parameters
ConnectToHost(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 CreateHost(int, int, int, int) or CreateHostBound(string, int, int, int, int, int) before calling this method.
public ENetPacketPeer ConnectToHost(string address, int port, int channels = 0, int data = 0)
Parameters
Returns
CreateHost(int, int, int, int)
Create an ENetHost that will allow up to maxPeers
connected peers, each allocating up to maxChannels
channels, optionally limiting bandwidth to inBandwidth
and outBandwidth
.
public Error CreateHost(int maxPeers = 32, int maxChannels = 0, int inBandwidth = 0, int outBandwidth = 0)
Parameters
Returns
CreateHostBound(string, int, int, int, int, int)
Create an ENetHost like CreateHost(int, int, int, int) which is also bound to the given bindAddress
and bindPort
.
public Error CreateHostBound(string bindAddress, int bindPort, int maxPeers = 32, int maxChannels = 0, int inBandwidth = 0, int outBandwidth = 0)
Parameters
Returns
Destroy()
Destroys the host and all resources associated with it.
public void Destroy()
DtlsClientSetup(string, TlsOptions)
Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before ConnectToHost(string, int, int, int) to have ENet connect using DTLS validating the server certificate against hostname
. You can pass the optional clientOptions
parameter to customize the trusted certification authorities, or disable the common name verification. See Client(X509Certificate, string) and ClientUnsafe(X509Certificate).
public Error DtlsClientSetup(string hostname, TlsOptions clientOptions = null)
Parameters
hostname
stringclientOptions
TlsOptions
Returns
DtlsServerSetup(TlsOptions)
Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet servers. Call this right after CreateHostBound(string, int, int, int, int, int) to have ENet expect peers to connect using DTLS. See Server(CryptoKey, X509Certificate).
public Error DtlsServerSetup(TlsOptions serverOptions)
Parameters
serverOptions
TlsOptions
Returns
Flush()
Sends any queued packets on the host specified to its designated peers.
public void Flush()
GetLocalPort()
Returns the local port to which this peer is bound.
public int GetLocalPort()
Returns
GetMaxChannels()
Returns the maximum number of channels allowed for connected peers.
public int GetMaxChannels()
Returns
GetPeers()
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.
public Array<ENetPacketPeer> GetPeers()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
PopStatistic(HostStatistic)
Returns and resets host statistics. See ENetConnection.HostStatistic for more info.
public double PopStatistic(ENetConnection.HostStatistic statistic)
Parameters
statistic
ENetConnection.HostStatistic
Returns
RefuseNewConnections(bool)
Configures the DTLS server to automatically drop new connections.
Note: This method is only relevant after calling DtlsServerSetup(TlsOptions).
public void RefuseNewConnections(bool refuse)
Parameters
refuse
bool
Service(int)
Waits for events on the host specified and shuttles packets between the host and its peers. The returned Array will have 4 elements. An ENetConnection.EventType, the ENetPacketPeer which generated the event, the event associated data (if any), the event associated channel (if any). If the generated event is Receive, the received packet will be queued to the associated ENetPacketPeer.
Call this function regularly to handle connections, disconnections, and to receive new packets.
public Array Service(int timeout = 0)
Parameters
timeout
int
Returns
SocketSend(string, int, byte[])
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.
public void SocketSend(string destinationAddress, int destinationPort, byte[] packet)