BitMessage.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. void set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits);
  21. uint8_t* get_header(); // get message header for writing
  22. const uint8_t* get_header() const; // get message header for reading
  23. uint8_t* get_data(); // get message data for writing
  24. const uint8_t* get_data() const; // get message data for reading
  25. size_t get_max_size() const; // get the maximum message size
  26. bool is_overflowed(); // is message overflowed
  27. bool is_init(); // is message initialized
  28. size_t get_header_size() const; // return 12 bytes
  29. size_t get_size() const; // size of the message in bytes
  30. void set_size(size_t size); // set the message size
  31. int32_t get_write_bit() const; // get current write bit
  32. void set_write_bit(int32_t bit); // set current write bit
  33. int32_t get_num_bits_written() const; // returns number of bits written
  34. int32_t get_remaining_write_bits() const; // space left in bits for writing
  35. void save_write_state(int32_t& s,int32_t& b) const; // save the write state
  36. void restore_write_state(int32_t s,int32_t b); // restore the write state
  37. int32_t get_read_count() const; // bytes read so far
  38. void set_read_count(int32_t bytes); // set the number of bytes and bits read
  39. int32_t get_read_bit() const; // get current read bit
  40. void set_read_bit(int32_t bit); // set current read bit
  41. int32_t get_num_bits_read() const; // returns number of bits read
  42. int32_t get_remaining_read_bits() const; // number of bits left to read
  43. void save_read_state(int32_t& c, int32_t& b) const; // save the read state
  44. void restore_read_state(int32_t c, int32_t b); // restore the read state
  45. // write state utilities
  46. void begin_writing(); // begin writing
  47. int32_t get_remaining_space() const; // space left in bytes
  48. void write_byte_align(); // write up to the next byte boundary
  49. void write_bits(int32_t value, int32_t num_bits); // write the specified number of bits
  50. void write_int8(int32_t c);
  51. void write_uint8(int32_t c);
  52. void write_int16(int32_t c);
  53. void write_uint16(int32_t c);
  54. void write_int32(int32_t c);
  55. void write_real(real f);
  56. void write_vec3(const Vec3& v);
  57. void write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
  58. void write_data(const void* data, int32_t length);
  59. void write_netaddr(const os::NetAddress addr);
  60. // read state utilities
  61. void begin_reading() const; // begin reading.
  62. int32_t get_remaing_data() const; // number of bytes left to read
  63. void read_byte_align() const; // read up to the next byte boundary
  64. int32_t read_bits(int32_t num_bits) const; // read the specified number of bits
  65. int32_t read_int8() const;
  66. int32_t read_uint8() const;
  67. int32_t read_int16() const;
  68. int32_t read_uint16() const;
  69. int32_t read_int32() const;
  70. real read_real() const;
  71. Vec3 read_vec3() const;
  72. int32_t read_string(char* buffer, int32_t buffer_size) const;
  73. int32_t read_data(void* data, int32_t length) const;
  74. void read_netaddr(os::NetAddress* addr) const;
  75. void print() const;
  76. private:
  77. uint8_t* get_byte_space(int32_t len);
  78. bool check_overflow(int32_t num_bits); // check buffer overflow
  79. private:
  80. Allocator* m_allocator; // memory allocator
  81. uint8_t* m_header;
  82. uint8_t* m_data;
  83. uint8_t* m_write; // pointer to data for writing
  84. const uint8_t* m_read; // point32_ter to data for reading
  85. size_t m_max_size; // maximum size of message in bytes
  86. size_t m_cur_size; // current size of message in bytes
  87. mutable int32_t m_read_count; // number of bytes read so far
  88. int32_t m_write_bit; // number of bits written to the last written byte
  89. mutable int32_t m_read_bit; // number of bits read from the last read byte
  90. bool m_overflowed; // overflow flag
  91. bool m_init; // is init flag
  92. };
  93. } // namespace network
  94. } // namespace crown