class_streampeer.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/StreamPeer.xml.
  6. .. _class_StreamPeer:
  7. StreamPeer
  8. ==========
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. **Inherited By:** :ref:`StreamPeerBuffer<class_StreamPeerBuffer>`, :ref:`StreamPeerExtension<class_StreamPeerExtension>`, :ref:`StreamPeerGZIP<class_StreamPeerGZIP>`, :ref:`StreamPeerTCP<class_StreamPeerTCP>`, :ref:`StreamPeerTLS<class_StreamPeerTLS>`
  11. Abstract base class for interacting with streams.
  12. .. rst-class:: classref-introduction-group
  13. Description
  14. -----------
  15. StreamPeer is an abstract base class mostly used for stream-based protocols (such as TCP). It provides an API for sending and receiving data through streams as raw data or strings.
  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:`bool<class_bool>` | :ref:`big_endian<class_StreamPeer_property_big_endian>` | ``false`` |
  24. +-------------------------+---------------------------------------------------------+-----------+
  25. .. rst-class:: classref-reftable-group
  26. Methods
  27. -------
  28. .. table::
  29. :widths: auto
  30. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`int<class_int>` | :ref:`get_16<class_StreamPeer_method_get_16>` **(** **)** |
  32. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`int<class_int>` | :ref:`get_32<class_StreamPeer_method_get_32>` **(** **)** |
  34. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`int<class_int>` | :ref:`get_64<class_StreamPeer_method_get_64>` **(** **)** |
  36. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`int<class_int>` | :ref:`get_8<class_StreamPeer_method_get_8>` **(** **)** |
  38. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`int<class_int>` | :ref:`get_available_bytes<class_StreamPeer_method_get_available_bytes>` **(** **)** |const| |
  40. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Array<class_Array>` | :ref:`get_data<class_StreamPeer_method_get_data>` **(** :ref:`int<class_int>` bytes **)** |
  42. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`float<class_float>` | :ref:`get_double<class_StreamPeer_method_get_double>` **(** **)** |
  44. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`float<class_float>` | :ref:`get_float<class_StreamPeer_method_get_float>` **(** **)** |
  46. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Array<class_Array>` | :ref:`get_partial_data<class_StreamPeer_method_get_partial_data>` **(** :ref:`int<class_int>` bytes **)** |
  48. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`String<class_String>` | :ref:`get_string<class_StreamPeer_method_get_string>` **(** :ref:`int<class_int>` bytes=-1 **)** |
  50. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`int<class_int>` | :ref:`get_u16<class_StreamPeer_method_get_u16>` **(** **)** |
  52. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`int<class_int>` | :ref:`get_u32<class_StreamPeer_method_get_u32>` **(** **)** |
  54. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`int<class_int>` | :ref:`get_u64<class_StreamPeer_method_get_u64>` **(** **)** |
  56. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`int<class_int>` | :ref:`get_u8<class_StreamPeer_method_get_u8>` **(** **)** |
  58. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`String<class_String>` | :ref:`get_utf8_string<class_StreamPeer_method_get_utf8_string>` **(** :ref:`int<class_int>` bytes=-1 **)** |
  60. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Variant<class_Variant>` | :ref:`get_var<class_StreamPeer_method_get_var>` **(** :ref:`bool<class_bool>` allow_objects=false **)** |
  62. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  63. | void | :ref:`put_16<class_StreamPeer_method_put_16>` **(** :ref:`int<class_int>` value **)** |
  64. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  65. | void | :ref:`put_32<class_StreamPeer_method_put_32>` **(** :ref:`int<class_int>` value **)** |
  66. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  67. | void | :ref:`put_64<class_StreamPeer_method_put_64>` **(** :ref:`int<class_int>` value **)** |
  68. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  69. | void | :ref:`put_8<class_StreamPeer_method_put_8>` **(** :ref:`int<class_int>` value **)** |
  70. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`put_data<class_StreamPeer_method_put_data>` **(** :ref:`PackedByteArray<class_PackedByteArray>` data **)** |
  72. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  73. | void | :ref:`put_double<class_StreamPeer_method_put_double>` **(** :ref:`float<class_float>` value **)** |
  74. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  75. | void | :ref:`put_float<class_StreamPeer_method_put_float>` **(** :ref:`float<class_float>` value **)** |
  76. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`Array<class_Array>` | :ref:`put_partial_data<class_StreamPeer_method_put_partial_data>` **(** :ref:`PackedByteArray<class_PackedByteArray>` data **)** |
  78. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  79. | void | :ref:`put_string<class_StreamPeer_method_put_string>` **(** :ref:`String<class_String>` value **)** |
  80. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  81. | void | :ref:`put_u16<class_StreamPeer_method_put_u16>` **(** :ref:`int<class_int>` value **)** |
  82. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  83. | void | :ref:`put_u32<class_StreamPeer_method_put_u32>` **(** :ref:`int<class_int>` value **)** |
  84. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  85. | void | :ref:`put_u64<class_StreamPeer_method_put_u64>` **(** :ref:`int<class_int>` value **)** |
  86. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  87. | void | :ref:`put_u8<class_StreamPeer_method_put_u8>` **(** :ref:`int<class_int>` value **)** |
  88. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  89. | void | :ref:`put_utf8_string<class_StreamPeer_method_put_utf8_string>` **(** :ref:`String<class_String>` value **)** |
  90. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  91. | void | :ref:`put_var<class_StreamPeer_method_put_var>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)** |
  92. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
  93. .. rst-class:: classref-section-separator
  94. ----
  95. .. rst-class:: classref-descriptions-group
  96. Property Descriptions
  97. ---------------------
  98. .. _class_StreamPeer_property_big_endian:
  99. .. rst-class:: classref-property
  100. :ref:`bool<class_bool>` **big_endian** = ``false``
  101. .. rst-class:: classref-property-setget
  102. - void **set_big_endian** **(** :ref:`bool<class_bool>` value **)**
  103. - :ref:`bool<class_bool>` **is_big_endian_enabled** **(** **)**
  104. If ``true``, this **StreamPeer** will using big-endian format for encoding and decoding.
  105. .. rst-class:: classref-section-separator
  106. ----
  107. .. rst-class:: classref-descriptions-group
  108. Method Descriptions
  109. -------------------
  110. .. _class_StreamPeer_method_get_16:
  111. .. rst-class:: classref-method
  112. :ref:`int<class_int>` **get_16** **(** **)**
  113. Gets a signed 16-bit value from the stream.
  114. .. rst-class:: classref-item-separator
  115. ----
  116. .. _class_StreamPeer_method_get_32:
  117. .. rst-class:: classref-method
  118. :ref:`int<class_int>` **get_32** **(** **)**
  119. Gets a signed 32-bit value from the stream.
  120. .. rst-class:: classref-item-separator
  121. ----
  122. .. _class_StreamPeer_method_get_64:
  123. .. rst-class:: classref-method
  124. :ref:`int<class_int>` **get_64** **(** **)**
  125. Gets a signed 64-bit value from the stream.
  126. .. rst-class:: classref-item-separator
  127. ----
  128. .. _class_StreamPeer_method_get_8:
  129. .. rst-class:: classref-method
  130. :ref:`int<class_int>` **get_8** **(** **)**
  131. Gets a signed byte from the stream.
  132. .. rst-class:: classref-item-separator
  133. ----
  134. .. _class_StreamPeer_method_get_available_bytes:
  135. .. rst-class:: classref-method
  136. :ref:`int<class_int>` **get_available_bytes** **(** **)** |const|
  137. Returns the number of bytes this **StreamPeer** has available.
  138. .. rst-class:: classref-item-separator
  139. ----
  140. .. _class_StreamPeer_method_get_data:
  141. .. rst-class:: classref-method
  142. :ref:`Array<class_Array>` **get_data** **(** :ref:`int<class_int>` bytes **)**
  143. Returns a chunk data with the received bytes. The number of bytes to be received can be requested in the ``bytes`` argument. If not enough bytes are available, the function will block until the desired amount is received. This function returns two values, an :ref:`Error<enum_@GlobalScope_Error>` code and a data array.
  144. .. rst-class:: classref-item-separator
  145. ----
  146. .. _class_StreamPeer_method_get_double:
  147. .. rst-class:: classref-method
  148. :ref:`float<class_float>` **get_double** **(** **)**
  149. Gets a double-precision float from the stream.
  150. .. rst-class:: classref-item-separator
  151. ----
  152. .. _class_StreamPeer_method_get_float:
  153. .. rst-class:: classref-method
  154. :ref:`float<class_float>` **get_float** **(** **)**
  155. Gets a single-precision float from the stream.
  156. .. rst-class:: classref-item-separator
  157. ----
  158. .. _class_StreamPeer_method_get_partial_data:
  159. .. rst-class:: classref-method
  160. :ref:`Array<class_Array>` **get_partial_data** **(** :ref:`int<class_int>` bytes **)**
  161. Returns a chunk data with the received bytes. The number of bytes to be received can be requested in the "bytes" argument. If not enough bytes are available, the function will return how many were actually received. This function returns two values, an :ref:`Error<enum_@GlobalScope_Error>` code, and a data array.
  162. .. rst-class:: classref-item-separator
  163. ----
  164. .. _class_StreamPeer_method_get_string:
  165. .. rst-class:: classref-method
  166. :ref:`String<class_String>` **get_string** **(** :ref:`int<class_int>` bytes=-1 **)**
  167. Gets an ASCII string with byte-length ``bytes`` from the stream. If ``bytes`` is negative (default) the length will be read from the stream using the reverse process of :ref:`put_string<class_StreamPeer_method_put_string>`.
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_StreamPeer_method_get_u16:
  171. .. rst-class:: classref-method
  172. :ref:`int<class_int>` **get_u16** **(** **)**
  173. Gets an unsigned 16-bit value from the stream.
  174. .. rst-class:: classref-item-separator
  175. ----
  176. .. _class_StreamPeer_method_get_u32:
  177. .. rst-class:: classref-method
  178. :ref:`int<class_int>` **get_u32** **(** **)**
  179. Gets an unsigned 32-bit value from the stream.
  180. .. rst-class:: classref-item-separator
  181. ----
  182. .. _class_StreamPeer_method_get_u64:
  183. .. rst-class:: classref-method
  184. :ref:`int<class_int>` **get_u64** **(** **)**
  185. Gets an unsigned 64-bit value from the stream.
  186. .. rst-class:: classref-item-separator
  187. ----
  188. .. _class_StreamPeer_method_get_u8:
  189. .. rst-class:: classref-method
  190. :ref:`int<class_int>` **get_u8** **(** **)**
  191. Gets an unsigned byte from the stream.
  192. .. rst-class:: classref-item-separator
  193. ----
  194. .. _class_StreamPeer_method_get_utf8_string:
  195. .. rst-class:: classref-method
  196. :ref:`String<class_String>` **get_utf8_string** **(** :ref:`int<class_int>` bytes=-1 **)**
  197. Gets a UTF-8 string with byte-length ``bytes`` from the stream (this decodes the string sent as UTF-8). If ``bytes`` is negative (default) the length will be read from the stream using the reverse process of :ref:`put_utf8_string<class_StreamPeer_method_put_utf8_string>`.
  198. .. rst-class:: classref-item-separator
  199. ----
  200. .. _class_StreamPeer_method_get_var:
  201. .. rst-class:: classref-method
  202. :ref:`Variant<class_Variant>` **get_var** **(** :ref:`bool<class_bool>` allow_objects=false **)**
  203. Gets a Variant from the stream. If ``allow_objects`` is ``true``, decoding objects is allowed.
  204. Internally, this uses the same decoding mechanism as the :ref:`@GlobalScope.bytes_to_var<class_@GlobalScope_method_bytes_to_var>` method.
  205. \ **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.
  206. .. rst-class:: classref-item-separator
  207. ----
  208. .. _class_StreamPeer_method_put_16:
  209. .. rst-class:: classref-method
  210. void **put_16** **(** :ref:`int<class_int>` value **)**
  211. Puts a signed 16-bit value into the stream.
  212. .. rst-class:: classref-item-separator
  213. ----
  214. .. _class_StreamPeer_method_put_32:
  215. .. rst-class:: classref-method
  216. void **put_32** **(** :ref:`int<class_int>` value **)**
  217. Puts a signed 32-bit value into the stream.
  218. .. rst-class:: classref-item-separator
  219. ----
  220. .. _class_StreamPeer_method_put_64:
  221. .. rst-class:: classref-method
  222. void **put_64** **(** :ref:`int<class_int>` value **)**
  223. Puts a signed 64-bit value into the stream.
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_StreamPeer_method_put_8:
  227. .. rst-class:: classref-method
  228. void **put_8** **(** :ref:`int<class_int>` value **)**
  229. Puts a signed byte into the stream.
  230. .. rst-class:: classref-item-separator
  231. ----
  232. .. _class_StreamPeer_method_put_data:
  233. .. rst-class:: classref-method
  234. :ref:`Error<enum_@GlobalScope_Error>` **put_data** **(** :ref:`PackedByteArray<class_PackedByteArray>` data **)**
  235. Sends a chunk of data through the connection, blocking if necessary until the data is done sending. This function returns an :ref:`Error<enum_@GlobalScope_Error>` code.
  236. .. rst-class:: classref-item-separator
  237. ----
  238. .. _class_StreamPeer_method_put_double:
  239. .. rst-class:: classref-method
  240. void **put_double** **(** :ref:`float<class_float>` value **)**
  241. Puts a double-precision float into the stream.
  242. .. rst-class:: classref-item-separator
  243. ----
  244. .. _class_StreamPeer_method_put_float:
  245. .. rst-class:: classref-method
  246. void **put_float** **(** :ref:`float<class_float>` value **)**
  247. Puts a single-precision float into the stream.
  248. .. rst-class:: classref-item-separator
  249. ----
  250. .. _class_StreamPeer_method_put_partial_data:
  251. .. rst-class:: classref-method
  252. :ref:`Array<class_Array>` **put_partial_data** **(** :ref:`PackedByteArray<class_PackedByteArray>` data **)**
  253. Sends a chunk of data through the connection. If all the data could not be sent at once, only part of it will. This function returns two values, an :ref:`Error<enum_@GlobalScope_Error>` code and an integer, describing how much data was actually sent.
  254. .. rst-class:: classref-item-separator
  255. ----
  256. .. _class_StreamPeer_method_put_string:
  257. .. rst-class:: classref-method
  258. void **put_string** **(** :ref:`String<class_String>` value **)**
  259. Puts a zero-terminated ASCII string into the stream prepended by a 32-bit unsigned integer representing its size.
  260. \ **Note:** To put an ASCII string without prepending its size, you can use :ref:`put_data<class_StreamPeer_method_put_data>`:
  261. .. tabs::
  262. .. code-tab:: gdscript
  263. put_data("Hello world".to_ascii_buffer())
  264. .. code-tab:: csharp
  265. PutData("Hello World".ToAsciiBuffer());
  266. .. rst-class:: classref-item-separator
  267. ----
  268. .. _class_StreamPeer_method_put_u16:
  269. .. rst-class:: classref-method
  270. void **put_u16** **(** :ref:`int<class_int>` value **)**
  271. Puts an unsigned 16-bit value into the stream.
  272. .. rst-class:: classref-item-separator
  273. ----
  274. .. _class_StreamPeer_method_put_u32:
  275. .. rst-class:: classref-method
  276. void **put_u32** **(** :ref:`int<class_int>` value **)**
  277. Puts an unsigned 32-bit value into the stream.
  278. .. rst-class:: classref-item-separator
  279. ----
  280. .. _class_StreamPeer_method_put_u64:
  281. .. rst-class:: classref-method
  282. void **put_u64** **(** :ref:`int<class_int>` value **)**
  283. Puts an unsigned 64-bit value into the stream.
  284. .. rst-class:: classref-item-separator
  285. ----
  286. .. _class_StreamPeer_method_put_u8:
  287. .. rst-class:: classref-method
  288. void **put_u8** **(** :ref:`int<class_int>` value **)**
  289. Puts an unsigned byte into the stream.
  290. .. rst-class:: classref-item-separator
  291. ----
  292. .. _class_StreamPeer_method_put_utf8_string:
  293. .. rst-class:: classref-method
  294. void **put_utf8_string** **(** :ref:`String<class_String>` value **)**
  295. Puts a zero-terminated UTF-8 string into the stream prepended by a 32 bits unsigned integer representing its size.
  296. \ **Note:** To put a UTF-8 string without prepending its size, you can use :ref:`put_data<class_StreamPeer_method_put_data>`:
  297. .. tabs::
  298. .. code-tab:: gdscript
  299. put_data("Hello world".to_utf8_buffer())
  300. .. code-tab:: csharp
  301. PutData("Hello World".ToUtf8Buffer());
  302. .. rst-class:: classref-item-separator
  303. ----
  304. .. _class_StreamPeer_method_put_var:
  305. .. rst-class:: classref-method
  306. void **put_var** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false **)**
  307. Puts a Variant into the stream. If ``full_objects`` is ``true`` encoding objects is allowed (and can potentially include code).
  308. Internally, this uses the same encoding mechanism as the :ref:`@GlobalScope.var_to_bytes<class_@GlobalScope_method_var_to_bytes>` method.
  309. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  310. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  311. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  312. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  313. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  314. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`