2
0

BitMessage.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Assert.h"
  24. #include <cstring>
  25. #include "NetAddress.h"
  26. #include "BitMessage.h"
  27. namespace crown
  28. {
  29. namespace network
  30. {
  31. //---------------------------------------------------------------------------------------------
  32. BitMessage::BitMessage(Allocator& allocator) :
  33. m_allocator(allocator),
  34. m_header(NULL),
  35. m_data(NULL),
  36. m_write(NULL),
  37. m_read(NULL),
  38. m_max_size(0),
  39. m_cur_size(0),
  40. m_read_count(0),
  41. m_write_bit(0),
  42. m_read_bit(0),
  43. m_overflowed(false),
  44. m_init(false)
  45. {
  46. }
  47. //---------------------------------------------------------------------------------------------
  48. BitMessage::~BitMessage()
  49. {
  50. if (m_header)
  51. {
  52. m_allocator.deallocate((void*)m_header);
  53. }
  54. if (m_data)
  55. {
  56. m_allocator.deallocate((void*)m_data);
  57. }
  58. }
  59. //---------------------------------------------------------------------------------------------
  60. uint8_t* BitMessage::get_byte_space(int32_t len)
  61. {
  62. uint8_t *ptr;
  63. if (!m_write)
  64. {
  65. printf("cannot write to message");
  66. }
  67. // round up to the next byte
  68. write_byte_align();
  69. // check for overflow
  70. check_overflow(len << 3);
  71. // allocate space
  72. ptr = m_write + m_cur_size;
  73. m_cur_size += len;
  74. return ptr;
  75. }
  76. //---------------------------------------------------------------------------------------------
  77. bool BitMessage::check_overflow(uint32_t num_bits)
  78. {
  79. if (num_bits > get_remaining_write_bits())
  80. {
  81. if (num_bits > (m_max_size << 3))
  82. {
  83. printf(" %i bits is > full message size", num_bits );
  84. }
  85. printf("overflow\n");
  86. begin_writing();
  87. m_overflowed = true;
  88. return true;
  89. }
  90. return false;
  91. }
  92. //---------------------------------------------------------------------------------------------
  93. void BitMessage::init(int32_t len)
  94. {
  95. m_header = (uint8_t*)m_allocator.allocate(12);
  96. m_data = (uint8_t*)m_allocator.allocate(len);
  97. m_write = m_data;
  98. m_read = m_data;
  99. m_max_size = len;
  100. m_init = true;
  101. }
  102. //---------------------------------------------------------------------------------------------
  103. void BitMessage::set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits)
  104. {
  105. uint8_t header[12];
  106. header[0] = (uint8_t)(id >> 24);
  107. header[1] = (uint8_t)(id >> 16);
  108. header[2] = (uint8_t)(id >> 8);
  109. header[3] = (uint8_t)id;
  110. header[4] = (uint8_t)(sequence >> 8);
  111. header[5] = (uint8_t)sequence;
  112. header[6] = (uint8_t)(ack >> 8);
  113. header[7] = (uint8_t)ack;
  114. header[8] = (uint8_t)(ack_bits >> 24);
  115. header[9] = (uint8_t)(ack_bits >> 16);
  116. header[10] = (uint8_t)(ack_bits >> 8);
  117. header[11] = (uint8_t)(ack_bits);
  118. memcpy(m_header, header, 12);
  119. }
  120. //---------------------------------------------------------------------------------------------
  121. uint8_t* BitMessage::get_header()
  122. {
  123. return m_header;
  124. }
  125. //---------------------------------------------------------------------------------------------
  126. const uint8_t* BitMessage::get_header() const
  127. {
  128. return m_header;
  129. }
  130. //---------------------------------------------------------------------------------------------
  131. uint8_t* BitMessage::get_data()
  132. {
  133. return m_write;
  134. }
  135. //---------------------------------------------------------------------------------------------
  136. const uint8_t* BitMessage::get_data() const
  137. {
  138. return m_read;
  139. }
  140. //---------------------------------------------------------------------------------------------
  141. size_t BitMessage::get_max_size() const
  142. {
  143. return m_max_size;
  144. }
  145. //---------------------------------------------------------------------------------------------
  146. bool BitMessage::is_overflowed()
  147. {
  148. return m_overflowed;
  149. }
  150. //---------------------------------------------------------------------------------------------
  151. bool BitMessage::is_init()
  152. {
  153. return m_init;
  154. }
  155. //---------------------------------------------------------------------------------------------
  156. size_t BitMessage::get_header_size() const
  157. {
  158. return 12;
  159. }
  160. //---------------------------------------------------------------------------------------------
  161. size_t BitMessage::get_size() const
  162. {
  163. return m_cur_size;
  164. }
  165. //---------------------------------------------------------------------------------------------
  166. void BitMessage::set_size(size_t size)
  167. {
  168. if (size > m_max_size)
  169. {
  170. m_cur_size = m_max_size;
  171. }
  172. else
  173. {
  174. m_cur_size = size;
  175. }
  176. }
  177. //---------------------------------------------------------------------------------------------
  178. int32_t BitMessage::get_write_bit() const
  179. {
  180. return m_write_bit;
  181. }
  182. //---------------------------------------------------------------------------------------------
  183. void BitMessage::set_write_bit(int32_t bit)
  184. {
  185. m_write_bit = bit & 7;
  186. if (m_write_bit)
  187. {
  188. m_write[m_cur_size-1] &= (1 << m_write_bit) - 1;
  189. }
  190. }
  191. //---------------------------------------------------------------------------------------------
  192. int32_t BitMessage::get_num_bits_written() const
  193. {
  194. return ((m_cur_size << 3) - ((8 - m_write_bit) & 7));
  195. }
  196. //---------------------------------------------------------------------------------------------
  197. int32_t BitMessage::get_remaining_write_bits() const
  198. {
  199. return (m_max_size << 3) - get_num_bits_written();
  200. }
  201. //---------------------------------------------------------------------------------------------
  202. void BitMessage::save_write_state(int32_t& s,int32_t& b) const
  203. {
  204. s = m_cur_size;
  205. b = m_write_bit;
  206. }
  207. //---------------------------------------------------------------------------------------------
  208. void BitMessage::restore_write_state(int32_t s,int32_t b)
  209. {
  210. m_cur_size = s;
  211. m_write_bit = b & 7;
  212. if (m_write_bit)
  213. {
  214. m_write[m_cur_size-1] &= (1 << m_write_bit) - 1;
  215. }
  216. }
  217. //---------------------------------------------------------------------------------------------
  218. int32_t BitMessage::get_read_count() const
  219. {
  220. return m_read_count;
  221. }
  222. //---------------------------------------------------------------------------------------------
  223. void BitMessage::set_read_count(int32_t bytes)
  224. {
  225. m_read_count = bytes;
  226. }
  227. //---------------------------------------------------------------------------------------------
  228. int32_t BitMessage::get_read_bit() const
  229. {
  230. return m_read_bit;
  231. }
  232. //---------------------------------------------------------------------------------------------
  233. void BitMessage::set_read_bit(int32_t bit)
  234. {
  235. m_read_bit = bit & 7;
  236. }
  237. //---------------------------------------------------------------------------------------------
  238. int32_t BitMessage::get_num_bits_read() const
  239. {
  240. return ((m_read_count << 3) - ((8 - m_read_bit) & 7));
  241. }
  242. //---------------------------------------------------------------------------------------------
  243. int32_t BitMessage::get_remaining_read_bits() const
  244. {
  245. return (m_cur_size << 3) - get_num_bits_read();
  246. }
  247. //---------------------------------------------------------------------------------------------
  248. void BitMessage::save_read_state(int32_t& c, int32_t& b) const
  249. {
  250. c = m_read_count;
  251. b = m_read_bit;
  252. }
  253. //---------------------------------------------------------------------------------------------
  254. void BitMessage::restore_read_state(int32_t c, int32_t b)
  255. {
  256. m_read_count = c;
  257. m_read_bit = b & 7;
  258. }
  259. //---------------------------------------------------------------------------------------------
  260. void BitMessage::begin_writing()
  261. {
  262. m_cur_size = 0;
  263. m_write_bit = 0;
  264. m_overflowed = false;
  265. }
  266. //---------------------------------------------------------------------------------------------
  267. int32_t BitMessage::get_remaining_space() const
  268. {
  269. return m_max_size - m_cur_size;
  270. }
  271. //---------------------------------------------------------------------------------------------
  272. void BitMessage::write_byte_align()
  273. {
  274. m_write_bit = 0;
  275. }
  276. //---------------------------------------------------------------------------------------------
  277. void BitMessage::write_bits(int32_t value, int32_t num_bits)
  278. {
  279. int32_t put;
  280. int32_t fraction;
  281. // check if m_write is void
  282. if (!m_write)
  283. {
  284. printf( "cannot write to message" );
  285. }
  286. // check if the number of bits is valid
  287. if (num_bits == 0 || num_bits < -31 || num_bits > 32)
  288. {
  289. printf( "bad numBits %i", num_bits);
  290. }
  291. // check for value overflows
  292. // this should be an error floatly, as it can go unnoticed and cause either bandwidth or corrupted data transmitted
  293. if (num_bits != 32)
  294. {
  295. if (num_bits > 0)
  296. {
  297. if (value > (1 << num_bits) - 1)
  298. {
  299. printf( "value overflow %d %d", value, num_bits );
  300. }
  301. else if (value < 0)
  302. {
  303. printf( "value overflow %d %d", value, num_bits );
  304. }
  305. }
  306. else
  307. {
  308. int32_t r = 1 << (-1 - num_bits);
  309. if (value > r - 1)
  310. {
  311. printf( "value overflow %d %d", value, num_bits );
  312. }
  313. else if (value < -r)
  314. {
  315. printf( "value overflow %d %d", value, num_bits );
  316. }
  317. }
  318. }
  319. // Change sign if it's negative
  320. if (num_bits < 0 )
  321. {
  322. num_bits = -num_bits;
  323. }
  324. // check for msg overflow
  325. if (check_overflow(num_bits))
  326. {
  327. return;
  328. }
  329. // write the bits
  330. while(num_bits)
  331. {
  332. if (m_write_bit == 0)
  333. {
  334. m_write[m_cur_size] = 0;
  335. m_cur_size++;
  336. }
  337. put = 8 - m_write_bit;
  338. if (put > num_bits)
  339. {
  340. put = num_bits;
  341. }
  342. fraction = value & ((1 << put) - 1);
  343. m_write[m_cur_size - 1] |= fraction << m_write_bit;
  344. num_bits -= put;
  345. value >>= put;
  346. m_write_bit = (m_write_bit + put) & 7;
  347. }
  348. }
  349. //---------------------------------------------------------------------------------------------
  350. void BitMessage::write_int8(int32_t c)
  351. {
  352. write_bits(c, -8);
  353. }
  354. //---------------------------------------------------------------------------------------------
  355. void BitMessage::write_uint8(int32_t c)
  356. {
  357. write_bits(c, 8);
  358. }
  359. //---------------------------------------------------------------------------------------------
  360. void BitMessage::write_int16(int32_t c)
  361. {
  362. write_bits(c, -16);
  363. }
  364. //---------------------------------------------------------------------------------------------
  365. void BitMessage::write_uint16(int32_t c)
  366. {
  367. write_bits(c, 16);
  368. }
  369. //---------------------------------------------------------------------------------------------
  370. void BitMessage::write_int32(int32_t c)
  371. {
  372. write_bits(c, 32);
  373. }
  374. //---------------------------------------------------------------------------------------------
  375. void BitMessage::write_float(float f)
  376. {
  377. write_bits(*reinterpret_cast<int32_t *>(&f), 32);
  378. }
  379. //---------------------------------------------------------------------------------------------
  380. void BitMessage::write_vec3(const Vector3& v)
  381. {
  382. write_float(v.x);
  383. write_float(v.y);
  384. write_float(v.z);
  385. }
  386. //---------------------------------------------------------------------------------------------
  387. void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
  388. {
  389. if (!s)
  390. {
  391. write_data("", 1);
  392. }
  393. else
  394. {
  395. int32_t i;
  396. int32_t len = std::strlen(s);
  397. uint8_t* data_ptr;
  398. const uint8_t* byte_ptr;
  399. // calculates length
  400. len = std::strlen(s);
  401. if (max_len >= 0 && len >= max_len)
  402. {
  403. len = max_len - 1;
  404. }
  405. data_ptr = get_byte_space(len + 1);
  406. byte_ptr = reinterpret_cast<const uint8_t*>(s);
  407. if (make_7_bit)
  408. {
  409. for (i = 0; i < len; i++)
  410. {
  411. if ( byte_ptr[i] > 127 )
  412. {
  413. data_ptr[i] = '.';
  414. }
  415. else
  416. {
  417. data_ptr[i] = byte_ptr[i];
  418. }
  419. }
  420. }
  421. else
  422. {
  423. for (i = 0; i < len; i++)
  424. {
  425. data_ptr[i] = byte_ptr[i];
  426. }
  427. }
  428. data_ptr[i] = '\0';
  429. }
  430. }
  431. //---------------------------------------------------------------------------------------------
  432. void BitMessage::write_data(const void* data, int32_t length)
  433. {
  434. memcpy(get_byte_space(length), data, length);
  435. }
  436. //---------------------------------------------------------------------------------------------
  437. void BitMessage::write_netaddr(const os::NetAddress addr)
  438. {
  439. uint8_t* ptr;
  440. ptr = get_byte_space(4);
  441. memcpy(ptr, addr.m_address, 4);
  442. write_uint16(addr.port());
  443. }
  444. //---------------------------------------------------------------------------------------------
  445. void BitMessage::begin_reading() const
  446. {
  447. m_read_count = 0;
  448. m_read_bit = 0;
  449. }
  450. //---------------------------------------------------------------------------------------------
  451. int32_t BitMessage::get_remaing_data() const
  452. {
  453. return m_cur_size - m_read_count;
  454. }
  455. //---------------------------------------------------------------------------------------------
  456. void BitMessage::read_byte_align() const
  457. {
  458. m_read_bit = 0;
  459. }
  460. //---------------------------------------------------------------------------------------------
  461. int32_t BitMessage::read_bits(int32_t num_bits) const
  462. {
  463. int32_t value;
  464. int32_t value_bits;
  465. int32_t get;
  466. int32_t fraction;
  467. bool sgn;
  468. if (!m_read)
  469. {
  470. printf("cannot read from message");
  471. }
  472. // check if the number of bits is valid
  473. if ( num_bits == 0 || num_bits < -31 || num_bits > 32 )
  474. {
  475. printf("bad number of bits %i", num_bits );
  476. }
  477. value = 0;
  478. value_bits = 0;
  479. // change sign if it is negative
  480. if (num_bits < 0)
  481. {
  482. num_bits = -num_bits;
  483. sgn = true;
  484. }
  485. else
  486. {
  487. sgn = false;
  488. }
  489. // check for overflow
  490. if (num_bits > get_remaining_read_bits())
  491. {
  492. return -1;
  493. }
  494. while (value_bits < num_bits)
  495. {
  496. if (m_read_bit == 0)
  497. {
  498. m_read_count++;
  499. }
  500. get = 8 - m_read_bit;
  501. if (get > (num_bits - value_bits))
  502. {
  503. get = num_bits - value_bits;
  504. }
  505. fraction = m_read[m_read_count - 1];
  506. fraction >>= m_read_bit;
  507. fraction &= (1 << get) - 1;
  508. value |= fraction << value_bits;
  509. value_bits += get;
  510. m_read_bit = (m_read_bit + get) & 7;
  511. }
  512. if (sgn)
  513. {
  514. if (value & (1 << (num_bits - 1)))
  515. {
  516. value |= -1 ^ (( 1 << num_bits) - 1);
  517. }
  518. }
  519. return value;
  520. }
  521. //---------------------------------------------------------------------------------------------
  522. int32_t BitMessage::read_int8() const
  523. {
  524. return (int32_t)read_bits(-8);
  525. }
  526. //---------------------------------------------------------------------------------------------
  527. int32_t BitMessage::read_uint8() const
  528. {
  529. return (int32_t)read_bits(8);
  530. }
  531. //---------------------------------------------------------------------------------------------
  532. int32_t BitMessage::read_int16() const
  533. {
  534. return (int32_t)read_bits(-16);
  535. }
  536. //---------------------------------------------------------------------------------------------
  537. int32_t BitMessage::read_uint16() const
  538. {
  539. return (int32_t)read_bits(16);
  540. }
  541. //---------------------------------------------------------------------------------------------
  542. int32_t BitMessage::read_int32() const
  543. {
  544. return (int32_t)read_bits(32);
  545. }
  546. //---------------------------------------------------------------------------------------------
  547. float BitMessage::read_float() const
  548. {
  549. float value;
  550. *reinterpret_cast<int*>(&value) = read_bits(32);
  551. return value;
  552. }
  553. //---------------------------------------------------------------------------------------------
  554. Vector3 BitMessage::read_vec3() const
  555. {
  556. Vector3 v;
  557. v.x = read_float();
  558. v.y = read_float();
  559. v.z = read_float();
  560. return v;
  561. }
  562. //---------------------------------------------------------------------------------------------
  563. int32_t BitMessage::read_string(char* buffer, int32_t buffer_size) const
  564. {
  565. int l = 0;
  566. int c;
  567. read_byte_align();
  568. while(1)
  569. {
  570. c = read_uint8();
  571. if (c <= 0 || c >= 255)
  572. {
  573. break;
  574. }
  575. // translate all fmt spec to avoid crash bugs in string routines
  576. if ( c == '%' )
  577. {
  578. c = '.';
  579. }
  580. // we will read past any excessively long string, so
  581. // the following data can be read, but the string will
  582. // be truncated
  583. if (l < buffer_size - 1)
  584. {
  585. buffer[l] = c;
  586. l++;
  587. }
  588. }
  589. buffer[l] = 0;
  590. return l;
  591. }
  592. //---------------------------------------------------------------------------------------------
  593. int32_t BitMessage::read_data(void* data, int32_t length) const
  594. {
  595. int count;
  596. read_byte_align();
  597. count = m_read_count;
  598. if (m_read_count + length > m_cur_size)
  599. {
  600. if (data)
  601. {
  602. memcpy(data, m_read + m_read_count, get_remaing_data());
  603. }
  604. m_read_count = m_cur_size;
  605. }
  606. else
  607. {
  608. if (data)
  609. {
  610. memcpy(data, m_read + m_read_count, length);
  611. }
  612. m_read_count += length;
  613. }
  614. return (m_read_count - count);
  615. }
  616. //---------------------------------------------------------------------------------------------
  617. void BitMessage::read_netaddr(os::NetAddress* addr) const
  618. {
  619. for (int i = 0; i < 4; i++)
  620. {
  621. addr->m_address[i] = read_uint8();
  622. }
  623. addr->m_port = read_uint16();
  624. }
  625. //---------------------------------------------------------------------------------------------
  626. void BitMessage::print() const
  627. {
  628. os::printf("MAX_SIZE: %d\n", m_max_size);
  629. os::printf("CUR_SIZE: %d\n", m_cur_size);
  630. }
  631. } //namespace network
  632. } //namespace crown