frag_basic.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef DEPTHGUIDE_FRAG_BASIC_H
  2. #define DEPTHGUIDE_FRAG_BASIC_H
  3. #include "frag_base.hpp"
  4. #include <boost/dynamic_bitset.hpp>
  5. #include <list>
  6. class frag_basic : public frag_base {
  7. public:
  8. using frag_base::frag_base;
  9. bool send(const data_type &data) override;
  10. size_t max_length() const override { return UINT32_MAX; }
  11. private:
  12. struct frag_header_type {
  13. uint32_t msg_id = 0;
  14. uint32_t msg_size = 0;
  15. uint32_t bl_num = 0;
  16. uint32_t bl_id = 0;
  17. uint32_t bl_offset = 0;
  18. using header_size_type = uint8_t;
  19. static constexpr header_size_type header_size = 5 * sizeof(uint32_t);
  20. static constexpr size_t extra_size = header_size + sizeof(header_size);
  21. template<typename ReaderType>
  22. void fill_from(ReaderType &reader) {
  23. reader >> msg_id >> msg_size >> bl_num >> bl_id >> bl_offset;
  24. }
  25. template<typename WriterType>
  26. void write_to(WriterType &writer) {
  27. writer << msg_id << msg_size << bl_num << bl_id << bl_offset;
  28. }
  29. };
  30. // for sending message
  31. uint32_t last_send_id = 0;
  32. // for receiving message
  33. using bitset_type = boost::dynamic_bitset<>;
  34. struct msg_info : boost::noncopyable {
  35. uint32_t msg_id = 0;
  36. bitset_type bl_ok;
  37. data_type data;
  38. size_t bytes_ok = 0;
  39. explicit msg_info(frag_header_type header)
  40. : bl_ok(header.bl_num), data(header.msg_size) {
  41. msg_id = header.msg_id;
  42. }
  43. bool verify(frag_header_type header) const;
  44. bool is_complete() const;
  45. void merge(frag_header_type header, const data_type &data);
  46. };
  47. using queue_type = std::list<msg_info>;
  48. uint32_t last_recv_id = 0; // id of last returned message
  49. queue_type msg_q; // message queue
  50. void append_data(const data_type &data) override;
  51. };
  52. #endif //DEPTHGUIDE_FRAG_BASIC_H