Files
godot-docs-l10n/classes/zh_Hant/class_packetpeer.rst

173 lines
8.7 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_PacketPeer:
PacketPeer
==========
**繼承:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
**被繼承:** :ref:`ENetPacketPeer<class_ENetPacketPeer>`, :ref:`MultiplayerPeer<class_MultiplayerPeer>`, :ref:`PacketPeerDTLS<class_PacketPeerDTLS>`, :ref:`PacketPeerExtension<class_PacketPeerExtension>`, :ref:`PacketPeerStream<class_PacketPeerStream>`, :ref:`PacketPeerUDP<class_PacketPeerUDP>`, :ref:`WebRTCDataChannel<class_WebRTCDataChannel>`, :ref:`WebSocketPeer<class_WebSocketPeer>`
基於包的協議的抽象和基底類別。
.. rst-class:: classref-introduction-group
說明
----
PacketPeer 是基於封包的協定(如 UDP的抽象和基底類別。它提供了用於發送和接收數據包的 API可以發送原始資料或變數。這使得在協定之間傳輸資料變得容易不必將資料編碼為低級位元組或擔心網路排序問題。
\ **注意:**\ 匯出到安卓時,在匯出專案、或使用一鍵部署之前,請務必在安卓匯出預設中,開啟 ``INTERNET`` 許可權。否則,任何型別的網路通信,都將被 Android 阻止。
.. rst-class:: classref-reftable-group
屬性
----
.. table::
:widths: auto
+-----------------------+---------------------------------------------------------------------------------+-------------+
| :ref:`int<class_int>` | :ref:`encode_buffer_max_size<class_PacketPeer_property_encode_buffer_max_size>` | ``8388608`` |
+-----------------------+---------------------------------------------------------------------------------+-------------+
.. rst-class:: classref-reftable-group
方法
----
.. table::
:widths: auto
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`int<class_int>` | :ref:`get_available_packet_count<class_PacketPeer_method_get_available_packet_count>`\ (\ ) |const| |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_packet<class_PacketPeer_method_get_packet>`\ (\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_packet_error<class_PacketPeer_method_get_packet_error>`\ (\ ) |const| |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Variant<class_Variant>` | :ref:`get_var<class_PacketPeer_method_get_var>`\ (\ allow_objects\: :ref:`bool<class_bool>` = false\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_packet<class_PacketPeer_method_put_packet>`\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_var<class_PacketPeer_method_put_var>`\ (\ var\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) |
+-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
屬性說明
--------
.. _class_PacketPeer_property_encode_buffer_max_size:
.. rst-class:: classref-property
:ref:`int<class_int>` **encode_buffer_max_size** = ``8388608`` :ref:`🔗<class_PacketPeer_property_encode_buffer_max_size>`
.. rst-class:: classref-property-setget
- |void| **set_encode_buffer_max_size**\ (\ value\: :ref:`int<class_int>`\ )
- :ref:`int<class_int>` **get_encode_buffer_max_size**\ (\ )
Maximum buffer size allowed when encoding :ref:`Variant<class_Variant>`\ s. Raise this value to support heavier memory allocations.
The :ref:`put_var()<class_PacketPeer_method_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 :ref:`Variant<class_Variant>`. If the :ref:`Variant<class_Variant>` is bigger than :ref:`encode_buffer_max_size<class_PacketPeer_property_encode_buffer_max_size>`, the method will error out with :ref:`@GlobalScope.ERR_OUT_OF_MEMORY<class_@GlobalScope_constant_ERR_OUT_OF_MEMORY>`.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
方法說明
--------
.. _class_PacketPeer_method_get_available_packet_count:
.. rst-class:: classref-method
:ref:`int<class_int>` **get_available_packet_count**\ (\ ) |const| :ref:`🔗<class_PacketPeer_method_get_available_packet_count>`
返回環形緩衝區中目前可用的封包數。
.. rst-class:: classref-item-separator
----
.. _class_PacketPeer_method_get_packet:
.. rst-class:: classref-method
:ref:`PackedByteArray<class_PackedByteArray>` **get_packet**\ (\ ) :ref:`🔗<class_PacketPeer_method_get_packet>`
獲取原始封包。
.. rst-class:: classref-item-separator
----
.. _class_PacketPeer_method_get_packet_error:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **get_packet_error**\ (\ ) |const| :ref:`🔗<class_PacketPeer_method_get_packet_error>`
返回最後接收的封包的錯誤狀態(通過 :ref:`get_packet()<class_PacketPeer_method_get_packet>`:ref:`get_var()<class_PacketPeer_method_get_var>`\ )。
.. rst-class:: classref-item-separator
----
.. _class_PacketPeer_method_get_var:
.. rst-class:: classref-method
:ref:`Variant<class_Variant>` **get_var**\ (\ allow_objects\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_PacketPeer_method_get_var>`
獲取 Variant。如果 ``allow_objects````true``\ ,則允許解碼出對象。
在內部,這使用與 :ref:`@GlobalScope.bytes_to_var()<class_@GlobalScope_method_bytes_to_var>` 方法相同的解碼機制。
\ **警告:**\ 反序列化出的物件可能包含會被執行的程式碼。如果序列化的物件來自不受信任的來源,請不要使用此選項,以避免潛在的安全威脅,例如遠端程式碼執行。
.. rst-class:: classref-item-separator
----
.. _class_PacketPeer_method_put_packet:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **put_packet**\ (\ buffer\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) :ref:`🔗<class_PacketPeer_method_put_packet>`
發送一個原始封包。
.. rst-class:: classref-item-separator
----
.. _class_PacketPeer_method_put_var:
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` **put_var**\ (\ var\: :ref:`Variant<class_Variant>`, full_objects\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_PacketPeer_method_put_var>`
將 :ref:`Variant<class_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 (無回傳值。)`