stream_peer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /**************************************************************************/
  2. /* stream_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 "stream_peer.h"
  31. #include "core/io/marshalls.h"
  32. Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) {
  33. int len = p_data.size();
  34. if (len == 0) {
  35. return OK;
  36. }
  37. const uint8_t *r = p_data.ptr();
  38. return put_data(&r[0], len);
  39. }
  40. Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) {
  41. Array ret;
  42. int len = p_data.size();
  43. if (len == 0) {
  44. ret.push_back(OK);
  45. ret.push_back(0);
  46. return ret;
  47. }
  48. const uint8_t *r = p_data.ptr();
  49. int sent;
  50. Error err = put_partial_data(&r[0], len, sent);
  51. if (err != OK) {
  52. sent = 0;
  53. }
  54. ret.push_back(err);
  55. ret.push_back(sent);
  56. return ret;
  57. }
  58. Array StreamPeer::_get_data(int p_bytes) {
  59. Array ret;
  60. Vector<uint8_t> data;
  61. data.resize(p_bytes);
  62. if (data.size() != p_bytes) {
  63. ret.push_back(ERR_OUT_OF_MEMORY);
  64. ret.push_back(Vector<uint8_t>());
  65. return ret;
  66. }
  67. uint8_t *w = data.ptrw();
  68. Error err = get_data(&w[0], p_bytes);
  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. Vector<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(Vector<uint8_t>());
  80. return ret;
  81. }
  82. uint8_t *w = data.ptrw();
  83. int received;
  84. Error err = get_partial_data(&w[0], p_bytes, received);
  85. if (err != OK) {
  86. data.clear();
  87. } else if (received != data.size()) {
  88. data.resize(received);
  89. }
  90. ret.push_back(err);
  91. ret.push_back(data);
  92. return ret;
  93. }
  94. void StreamPeer::set_big_endian(bool p_big_endian) {
  95. big_endian = p_big_endian;
  96. }
  97. bool StreamPeer::is_big_endian_enabled() const {
  98. return big_endian;
  99. }
  100. void StreamPeer::put_u8(uint8_t p_val) {
  101. put_data((const uint8_t *)&p_val, 1);
  102. }
  103. void StreamPeer::put_8(int8_t p_val) {
  104. put_data((const uint8_t *)&p_val, 1);
  105. }
  106. void StreamPeer::put_u16(uint16_t p_val) {
  107. if (big_endian) {
  108. p_val = BSWAP16(p_val);
  109. }
  110. uint8_t buf[2];
  111. encode_uint16(p_val, buf);
  112. put_data(buf, 2);
  113. }
  114. void StreamPeer::put_16(int16_t p_val) {
  115. if (big_endian) {
  116. p_val = BSWAP16(p_val);
  117. }
  118. uint8_t buf[2];
  119. encode_uint16(p_val, buf);
  120. put_data(buf, 2);
  121. }
  122. void StreamPeer::put_u32(uint32_t p_val) {
  123. if (big_endian) {
  124. p_val = BSWAP32(p_val);
  125. }
  126. uint8_t buf[4];
  127. encode_uint32(p_val, buf);
  128. put_data(buf, 4);
  129. }
  130. void StreamPeer::put_32(int32_t p_val) {
  131. if (big_endian) {
  132. p_val = BSWAP32(p_val);
  133. }
  134. uint8_t buf[4];
  135. encode_uint32(p_val, buf);
  136. put_data(buf, 4);
  137. }
  138. void StreamPeer::put_u64(uint64_t p_val) {
  139. if (big_endian) {
  140. p_val = BSWAP64(p_val);
  141. }
  142. uint8_t buf[8];
  143. encode_uint64(p_val, buf);
  144. put_data(buf, 8);
  145. }
  146. void StreamPeer::put_64(int64_t p_val) {
  147. if (big_endian) {
  148. p_val = BSWAP64(p_val);
  149. }
  150. uint8_t buf[8];
  151. encode_uint64(p_val, buf);
  152. put_data(buf, 8);
  153. }
  154. void StreamPeer::put_float(float p_val) {
  155. uint8_t buf[4];
  156. encode_float(p_val, buf);
  157. if (big_endian) {
  158. uint32_t *p32 = (uint32_t *)buf;
  159. *p32 = BSWAP32(*p32);
  160. }
  161. put_data(buf, 4);
  162. }
  163. void StreamPeer::put_double(double p_val) {
  164. uint8_t buf[8];
  165. encode_double(p_val, buf);
  166. if (big_endian) {
  167. uint64_t *p64 = (uint64_t *)buf;
  168. *p64 = BSWAP64(*p64);
  169. }
  170. put_data(buf, 8);
  171. }
  172. void StreamPeer::put_string(const String &p_string) {
  173. CharString cs = p_string.ascii();
  174. put_u32(cs.length());
  175. put_data((const uint8_t *)cs.get_data(), cs.length());
  176. }
  177. void StreamPeer::put_utf8_string(const String &p_string) {
  178. CharString cs = p_string.utf8();
  179. put_u32(cs.length());
  180. put_data((const uint8_t *)cs.get_data(), cs.length());
  181. }
  182. void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) {
  183. int len = 0;
  184. Vector<uint8_t> buf;
  185. encode_variant(p_variant, nullptr, len, p_full_objects);
  186. buf.resize(len);
  187. put_32(len);
  188. encode_variant(p_variant, buf.ptrw(), len, p_full_objects);
  189. put_data(buf.ptr(), buf.size());
  190. }
  191. uint8_t StreamPeer::get_u8() {
  192. uint8_t buf[1];
  193. get_data(buf, 1);
  194. return buf[0];
  195. }
  196. int8_t StreamPeer::get_8() {
  197. uint8_t buf[1];
  198. get_data(buf, 1);
  199. return buf[0];
  200. }
  201. uint16_t StreamPeer::get_u16() {
  202. uint8_t buf[2];
  203. get_data(buf, 2);
  204. uint16_t r = decode_uint16(buf);
  205. if (big_endian) {
  206. r = BSWAP16(r);
  207. }
  208. return r;
  209. }
  210. int16_t StreamPeer::get_16() {
  211. uint8_t buf[2];
  212. get_data(buf, 2);
  213. uint16_t r = decode_uint16(buf);
  214. if (big_endian) {
  215. r = BSWAP16(r);
  216. }
  217. return r;
  218. }
  219. uint32_t StreamPeer::get_u32() {
  220. uint8_t buf[4];
  221. get_data(buf, 4);
  222. uint32_t r = decode_uint32(buf);
  223. if (big_endian) {
  224. r = BSWAP32(r);
  225. }
  226. return r;
  227. }
  228. int32_t StreamPeer::get_32() {
  229. uint8_t buf[4];
  230. get_data(buf, 4);
  231. uint32_t r = decode_uint32(buf);
  232. if (big_endian) {
  233. r = BSWAP32(r);
  234. }
  235. return r;
  236. }
  237. uint64_t StreamPeer::get_u64() {
  238. uint8_t buf[8];
  239. get_data(buf, 8);
  240. uint64_t r = decode_uint64(buf);
  241. if (big_endian) {
  242. r = BSWAP64(r);
  243. }
  244. return r;
  245. }
  246. int64_t StreamPeer::get_64() {
  247. uint8_t buf[8];
  248. get_data(buf, 8);
  249. uint64_t r = decode_uint64(buf);
  250. if (big_endian) {
  251. r = BSWAP64(r);
  252. }
  253. return r;
  254. }
  255. float StreamPeer::get_float() {
  256. uint8_t buf[4];
  257. get_data(buf, 4);
  258. if (big_endian) {
  259. uint32_t *p32 = (uint32_t *)buf;
  260. *p32 = BSWAP32(*p32);
  261. }
  262. return decode_float(buf);
  263. }
  264. double StreamPeer::get_double() {
  265. uint8_t buf[8];
  266. get_data(buf, 8);
  267. if (big_endian) {
  268. uint64_t *p64 = (uint64_t *)buf;
  269. *p64 = BSWAP64(*p64);
  270. }
  271. return decode_double(buf);
  272. }
  273. String StreamPeer::get_string(int p_bytes) {
  274. if (p_bytes < 0) {
  275. p_bytes = get_u32();
  276. }
  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. }
  290. ERR_FAIL_COND_V(p_bytes < 0, String());
  291. Vector<uint8_t> buf;
  292. Error err = buf.resize(p_bytes);
  293. ERR_FAIL_COND_V(err != OK, String());
  294. err = get_data(buf.ptrw(), p_bytes);
  295. ERR_FAIL_COND_V(err != OK, String());
  296. String ret;
  297. ret.parse_utf8((const char *)buf.ptr(), buf.size());
  298. return ret;
  299. }
  300. Variant StreamPeer::get_var(bool p_allow_objects) {
  301. int len = get_32();
  302. Vector<uint8_t> var;
  303. Error err = var.resize(len);
  304. ERR_FAIL_COND_V(err != OK, Variant());
  305. err = get_data(var.ptrw(), len);
  306. ERR_FAIL_COND_V(err != OK, Variant());
  307. Variant ret;
  308. err = decode_variant(ret, var.ptr(), len, nullptr, p_allow_objects);
  309. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  310. return ret;
  311. }
  312. void StreamPeer::_bind_methods() {
  313. ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data);
  314. ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data);
  315. ClassDB::bind_method(D_METHOD("get_data", "bytes"), &StreamPeer::_get_data);
  316. ClassDB::bind_method(D_METHOD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data);
  317. ClassDB::bind_method(D_METHOD("get_available_bytes"), &StreamPeer::get_available_bytes);
  318. ClassDB::bind_method(D_METHOD("set_big_endian", "enable"), &StreamPeer::set_big_endian);
  319. ClassDB::bind_method(D_METHOD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled);
  320. ClassDB::bind_method(D_METHOD("put_8", "value"), &StreamPeer::put_8);
  321. ClassDB::bind_method(D_METHOD("put_u8", "value"), &StreamPeer::put_u8);
  322. ClassDB::bind_method(D_METHOD("put_16", "value"), &StreamPeer::put_16);
  323. ClassDB::bind_method(D_METHOD("put_u16", "value"), &StreamPeer::put_u16);
  324. ClassDB::bind_method(D_METHOD("put_32", "value"), &StreamPeer::put_32);
  325. ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32);
  326. ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64);
  327. ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64);
  328. ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float);
  329. ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double);
  330. ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string);
  331. ClassDB::bind_method(D_METHOD("put_utf8_string", "value"), &StreamPeer::put_utf8_string);
  332. ClassDB::bind_method(D_METHOD("put_var", "value", "full_objects"), &StreamPeer::put_var, DEFVAL(false));
  333. ClassDB::bind_method(D_METHOD("get_8"), &StreamPeer::get_8);
  334. ClassDB::bind_method(D_METHOD("get_u8"), &StreamPeer::get_u8);
  335. ClassDB::bind_method(D_METHOD("get_16"), &StreamPeer::get_16);
  336. ClassDB::bind_method(D_METHOD("get_u16"), &StreamPeer::get_u16);
  337. ClassDB::bind_method(D_METHOD("get_32"), &StreamPeer::get_32);
  338. ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32);
  339. ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64);
  340. ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64);
  341. ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float);
  342. ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double);
  343. ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1));
  344. ClassDB::bind_method(D_METHOD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string, DEFVAL(-1));
  345. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &StreamPeer::get_var, DEFVAL(false));
  346. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled");
  347. }
  348. ////////////////////////////////
  349. Error StreamPeerExtension::get_data(uint8_t *r_buffer, int p_bytes) {
  350. Error err;
  351. int received = 0;
  352. if (GDVIRTUAL_CALL(_get_data, r_buffer, p_bytes, &received, err)) {
  353. return err;
  354. }
  355. WARN_PRINT_ONCE("StreamPeerExtension::_get_data is unimplemented!");
  356. return FAILED;
  357. }
  358. Error StreamPeerExtension::get_partial_data(uint8_t *r_buffer, int p_bytes, int &r_received) {
  359. Error err;
  360. if (GDVIRTUAL_CALL(_get_partial_data, r_buffer, p_bytes, &r_received, err)) {
  361. return err;
  362. }
  363. WARN_PRINT_ONCE("StreamPeerExtension::_get_partial_data is unimplemented!");
  364. return FAILED;
  365. }
  366. Error StreamPeerExtension::put_data(const uint8_t *p_data, int p_bytes) {
  367. Error err;
  368. int sent = 0;
  369. if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &sent, err)) {
  370. return err;
  371. }
  372. WARN_PRINT_ONCE("StreamPeerExtension::_put_data is unimplemented!");
  373. return FAILED;
  374. }
  375. Error StreamPeerExtension::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  376. Error err;
  377. if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &r_sent, err)) {
  378. return err;
  379. }
  380. WARN_PRINT_ONCE("StreamPeerExtension::_put_partial_data is unimplemented!");
  381. return FAILED;
  382. }
  383. void StreamPeerExtension::_bind_methods() {
  384. GDVIRTUAL_BIND(_get_data, "r_buffer", "r_bytes", "r_received");
  385. GDVIRTUAL_BIND(_get_partial_data, "r_buffer", "r_bytes", "r_received");
  386. GDVIRTUAL_BIND(_put_data, "p_data", "p_bytes", "r_sent");
  387. GDVIRTUAL_BIND(_put_partial_data, "p_data", "p_bytes", "r_sent");
  388. GDVIRTUAL_BIND(_get_available_bytes);
  389. }
  390. ////////////////////////////////
  391. void StreamPeerBuffer::_bind_methods() {
  392. ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek);
  393. ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size);
  394. ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position);
  395. ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize);
  396. ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array);
  397. ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array);
  398. ClassDB::bind_method(D_METHOD("clear"), &StreamPeerBuffer::clear);
  399. ClassDB::bind_method(D_METHOD("duplicate"), &StreamPeerBuffer::duplicate);
  400. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data_array"), "set_data_array", "get_data_array");
  401. }
  402. Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) {
  403. if (p_bytes <= 0) {
  404. return OK;
  405. }
  406. if (pointer + p_bytes > data.size()) {
  407. data.resize(pointer + p_bytes);
  408. }
  409. uint8_t *w = data.ptrw();
  410. memcpy(&w[pointer], p_data, p_bytes);
  411. pointer += p_bytes;
  412. return OK;
  413. }
  414. Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  415. r_sent = p_bytes;
  416. return put_data(p_data, p_bytes);
  417. }
  418. Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) {
  419. int recv;
  420. get_partial_data(p_buffer, p_bytes, recv);
  421. if (recv != p_bytes) {
  422. return ERR_INVALID_PARAMETER;
  423. }
  424. return OK;
  425. }
  426. Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  427. if (pointer + p_bytes > data.size()) {
  428. r_received = data.size() - pointer;
  429. if (r_received <= 0) {
  430. r_received = 0;
  431. return OK; //you got 0
  432. }
  433. } else {
  434. r_received = p_bytes;
  435. }
  436. const uint8_t *r = data.ptr();
  437. memcpy(p_buffer, r + pointer, r_received);
  438. pointer += r_received;
  439. // FIXME: return what? OK or ERR_*
  440. // return OK for now so we don't maybe return garbage
  441. return OK;
  442. }
  443. int StreamPeerBuffer::get_available_bytes() const {
  444. return data.size() - pointer;
  445. }
  446. void StreamPeerBuffer::seek(int p_pos) {
  447. ERR_FAIL_COND(p_pos < 0);
  448. ERR_FAIL_COND(p_pos > data.size());
  449. pointer = p_pos;
  450. }
  451. int StreamPeerBuffer::get_size() const {
  452. return data.size();
  453. }
  454. int StreamPeerBuffer::get_position() const {
  455. return pointer;
  456. }
  457. void StreamPeerBuffer::resize(int p_size) {
  458. data.resize(p_size);
  459. }
  460. void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) {
  461. data = p_data;
  462. pointer = 0;
  463. }
  464. Vector<uint8_t> StreamPeerBuffer::get_data_array() const {
  465. return data;
  466. }
  467. void StreamPeerBuffer::clear() {
  468. data.clear();
  469. pointer = 0;
  470. }
  471. Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
  472. Ref<StreamPeerBuffer> spb;
  473. spb.instantiate();
  474. spb->data = data;
  475. return spb;
  476. }