ENetConnection
A wrapper class for an ENetHost(http://enet.bespin.org/group__host.html).
A wrapper class for an ENetHost(http://enet.bespin.org/group__host.html).
ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
ENetConnectionConstructor
public ENetConnection();Methods20
BandwidthLimitvoid
public void BandwidthLimit(int inBandwidth = 0, int outBandwidth = 0);Adjusts the bandwidth limits of a host.
| Name | Type | Default |
|---|---|---|
inBandwidth | int | 0 |
outBandwidth | int | 0 |
Broadcast2 overloads
public void Broadcast(int channel, byte[] packet, int flags);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.
| Name | Type | Default |
|---|---|---|
channel | int | — |
packet | byte[] | — |
flags | int | — |
public void Broadcast(int channel, ReadOnlySpan<byte> packet, int flags);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.
| Name | Type | Default |
|---|---|---|
channel | int | — |
packet | ReadOnlySpan<byte> | — |
flags | int | — |
ChannelLimitvoid
public void ChannelLimit(int limit);Limits the maximum allowed channels of future incoming connections.
| Name | Type | Default |
|---|---|---|
limit | int | — |
Compressvoid
public void Compress(ENetConnection.CompressionMode mode);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.
| Name | Type | Default |
|---|---|---|
mode | ENetConnection.CompressionMode | — |
ConnectToHostENetPacketPeer
public ENetPacketPeer ConnectToHost(string address, int port, int channels = 0, int data = 0);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.CreateHost or ENetConnection.CreateHostBound on both ends before calling this method.
| Name | Type | Default |
|---|---|---|
address | string | — |
port | int | — |
channels | int | 0 |
data | int | 0 |
ENetPacketPeerCreateHostError
public Error CreateHost(int maxPeers = 32, int maxChannels = 0, int inBandwidth = 0, int outBandwidth = 0);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.CreateHostBound 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.
| Name | Type | Default |
|---|---|---|
maxPeers | int | 32 |
maxChannels | int | 0 |
inBandwidth | int | 0 |
outBandwidth | int | 0 |
ErrorCreateHostBoundError
public Error CreateHostBound(string bindAddress, int bindPort, int maxPeers = 32, int maxChannels = 0, int inBandwidth = 0, int outBandwidth = 0);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.
| Name | Type | Default |
|---|---|---|
bindAddress | string | — |
bindPort | int | — |
maxPeers | int | 32 |
maxChannels | int | 0 |
inBandwidth | int | 0 |
outBandwidth | int | 0 |
ErrorDestroyvoid
public void Destroy();Destroys the host and all resources associated with it.
DtlsClientSetupError
public Error DtlsClientSetup(string hostname, TlsOptions clientOptions = null);Configure this ENetHost to use the custom Reckfell extension allowing DTLS encryption for ENet clients. Call this before ENetConnection.ConnectToHost 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.ClientUnsafe.
| Name | Type | Default |
|---|---|---|
hostname | string | — |
clientOptions | TlsOptions | null |
ErrorDtlsServerSetupError
public Error DtlsServerSetup(TlsOptions serverOptions);Configure this ENetHost to use the custom Reckfell extension allowing DTLS encryption for ENet servers. Call this right after ENetConnection.CreateHostBound to have ENet expect peers to connect using DTLS. See TlsOptions.Server.
| Name | Type | Default |
|---|---|---|
serverOptions | TlsOptions | — |
ErrorFlushvoid
public void Flush();Sends any queued packets on the host specified to its designated peers.
GetLocalPortint
public int GetLocalPort();Returns the local port to which this peer is bound.
intGetMaxChannelsint
public int GetMaxChannels();Returns the maximum number of channels allowed for connected peers.
intGetPeersReckfell.Collections.Array<ENetPacketPeer>
public Reckfell.Collections.Array<ENetPacketPeer> 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.
Reckfell.Collections.Array<ENetPacketPeer>PopStatisticdouble
public double PopStatistic(ENetConnection.HostStatistic statistic);Returns and resets host statistics.
| Name | Type | Default |
|---|---|---|
statistic | ENetConnection.HostStatistic | — |
doubleRefuseNewConnectionsvoid
public void RefuseNewConnections(bool refuse);Configures the DTLS server to automatically drop new connections.
Note: This method is only relevant after calling ENetConnection.DtlsServerSetup.
| Name | Type | Default |
|---|---|---|
refuse | bool | — |
ServiceReckfell.Collections.Array
public Reckfell.Collections.Array Service(int timeout = 0);Waits for events on this connection and shuttles packets between the host and its peers, with the given timeout (in milliseconds). The returned Reckfell.Collections.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 ENetConnection.EventType.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).
| Name | Type | Default |
|---|---|---|
timeout | int | 0 |
Reckfell.Collections.ArraySocketSend2 overloads
public void SocketSend(string destinationAddress, int destinationPort, byte[] packet);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(https://en.wikipedia.org/wiki/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.
| Name | Type | Default |
|---|---|---|
destinationAddress | string | — |
destinationPort | int | — |
packet | byte[] | — |
public void SocketSend(string destinationAddress, int destinationPort, ReadOnlySpan<byte> packet);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(https://en.wikipedia.org/wiki/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.
| Name | Type | Default |
|---|---|---|
destinationAddress | string | — |
destinationPort | int | — |
packet | ReadOnlySpan<byte> | — |
ENetConnection.CompressionMode
| Value | Description |
|---|---|
Fastlz = 2 | FastLZ(https://fastlz.org/) compression. This option uses less CPU resources compared to |
None = 0 | 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. |
RangeCoder = 1 | ENet's built-in range encoding. Works well on small packets, but is not the most efficient algorithm on packets larger than 4 KB. |
Zlib = 3 | Zlib(https://www.zlib.net/) compression. This option uses less bandwidth compared to |
Zstd = 4 | 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. |
ENetConnection.EventType
| Value | Description |
|---|---|
Connect = 1 | A connection request initiated by enet_host_connect has completed. The array will contain the peer which successfully connected. |
Disconnect = 2 | A peer has disconnected. This event is generated on a successful completion of a disconnect initiated by |
Error = -1 | An error occurred during |
None = 0 | No event occurred within the specified time limit. |
Receive = 3 | A 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 |
ENetConnection.HostStatistic
| Value | Description |
|---|---|
ReceivedData = 2 | Total data received. |
ReceivedPackets = 3 | Total UDP packets received. |
SentData = 0 | Total data sent. |
SentPackets = 1 | Total UDP packets sent. |