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.

StreamPeer

繼承: RefCounted < Object

被繼承: StreamPeerBuffer, StreamPeerExtension, StreamPeerGZIP, StreamPeerSocket, StreamPeerTLS

與流互動的抽象基底類別。

說明

StreamPeer 是一種抽象基底類別,常用於流式協議(例如 TCP)。它提供了通過流發送資料的 API,將資料作為原始資料或字串處理。

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

屬性

bool

big_endian

false

方法

int

get_8()

int

get_16()

int

get_32()

int

get_64()

int

get_available_bytes() const

Array

get_data(bytes: int)

float

get_double()

float

get_float()

float

get_half()

Array

get_partial_data(bytes: int)

String

get_string(bytes: int = -1)

int

get_u8()

int

get_u16()

int

get_u32()

int

get_u64()

String

get_utf8_string(bytes: int = -1)

Variant

get_var(allow_objects: bool = false)

void

put_8(value: int)

void

put_16(value: int)

void

put_32(value: int)

void

put_64(value: int)

Error

put_data(data: PackedByteArray)

void

put_double(value: float)

void

put_float(value: float)

void

put_half(value: float)

Array

put_partial_data(data: PackedByteArray)

void

put_string(value: String)

void

put_u8(value: int)

void

put_u16(value: int)

void

put_u32(value: int)

void

put_u64(value: int)

void

put_utf8_string(value: String)

void

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


屬性說明

bool big_endian = false 🔗

  • void set_big_endian(value: bool)

  • bool is_big_endian_enabled()

true 時,該 StreamPeer 進行編解碼時會使用大端格式。


方法說明

int get_8() 🔗

從流中獲取有符號位元組。


int get_16() 🔗

從流中獲取有符號 16 位元值。


int get_32() 🔗

從流中獲取有符號 32 位元值。


int get_64() 🔗

從流中獲取有符號 64 位元值。


int get_available_bytes() const 🔗

返回該 StreamPeer 可用的位元組數。


Array get_data(bytes: int) 🔗

Returns a chunk data with the received bytes, as an Array containing two elements: an Error constant and a PackedByteArray. bytes is the number of bytes to be received. If not enough bytes are available, the function will block until the desired amount is received.


float get_double() 🔗

從流中獲取一個雙精度浮點數。


float get_float() 🔗

從流中獲取一個單精確度浮點數。


float get_half() 🔗

Gets a half-precision float from the stream.


Array get_partial_data(bytes: int) 🔗

Returns a chunk data with the received bytes, as an Array containing two elements: an Error constant and a PackedByteArray. bytes is the number of bytes to be received. If not enough bytes are available, the function will return how many were actually received.


String get_string(bytes: int = -1) 🔗

從流中獲取一個位元組長度為 bytes 的 ASCII 字串。如果 bytes 為負(預設),會按照 put_string() 的逆向操作從流中讀取長度。


int get_u8() 🔗

從流中獲取一個無符號位元組。


int get_u16() 🔗

從流中獲取一個無符號 16 位元值。


int get_u32() 🔗

從流中獲取一個無符號 32 位元值。


int get_u64() 🔗

從流中獲取一個無符號 64 位元值。


String get_utf8_string(bytes: int = -1) 🔗

從流中獲取一個位元組長度為 bytes 的 UTF-8 字串(將發送的字串解碼為 UTF-8)。如果 bytes 為負(預設),會按照 put_utf8_string() 的逆向操作從流中讀取長度。


Variant get_var(allow_objects: bool = false) 🔗

從流中獲取一個 Variant。如果 allow_objectstrue,則會允許解碼出物件。

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

警告:反序列化的物件可能包含會被執行的程式碼。如果序列化的物件來自不可信的來源,請勿使用該選項,以免造成遠端程式碼執行等安全威脅。


void put_8(value: int) 🔗

向流中放入一個有符號位元組。


void put_16(value: int) 🔗

向流中放入一個有符號 16 位元值。


void put_32(value: int) 🔗

向流中放入一個有符號 32 位元值。


void put_64(value: int) 🔗

向流中放入一個有符號 64 位元值。


Error put_data(data: PackedByteArray) 🔗

通過連接發送塊資料,資料完成發送前會阻塞。該函式返回 Error 錯誤碼。


void put_double(value: float) 🔗

向流中放入一個雙精度浮點數。


void put_float(value: float) 🔗

向流中放入一個單精確度浮點數。


void put_half(value: float) 🔗

Puts a half-precision float into the stream.


Array put_partial_data(data: PackedByteArray) 🔗

通過連接發送資料。如果資料無法一次性發完,則僅會發送部分資料。該函式返回兩個值,一個 Error 錯誤碼以及一個整數,表示實際發送的資料量。


void put_string(value: String) 🔗

向流中放入一個以零結尾的 ASCII 字串,會前置一個表示其大小的 32 位元無符號整數。

注意:如果要放置 ASCII 字串,而不前置大小,可以使用 put_data()

put_data("Hello world".to_ascii_buffer())

void put_u8(value: int) 🔗

向流中放入一個無符號位元組。


void put_u16(value: int) 🔗

向流中放入一個無符號 16 位元值。


void put_u32(value: int) 🔗

向流中放入一個無符號 32 位元值。


void put_u64(value: int) 🔗

向流中放入一個無符號 64 位元值。


void put_utf8_string(value: String) 🔗

向流中放入一個以零結尾的 UTF-8 字串,前置一個表示其大小的 32 位元無符號整數。

注意:如果要放置 UTF-8 字串,而不前置其大小,可以使用 put_data()

put_data("Hello world".to_utf8_buffer())

void put_var(value: Variant, full_objects: bool = false) 🔗

向流中放入一個 Variant。如果 full_objectstrue,則會允許將物件編碼(其中可能包含程式碼)。

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