BitMessage.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Types.h"
  24. #include "Allocator.h"
  25. #include "Vec3.h"
  26. namespace crown
  27. {
  28. namespace network
  29. {
  30. /// Bit-packet reliable message.
  31. /// Usage: After every instantition, must be initialized with @init(len)
  32. /// TODO: rework as POD is needed; this feature provides compatibility with queue
  33. class BitMessage
  34. {
  35. public:
  36. BitMessage(Allocator& allocator);
  37. ~BitMessage();
  38. void init(int32_t len); // init with data length in byte
  39. void set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits);
  40. uint8_t* get_header(); // get message header for writing
  41. const uint8_t* get_header() const; // get message header for reading
  42. uint8_t* get_data(); // get message data for writing
  43. const uint8_t* get_data() const; // get message data for reading
  44. size_t get_max_size() const; // get the maximum message size
  45. bool is_overflowed(); // is message overflowed
  46. bool is_init(); // is message initialized
  47. size_t get_header_size() const; // return 12 bytes
  48. size_t get_size() const; // size of the message in bytes
  49. void set_size(size_t size); // set the message size
  50. int32_t get_write_bit() const; // get current write bit
  51. void set_write_bit(int32_t bit); // set current write bit
  52. int32_t get_num_bits_written() const; // returns number of bits written
  53. int32_t get_remaining_write_bits() const; // space left in bits for writing
  54. void save_write_state(int32_t& s,int32_t& b) const; // save the write state
  55. void restore_write_state(int32_t s,int32_t b); // restore the write state
  56. int32_t get_read_count() const; // bytes read so far
  57. void set_read_count(int32_t bytes); // set the number of bytes and bits read
  58. int32_t get_read_bit() const; // get current read bit
  59. void set_read_bit(int32_t bit); // set current read bit
  60. int32_t get_num_bits_read() const; // returns number of bits read
  61. int32_t get_remaining_read_bits() const; // number of bits left to read
  62. void save_read_state(int32_t& c, int32_t& b) const; // save the read state
  63. void restore_read_state(int32_t c, int32_t b); // restore the read state
  64. // write state utilities
  65. void begin_writing(); // begin writing
  66. int32_t get_remaining_space() const; // space left in bytes
  67. void write_byte_align(); // write up to the next byte boundary
  68. void write_bits(int32_t value, int32_t num_bits); // write the specified number of bits
  69. void write_int8(int32_t c);
  70. void write_uint8(int32_t c);
  71. void write_int16(int32_t c);
  72. void write_uint16(int32_t c);
  73. void write_int32(int32_t c);
  74. void write_real(real f);
  75. void write_vec3(const Vec3& v);
  76. void write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
  77. void write_data(const void* data, int32_t length);
  78. void write_netaddr(const os::NetAddress addr);
  79. // read state utilities
  80. void begin_reading() const; // begin reading.
  81. int32_t get_remaing_data() const; // number of bytes left to read
  82. void read_byte_align() const; // read up to the next byte boundary
  83. int32_t read_bits(int32_t num_bits) const; // read the specified number of bits
  84. int32_t read_int8() const;
  85. int32_t read_uint8() const;
  86. int32_t read_int16() const;
  87. int32_t read_uint16() const;
  88. int32_t read_int32() const;
  89. real read_real() const;
  90. Vec3 read_vec3() const;
  91. int32_t read_string(char* buffer, int32_t buffer_size) const;
  92. int32_t read_data(void* data, int32_t length) const;
  93. void read_netaddr(os::NetAddress* addr) const;
  94. void print() const;
  95. private:
  96. uint8_t* get_byte_space(int32_t len);
  97. bool check_overflow(uint32_t num_bits); // check buffer overflow
  98. private:
  99. Allocator& m_allocator; // memory allocator
  100. uint8_t* m_header;
  101. uint8_t* m_data;
  102. uint8_t* m_write; // pointer to data for writing
  103. const uint8_t* m_read; // point32_ter to data for reading
  104. size_t m_max_size; // maximum size of message in bytes
  105. size_t m_cur_size; // current size of message in bytes
  106. mutable int32_t m_read_count; // number of bytes read so far
  107. int32_t m_write_bit; // number of bits written to the last written byte
  108. mutable int32_t m_read_bit; // number of bits read from the last read byte
  109. bool m_overflowed; // overflow flag
  110. bool m_init; // is init flag
  111. };
  112. } // namespace network
  113. } // namespace crown