Files
godot-docs-l10n/classes/zh_Hant/class_streampeer.rst
Rémi Verschelde c3f2364c10 Sync classref with 4.6 branch
Lots of translations invalidated (fuzzied) as we just synced Weblate.
2025-12-19 16:39:51 +01:00

579 lines
26 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

:github_url: hide
.. _class_StreamPeer:
StreamPeer
==========
**繼承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
**被繼承:** :ref:`StreamPeerBuffer<class_StreamPeerBuffer>`, :ref:`StreamPeerExtension<class_StreamPeerExtension>`, :ref:`StreamPeerGZIP<class_StreamPeerGZIP>`, :ref:`StreamPeerSocket<class_StreamPeerSocket>`, :ref:`StreamPeerTLS<class_StreamPeerTLS>`
與流互動的抽象基底類別。
.. rst-class:: classref-introduction-group
說明
----
StreamPeer 是一種抽象基底類別,常用於流式協議(例如 TCP。它提供了通過流發送資料的 API將資料作為原始資料或字串處理。
\ **注意:**\ 匯出到安卓時,在匯出專案或使用一鍵部署之前,請務必在安卓匯出預設中,開啟 ``INTERNET`` 許可權。否則,任何型別的網路通信都將被 Android 阻止。
.. rst-class:: classref-reftable-group
屬性
----
.. table::
:widths: auto
+-------------------------+---------------------------------------------------------+-----------+
| :ref:`bool<class_bool>` | :ref:`big_endian<class_StreamPeer_property_big_endian>` | ``false`` |
+-------------------------+---------------------------------------------------------+-----------+
.. rst-class:: classref-reftable-group
方法
----
.. table::
:widths: auto
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_8<class_StreamPeer_method_get_8>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_16<class_StreamPeer_method_get_16>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_32<class_StreamPeer_method_get_32>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_64<class_StreamPeer_method_get_64>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_available_bytes<class_StreamPeer_method_get_available_bytes>`\ (\ ) |const| |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`get_data<class_StreamPeer_method_get_data>`\ (\ bytes\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`get_double<class_StreamPeer_method_get_double>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`get_float<class_StreamPeer_method_get_float>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`float<class_float>` | :ref:`get_half<class_StreamPeer_method_get_half>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`get_partial_data<class_StreamPeer_method_get_partial_data>`\ (\ bytes\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`String<class_String>` | :ref:`get_string<class_StreamPeer_method_get_string>`\ (\ bytes\: :ref:`int<class_int>` = -1\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_u8<class_StreamPeer_method_get_u8>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_u16<class_StreamPeer_method_get_u16>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_u32<class_StreamPeer_method_get_u32>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_u64<class_StreamPeer_method_get_u64>`\ (\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`String<class_String>` | :ref:`get_utf8_string<class_StreamPeer_method_get_utf8_string>`\ (\ bytes\: :ref:`int<class_int>` = -1\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`get_var<class_StreamPeer_method_get_var>`\ (\ allow_objects\: :ref:`bool<class_bool>` = false\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_8<class_StreamPeer_method_put_8>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_16<class_StreamPeer_method_put_16>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_32<class_StreamPeer_method_put_32>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_64<class_StreamPeer_method_put_64>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_data<class_StreamPeer_method_put_data>`\ (\ data\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_double<class_StreamPeer_method_put_double>`\ (\ value\: :ref:`float<class_float>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_float<class_StreamPeer_method_put_float>`\ (\ value\: :ref:`float<class_float>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_half<class_StreamPeer_method_put_half>`\ (\ value\: :ref:`float<class_float>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Array<class_Array>` | :ref:`put_partial_data<class_StreamPeer_method_put_partial_data>`\ (\ data\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_string<class_StreamPeer_method_put_string>`\ (\ value\: :ref:`String<class_String>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_u8<class_StreamPeer_method_put_u8>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_u16<class_StreamPeer_method_put_u16>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_u32<class_StreamPeer_method_put_u32>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_u64<class_StreamPeer_method_put_u64>`\ (\ value\: :ref:`int<class_int>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_utf8_string<class_StreamPeer_method_put_utf8_string>`\ (\ value\: :ref:`String<class_String>`\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| |void| | :ref:`put_var<class_StreamPeer_method_put_var>`\ (\ value\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) |
+---------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
屬性說明
--------
.. _class_StreamPeer_property_big_endian:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **big_endian** = ``false`` :ref:`🔗<class_StreamPeer_property_big_endian>`
.. rst-class:: classref-property-setget
- |void| **set_big_endian**\ (\ value\: :ref:`bool<class_bool>`\ )
- :ref:`bool<class_bool>` **is_big_endian_enabled**\ (\ )
``true`` 時,該 **StreamPeer** 進行編解碼時會使用大端格式。
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
方法說明
--------
.. _class_StreamPeer_method_get_8:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_8**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_8>`
從流中獲取有符號位元組。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_16:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_16**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_16>`
從流中獲取有符號 16 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_32:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_32**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_32>`
從流中獲取有符號 32 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_64:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_64**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_64>`
從流中獲取有符號 64 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_available_bytes:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_available_bytes**\ (\ ) |const| :ref:`🔗<class_StreamPeer_method_get_available_bytes>`
返回該 **StreamPeer** 可用的位元組數。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_data:
.. rst-class:: classref-method
:ref:`Array<class_Array>` **get_data**\ (\ bytes\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_get_data>`
Returns a chunk data with the received bytes, as an :ref:`Array<class_Array>` containing two elements: an :ref:`Error<enum_@GlobalScope_Error>` constant and a :ref:`PackedByteArray<class_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.
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_double:
.. rst-class:: classref-method
:ref:`float<class_float>` **get_double**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_double>`
從流中獲取一個雙精度浮點數。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_float:
.. rst-class:: classref-method
:ref:`float<class_float>` **get_float**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_float>`
從流中獲取一個單精確度浮點數。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_half:
.. rst-class:: classref-method
:ref:`float<class_float>` **get_half**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_half>`
Gets a half-precision float from the stream.
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_partial_data:
.. rst-class:: classref-method
:ref:`Array<class_Array>` **get_partial_data**\ (\ bytes\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_get_partial_data>`
Returns a chunk data with the received bytes, as an :ref:`Array<class_Array>` containing two elements: an :ref:`Error<enum_@GlobalScope_Error>` constant and a :ref:`PackedByteArray<class_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.
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_string:
.. rst-class:: classref-method
:ref:`String<class_String>` **get_string**\ (\ bytes\: :ref:`int<class_int>` = -1\ ) :ref:`🔗<class_StreamPeer_method_get_string>`
從流中獲取一個位元組長度為 ``bytes`` 的 ASCII 字串。如果 ``bytes`` 為負(預設),會按照 :ref:`put_string()<class_StreamPeer_method_put_string>` 的逆向操作從流中讀取長度。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_u8:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_u8**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_u8>`
從流中獲取一個無符號位元組。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_u16:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_u16**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_u16>`
從流中獲取一個無符號 16 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_u32:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_u32**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_u32>`
從流中獲取一個無符號 32 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_u64:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_u64**\ (\ ) :ref:`🔗<class_StreamPeer_method_get_u64>`
從流中獲取一個無符號 64 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_utf8_string:
.. rst-class:: classref-method
:ref:`String<class_String>` **get_utf8_string**\ (\ bytes\: :ref:`int<class_int>` = -1\ ) :ref:`🔗<class_StreamPeer_method_get_utf8_string>`
從流中獲取一個位元組長度為 ``bytes`` 的 UTF-8 字串(將發送的字串解碼為 UTF-8。如果 ``bytes`` 為負(預設),會按照 :ref:`put_utf8_string()<class_StreamPeer_method_put_utf8_string>` 的逆向操作從流中讀取長度。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_get_var:
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` **get_var**\ (\ allow_objects\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_StreamPeer_method_get_var>`
從流中獲取一個 Variant。如果 ``allow_objects````true``\ ,則會允許解碼出物件。
內部實作時,使用的解碼機制與 :ref:`@GlobalScope.bytes_to_var()<class_@GlobalScope_method_bytes_to_var>` 方法相同。
\ **警告:**\ 反序列化的物件可能包含會被執行的程式碼。如果序列化的物件來自不可信的來源,請勿使用該選項,以免造成遠端程式碼執行等安全威脅。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_8:
.. rst-class:: classref-method
|void| **put_8**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_8>`
向流中放入一個有符號位元組。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_16:
.. rst-class:: classref-method
|void| **put_16**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_16>`
向流中放入一個有符號 16 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_32:
.. rst-class:: classref-method
|void| **put_32**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_32>`
向流中放入一個有符號 32 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_64:
.. rst-class:: classref-method
|void| **put_64**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_64>`
向流中放入一個有符號 64 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_data:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **put_data**\ (\ data\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_StreamPeer_method_put_data>`
通過連接發送塊資料,資料完成發送前會阻塞。該函式返回 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_double:
.. rst-class:: classref-method
|void| **put_double**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_StreamPeer_method_put_double>`
向流中放入一個雙精度浮點數。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_float:
.. rst-class:: classref-method
|void| **put_float**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_StreamPeer_method_put_float>`
向流中放入一個單精確度浮點數。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_half:
.. rst-class:: classref-method
|void| **put_half**\ (\ value\: :ref:`float<class_float>`\ ) :ref:`🔗<class_StreamPeer_method_put_half>`
Puts a half-precision float into the stream.
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_partial_data:
.. rst-class:: classref-method
:ref:`Array<class_Array>` **put_partial_data**\ (\ data\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_StreamPeer_method_put_partial_data>`
通過連接發送資料。如果資料無法一次性發完,則僅會發送部分資料。該函式返回兩個值,一個 :ref:`Error<enum_@GlobalScope_Error>` 錯誤碼以及一個整數,表示實際發送的資料量。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_string:
.. rst-class:: classref-method
|void| **put_string**\ (\ value\: :ref:`String<class_String>`\ ) :ref:`🔗<class_StreamPeer_method_put_string>`
向流中放入一個以零結尾的 ASCII 字串,會前置一個表示其大小的 32 位元無符號整數。
\ **注意:**\ 如果要放置 ASCII 字串,而不前置大小,可以使用 :ref:`put_data()<class_StreamPeer_method_put_data>`\
.. tabs::
.. code-tab:: gdscript
put_data("Hello world".to_ascii_buffer())
.. code-tab:: csharp
PutData("Hello World".ToAsciiBuffer());
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_u8:
.. rst-class:: classref-method
|void| **put_u8**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_u8>`
向流中放入一個無符號位元組。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_u16:
.. rst-class:: classref-method
|void| **put_u16**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_u16>`
向流中放入一個無符號 16 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_u32:
.. rst-class:: classref-method
|void| **put_u32**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_u32>`
向流中放入一個無符號 32 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_u64:
.. rst-class:: classref-method
|void| **put_u64**\ (\ value\: :ref:`int<class_int>`\ ) :ref:`🔗<class_StreamPeer_method_put_u64>`
向流中放入一個無符號 64 位元值。
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_utf8_string:
.. rst-class:: classref-method
|void| **put_utf8_string**\ (\ value\: :ref:`String<class_String>`\ ) :ref:`🔗<class_StreamPeer_method_put_utf8_string>`
向流中放入一個以零結尾的 UTF-8 字串,前置一個表示其大小的 32 位元無符號整數。
\ **注意:**\ 如果要放置 UTF-8 字串,而不前置其大小,可以使用 :ref:`put_data()<class_StreamPeer_method_put_data>`\
.. tabs::
.. code-tab:: gdscript
put_data("Hello world".to_utf8_buffer())
.. code-tab:: csharp
PutData("Hello World".ToUtf8Buffer());
.. rst-class:: classref-item-separator
----
.. _class_StreamPeer_method_put_var:
.. rst-class:: classref-method
|void| **put_var**\ (\ value\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_StreamPeer_method_put_var>`
向流中放入一個 Variant。如果 ``full_objects````true``\ ,則會允許將物件編碼(其中可能包含程式碼)。
內部實作時,使用的編碼機制與 :ref:`@GlobalScope.var_to_bytes()<class_@GlobalScope_method_var_to_bytes>` 方法相同。
.. |virtual| replace:: :abbr:`virtual (本方法通常需要使用者覆寫才能生效。)`
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
.. |const| replace:: :abbr:`const (本方法沒有副作用。不會修改該實例的任何成員變數。)`
.. |vararg| replace:: :abbr:`vararg (本方法除了這裡描述的參數外,還可以接受任意數量的參數。)`
.. |constructor| replace:: :abbr:`constructor (本方法用於建構一個型別。)`
.. |static| replace:: :abbr:`static (本方法無需實例即可呼叫,因此可以直接使用類別名稱呼叫。)`
.. |operator| replace:: :abbr:`operator (本方法描述將本型別作為左運算元時可用的有效運算子。)`
.. |bitfield| replace:: :abbr:`BitField (此值是由下列旗標組成的位元遮罩整數。)`
.. |void| replace:: :abbr:`void (無回傳值。)`