stream_peer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*************************************************************************/
  2. /* stream_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 "stream_peer.h"
  31. #include "io/marshalls.h"
  32. Error StreamPeer::_put_data(const DVector<uint8_t> &p_data) {
  33. int len = p_data.size();
  34. if (len == 0)
  35. return OK;
  36. DVector<uint8_t>::Read r = p_data.read();
  37. return put_data(&r[0], len);
  38. }
  39. Array StreamPeer::_put_partial_data(const DVector<uint8_t> &p_data) {
  40. Array ret;
  41. int len = p_data.size();
  42. if (len == 0) {
  43. ret.push_back(OK);
  44. ret.push_back(0);
  45. return ret;
  46. }
  47. DVector<uint8_t>::Read r = p_data.read();
  48. int sent;
  49. Error err = put_partial_data(&r[0], len, sent);
  50. if (err != OK) {
  51. sent = 0;
  52. }
  53. ret.push_back(err);
  54. ret.push_back(sent);
  55. return ret;
  56. }
  57. Array StreamPeer::_get_data(int p_bytes) {
  58. Array ret;
  59. DVector<uint8_t> data;
  60. data.resize(p_bytes);
  61. if (data.size() != p_bytes) {
  62. ret.push_back(ERR_OUT_OF_MEMORY);
  63. ret.push_back(DVector<uint8_t>());
  64. return ret;
  65. }
  66. DVector<uint8_t>::Write w = data.write();
  67. Error err = get_data(&w[0], p_bytes);
  68. w = DVector<uint8_t>::Write();
  69. ret.push_back(err);
  70. ret.push_back(data);
  71. return ret;
  72. }
  73. Array StreamPeer::_get_partial_data(int p_bytes) {
  74. Array ret;
  75. DVector<uint8_t> data;
  76. data.resize(p_bytes);
  77. if (data.size() != p_bytes) {
  78. ret.push_back(ERR_OUT_OF_MEMORY);
  79. ret.push_back(DVector<uint8_t>());
  80. return ret;
  81. }
  82. DVector<uint8_t>::Write w = data.write();
  83. int received;
  84. Error err = get_partial_data(&w[0], p_bytes, received);
  85. w = DVector<uint8_t>::Write();
  86. if (err != OK) {
  87. data.resize(0);
  88. } else if (received != data.size()) {
  89. data.resize(received);
  90. }
  91. ret.push_back(err);
  92. ret.push_back(data);
  93. return ret;
  94. }
  95. void StreamPeer::set_big_endian(bool p_enable) {
  96. big_endian = p_enable;
  97. }
  98. bool StreamPeer::is_big_endian_enabled() const {
  99. return big_endian;
  100. }
  101. void StreamPeer::put_u8(uint8_t p_val) {
  102. put_data((const uint8_t *)&p_val, 1);
  103. }
  104. void StreamPeer::put_8(int8_t p_val) {
  105. put_data((const uint8_t *)&p_val, 1);
  106. }
  107. void StreamPeer::put_u16(uint16_t p_val) {
  108. if (big_endian) {
  109. p_val = BSWAP16(p_val);
  110. }
  111. uint8_t buf[2];
  112. encode_uint16(p_val, buf);
  113. put_data(buf, 2);
  114. }
  115. void StreamPeer::put_16(int16_t p_val) {
  116. if (big_endian) {
  117. p_val = BSWAP16(p_val);
  118. }
  119. uint8_t buf[2];
  120. encode_uint16(p_val, buf);
  121. put_data(buf, 2);
  122. }
  123. void StreamPeer::put_u32(uint32_t p_val) {
  124. if (big_endian) {
  125. p_val = BSWAP32(p_val);
  126. }
  127. uint8_t buf[4];
  128. encode_uint32(p_val, buf);
  129. put_data(buf, 4);
  130. }
  131. void StreamPeer::put_32(int32_t p_val) {
  132. if (big_endian) {
  133. p_val = BSWAP32(p_val);
  134. }
  135. uint8_t buf[4];
  136. encode_uint32(p_val, buf);
  137. put_data(buf, 4);
  138. }
  139. void StreamPeer::put_u64(uint64_t p_val) {
  140. if (big_endian) {
  141. p_val = BSWAP64(p_val);
  142. }
  143. uint8_t buf[8];
  144. encode_uint64(p_val, buf);
  145. put_data(buf, 8);
  146. }
  147. void StreamPeer::put_64(int64_t p_val) {
  148. if (big_endian) {
  149. p_val = BSWAP64(p_val);
  150. }
  151. uint8_t buf[8];
  152. encode_uint64(p_val, buf);
  153. put_data(buf, 8);
  154. }
  155. void StreamPeer::put_float(float p_val) {
  156. uint8_t buf[4];
  157. encode_float(p_val, buf);
  158. if (big_endian) {
  159. uint32_t *p32 = (uint32_t *)buf;
  160. *p32 = BSWAP32(*p32);
  161. }
  162. put_data(buf, 4);
  163. }
  164. void StreamPeer::put_double(double p_val) {
  165. uint8_t buf[8];
  166. encode_double(p_val, buf);
  167. if (big_endian) {
  168. uint64_t *p64 = (uint64_t *)buf;
  169. *p64 = BSWAP64(*p64);
  170. }
  171. put_data(buf, 8);
  172. }
  173. void StreamPeer::put_utf8_string(const String &p_string) {
  174. CharString cs = p_string.utf8();
  175. put_data((const uint8_t *)cs.get_data(), cs.length());
  176. }
  177. void StreamPeer::put_var(const Variant &p_variant) {
  178. int len = 0;
  179. Vector<uint8_t> buf;
  180. encode_variant(p_variant, NULL, len);
  181. buf.resize(len);
  182. put_32(len);
  183. encode_variant(p_variant, buf.ptr(), len);
  184. put_data(buf.ptr(), buf.size());
  185. }
  186. uint8_t StreamPeer::get_u8() {
  187. uint8_t buf[1];
  188. get_data(buf, 1);
  189. return buf[0];
  190. }
  191. int8_t StreamPeer::get_8() {
  192. uint8_t buf[1];
  193. get_data(buf, 1);
  194. return buf[0];
  195. }
  196. uint16_t StreamPeer::get_u16() {
  197. uint8_t buf[2];
  198. get_data(buf, 2);
  199. uint16_t r = decode_uint16(buf);
  200. if (big_endian) {
  201. r = BSWAP16(r);
  202. }
  203. return r;
  204. }
  205. int16_t StreamPeer::get_16() {
  206. uint8_t buf[2];
  207. get_data(buf, 2);
  208. uint16_t r = decode_uint16(buf);
  209. if (big_endian) {
  210. r = BSWAP16(r);
  211. }
  212. return r;
  213. }
  214. uint32_t StreamPeer::get_u32() {
  215. uint8_t buf[4];
  216. get_data(buf, 4);
  217. uint32_t r = decode_uint32(buf);
  218. if (big_endian) {
  219. r = BSWAP32(r);
  220. }
  221. return r;
  222. }
  223. int32_t StreamPeer::get_32() {
  224. uint8_t buf[4];
  225. get_data(buf, 4);
  226. uint32_t r = decode_uint32(buf);
  227. if (big_endian) {
  228. r = BSWAP32(r);
  229. }
  230. return r;
  231. }
  232. uint64_t StreamPeer::get_u64() {
  233. uint8_t buf[8];
  234. get_data(buf, 8);
  235. uint64_t r = decode_uint64(buf);
  236. if (big_endian) {
  237. r = BSWAP64(r);
  238. }
  239. return r;
  240. }
  241. int64_t StreamPeer::get_64() {
  242. uint8_t buf[8];
  243. get_data(buf, 8);
  244. uint64_t r = decode_uint64(buf);
  245. if (big_endian) {
  246. r = BSWAP64(r);
  247. }
  248. return r;
  249. }
  250. float StreamPeer::get_float() {
  251. uint8_t buf[4];
  252. get_data(buf, 4);
  253. if (big_endian) {
  254. uint32_t *p32 = (uint32_t *)buf;
  255. *p32 = BSWAP32(*p32);
  256. }
  257. return decode_float(buf);
  258. }
  259. float StreamPeer::get_double() {
  260. uint8_t buf[8];
  261. get_data(buf, 8);
  262. if (big_endian) {
  263. uint64_t *p64 = (uint64_t *)buf;
  264. *p64 = BSWAP64(*p64);
  265. }
  266. return decode_double(buf);
  267. }
  268. String StreamPeer::get_string(int p_bytes) {
  269. ERR_FAIL_COND_V(p_bytes < 0, String());
  270. Vector<char> buf;
  271. buf.resize(p_bytes + 1);
  272. get_data((uint8_t *)&buf[0], p_bytes);
  273. buf[p_bytes] = 0;
  274. return buf.ptr();
  275. }
  276. String StreamPeer::get_utf8_string(int p_bytes) {
  277. ERR_FAIL_COND_V(p_bytes < 0, String());
  278. Vector<uint8_t> buf;
  279. buf.resize(p_bytes);
  280. get_data(buf.ptr(), p_bytes);
  281. String ret;
  282. ret.parse_utf8((const char *)buf.ptr(), buf.size());
  283. return ret;
  284. }
  285. Variant StreamPeer::get_var() {
  286. int len = get_32();
  287. Vector<uint8_t> var;
  288. var.resize(len);
  289. get_data(var.ptr(), len);
  290. Variant ret;
  291. decode_variant(ret, var.ptr(), len);
  292. return ret;
  293. }
  294. void StreamPeer::_bind_methods() {
  295. ObjectTypeDB::bind_method(_MD("put_data", "data"), &StreamPeer::_put_data);
  296. ObjectTypeDB::bind_method(_MD("put_partial_data", "data"), &StreamPeer::_put_partial_data);
  297. ObjectTypeDB::bind_method(_MD("get_data", "bytes"), &StreamPeer::_get_data);
  298. ObjectTypeDB::bind_method(_MD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data);
  299. ObjectTypeDB::bind_method(_MD("get_available_bytes"), &StreamPeer::get_available_bytes);
  300. ObjectTypeDB::bind_method(_MD("set_big_endian", "enable"), &StreamPeer::set_big_endian);
  301. ObjectTypeDB::bind_method(_MD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled);
  302. ObjectTypeDB::bind_method(_MD("put_8", "val"), &StreamPeer::put_8);
  303. ObjectTypeDB::bind_method(_MD("put_u8", "val"), &StreamPeer::put_u8);
  304. ObjectTypeDB::bind_method(_MD("put_16", "val"), &StreamPeer::put_16);
  305. ObjectTypeDB::bind_method(_MD("put_u16", "val"), &StreamPeer::put_u16);
  306. ObjectTypeDB::bind_method(_MD("put_32", "val"), &StreamPeer::put_32);
  307. ObjectTypeDB::bind_method(_MD("put_u32", "val"), &StreamPeer::put_u32);
  308. ObjectTypeDB::bind_method(_MD("put_64", "val"), &StreamPeer::put_64);
  309. ObjectTypeDB::bind_method(_MD("put_u64", "val"), &StreamPeer::put_u64);
  310. ObjectTypeDB::bind_method(_MD("put_float", "val"), &StreamPeer::put_float);
  311. ObjectTypeDB::bind_method(_MD("put_double", "val"), &StreamPeer::put_double);
  312. ObjectTypeDB::bind_method(_MD("put_utf8_string", "val"), &StreamPeer::put_utf8_string);
  313. ObjectTypeDB::bind_method(_MD("put_var", "val:Variant"), &StreamPeer::put_var);
  314. ObjectTypeDB::bind_method(_MD("get_8"), &StreamPeer::get_8);
  315. ObjectTypeDB::bind_method(_MD("get_u8"), &StreamPeer::get_u8);
  316. ObjectTypeDB::bind_method(_MD("get_16"), &StreamPeer::get_16);
  317. ObjectTypeDB::bind_method(_MD("get_u16"), &StreamPeer::get_u16);
  318. ObjectTypeDB::bind_method(_MD("get_32"), &StreamPeer::get_32);
  319. ObjectTypeDB::bind_method(_MD("get_u32"), &StreamPeer::get_u32);
  320. ObjectTypeDB::bind_method(_MD("get_64"), &StreamPeer::get_64);
  321. ObjectTypeDB::bind_method(_MD("get_u64"), &StreamPeer::get_u64);
  322. ObjectTypeDB::bind_method(_MD("get_float"), &StreamPeer::get_float);
  323. ObjectTypeDB::bind_method(_MD("get_double"), &StreamPeer::get_double);
  324. ObjectTypeDB::bind_method(_MD("get_string", "bytes"), &StreamPeer::get_string);
  325. ObjectTypeDB::bind_method(_MD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string);
  326. ObjectTypeDB::bind_method(_MD("get_var:Variant"), &StreamPeer::get_var);
  327. }
  328. ////////////////////////////////
  329. void StreamPeerBuffer::_bind_methods() {
  330. ObjectTypeDB::bind_method(_MD("seek", "pos"), &StreamPeerBuffer::seek);
  331. ObjectTypeDB::bind_method(_MD("get_size"), &StreamPeerBuffer::get_size);
  332. ObjectTypeDB::bind_method(_MD("get_pos"), &StreamPeerBuffer::get_pos);
  333. ObjectTypeDB::bind_method(_MD("resize", "size"), &StreamPeerBuffer::resize);
  334. ObjectTypeDB::bind_method(_MD("set_data_array", "data"), &StreamPeerBuffer::set_data_array);
  335. ObjectTypeDB::bind_method(_MD("get_data_array"), &StreamPeerBuffer::get_data_array);
  336. ObjectTypeDB::bind_method(_MD("clear"), &StreamPeerBuffer::clear);
  337. ObjectTypeDB::bind_method(_MD("duplicate"), &StreamPeerBuffer::duplicate);
  338. }
  339. Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) {
  340. if (p_bytes <= 0)
  341. return OK;
  342. if (pointer + p_bytes > data.size()) {
  343. data.resize(pointer + p_bytes);
  344. }
  345. DVector<uint8_t>::Write w = data.write();
  346. copymem(&w[pointer], p_data, p_bytes);
  347. pointer += p_bytes;
  348. return OK;
  349. }
  350. Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  351. r_sent = p_bytes;
  352. return put_data(p_data, p_bytes);
  353. }
  354. Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) {
  355. int recv;
  356. get_partial_data(p_buffer, p_bytes, recv);
  357. if (recv != p_bytes)
  358. return ERR_INVALID_PARAMETER;
  359. return OK;
  360. }
  361. Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  362. if (pointer + p_bytes > data.size()) {
  363. r_received = data.size() - pointer;
  364. if (r_received <= 0) {
  365. r_received = 0;
  366. return OK; //you got 0
  367. }
  368. } else {
  369. r_received = p_bytes;
  370. }
  371. DVector<uint8_t>::Read r = data.read();
  372. copymem(p_buffer, r.ptr() + pointer, r_received);
  373. pointer += r_received;
  374. // FIXME: return what? OK or ERR_*
  375. }
  376. int StreamPeerBuffer::get_available_bytes() const {
  377. return data.size() - pointer;
  378. }
  379. void StreamPeerBuffer::seek(int p_pos) {
  380. ERR_FAIL_COND(p_pos < 0);
  381. ERR_FAIL_COND(p_pos > data.size());
  382. pointer = p_pos;
  383. }
  384. int StreamPeerBuffer::get_size() const {
  385. return data.size();
  386. }
  387. int StreamPeerBuffer::get_pos() const {
  388. return pointer;
  389. }
  390. void StreamPeerBuffer::resize(int p_size) {
  391. data.resize(p_size);
  392. }
  393. void StreamPeerBuffer::set_data_array(const DVector<uint8_t> &p_data) {
  394. data = p_data;
  395. pointer = 0;
  396. }
  397. DVector<uint8_t> StreamPeerBuffer::get_data_array() const {
  398. return data;
  399. }
  400. void StreamPeerBuffer::clear() {
  401. data.resize(0);
  402. pointer = 0;
  403. }
  404. Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
  405. Ref<StreamPeerBuffer> spb;
  406. spb.instance();
  407. spb->data = data;
  408. return spb;
  409. }
  410. StreamPeerBuffer::StreamPeerBuffer() {
  411. pointer = 0;
  412. }