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.

PacketPeer

繼承: RefCounted < Object

被繼承: ENetPacketPeer, MultiplayerPeer, PacketPeerDTLS, PacketPeerExtension, PacketPeerStream, PacketPeerUDP, WebRTCDataChannel, WebSocketPeer

基於包的協議的抽象和基底類別。

說明

PacketPeer 是基於封包的協定(如 UDP)的抽象和基底類別。它提供了用於發送和接收數據包的 API,可以發送原始資料或變數。這使得在協定之間傳輸資料變得容易,不必將資料編碼為低級位元組或擔心網路排序問題。

注意:匯出到安卓時,在匯出專案、或使用一鍵部署之前,請務必在安卓匯出預設中,開啟 INTERNET 許可權。否則,任何型別的網路通信,都將被 Android 阻止。

屬性

int

encode_buffer_max_size

8388608

方法

int

get_available_packet_count() const

PackedByteArray

get_packet()

Error

get_packet_error() const

Variant

get_var(allow_objects: bool = false)

Error

put_packet(buffer: PackedByteArray)

Error

put_var(var: Variant, full_objects: bool = false)


屬性說明

int encode_buffer_max_size = 8388608 🔗

  • void set_encode_buffer_max_size(value: int)

  • int get_encode_buffer_max_size()

Maximum buffer size allowed when encoding Variants. Raise this value to support heavier memory allocations.

The put_var() method allocates memory on the stack, and the buffer used will grow automatically to the closest power of two to match the size of the Variant. If the Variant is bigger than encode_buffer_max_size, the method will error out with @GlobalScope.ERR_OUT_OF_MEMORY.


方法說明

int get_available_packet_count() const 🔗

返回環形緩衝區中目前可用的封包數。


PackedByteArray get_packet() 🔗

獲取原始封包。


Error get_packet_error() const 🔗

返回最後接收的封包的錯誤狀態(通過 get_packet()get_var())。


Variant get_var(allow_objects: bool = false) 🔗

獲取 Variant。如果 allow_objectstrue,則允許解碼出對象。

在內部,這使用與 @GlobalScope.bytes_to_var() 方法相同的解碼機制。

警告:反序列化出的物件可能包含會被執行的程式碼。如果序列化的物件來自不受信任的來源,請不要使用此選項,以避免潛在的安全威脅,例如遠端程式碼執行。


Error put_packet(buffer: PackedByteArray) 🔗

發送一個原始封包。


Error put_var(var: Variant, full_objects: bool = false) 🔗

Variant 作為封包發送。如果 full_objectstrue,則允許將物件編碼(其中可能包含程式碼)。

在內部,這使用與 @GlobalScope.var_to_bytes() 方法相同的編碼機制。