BitMessage.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #pragma once
  24. #include "Types.h"
  25. #include "Allocator.h"
  26. #include "Vec3.h"
  27. #include "NetAddress.h"
  28. namespace crown
  29. {
  30. namespace network
  31. {
  32. /// Bit-packet reliable message.
  33. /// Usage: After every instantition, must be initialized with @a init(len)
  34. /// TODO: rework as POD is needed; this feature provides compatibility with queue
  35. class BitMessage
  36. {
  37. public:
  38. BitMessage(Allocator& allocator);
  39. ~BitMessage();
  40. void init(int32_t len); // init with data length in byte
  41. void set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits);
  42. uint8_t* get_header(); // get message header for writing
  43. const uint8_t* get_header() const; // get message header for reading
  44. uint8_t* get_data(); // get message data for writing
  45. const uint8_t* get_data() const; // get message data for reading
  46. size_t get_max_size() const; // get the maximum message size
  47. bool is_overflowed(); // is message overflowed
  48. bool is_init(); // is message initialized
  49. size_t get_header_size() const; // return 12 bytes
  50. size_t get_size() const; // size of the message in bytes
  51. void set_size(size_t size); // set the message size
  52. int32_t get_write_bit() const; // get current write bit
  53. void set_write_bit(int32_t bit); // set current write bit
  54. int32_t get_num_bits_written() const; // returns number of bits written
  55. int32_t get_remaining_write_bits() const; // space left in bits for writing
  56. void save_write_state(int32_t& s,int32_t& b) const; // save the write state
  57. void restore_write_state(int32_t s,int32_t b); // restore the write state
  58. int32_t get_read_count() const; // bytes read so far
  59. void set_read_count(int32_t bytes); // set the number of bytes and bits read
  60. int32_t get_read_bit() const; // get current read bit
  61. void set_read_bit(int32_t bit); // set current read bit
  62. int32_t get_num_bits_read() const; // returns number of bits read
  63. int32_t get_remaining_read_bits() const; // number of bits left to read
  64. void save_read_state(int32_t& c, int32_t& b) const; // save the read state
  65. void restore_read_state(int32_t c, int32_t b); // restore the read state
  66. // write state utilities
  67. void begin_writing(); // begin writing
  68. int32_t get_remaining_space() const; // space left in bytes
  69. void write_byte_align(); // write up to the next byte boundary
  70. void write_bits(int32_t value, int32_t num_bits); // write the specified number of bits
  71. void write_int8(int32_t c);
  72. void write_uint8(int32_t c);
  73. void write_int16(int32_t c);
  74. void write_uint16(int32_t c);
  75. void write_int32(int32_t c);
  76. void write_float(float f);
  77. void write_vec3(const Vec3& v);
  78. void write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
  79. void write_data(const void* data, int32_t length);
  80. void write_netaddr(const os::NetAddress addr);
  81. // read state utilities
  82. void begin_reading() const; // begin reading.
  83. int32_t get_remaing_data() const; // number of bytes left to read
  84. void read_byte_align() const; // read up to the next byte boundary
  85. int32_t read_bits(int32_t num_bits) const; // read the specified number of bits
  86. int32_t read_int8() const;
  87. int32_t read_uint8() const;
  88. int32_t read_int16() const;
  89. int32_t read_uint16() const;
  90. int32_t read_int32() const;
  91. float read_float() const;
  92. Vec3 read_vec3() const;
  93. int32_t read_string(char* buffer, int32_t buffer_size) const;
  94. int32_t read_data(void* data, int32_t length) const;
  95. void read_netaddr(os::NetAddress* addr) const;
  96. void print() const;
  97. private:
  98. uint8_t* get_byte_space(int32_t len);
  99. bool check_overflow(uint32_t num_bits); // check buffer overflow
  100. private:
  101. Allocator& m_allocator; // memory allocator
  102. uint8_t* m_header;
  103. uint8_t* m_data;
  104. uint8_t* m_write; // pointer to data for writing
  105. const uint8_t* m_read; // point32_ter to data for reading
  106. size_t m_max_size; // maximum size of message in bytes
  107. size_t m_cur_size; // current size of message in bytes
  108. mutable int32_t m_read_count; // number of bytes read so far
  109. int32_t m_write_bit; // number of bits written to the last written byte
  110. mutable int32_t m_read_bit; // number of bits read from the last read byte
  111. bool m_overflowed; // overflow flag
  112. bool m_init; // is init flag
  113. };
  114. } // namespace network
  115. } // namespace crown