PacketQueue.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include "Types.h"
  3. namespace crown
  4. {
  5. namespace network
  6. {
  7. class PacketQueue
  8. {
  9. public:
  10. struct PacketData
  11. {
  12. uint16_t sequence;
  13. uint32_t time;
  14. size_t size;
  15. };
  16. public:
  17. PacketQueue();
  18. ~PacketQueue();
  19. bool add(const PacketData& pd);
  20. bool get(PacketData& pd);
  21. size_t get_total_size() const;
  22. size_t get_space_left() const;
  23. int32_t get_first() const;
  24. int32_t get_last() const;
  25. private:
  26. void write_uint8(int32_t value);
  27. void write_uint16(int32_t value);
  28. void write_int32(int32_t value);
  29. int32_t read_uint8();
  30. int32_t read_uint16();
  31. int32_t read_int32();
  32. private:
  33. static const uint32_t MAX_QUEUE_SIZE = 16384;
  34. uint8_t m_buffer[MAX_QUEUE_SIZE];
  35. uint32_t m_first; // sequence number of first message in queue
  36. uint32_t m_last; // sequence number of last message in queue
  37. uint32_t m_start_index; // index pointing to the first byte of the first message
  38. uint32_t m_end_index; // index pointing to the first byte after the last message
  39. };
  40. } // namespace network
  41. } // namespace crown