BitMessage.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include "Types.h"
  3. #include "OS.h"
  4. #include "Allocator.h"
  5. #include "Vec3.h"
  6. namespace crown
  7. {
  8. namespace network
  9. {
  10. /**
  11. * bit-packet reliable message.
  12. * Usage: After every instantition, must be initialized with @init(len)
  13. */
  14. class BitMessage
  15. {
  16. public:
  17. BitMessage(Allocator& allocator);
  18. ~BitMessage();
  19. void init(int32_t len); // init with data length in byte
  20. const uint8_t* get_header(); // get message header
  21. uint8_t* get_data(); // get data for writing
  22. const uint8_t* get_data() const; // get data for reading
  23. size_t get_max_size() const; // get the maximum message size
  24. bool is_overflowed(); // get overflowed flag
  25. size_t get_size() const; // size of the message in bytes
  26. void set_size(size_t size); // set the message size
  27. int32_t get_write_bit() const; // get current write bit
  28. void set_write_bit(int32_t bit); // set current write bit
  29. int32_t get_num_bits_written() const; // returns number of bits written
  30. int32_t get_remaining_write_bits() const; // space left in bits for writing
  31. void save_write_state(int32_t& s,int32_t& b) const; // save the write state
  32. void restore_write_state(int32_t s,int32_t b); // restore the write state
  33. int32_t get_read_count() const; // bytes read so far
  34. void set_read_count(int32_t bytes); // set the number of bytes and bits read
  35. int32_t get_read_bit() const; // get current read bit
  36. void set_read_bit(int32_t bit); // set current read bit
  37. int32_t get_num_bits_read() const; // returns number of bits read
  38. int32_t get_remaining_read_bits() const; // number of bits left to read
  39. void save_read_state(int32_t& c, int32_t& b) const; // save the read state
  40. void restore_read_state(int32_t c, int32_t b); // restore the read state
  41. // write state utilities
  42. void begin_writing(); // begin writing
  43. int32_t get_remaining_space() const; // space left in bytes
  44. void write_byte_align(); // write up to the next byte boundary
  45. void write_bits(int32_t value, int32_t num_bits); // write the specified number of bits
  46. void write_int8(int32_t c);
  47. void write_uint8(int32_t c);
  48. void write_int16(int32_t c);
  49. void write_uint16(int32_t c);
  50. void write_int32(int32_t c);
  51. void write_real(real f);
  52. void write_vec3(const Vec3& v);
  53. void write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
  54. void write_data(const void* data, int32_t length);
  55. void write_netaddr(const os::NetAddress addr);
  56. // read state utilities
  57. void begin_reading() const; // begin reading.
  58. int32_t get_remaing_data() const; // number of bytes left to read
  59. void read_byte_align() const; // read up to the next byte boundary
  60. int32_t read_bits(int32_t num_bits) const; // read the specified number of bits
  61. int32_t read_int8() const;
  62. int32_t read_uint8() const;
  63. int32_t read_int16() const;
  64. int32_t read_uint16() const;
  65. int32_t read_int32() const;
  66. real read_real() const;
  67. Vec3 read_vec3() const;
  68. int32_t read_string(char* buffer, int32_t buffer_size) const;
  69. int32_t read_data(void* data, int32_t length) const;
  70. void read_netaddr(os::NetAddress* addr) const;
  71. void print() const;
  72. private:
  73. uint8_t* get_byte_space(int32_t len);
  74. bool check_overflow(int32_t num_bits); // check buffer overflow
  75. private:
  76. Allocator* m_allocator; // memory allocator
  77. uint8_t* m_header;
  78. uint8_t* m_data;
  79. uint8_t* m_write; // pointer to data for writing
  80. const uint8_t* m_read; // point32_ter to data for reading
  81. size_t m_max_size; // maximum size of message in bytes
  82. size_t m_cur_size; // current size of message in bytes
  83. mutable int32_t m_read_count; // number of bytes read so far
  84. int32_t m_write_bit; // number of bits written to the last written byte
  85. mutable int32_t m_read_bit; // number of bits read from the last read byte
  86. bool m_overflowed; // overflow flag
  87. };
  88. } // namespace network
  89. } // namespace crown