packet_peer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**************************************************************************/
  2. /* packet_peer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "packet_peer.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/marshalls.h"
  33. /* helpers / binders */
  34. void PacketPeer::set_encode_buffer_max_size(int p_max_size) {
  35. ERR_FAIL_COND_MSG(p_max_size < 1024, "Max encode buffer must be at least 1024 bytes");
  36. ERR_FAIL_COND_MSG(p_max_size > 256 * 1024 * 1024, "Max encode buffer cannot exceed 256 MiB");
  37. encode_buffer_max_size = next_power_of_2(p_max_size);
  38. encode_buffer.clear();
  39. }
  40. int PacketPeer::get_encode_buffer_max_size() const {
  41. return encode_buffer_max_size;
  42. }
  43. Error PacketPeer::get_packet_buffer(Vector<uint8_t> &r_buffer) {
  44. const uint8_t *buffer;
  45. int buffer_size;
  46. Error err = get_packet(&buffer, buffer_size);
  47. if (err) {
  48. return err;
  49. }
  50. r_buffer.resize(buffer_size);
  51. if (buffer_size == 0) {
  52. return OK;
  53. }
  54. uint8_t *w = r_buffer.ptrw();
  55. for (int i = 0; i < buffer_size; i++) {
  56. w[i] = buffer[i];
  57. }
  58. return OK;
  59. }
  60. Error PacketPeer::put_packet_buffer(const Vector<uint8_t> &p_buffer) {
  61. int len = p_buffer.size();
  62. if (len == 0) {
  63. return OK;
  64. }
  65. const uint8_t *r = p_buffer.ptr();
  66. return put_packet(&r[0], len);
  67. }
  68. Error PacketPeer::get_var(Variant &r_variant, bool p_allow_objects) {
  69. const uint8_t *buffer;
  70. int buffer_size;
  71. Error err = get_packet(&buffer, buffer_size);
  72. if (err) {
  73. return err;
  74. }
  75. return decode_variant(r_variant, buffer, buffer_size, nullptr, p_allow_objects);
  76. }
  77. Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) {
  78. int len;
  79. Error err = encode_variant(p_packet, nullptr, len, p_full_objects); // compute len first
  80. if (err) {
  81. return err;
  82. }
  83. if (len == 0) {
  84. return OK;
  85. }
  86. ERR_FAIL_COND_V_MSG(len > encode_buffer_max_size, ERR_OUT_OF_MEMORY, "Failed to encode variant, encode size is bigger then encode_buffer_max_size. Consider raising it via 'set_encode_buffer_max_size'.");
  87. if (unlikely(encode_buffer.size() < len)) {
  88. encode_buffer.resize(0); // Avoid realloc
  89. encode_buffer.resize(next_power_of_2(len));
  90. }
  91. uint8_t *w = encode_buffer.ptrw();
  92. err = encode_variant(p_packet, w, len, p_full_objects);
  93. ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to encode Variant.");
  94. return put_packet(w, len);
  95. }
  96. Variant PacketPeer::_bnd_get_var(bool p_allow_objects) {
  97. Variant var;
  98. Error err = get_var(var, p_allow_objects);
  99. ERR_FAIL_COND_V(err != OK, Variant());
  100. return var;
  101. }
  102. Error PacketPeer::_put_packet(const Vector<uint8_t> &p_buffer) {
  103. return put_packet_buffer(p_buffer);
  104. }
  105. Vector<uint8_t> PacketPeer::_get_packet() {
  106. Vector<uint8_t> raw;
  107. last_get_error = get_packet_buffer(raw);
  108. return raw;
  109. }
  110. Error PacketPeer::_get_packet_error() const {
  111. return last_get_error;
  112. }
  113. void PacketPeer::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &PacketPeer::_bnd_get_var, DEFVAL(false));
  115. ClassDB::bind_method(D_METHOD("put_var", "var", "full_objects"), &PacketPeer::put_var, DEFVAL(false));
  116. ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet);
  117. ClassDB::bind_method(D_METHOD("put_packet", "buffer"), &PacketPeer::_put_packet);
  118. ClassDB::bind_method(D_METHOD("get_packet_error"), &PacketPeer::_get_packet_error);
  119. ClassDB::bind_method(D_METHOD("get_available_packet_count"), &PacketPeer::get_available_packet_count);
  120. ClassDB::bind_method(D_METHOD("get_encode_buffer_max_size"), &PacketPeer::get_encode_buffer_max_size);
  121. ClassDB::bind_method(D_METHOD("set_encode_buffer_max_size", "max_size"), &PacketPeer::set_encode_buffer_max_size);
  122. ADD_PROPERTY(PropertyInfo(Variant::INT, "encode_buffer_max_size"), "set_encode_buffer_max_size", "get_encode_buffer_max_size");
  123. }
  124. /***************/
  125. Error PacketPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  126. Error err;
  127. if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) {
  128. return err;
  129. }
  130. WARN_PRINT_ONCE("PacketPeerExtension::_get_packet_native is unimplemented!");
  131. return FAILED;
  132. }
  133. Error PacketPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  134. Error err;
  135. if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) {
  136. return err;
  137. }
  138. WARN_PRINT_ONCE("PacketPeerExtension::_put_packet_native is unimplemented!");
  139. return FAILED;
  140. }
  141. void PacketPeerExtension::_bind_methods() {
  142. GDVIRTUAL_BIND(_get_packet, "r_buffer", "r_buffer_size");
  143. GDVIRTUAL_BIND(_put_packet, "p_buffer", "p_buffer_size");
  144. GDVIRTUAL_BIND(_get_available_packet_count);
  145. GDVIRTUAL_BIND(_get_max_packet_size);
  146. }
  147. /***************/
  148. void PacketPeerStream::_bind_methods() {
  149. ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer);
  150. ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer);
  151. ClassDB::bind_method(D_METHOD("set_input_buffer_max_size", "max_size_bytes"), &PacketPeerStream::set_input_buffer_max_size);
  152. ClassDB::bind_method(D_METHOD("set_output_buffer_max_size", "max_size_bytes"), &PacketPeerStream::set_output_buffer_max_size);
  153. ClassDB::bind_method(D_METHOD("get_input_buffer_max_size"), &PacketPeerStream::get_input_buffer_max_size);
  154. ClassDB::bind_method(D_METHOD("get_output_buffer_max_size"), &PacketPeerStream::get_output_buffer_max_size);
  155. ADD_PROPERTY(PropertyInfo(Variant::INT, "input_buffer_max_size"), "set_input_buffer_max_size", "get_input_buffer_max_size");
  156. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_buffer_max_size"), "set_output_buffer_max_size", "get_output_buffer_max_size");
  157. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream_peer", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", PROPERTY_USAGE_NONE), "set_stream_peer", "get_stream_peer");
  158. }
  159. Error PacketPeerStream::_poll_buffer() const {
  160. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  161. int read = 0;
  162. ERR_FAIL_COND_V(input_buffer.size() < ring_buffer.space_left(), ERR_UNAVAILABLE);
  163. Error err = peer->get_partial_data(input_buffer.ptrw(), ring_buffer.space_left(), read);
  164. if (err) {
  165. return err;
  166. }
  167. if (read == 0) {
  168. return OK;
  169. }
  170. int w = ring_buffer.write(&input_buffer[0], read);
  171. ERR_FAIL_COND_V(w != read, ERR_BUG);
  172. return OK;
  173. }
  174. int PacketPeerStream::get_available_packet_count() const {
  175. _poll_buffer();
  176. uint32_t remaining = ring_buffer.data_left();
  177. int ofs = 0;
  178. int count = 0;
  179. while (remaining >= 4) {
  180. uint8_t lbuf[4];
  181. ring_buffer.copy(lbuf, ofs, 4);
  182. uint32_t len = decode_uint32(lbuf);
  183. remaining -= 4;
  184. ofs += 4;
  185. if (len > remaining) {
  186. break;
  187. }
  188. remaining -= len;
  189. ofs += len;
  190. count++;
  191. }
  192. return count;
  193. }
  194. Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  195. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  196. _poll_buffer();
  197. int remaining = ring_buffer.data_left();
  198. ERR_FAIL_COND_V(remaining < 4, ERR_UNAVAILABLE);
  199. uint8_t lbuf[4];
  200. ring_buffer.copy(lbuf, 0, 4);
  201. remaining -= 4;
  202. uint32_t len = decode_uint32(lbuf);
  203. ERR_FAIL_COND_V(remaining < (int)len, ERR_UNAVAILABLE);
  204. ERR_FAIL_COND_V(input_buffer.size() < (int)len, ERR_UNAVAILABLE);
  205. ring_buffer.read(lbuf, 4); //get rid of first 4 bytes
  206. ring_buffer.read(input_buffer.ptrw(), len); // read packet
  207. *r_buffer = &input_buffer[0];
  208. r_buffer_size = len;
  209. return OK;
  210. }
  211. Error PacketPeerStream::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  212. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  213. Error err = _poll_buffer(); //won't hurt to poll here too
  214. if (err) {
  215. return err;
  216. }
  217. if (p_buffer_size == 0) {
  218. return OK;
  219. }
  220. ERR_FAIL_COND_V(p_buffer_size < 0, ERR_INVALID_PARAMETER);
  221. ERR_FAIL_COND_V(p_buffer_size + 4 > output_buffer.size(), ERR_INVALID_PARAMETER);
  222. encode_uint32(p_buffer_size, output_buffer.ptrw());
  223. uint8_t *dst = &output_buffer.write[4];
  224. for (int i = 0; i < p_buffer_size; i++) {
  225. dst[i] = p_buffer[i];
  226. }
  227. return peer->put_data(&output_buffer[0], p_buffer_size + 4);
  228. }
  229. int PacketPeerStream::get_max_packet_size() const {
  230. return output_buffer.size();
  231. }
  232. void PacketPeerStream::set_stream_peer(const Ref<StreamPeer> &p_peer) {
  233. if (p_peer.ptr() != peer.ptr()) {
  234. ring_buffer.advance_read(ring_buffer.data_left()); // Reset the ring buffer.
  235. }
  236. peer = p_peer;
  237. }
  238. Ref<StreamPeer> PacketPeerStream::get_stream_peer() const {
  239. return peer;
  240. }
  241. void PacketPeerStream::set_input_buffer_max_size(int p_max_size) {
  242. ERR_FAIL_COND_MSG(p_max_size < 0, "Max size of input buffer size cannot be smaller than 0.");
  243. //warning may lose packets
  244. ERR_FAIL_COND_MSG(ring_buffer.data_left(), "Buffer in use, resizing would cause loss of data.");
  245. ring_buffer.resize(nearest_shift(next_power_of_2(p_max_size + 4)) - 1);
  246. input_buffer.resize(next_power_of_2(p_max_size + 4));
  247. }
  248. int PacketPeerStream::get_input_buffer_max_size() const {
  249. return input_buffer.size() - 4;
  250. }
  251. void PacketPeerStream::set_output_buffer_max_size(int p_max_size) {
  252. output_buffer.resize(next_power_of_2(p_max_size + 4));
  253. }
  254. int PacketPeerStream::get_output_buffer_max_size() const {
  255. return output_buffer.size() - 4;
  256. }
  257. PacketPeerStream::PacketPeerStream() {
  258. int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2");
  259. ring_buffer.resize(rbsize);
  260. input_buffer.resize(1 << rbsize);
  261. output_buffer.resize(1 << rbsize);
  262. }