stream_peer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*************************************************************************/
  2. /* stream_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 "stream_peer.h"
  31. #include "core/io/marshalls.h"
  32. Error StreamPeer::_put_data(const PoolVector<uint8_t> &p_data) {
  33. int len = p_data.size();
  34. if (len == 0)
  35. return OK;
  36. PoolVector<uint8_t>::Read r = p_data.read();
  37. return put_data(&r[0], len);
  38. }
  39. Array StreamPeer::_put_partial_data(const PoolVector<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. PoolVector<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. PoolVector<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(PoolVector<uint8_t>());
  64. return ret;
  65. }
  66. PoolVector<uint8_t>::Write w = data.write();
  67. Error err = get_data(&w[0], p_bytes);
  68. w = PoolVector<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. PoolVector<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(PoolVector<uint8_t>());
  80. return ret;
  81. }
  82. PoolVector<uint8_t>::Write w = data.write();
  83. int received;
  84. Error err = get_partial_data(&w[0], p_bytes, received);
  85. w = PoolVector<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_string(const String &p_string) {
  174. CharString cs = p_string.ascii();
  175. put_u32(cs.length());
  176. put_data((const uint8_t *)cs.get_data(), cs.length());
  177. }
  178. void StreamPeer::put_utf8_string(const String &p_string) {
  179. CharString cs = p_string.utf8();
  180. put_u32(cs.length());
  181. put_data((const uint8_t *)cs.get_data(), cs.length());
  182. }
  183. void StreamPeer::put_var(const Variant &p_variant) {
  184. int len = 0;
  185. Vector<uint8_t> buf;
  186. encode_variant(p_variant, NULL, len);
  187. buf.resize(len);
  188. put_32(len);
  189. encode_variant(p_variant, buf.ptrw(), len);
  190. put_data(buf.ptr(), buf.size());
  191. }
  192. uint8_t StreamPeer::get_u8() {
  193. uint8_t buf[1];
  194. get_data(buf, 1);
  195. return buf[0];
  196. }
  197. int8_t StreamPeer::get_8() {
  198. uint8_t buf[1];
  199. get_data(buf, 1);
  200. return buf[0];
  201. }
  202. uint16_t StreamPeer::get_u16() {
  203. uint8_t buf[2];
  204. get_data(buf, 2);
  205. uint16_t r = decode_uint16(buf);
  206. if (big_endian) {
  207. r = BSWAP16(r);
  208. }
  209. return r;
  210. }
  211. int16_t StreamPeer::get_16() {
  212. uint8_t buf[2];
  213. get_data(buf, 2);
  214. uint16_t r = decode_uint16(buf);
  215. if (big_endian) {
  216. r = BSWAP16(r);
  217. }
  218. return r;
  219. }
  220. uint32_t StreamPeer::get_u32() {
  221. uint8_t buf[4];
  222. get_data(buf, 4);
  223. uint32_t r = decode_uint32(buf);
  224. if (big_endian) {
  225. r = BSWAP32(r);
  226. }
  227. return r;
  228. }
  229. int32_t StreamPeer::get_32() {
  230. uint8_t buf[4];
  231. get_data(buf, 4);
  232. uint32_t r = decode_uint32(buf);
  233. if (big_endian) {
  234. r = BSWAP32(r);
  235. }
  236. return r;
  237. }
  238. uint64_t StreamPeer::get_u64() {
  239. uint8_t buf[8];
  240. get_data(buf, 8);
  241. uint64_t r = decode_uint64(buf);
  242. if (big_endian) {
  243. r = BSWAP64(r);
  244. }
  245. return r;
  246. }
  247. int64_t StreamPeer::get_64() {
  248. uint8_t buf[8];
  249. get_data(buf, 8);
  250. uint64_t r = decode_uint64(buf);
  251. if (big_endian) {
  252. r = BSWAP64(r);
  253. }
  254. return r;
  255. }
  256. float StreamPeer::get_float() {
  257. uint8_t buf[4];
  258. get_data(buf, 4);
  259. if (big_endian) {
  260. uint32_t *p32 = (uint32_t *)buf;
  261. *p32 = BSWAP32(*p32);
  262. }
  263. return decode_float(buf);
  264. }
  265. double StreamPeer::get_double() {
  266. uint8_t buf[8];
  267. get_data(buf, 8);
  268. if (big_endian) {
  269. uint64_t *p64 = (uint64_t *)buf;
  270. *p64 = BSWAP64(*p64);
  271. }
  272. return decode_double(buf);
  273. }
  274. String StreamPeer::get_string(int p_bytes) {
  275. if (p_bytes < 0)
  276. p_bytes = get_u32();
  277. ERR_FAIL_COND_V(p_bytes < 0, String());
  278. Vector<char> buf;
  279. Error err = buf.resize(p_bytes + 1);
  280. ERR_FAIL_COND_V(err != OK, String());
  281. err = get_data((uint8_t *)&buf[0], p_bytes);
  282. ERR_FAIL_COND_V(err != OK, String());
  283. buf.write[p_bytes] = 0;
  284. return buf.ptr();
  285. }
  286. String StreamPeer::get_utf8_string(int p_bytes) {
  287. if (p_bytes < 0)
  288. p_bytes = get_u32();
  289. ERR_FAIL_COND_V(p_bytes < 0, String());
  290. Vector<uint8_t> buf;
  291. Error err = buf.resize(p_bytes);
  292. ERR_FAIL_COND_V(err != OK, String());
  293. err = get_data(buf.ptrw(), p_bytes);
  294. ERR_FAIL_COND_V(err != OK, String());
  295. String ret;
  296. ret.parse_utf8((const char *)buf.ptr(), buf.size());
  297. return ret;
  298. }
  299. Variant StreamPeer::get_var() {
  300. int len = get_32();
  301. Vector<uint8_t> var;
  302. Error err = var.resize(len);
  303. ERR_FAIL_COND_V(err != OK, Variant());
  304. err = get_data(var.ptrw(), len);
  305. ERR_FAIL_COND_V(err != OK, Variant());
  306. Variant ret;
  307. decode_variant(ret, var.ptr(), len);
  308. return ret;
  309. }
  310. void StreamPeer::_bind_methods() {
  311. ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data);
  312. ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data);
  313. ClassDB::bind_method(D_METHOD("get_data", "bytes"), &StreamPeer::_get_data);
  314. ClassDB::bind_method(D_METHOD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data);
  315. ClassDB::bind_method(D_METHOD("get_available_bytes"), &StreamPeer::get_available_bytes);
  316. ClassDB::bind_method(D_METHOD("set_big_endian", "enable"), &StreamPeer::set_big_endian);
  317. ClassDB::bind_method(D_METHOD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled);
  318. ClassDB::bind_method(D_METHOD("put_8", "value"), &StreamPeer::put_8);
  319. ClassDB::bind_method(D_METHOD("put_u8", "value"), &StreamPeer::put_u8);
  320. ClassDB::bind_method(D_METHOD("put_16", "value"), &StreamPeer::put_16);
  321. ClassDB::bind_method(D_METHOD("put_u16", "value"), &StreamPeer::put_u16);
  322. ClassDB::bind_method(D_METHOD("put_32", "value"), &StreamPeer::put_32);
  323. ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32);
  324. ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64);
  325. ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64);
  326. ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float);
  327. ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double);
  328. ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string);
  329. ClassDB::bind_method(D_METHOD("put_utf8_string", "value"), &StreamPeer::put_utf8_string);
  330. ClassDB::bind_method(D_METHOD("put_var", "value"), &StreamPeer::put_var);
  331. ClassDB::bind_method(D_METHOD("get_8"), &StreamPeer::get_8);
  332. ClassDB::bind_method(D_METHOD("get_u8"), &StreamPeer::get_u8);
  333. ClassDB::bind_method(D_METHOD("get_16"), &StreamPeer::get_16);
  334. ClassDB::bind_method(D_METHOD("get_u16"), &StreamPeer::get_u16);
  335. ClassDB::bind_method(D_METHOD("get_32"), &StreamPeer::get_32);
  336. ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32);
  337. ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64);
  338. ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64);
  339. ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float);
  340. ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double);
  341. ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1));
  342. ClassDB::bind_method(D_METHOD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string, DEFVAL(-1));
  343. ClassDB::bind_method(D_METHOD("get_var"), &StreamPeer::get_var);
  344. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled");
  345. }
  346. ////////////////////////////////
  347. void StreamPeerBuffer::_bind_methods() {
  348. ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek);
  349. ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size);
  350. ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position);
  351. ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize);
  352. ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array);
  353. ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array);
  354. ClassDB::bind_method(D_METHOD("clear"), &StreamPeerBuffer::clear);
  355. ClassDB::bind_method(D_METHOD("duplicate"), &StreamPeerBuffer::duplicate);
  356. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data_array"), "set_data_array", "get_data_array");
  357. }
  358. Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) {
  359. if (p_bytes <= 0)
  360. return OK;
  361. if (pointer + p_bytes > data.size()) {
  362. data.resize(pointer + p_bytes);
  363. }
  364. PoolVector<uint8_t>::Write w = data.write();
  365. copymem(&w[pointer], p_data, p_bytes);
  366. pointer += p_bytes;
  367. return OK;
  368. }
  369. Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  370. r_sent = p_bytes;
  371. return put_data(p_data, p_bytes);
  372. }
  373. Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) {
  374. int recv;
  375. get_partial_data(p_buffer, p_bytes, recv);
  376. if (recv != p_bytes)
  377. return ERR_INVALID_PARAMETER;
  378. return OK;
  379. }
  380. Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  381. if (pointer + p_bytes > data.size()) {
  382. r_received = data.size() - pointer;
  383. if (r_received <= 0) {
  384. r_received = 0;
  385. return OK; //you got 0
  386. }
  387. } else {
  388. r_received = p_bytes;
  389. }
  390. PoolVector<uint8_t>::Read r = data.read();
  391. copymem(p_buffer, r.ptr() + pointer, r_received);
  392. pointer += r_received;
  393. // FIXME: return what? OK or ERR_*
  394. // return OK for now so we don't maybe return garbage
  395. return OK;
  396. }
  397. int StreamPeerBuffer::get_available_bytes() const {
  398. return data.size() - pointer;
  399. }
  400. void StreamPeerBuffer::seek(int p_pos) {
  401. ERR_FAIL_COND(p_pos < 0);
  402. ERR_FAIL_COND(p_pos > data.size());
  403. pointer = p_pos;
  404. }
  405. int StreamPeerBuffer::get_size() const {
  406. return data.size();
  407. }
  408. int StreamPeerBuffer::get_position() const {
  409. return pointer;
  410. }
  411. void StreamPeerBuffer::resize(int p_size) {
  412. data.resize(p_size);
  413. }
  414. void StreamPeerBuffer::set_data_array(const PoolVector<uint8_t> &p_data) {
  415. data = p_data;
  416. pointer = 0;
  417. }
  418. PoolVector<uint8_t> StreamPeerBuffer::get_data_array() const {
  419. return data;
  420. }
  421. void StreamPeerBuffer::clear() {
  422. data.resize(0);
  423. pointer = 0;
  424. }
  425. Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
  426. Ref<StreamPeerBuffer> spb;
  427. spb.instance();
  428. spb->data = data;
  429. return spb;
  430. }
  431. StreamPeerBuffer::StreamPeerBuffer() {
  432. pointer = 0;
  433. }