class_packetpeer.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/PacketPeer.xml.
  6. .. _class_PacketPeer:
  7. PacketPeer
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. **Inherited By:** :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>`
  11. Abstraction and base class for packet-based protocols.
  12. .. rst-class:: classref-introduction-group
  13. Description
  14. -----------
  15. PacketPeer is an abstraction and base class for packet-based protocols (such as UDP). It provides an API for sending and receiving packets both as raw data or variables. This makes it easy to transfer data over a protocol, without having to encode data as low-level bytes or having to worry about network ordering.
  16. \ **Note:** When exporting to Android, make sure to enable the ``INTERNET`` permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
  17. .. rst-class:: classref-reftable-group
  18. Properties
  19. ----------
  20. .. table::
  21. :widths: auto
  22. +-----------------------+---------------------------------------------------------------------------------+-------------+
  23. | :ref:`int<class_int>` | :ref:`encode_buffer_max_size<class_PacketPeer_property_encode_buffer_max_size>` | ``8388608`` |
  24. +-----------------------+---------------------------------------------------------------------------------+-------------+
  25. .. rst-class:: classref-reftable-group
  26. Methods
  27. -------
  28. .. table::
  29. :widths: auto
  30. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`int<class_int>` | :ref:`get_available_packet_count<class_PacketPeer_method_get_available_packet_count>` **(** **)** |const| |
  32. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`get_packet<class_PacketPeer_method_get_packet>` **(** **)** |
  34. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`get_packet_error<class_PacketPeer_method_get_packet_error>` **(** **)** |const| |
  36. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_PacketPeer_method_get_var>` **(** :ref:`bool<class_bool>` allow_objects=false **)** |
  38. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_packet<class_PacketPeer_method_put_packet>` **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)** |
  40. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_var<class_PacketPeer_method_put_var>` **(** :ref:`Variant<class_Variant>` var, :ref:`bool<class_bool>` full_objects=false **)** |
  42. +-----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  43. .. rst-class:: classref-section-separator
  44. ----
  45. .. rst-class:: classref-descriptions-group
  46. Property Descriptions
  47. ---------------------
  48. .. _class_PacketPeer_property_encode_buffer_max_size:
  49. .. rst-class:: classref-property
  50. :ref:`int<class_int>` **encode_buffer_max_size** = ``8388608``
  51. .. rst-class:: classref-property-setget
  52. - void **set_encode_buffer_max_size** **(** :ref:`int<class_int>` value **)**
  53. - :ref:`int<class_int>` **get_encode_buffer_max_size** **(** **)**
  54. Maximum buffer size allowed when encoding :ref:`Variant<class_Variant>`\ s. Raise this value to support heavier memory allocations.
  55. 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>`.
  56. .. rst-class:: classref-section-separator
  57. ----
  58. .. rst-class:: classref-descriptions-group
  59. Method Descriptions
  60. -------------------
  61. .. _class_PacketPeer_method_get_available_packet_count:
  62. .. rst-class:: classref-method
  63. :ref:`int<class_int>` **get_available_packet_count** **(** **)** |const|
  64. Returns the number of packets currently available in the ring-buffer.
  65. .. rst-class:: classref-item-separator
  66. ----
  67. .. _class_PacketPeer_method_get_packet:
  68. .. rst-class:: classref-method
  69. :ref:`PackedByteArray<class_PackedByteArray>` **get_packet** **(** **)**
  70. Gets a raw packet.
  71. .. rst-class:: classref-item-separator
  72. ----
  73. .. _class_PacketPeer_method_get_packet_error:
  74. .. rst-class:: classref-method
  75. :ref:`Error<enum_@GlobalScope_Error>` **get_packet_error** **(** **)** |const|
  76. Returns the error state of the last packet received (via :ref:`get_packet<class_PacketPeer_method_get_packet>` and :ref:`get_var<class_PacketPeer_method_get_var>`).
  77. .. rst-class:: classref-item-separator
  78. ----
  79. .. _class_PacketPeer_method_get_var:
  80. .. rst-class:: classref-method
  81. :ref:`Variant<class_Variant>` **get_var** **(** :ref:`bool<class_bool>` allow_objects=false **)**
  82. Gets a Variant. If ``allow_objects`` is ``true``, decoding objects is allowed.
  83. Internally, this uses the same decoding mechanism as the :ref:`@GlobalScope.bytes_to_var<class_@GlobalScope_method_bytes_to_var>` method.
  84. \ **Warning:** Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
  85. .. rst-class:: classref-item-separator
  86. ----
  87. .. _class_PacketPeer_method_put_packet:
  88. .. rst-class:: classref-method
  89. :ref:`Error<enum_@GlobalScope_Error>` **put_packet** **(** :ref:`PackedByteArray<class_PackedByteArray>` buffer **)**
  90. Sends a raw packet.
  91. .. rst-class:: classref-item-separator
  92. ----
  93. .. _class_PacketPeer_method_put_var:
  94. .. rst-class:: classref-method
  95. :ref:`Error<enum_@GlobalScope_Error>` **put_var** **(** :ref:`Variant<class_Variant>` var, :ref:`bool<class_bool>` full_objects=false **)**
  96. Sends a :ref:`Variant<class_Variant>` as a packet. If ``full_objects`` is ``true``, encoding objects is allowed (and can potentially include code).
  97. Internally, this uses the same encoding mechanism as the :ref:`@GlobalScope.var_to_bytes<class_@GlobalScope_method_var_to_bytes>` method.
  98. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  99. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  100. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  101. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  102. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  103. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  104. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`