packet_peer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*************************************************************************/
  2. /* packet_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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/io/marshalls.h"
  32. #include "core/project_settings.h"
  33. /* helpers / binders */
  34. PacketPeer::PacketPeer() :
  35. last_get_error(OK),
  36. allow_object_decoding(false) {
  37. }
  38. void PacketPeer::set_allow_object_decoding(bool p_enable) {
  39. allow_object_decoding = p_enable;
  40. }
  41. bool PacketPeer::is_object_decoding_allowed() const {
  42. return allow_object_decoding;
  43. }
  44. Error PacketPeer::get_packet_buffer(PoolVector<uint8_t> &r_buffer) {
  45. const uint8_t *buffer;
  46. int buffer_size;
  47. Error err = get_packet(&buffer, buffer_size);
  48. if (err)
  49. return err;
  50. r_buffer.resize(buffer_size);
  51. if (buffer_size == 0)
  52. return OK;
  53. PoolVector<uint8_t>::Write w = r_buffer.write();
  54. for (int i = 0; i < buffer_size; i++)
  55. w[i] = buffer[i];
  56. return OK;
  57. }
  58. Error PacketPeer::put_packet_buffer(const PoolVector<uint8_t> &p_buffer) {
  59. int len = p_buffer.size();
  60. if (len == 0)
  61. return OK;
  62. PoolVector<uint8_t>::Read r = p_buffer.read();
  63. return put_packet(&r[0], len);
  64. }
  65. Error PacketPeer::get_var(Variant &r_variant) {
  66. const uint8_t *buffer;
  67. int buffer_size;
  68. Error err = get_packet(&buffer, buffer_size);
  69. if (err)
  70. return err;
  71. return decode_variant(r_variant, buffer, buffer_size, NULL, allow_object_decoding);
  72. }
  73. Error PacketPeer::put_var(const Variant &p_packet) {
  74. int len;
  75. Error err = encode_variant(p_packet, NULL, len, !allow_object_decoding); // compute len first
  76. if (err)
  77. return err;
  78. if (len == 0)
  79. return OK;
  80. uint8_t *buf = (uint8_t *)alloca(len);
  81. ERR_FAIL_COND_V(!buf, ERR_OUT_OF_MEMORY);
  82. err = encode_variant(p_packet, buf, len, !allow_object_decoding);
  83. ERR_FAIL_COND_V(err, err);
  84. return put_packet(buf, len);
  85. }
  86. Variant PacketPeer::_bnd_get_var() {
  87. Variant var;
  88. get_var(var);
  89. return var;
  90. };
  91. Error PacketPeer::_put_packet(const PoolVector<uint8_t> &p_buffer) {
  92. return put_packet_buffer(p_buffer);
  93. }
  94. PoolVector<uint8_t> PacketPeer::_get_packet() {
  95. PoolVector<uint8_t> raw;
  96. last_get_error = get_packet_buffer(raw);
  97. return raw;
  98. }
  99. Error PacketPeer::_get_packet_error() const {
  100. return last_get_error;
  101. }
  102. void PacketPeer::_bind_methods() {
  103. ClassDB::bind_method(D_METHOD("get_var"), &PacketPeer::_bnd_get_var);
  104. ClassDB::bind_method(D_METHOD("put_var", "var"), &PacketPeer::put_var);
  105. ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet);
  106. ClassDB::bind_method(D_METHOD("put_packet", "buffer"), &PacketPeer::_put_packet);
  107. ClassDB::bind_method(D_METHOD("get_packet_error"), &PacketPeer::_get_packet_error);
  108. ClassDB::bind_method(D_METHOD("get_available_packet_count"), &PacketPeer::get_available_packet_count);
  109. ClassDB::bind_method(D_METHOD("set_allow_object_decoding", "enable"), &PacketPeer::set_allow_object_decoding);
  110. ClassDB::bind_method(D_METHOD("is_object_decoding_allowed"), &PacketPeer::is_object_decoding_allowed);
  111. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed");
  112. };
  113. /***************/
  114. void PacketPeerStream::_set_stream_peer(REF p_peer) {
  115. ERR_FAIL_COND(p_peer.is_null());
  116. set_stream_peer(p_peer);
  117. }
  118. void PacketPeerStream::_bind_methods() {
  119. ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer);
  120. ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer);
  121. ClassDB::bind_method(D_METHOD("set_input_buffer_max_size", "max_size_bytes"), &PacketPeerStream::set_input_buffer_max_size);
  122. ClassDB::bind_method(D_METHOD("set_output_buffer_max_size", "max_size_bytes"), &PacketPeerStream::set_output_buffer_max_size);
  123. ClassDB::bind_method(D_METHOD("get_input_buffer_max_size"), &PacketPeerStream::get_input_buffer_max_size);
  124. ClassDB::bind_method(D_METHOD("get_output_buffer_max_size"), &PacketPeerStream::get_output_buffer_max_size);
  125. ADD_PROPERTY(PropertyInfo(Variant::INT, "input_buffer_max_size"), "set_input_buffer_max_size", "get_input_buffer_max_size");
  126. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_buffer_max_size"), "set_output_buffer_max_size", "get_output_buffer_max_size");
  127. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream_peer", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", 0), "set_stream_peer", "get_stream_peer");
  128. }
  129. Error PacketPeerStream::_poll_buffer() const {
  130. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  131. int read = 0;
  132. ERR_FAIL_COND_V(input_buffer.size() < ring_buffer.space_left(), ERR_UNAVAILABLE);
  133. Error err = peer->get_partial_data(input_buffer.ptrw(), ring_buffer.space_left(), read);
  134. if (err)
  135. return err;
  136. if (read == 0)
  137. return OK;
  138. int w = ring_buffer.write(&input_buffer[0], read);
  139. ERR_FAIL_COND_V(w != read, ERR_BUG);
  140. return OK;
  141. }
  142. int PacketPeerStream::get_available_packet_count() const {
  143. _poll_buffer();
  144. uint32_t remaining = ring_buffer.data_left();
  145. int ofs = 0;
  146. int count = 0;
  147. while (remaining >= 4) {
  148. uint8_t lbuf[4];
  149. ring_buffer.copy(lbuf, ofs, 4);
  150. uint32_t len = decode_uint32(lbuf);
  151. remaining -= 4;
  152. ofs += 4;
  153. if (len > remaining)
  154. break;
  155. remaining -= len;
  156. ofs += len;
  157. count++;
  158. }
  159. return count;
  160. }
  161. Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  162. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  163. _poll_buffer();
  164. int remaining = ring_buffer.data_left();
  165. ERR_FAIL_COND_V(remaining < 4, ERR_UNAVAILABLE);
  166. uint8_t lbuf[4];
  167. ring_buffer.copy(lbuf, 0, 4);
  168. remaining -= 4;
  169. uint32_t len = decode_uint32(lbuf);
  170. ERR_FAIL_COND_V(remaining < (int)len, ERR_UNAVAILABLE);
  171. ERR_FAIL_COND_V(input_buffer.size() < len, ERR_UNAVAILABLE);
  172. ring_buffer.read(lbuf, 4); //get rid of first 4 bytes
  173. ring_buffer.read(input_buffer.ptrw(), len); // read packet
  174. *r_buffer = &input_buffer[0];
  175. r_buffer_size = len;
  176. return OK;
  177. }
  178. Error PacketPeerStream::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  179. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  180. Error err = _poll_buffer(); //won't hurt to poll here too
  181. if (err)
  182. return err;
  183. if (p_buffer_size == 0)
  184. return OK;
  185. ERR_FAIL_COND_V(p_buffer_size < 0, ERR_INVALID_PARAMETER);
  186. ERR_FAIL_COND_V(p_buffer_size + 4 > output_buffer.size(), ERR_INVALID_PARAMETER);
  187. encode_uint32(p_buffer_size, output_buffer.ptrw());
  188. uint8_t *dst = &output_buffer.write[4];
  189. for (int i = 0; i < p_buffer_size; i++)
  190. dst[i] = p_buffer[i];
  191. return peer->put_data(&output_buffer[0], p_buffer_size + 4);
  192. }
  193. int PacketPeerStream::get_max_packet_size() const {
  194. return output_buffer.size();
  195. }
  196. void PacketPeerStream::set_stream_peer(const Ref<StreamPeer> &p_peer) {
  197. //ERR_FAIL_COND(p_peer.is_null());
  198. if (p_peer.ptr() != peer.ptr()) {
  199. ring_buffer.advance_read(ring_buffer.data_left()); // reset the ring buffer
  200. };
  201. peer = p_peer;
  202. }
  203. Ref<StreamPeer> PacketPeerStream::get_stream_peer() const {
  204. return peer;
  205. }
  206. void PacketPeerStream::set_input_buffer_max_size(int p_max_size) {
  207. //warning may lose packets
  208. ERR_EXPLAIN("Buffer in use, resizing would cause loss of data");
  209. ERR_FAIL_COND(ring_buffer.data_left());
  210. ring_buffer.resize(nearest_shift(p_max_size + 4));
  211. input_buffer.resize(next_power_of_2(p_max_size + 4));
  212. }
  213. int PacketPeerStream::get_input_buffer_max_size() const {
  214. return input_buffer.size() - 4;
  215. }
  216. void PacketPeerStream::set_output_buffer_max_size(int p_max_size) {
  217. output_buffer.resize(next_power_of_2(p_max_size + 4));
  218. }
  219. int PacketPeerStream::get_output_buffer_max_size() const {
  220. return output_buffer.size() - 4;
  221. }
  222. PacketPeerStream::PacketPeerStream() {
  223. int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2");
  224. ring_buffer.resize(rbsize);
  225. input_buffer.resize(1 << rbsize);
  226. output_buffer.resize(1 << rbsize);
  227. }