Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
ENetConnection
繼承: RefCounted < Object
ENetHost 的包裝類。
說明
ENet 的目的是在 UDP(使用者封包通訊協定)之上,提供一個相對輕便、簡單和健壯的網路通信層。
教學
方法
void |
bandwidth_limit(in_bandwidth: int = 0, out_bandwidth: int = 0) |
void |
broadcast(channel: int, packet: PackedByteArray, flags: int) |
void |
channel_limit(limit: int) |
void |
compress(mode: CompressionMode) |
connect_to_host(address: String, port: int, channels: int = 0, data: int = 0) |
|
create_host(max_peers: int = 32, max_channels: int = 0, in_bandwidth: int = 0, out_bandwidth: int = 0) |
|
create_host_bound(bind_address: String, bind_port: int, max_peers: int = 32, max_channels: int = 0, in_bandwidth: int = 0, out_bandwidth: int = 0) |
|
void |
destroy() |
dtls_client_setup(hostname: String, client_options: TLSOptions = null) |
|
dtls_server_setup(server_options: TLSOptions) |
|
void |
flush() |
get_local_port() const |
|
get_max_channels() const |
|
pop_statistic(statistic: HostStatistic) |
|
void |
refuse_new_connections(refuse: bool) |
void |
socket_send(destination_address: String, destination_port: int, packet: PackedByteArray) |
列舉
enum CompressionMode: 🔗
CompressionMode COMPRESS_NONE = 0
無壓縮。這使用最多的頻寬,但具有佔用最少 CPU 資源的好處。這個選項可以用於 Wireshark 等工具使用,更容易進行網路除錯。
CompressionMode COMPRESS_RANGE_CODER = 1
ENet 的內建範圍編碼。適用於小封包,但對於大於 4 KB 的封包不是最有效的算法。
CompressionMode COMPRESS_FASTLZ = 2
FastLZ 壓縮。與 COMPRESS_ZLIB 相比,此選項使用的 CPU 資源更少,代價是使用更多的頻寬。
CompressionMode COMPRESS_ZLIB = 3
Zlib 壓縮。與 COMPRESS_FASTLZ 相比,此選項使用的頻寬更少,代價是使用更多的 CPU 資源。
CompressionMode COMPRESS_ZSTD = 4
Zstandard 壓縮。請注意,此演算法對小於 4 KB 的封包效率不高。因此,建議在大多數情況下使用其他壓縮演算法。
enum EventType: 🔗
EventType EVENT_ERROR = -1
service() 期間發生錯誤。你可能需要 destroy() 主機並重新建立。
EventType EVENT_NONE = 0
在指定的時間限制內沒有事件發生。
EventType EVENT_CONNECT = 1
由 enet_host_connect 發起的連接請求已完成。該陣列將包含成功連接的對等體。
EventType EVENT_DISCONNECT = 2
對等體已斷開連接。如果對等體超時,或者由 connect_to_host() 初始化的連接請求超時,則在由 ENetPacketPeer.peer_disconnect() 發起的斷開連接成功完成時,生成該事件。該陣列將包含斷開連接的對等體。資料欄位包含使用者提供的描述斷開連接的資料,如果沒有可用的資料,則為 0。
EventType EVENT_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 ENetPacketPeer.
enum HostStatistic: 🔗
HostStatistic HOST_TOTAL_SENT_DATA = 0
發送資料的總數。
HostStatistic HOST_TOTAL_SENT_PACKETS = 1
發送 UDP 封包的總數。
HostStatistic HOST_TOTAL_RECEIVED_DATA = 2
接收資料的總數。
HostStatistic HOST_TOTAL_RECEIVED_PACKETS = 3
接收 UDP 封包的總數。
方法說明
void bandwidth_limit(in_bandwidth: int = 0, out_bandwidth: int = 0) 🔗
調整主機的頻寬限制。
void broadcast(channel: int, packet: PackedByteArray, flags: int) 🔗
將一個 packet 加入佇列,以便將其通過指定的 channel 發送到與主機關聯的所有對等體。請參閱 ENetPacketPeer 中的 FLAG_* 常數以了解可用的封包旗標。
void channel_limit(limit: int) 🔗
限制未來傳入連接的最大允許通道數。
void compress(mode: CompressionMode) 🔗
設定用於網路封包的壓縮方法。這些在壓縮速度與頻寬之間有不同的權衡,如果需要使用壓縮,可能需要測試哪一種最適合你的用例。
注意:大多數遊戲的網路設計,都涉及頻繁發送許多小封包(每個小於 4 KB)。如果有疑問,建議保留預設壓縮演算法,因為它最適合這些小封包。
注意:壓縮模式必須在服務端及其所有使用者端上設定為相同的值。如果使用者端上設定的壓縮模式與服務端上設定的不同,則使用者端將無法連接。
ENetPacketPeer connect_to_host(address: String, port: int, channels: int = 0, data: int = 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 create_host() or create_host_bound() on both ends before calling this method.
Error create_host(max_peers: int = 32, max_channels: int = 0, in_bandwidth: int = 0, out_bandwidth: int = 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 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.
Error create_host_bound(bind_address: String, bind_port: int, max_peers: int = 32, max_channels: int = 0, in_bandwidth: int = 0, out_bandwidth: int = 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.
void destroy() 🔗
銷毀主機和與其關聯的所有資源。
Error dtls_client_setup(hostname: String, client_options: TLSOptions = null) 🔗
配置此 ENetHost 以使用允許對 ENet 使用者端進行 DTLS 加密的自訂 Godot 擴充。在 connect_to_host() 之前呼叫它,讓 ENet 連接使用 DTLS 根據 hostname 驗證伺服器憑證。可以通過可選的 client_options 參數來自訂受信任的憑證授權,或禁用通用名稱驗證。見 TLSOptions.client() 和 TLSOptions.client_unsafe()。
Error dtls_server_setup(server_options: TLSOptions) 🔗
配置該 ENetHost 以使用允許對 ENet 伺服器進行 DTLS 加密的自訂 Godot 擴充。在 create_host_bound() 之後立即呼叫該方法,以讓 ENet 期望對等體使用 DTLS 進行連接。請參閱 TLSOptions.server()。
void flush() 🔗
將指定主機上所有被佇列的封包發送到其指定的對等體。
返回該對等體綁定到的本地埠。
int get_max_channels() const 🔗
返回連接的對等體所允許的最大通道數。
Array[ENetPacketPeer] get_peers() 🔗
返回與該主機關聯的對等體列表。
注意:該列表可能包含一些未完全連接或仍在斷開連接的對等體。
float pop_statistic(statistic: HostStatistic) 🔗
Returns and resets host statistics.
void refuse_new_connections(refuse: bool) 🔗
將 DTLS 服務端配置為自動斷開新連接。
注意:這個方法只有在呼叫了 dtls_server_setup() 後才有用。
Array service(timeout: int = 0) 🔗
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 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).
void socket_send(destination_address: String, destination_port: int, packet: 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.