ENetConnection

package
v0.0.0-...-fa94a0d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsENetConnection() Instance
}

type CompressionMode

type CompressionMode int //gd:ENetConnection.CompressionMode
const (
	// 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.
	CompressNone CompressionMode = 0
	// ENet's built-in range encoding. Works well on small packets, but is not the most efficient algorithm on packets larger than 4 KB.
	CompressRangeCoder CompressionMode = 1
	// [FastLZ] compression. This option uses less CPU resources compared to [CompressZlib], at the expense of using more bandwidth.
	//
	// [FastLZ]: https://fastlz.org/
	CompressFastlz CompressionMode = 2
	// [Zlib] compression. This option uses less bandwidth compared to [CompressFastlz], at the expense of using more CPU resources.
	//
	// [Zlib]: https://www.zlib.net/
	CompressZlib CompressionMode = 3
	// [Zstandard] 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.
	//
	// [Zstandard]: https://facebook.github.io/zstd/
	CompressZstd CompressionMode = 4
)

type EventType

type EventType int //gd:ENetConnection.EventType
const (
	// An error occurred during [Service]. You will likely need to [Destroy] the host and recreate it.
	//
	// [Destroy]: https://pkg.go.dev/graphics.gd/classdb/#Instance.Destroy
	// [Service]: https://pkg.go.dev/graphics.gd/classdb/#Instance.Service
	EventError EventType = -1
	// No event occurred within the specified time limit.
	EventNone EventType = 0
	// A connection request initiated by enet_host_connect has completed. The array will contain the peer which successfully connected.
	EventConnect EventType = 1
	// A peer has disconnected. This event is generated on a successful completion of a disconnect initiated by [ENetPacketPeer.PeerDisconnect], if a peer has timed out, or if a connection request initialized by [ConnectToHost] 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.
	//
	// [ConnectToHost]: https://pkg.go.dev/graphics.gd/classdb/#Instance.ConnectToHost
	// [ENetPacketPeer.PeerDisconnect]: https://pkg.go.dev/graphics.gd/classdb/ENetPacketPeer#Instance.PeerDisconnect
	EventDisconnect EventType = 2
	// 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 [ENetPacketPeer].
	//
	// [ENetPacketPeer]: https://pkg.go.dev/graphics.gd/classdb/ENetPacketPeer
	EventReceive EventType = 3
)

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsENetConnection

func (self *Extension[T]) AsENetConnection() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type HostStatistic

type HostStatistic int //gd:ENetConnection.HostStatistic
const (
	// Total data sent.
	HostTotalSentData HostStatistic = 0
	// Total UDP packets sent.
	HostTotalSentPackets HostStatistic = 1
	// Total data received.
	HostTotalReceivedData HostStatistic = 2
	// Total UDP packets received.
	HostTotalReceivedPackets HostStatistic = 3
)

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.ENetConnection

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsENetConnection

func (self Instance) AsENetConnection() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) BandwidthLimit

func (self Instance) BandwidthLimit()

Adjusts the bandwidth limits of a host.

func (Instance) Broadcast

func (self Instance) Broadcast(channel int, packet []byte, flags 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.

func (Instance) ChannelLimit

func (self Instance) ChannelLimit(limit int)

Limits the maximum allowed channels of future incoming connections.

func (Instance) Compress

func (self Instance) Compress(mode 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.

func (Instance) ConnectToHost

func (self Instance) ConnectToHost(address string, port int) ENetPacketPeer.Instance

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 or CreateHostBound on both ends before calling this method.

func (Instance) CreateHost

func (self Instance) CreateHost() error

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 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.

func (Instance) CreateHostBound

func (self Instance) CreateHostBound(bind_address string, bind_port int) error

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.

func (Instance) Destroy

func (self Instance) Destroy()

Destroys the host and all resources associated with it.

func (Instance) DtlsClientSetup

func (self Instance) DtlsClientSetup(hostname string) error

Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before 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.

func (Instance) DtlsServerSetup

func (self Instance) DtlsServerSetup(server_options TLSOptions.Instance) error

Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet servers. Call this right after CreateHostBound to have ENet expect peers to connect using DTLS. See TLSOptions.Server.

func (Instance) Flush

func (self Instance) Flush()

Sends any queued packets on the host specified to its designated peers.

func (Instance) GetLocalPort

func (self Instance) GetLocalPort() int

Returns the local port to which this peer is bound.

func (Instance) GetMaxChannels

func (self Instance) GetMaxChannels() int

Returns the maximum number of channels allowed for connected peers.

func (Instance) GetPeers

func (self Instance) GetPeers() []ENetPacketPeer.Instance

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.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (Instance) PopStatistic

func (self Instance) PopStatistic(statistic HostStatistic) Float.X

Returns and resets host statistics.

func (Instance) RefuseNewConnections

func (self Instance) RefuseNewConnections(refuse bool)

Configures the DTLS server to automatically drop new connections.

Note: This method is only relevant after calling DtlsServerSetup.

func (Instance) Service

func (self Instance) Service() (EventType, ENetPacketPeer.Instance, int, int)

Waits for events on this connection and shuttles packets between the host and its peers, with the given 'timeout' (in milliseconds). The returned slice 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 EventReceive, 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).

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SocketSend

func (self Instance) SocketSend(destination_address string, destination_port int, packet []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.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type MoreArgs

type MoreArgs [1]gdclass.ENetConnection

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) BandwidthLimit

func (self MoreArgs) BandwidthLimit(in_bandwidth int, out_bandwidth int)

Adjusts the bandwidth limits of a host.

func (MoreArgs) ConnectToHost

func (self MoreArgs) ConnectToHost(address string, port int, channels int, data int) ENetPacketPeer.Instance

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 or CreateHostBound on both ends before calling this method.

func (MoreArgs) CreateHost

func (self MoreArgs) CreateHost(max_peers int, max_channels int, in_bandwidth int, out_bandwidth int) error

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 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.

func (MoreArgs) CreateHostBound

func (self MoreArgs) CreateHostBound(bind_address string, bind_port int, max_peers int, max_channels int, in_bandwidth int, out_bandwidth int) error

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.

func (MoreArgs) DtlsClientSetup

func (self MoreArgs) DtlsClientSetup(hostname string, client_options TLSOptions.Instance) error

Configure this ENetHost to use the custom Godot extension allowing DTLS encryption for ENet clients. Call this before 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.

func (MoreArgs) Service

func (self MoreArgs) Service(timeout int) (EventType, ENetPacketPeer.Instance, int, int)

Waits for events on this connection and shuttles packets between the host and its peers, with the given 'timeout' (in milliseconds). The returned slice 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 EventReceive, 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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL