Message.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include <cassert>
  2. #include <cstring>
  3. #include "MathUtils.h"
  4. #include "Message.h"
  5. namespace crown
  6. {
  7. namespace network
  8. {
  9. Message::Message() : w_data(NULL), r_data(NULL), max_size(0), cur_size(0), write_bit(0), read_count(0), read_bit(0)
  10. {
  11. }
  12. Message::~Message()
  13. {
  14. }
  15. uint8_t* Message::get_byte_space(int32_t len)
  16. {
  17. uint8_t *ptr;
  18. if (!w_data)
  19. {
  20. printf( "idBitMsg::GetByteSpace: cannot write to message" );
  21. }
  22. // round up to the next byte
  23. write_byte_align();
  24. // check for overflow
  25. check_overflow(len << 3);
  26. // allocate space
  27. ptr = w_data + cur_size;
  28. cur_size += len;
  29. return ptr;
  30. }
  31. bool Message::check_overflow(int32_t num_bits)
  32. {
  33. assert( num_bits >= 0 );
  34. if (num_bits > get_remaining_write_bits())
  35. {
  36. if (num_bits > (max_size << 3))
  37. {
  38. printf(" %i bits is > full message size", num_bits );
  39. }
  40. printf("overflow\n");
  41. begin_writing();
  42. overflowed = true;
  43. return true;
  44. }
  45. return false;
  46. }
  47. void Message::init(uint8_t *data, int32_t len)
  48. {
  49. w_data = data;
  50. r_data = data;
  51. max_size = len;
  52. }
  53. void Message::init(const uint8_t *data, int32_t len)
  54. {
  55. w_data = NULL;
  56. r_data = data;
  57. max_size = len;
  58. }
  59. uint8_t* Message::get_data()
  60. {
  61. return w_data;
  62. }
  63. const uint8_t* Message::get_data() const
  64. {
  65. return r_data;
  66. }
  67. int32_t Message::get_max_size() const
  68. {
  69. return max_size;
  70. }
  71. bool Message::is_overflowed()
  72. {
  73. return overflowed;
  74. }
  75. int32_t Message::get_size() const
  76. {
  77. return cur_size;
  78. }
  79. void Message::set_size(int32_t size)
  80. {
  81. if (size > max_size)
  82. {
  83. cur_size = max_size;
  84. }
  85. else
  86. {
  87. cur_size = size;
  88. }
  89. }
  90. int32_t Message::get_write_bit() const
  91. {
  92. return write_bit;
  93. }
  94. void Message::set_write_bit(int32_t bit)
  95. {
  96. write_bit = bit & 7;
  97. if (write_bit)
  98. {
  99. w_data[cur_size-1] &= (1 << write_bit) - 1;
  100. }
  101. }
  102. int32_t Message::get_num_bits_written() const
  103. {
  104. return ((cur_size << 3) - ((8 - write_bit) & 7));
  105. }
  106. int32_t Message::get_remaining_write_bits() const
  107. {
  108. return (max_size << 3) - get_num_bits_written();
  109. }
  110. void Message::save_write_state(int32_t& s,int32_t& b) const
  111. {
  112. s = cur_size;
  113. b = write_bit;
  114. }
  115. void Message::restore_write_state(int32_t s,int32_t b)
  116. {
  117. cur_size = s;
  118. write_bit = b & 7;
  119. if (write_bit)
  120. {
  121. w_data[cur_size-1] &= (1 << write_bit) - 1;
  122. }
  123. }
  124. int32_t Message::get_read_count() const
  125. {
  126. return read_count;
  127. }
  128. void Message::set_read_count(int32_t bytes)
  129. {
  130. read_count = bytes;
  131. }
  132. int32_t Message::get_read_bit() const
  133. {
  134. return read_bit;
  135. }
  136. void Message::set_read_bit(int32_t bit)
  137. {
  138. read_bit = bit & 7;
  139. }
  140. int32_t Message::get_num_bits_read() const
  141. {
  142. return ((read_count << 3) - ((8 - read_bit) & 7));
  143. }
  144. int32_t Message::get_remaining_read_bits() const
  145. {
  146. return (cur_size << 3) - get_num_bits_read();
  147. }
  148. void Message::save_read_state(int32_t& c, int32_t& b) const
  149. {
  150. c = read_count;
  151. b = read_bit;
  152. }
  153. void Message::restore_read_state(int32_t c, int32_t b)
  154. {
  155. read_count = c;
  156. read_bit = b & 7;
  157. }
  158. void Message::begin_writing()
  159. {
  160. cur_size = 0;
  161. write_bit = 0;
  162. overflowed = false;
  163. }
  164. int32_t Message::get_remaining_space() const
  165. {
  166. return max_size - cur_size;
  167. }
  168. void Message::write_byte_align()
  169. {
  170. write_bit = 0;
  171. }
  172. void Message::write_bits(int32_t value, int32_t num_bits)
  173. {
  174. int32_t put;
  175. int32_t fraction;
  176. // check if w_data is void
  177. if (!w_data)
  178. {
  179. printf( "cannot write to message" );
  180. }
  181. // check if the number of bits is valid
  182. if (num_bits == 0 || num_bits < -31 || num_bits > 32)
  183. {
  184. printf( "bad numBits %i", num_bits);
  185. }
  186. // check for value overflows
  187. // this should be an error really, as it can go unnoticed and cause either bandwidth or corrupted data transmitted
  188. if (num_bits != 32)
  189. {
  190. if (num_bits > 0)
  191. {
  192. if (value > (1 << num_bits) - 1)
  193. {
  194. printf( "value overflow %d %d", value, num_bits );
  195. }
  196. else if (value < 0)
  197. {
  198. printf( "value overflow %d %d", value, num_bits );
  199. }
  200. }
  201. else
  202. {
  203. int32_t r = 1 << (-1 - num_bits);
  204. if (value > r - 1)
  205. {
  206. printf( "value overflow %d %d", value, num_bits );
  207. }
  208. else if (value < -r)
  209. {
  210. printf( "value overflow %d %d", value, num_bits );
  211. }
  212. }
  213. }
  214. // Change sign if it is negative
  215. if (num_bits < 0 )
  216. {
  217. num_bits = -num_bits;
  218. }
  219. // check for msg overflow
  220. if (check_overflow(num_bits))
  221. {
  222. return;
  223. }
  224. // write the bits
  225. while(num_bits)
  226. {
  227. if (write_bit == 0)
  228. {
  229. w_data[cur_size] = 0;
  230. cur_size++;
  231. }
  232. put = 8 - write_bit;
  233. if (put > num_bits)
  234. {
  235. put = num_bits;
  236. }
  237. fraction = value & ((1 << put) - 1);
  238. w_data[cur_size - 1] |= fraction << write_bit;
  239. num_bits -= put;
  240. value >>= put;
  241. write_bit = (write_bit + put) & 7;
  242. }
  243. }
  244. void Message::write_int8(int32_t c)
  245. {
  246. write_bits(c, -8);
  247. }
  248. void Message::write_uint8(int32_t c)
  249. {
  250. write_bits(c, 8);
  251. }
  252. void Message::write_int16(int32_t c)
  253. {
  254. write_bits(c, -16);
  255. }
  256. void Message::write_uint16(int32_t c)
  257. {
  258. write_bits(c, 16);
  259. }
  260. void Message::write_int64(int32_t c)
  261. {
  262. write_bits(c, 32);
  263. }
  264. void Message::write_real(real f)
  265. {
  266. write_bits(*reinterpret_cast<int32_t *>(&f), 32);
  267. }
  268. void Message::write_real(real f, int32_t exp_bits, int32_t mant_bits)
  269. {
  270. //TODO:need to implement floatToBits function
  271. }
  272. void Message::write_vec3(const Vec3& v, int32_t num_bits)
  273. {
  274. write_bits(vec3_to_bits(v, num_bits), num_bits);
  275. }
  276. void Message::write_string(const char* s, int32_t max_len, bool make7Bit)
  277. {
  278. }
  279. void Message::write_data(const void* data, int32_t length)
  280. {
  281. memcpy(get_byte_space(length), data, length);
  282. }
  283. void Message::write_ipv4addr(const os::IPv4Address addr)
  284. {
  285. }
  286. void Message::begin_reading() const
  287. {
  288. }
  289. int32_t Message::get_remaing_data() const
  290. {
  291. }
  292. void Message::read_byte_align() const
  293. {
  294. }
  295. int32_t Message::read_bits(int32_t num_bits) const
  296. {
  297. int32_t value;
  298. int32_t value_bits;
  299. int32_t get;
  300. int32_t fraction;
  301. bool sgn;
  302. if ( !r_data ) {
  303. printf("cannot read from message");
  304. }
  305. // check if the number of bits is valid
  306. if ( num_bits == 0 || num_bits < -31 || num_bits > 32 ) {
  307. printf("bad number of bits %i", num_bits );
  308. }
  309. value = 0;
  310. value_bits = 0;
  311. // change sign if it is negative
  312. if (num_bits < 0)
  313. {
  314. num_bits = -num_bits;
  315. sgn = true;
  316. }
  317. else
  318. {
  319. sgn = false;
  320. }
  321. // check for overflow
  322. if (num_bits > get_remaining_read_bits())
  323. {
  324. return -1;
  325. }
  326. while (value_bits < num_bits )
  327. {
  328. if (read_bit == 0)
  329. {
  330. read_count++;
  331. }
  332. get = 8 - read_bit;
  333. if (get > (num_bits - value_bits))
  334. {
  335. get = num_bits - value_bits;
  336. }
  337. fraction = r_data[read_count - 1];
  338. fraction >>= read_bit;
  339. fraction &= (1 << get) - 1;
  340. value |= fraction << value_bits;
  341. value_bits += get;
  342. read_bit = (read_bit + get) & 7;
  343. }
  344. if (sgn)
  345. {
  346. if (value & (1 << (num_bits - 1)))
  347. {
  348. value |= -1 ^ (( 1 << num_bits) - 1);
  349. }
  350. }
  351. return value;
  352. }
  353. int32_t Message::read_int8() const
  354. {
  355. }
  356. int32_t Message::read_uint8() const
  357. {
  358. }
  359. int32_t Message::read_int16() const
  360. {
  361. }
  362. int32_t Message::read_uint16() const
  363. {
  364. }
  365. int32_t Message::read_int64() const
  366. {
  367. }
  368. real Message::read_real() const
  369. {
  370. }
  371. real Message::read_real(int32_t exp_bits, int32_t mant_bits) const
  372. {
  373. }
  374. Vec3 Message::read_vec3(int32_t num_bits) const
  375. {
  376. }
  377. int32_t Message::read_string(char* buffer, int32_t buffer_size) const
  378. {
  379. }
  380. int32_t Message::read_data(void* data, int32_t length) const
  381. {
  382. }
  383. void Message::read_ipv4addr(os::IPv4Address* addr) const
  384. {
  385. }
  386. int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
  387. {
  388. assert(num_bits >= 6 && num_bits <= 32);
  389. assert(v.squared_length() - 1.0f < 0.01f);
  390. int32_t max;
  391. int32_t bits;
  392. float bias;
  393. num_bits /= 3;
  394. max = (1 << (num_bits - 1)) - 1;
  395. bias = 0.5f / max;
  396. bits = FLOATSIGNBITSET(v.x) << (num_bits * 3 - 1);
  397. bits |= ((int32_t)((math::abs(v.x) + bias) * max)) << (num_bits * 2);
  398. bits |= FLOATSIGNBITSET(v.y) << (num_bits * 2 - 1);
  399. bits |= ((int32_t)((math::abs(v.y) + bias) * max)) << (num_bits * 1);
  400. bits |= FLOATSIGNBITSET(v.z) << (num_bits * 1 - 1);
  401. bits |= ((int32_t)((math::abs(v.z) + bias) * max)) << (num_bits * 0);
  402. return bits;
  403. }
  404. Vec3 Message::bits_to_vec3(int32_t bits, int32_t num_bits)
  405. {
  406. assert(num_bits >= 6 && num_bits <= 32);
  407. static float sign[2] = {1.0f, -1.0f};
  408. int max;
  409. float inv_max;
  410. Vec3 v;
  411. num_bits /= 3;
  412. max = (1 << (num_bits - 1)) - 1;
  413. inv_max = 1.0f / max;
  414. v.x = sign[(bits >> (num_bits * 3 - 1)) & 1] * ((bits >> (num_bits * 2)) & max) * inv_max;
  415. v.y = sign[(bits >> (num_bits * 2 - 1)) & 1] * ((bits >> (num_bits * 1)) & max) * inv_max;
  416. v.z = sign[(bits >> (num_bits * 1 - 1)) & 1] * ((bits >> (num_bits * 0)) & max) * inv_max;
  417. v.normalize();
  418. return v;
  419. }
  420. }
  421. }