stream_peer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*************************************************************************/
  2. /* stream_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "stream_peer.h"
  30. #include "io/marshalls.h"
  31. Error StreamPeer::_put_data(const DVector<uint8_t>& p_data) {
  32. int len = p_data.size();
  33. if (len==0)
  34. return OK;
  35. DVector<uint8_t>::Read r = p_data.read();
  36. return put_data(&r[0],len);
  37. }
  38. Array StreamPeer::_put_partial_data(const DVector<uint8_t>& p_data) {
  39. Array ret;
  40. int len = p_data.size();
  41. if (len==0) {
  42. ret.push_back(OK);
  43. ret.push_back(0);
  44. return ret;
  45. }
  46. DVector<uint8_t>::Read r = p_data.read();
  47. int sent;
  48. Error err = put_partial_data(&r[0],len,sent);
  49. if (err!=OK) {
  50. sent=0;
  51. }
  52. ret.push_back(err);
  53. ret.push_back(sent);
  54. return ret;
  55. }
  56. Array StreamPeer::_get_data(int p_bytes) {
  57. Array ret;
  58. DVector<uint8_t> data;
  59. data.resize(p_bytes);
  60. if (data.size()!=p_bytes) {
  61. ret.push_back(ERR_OUT_OF_MEMORY);
  62. ret.push_back(DVector<uint8_t>());
  63. return ret;
  64. }
  65. DVector<uint8_t>::Write w = data.write();
  66. Error err = get_data(&w[0],p_bytes);
  67. w = DVector<uint8_t>::Write();
  68. ret.push_back(err);
  69. ret.push_back(data);
  70. return ret;
  71. }
  72. Array StreamPeer::_get_partial_data(int p_bytes) {
  73. Array ret;
  74. DVector<uint8_t> data;
  75. data.resize(p_bytes);
  76. if (data.size()!=p_bytes) {
  77. ret.push_back(ERR_OUT_OF_MEMORY);
  78. ret.push_back(DVector<uint8_t>());
  79. return ret;
  80. }
  81. DVector<uint8_t>::Write w = data.write();
  82. int received;
  83. Error err = get_partial_data(&w[0],p_bytes,received);
  84. w = DVector<uint8_t>::Write();
  85. if (err!=OK) {
  86. data.resize(0);
  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_enable) {
  95. big_endian=p_enable;
  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_utf8_string(const String& p_string) {
  173. CharString cs=p_string.utf8();
  174. put_u32(p_string.length());
  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. Error err = buf.resize(p_bytes+1);
  272. ERR_FAIL_COND_V(err!=OK,String());
  273. err = get_data((uint8_t*)&buf[0],p_bytes);
  274. ERR_FAIL_COND_V(err!=OK,String());
  275. buf[p_bytes]=0;
  276. return buf.ptr();
  277. }
  278. String StreamPeer::get_utf8_string(int p_bytes){
  279. ERR_FAIL_COND_V(p_bytes<0,String());
  280. Vector<uint8_t> buf;
  281. Error err = buf.resize(p_bytes);
  282. ERR_FAIL_COND_V(err!=OK,String());
  283. err = get_data(buf.ptr(),p_bytes);
  284. ERR_FAIL_COND_V(err!=OK,String());
  285. String ret;
  286. ret.parse_utf8((const char*)buf.ptr(),buf.size());
  287. return ret;
  288. }
  289. Variant StreamPeer::get_var(){
  290. int len = get_32();
  291. Vector<uint8_t> var;
  292. Error err = var.resize(len);
  293. ERR_FAIL_COND_V(err!=OK,Variant());
  294. err = get_data(var.ptr(),len);
  295. ERR_FAIL_COND_V(err!=OK,Variant());
  296. Variant ret;
  297. decode_variant(ret,var.ptr(),len);
  298. return ret;
  299. }
  300. void StreamPeer::_bind_methods() {
  301. ClassDB::bind_method(_MD("put_data","data"),&StreamPeer::_put_data);
  302. ClassDB::bind_method(_MD("put_partial_data","data"),&StreamPeer::_put_partial_data);
  303. ClassDB::bind_method(_MD("get_data","bytes"),&StreamPeer::_get_data);
  304. ClassDB::bind_method(_MD("get_partial_data","bytes"),&StreamPeer::_get_partial_data);
  305. ClassDB::bind_method(_MD("get_available_bytes"),&StreamPeer::get_available_bytes);
  306. ClassDB::bind_method(_MD("set_big_endian","enable"),&StreamPeer::set_big_endian);
  307. ClassDB::bind_method(_MD("is_big_endian_enabled"),&StreamPeer::is_big_endian_enabled);
  308. ClassDB::bind_method(_MD("put_8","val"),&StreamPeer::put_8);
  309. ClassDB::bind_method(_MD("put_u8","val"),&StreamPeer::put_u8);
  310. ClassDB::bind_method(_MD("put_16","val"),&StreamPeer::put_16);
  311. ClassDB::bind_method(_MD("put_u16","val"),&StreamPeer::put_u16);
  312. ClassDB::bind_method(_MD("put_32","val"),&StreamPeer::put_32);
  313. ClassDB::bind_method(_MD("put_u32","val"),&StreamPeer::put_u32);
  314. ClassDB::bind_method(_MD("put_64","val"),&StreamPeer::put_64);
  315. ClassDB::bind_method(_MD("put_u64","val"),&StreamPeer::put_u64);
  316. ClassDB::bind_method(_MD("put_float","val"),&StreamPeer::put_float);
  317. ClassDB::bind_method(_MD("put_double","val"),&StreamPeer::put_double);
  318. ClassDB::bind_method(_MD("put_utf8_string","val"),&StreamPeer::put_utf8_string);
  319. ClassDB::bind_method(_MD("put_var","val:Variant"),&StreamPeer::put_var);
  320. ClassDB::bind_method(_MD("get_8"),&StreamPeer::get_8);
  321. ClassDB::bind_method(_MD("get_u8"),&StreamPeer::get_u8);
  322. ClassDB::bind_method(_MD("get_16"),&StreamPeer::get_16);
  323. ClassDB::bind_method(_MD("get_u16"),&StreamPeer::get_u16);
  324. ClassDB::bind_method(_MD("get_32"),&StreamPeer::get_32);
  325. ClassDB::bind_method(_MD("get_u32"),&StreamPeer::get_u32);
  326. ClassDB::bind_method(_MD("get_64"),&StreamPeer::get_64);
  327. ClassDB::bind_method(_MD("get_u64"),&StreamPeer::get_u64);
  328. ClassDB::bind_method(_MD("get_float"),&StreamPeer::get_float);
  329. ClassDB::bind_method(_MD("get_double"),&StreamPeer::get_double);
  330. ClassDB::bind_method(_MD("get_string","bytes"),&StreamPeer::get_string);
  331. ClassDB::bind_method(_MD("get_utf8_string","bytes"),&StreamPeer::get_utf8_string);
  332. ClassDB::bind_method(_MD("get_var:Variant"),&StreamPeer::get_var);
  333. }
  334. ////////////////////////////////
  335. void StreamPeerBuffer::_bind_methods() {
  336. ClassDB::bind_method(_MD("seek","pos"),&StreamPeerBuffer::seek);
  337. ClassDB::bind_method(_MD("get_size"),&StreamPeerBuffer::get_size);
  338. ClassDB::bind_method(_MD("get_pos"),&StreamPeerBuffer::get_pos);
  339. ClassDB::bind_method(_MD("resize","size"),&StreamPeerBuffer::resize);
  340. ClassDB::bind_method(_MD("set_data_array","data"),&StreamPeerBuffer::set_data_array);
  341. ClassDB::bind_method(_MD("get_data_array"),&StreamPeerBuffer::get_data_array);
  342. ClassDB::bind_method(_MD("clear"),&StreamPeerBuffer::clear);
  343. ClassDB::bind_method(_MD("duplicate:StreamPeerBuffer"),&StreamPeerBuffer::duplicate);
  344. }
  345. Error StreamPeerBuffer::put_data(const uint8_t* p_data,int p_bytes) {
  346. if (p_bytes<=0)
  347. return OK;
  348. if (pointer+p_bytes > data.size()) {
  349. data.resize(pointer+p_bytes);
  350. }
  351. DVector<uint8_t>::Write w = data.write();
  352. copymem(&w[pointer],p_data,p_bytes);
  353. pointer+=p_bytes;
  354. return OK;
  355. }
  356. Error StreamPeerBuffer::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent){
  357. r_sent=p_bytes;
  358. return put_data(p_data,p_bytes);
  359. }
  360. Error StreamPeerBuffer::get_data(uint8_t* p_buffer, int p_bytes){
  361. int recv;
  362. get_partial_data(p_buffer,p_bytes,recv);
  363. if (recv!=p_bytes)
  364. return ERR_INVALID_PARAMETER;
  365. return OK;
  366. }
  367. Error StreamPeerBuffer::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received){
  368. if (pointer+p_bytes > data.size()) {
  369. r_received=data.size()-pointer;
  370. if (r_received<=0) {
  371. r_received=0;
  372. return OK; //you got 0
  373. }
  374. } else {
  375. r_received=p_bytes;
  376. }
  377. DVector<uint8_t>::Read r = data.read();
  378. copymem(p_buffer,r.ptr(),r_received);
  379. }
  380. int StreamPeerBuffer::get_available_bytes() const {
  381. return data.size()-pointer;
  382. }
  383. void StreamPeerBuffer::seek(int p_pos){
  384. ERR_FAIL_COND(p_pos < 0);
  385. ERR_FAIL_COND(p_pos > data.size());
  386. pointer=p_pos;
  387. }
  388. int StreamPeerBuffer::get_size() const{
  389. return data.size();
  390. }
  391. int StreamPeerBuffer::get_pos() const {
  392. return pointer;
  393. }
  394. void StreamPeerBuffer::resize(int p_size){
  395. data.resize(p_size);
  396. }
  397. void StreamPeerBuffer::set_data_array(const DVector<uint8_t> & p_data){
  398. data=p_data;
  399. pointer=0;
  400. }
  401. DVector<uint8_t> StreamPeerBuffer::get_data_array() const{
  402. return data;
  403. }
  404. void StreamPeerBuffer::clear() {
  405. data.resize(0);
  406. pointer=0;
  407. }
  408. Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
  409. Ref<StreamPeerBuffer> spb;
  410. spb.instance();
  411. spb->data=data;
  412. return spb;
  413. }
  414. StreamPeerBuffer::StreamPeerBuffer() {
  415. pointer=0;
  416. }