packet_peer.cpp 7.4 KB

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