packet_buffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**************************************************************************/
  2. /* packet_buffer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PACKET_BUFFER_H
  31. #define PACKET_BUFFER_H
  32. #include "core/templates/ring_buffer.h"
  33. template <typename T>
  34. class PacketBuffer {
  35. private:
  36. typedef struct {
  37. uint32_t size;
  38. T info;
  39. } _Packet;
  40. Vector<_Packet> _packets;
  41. int _queued = 0;
  42. int _write_pos = 0;
  43. int _read_pos = 0;
  44. RingBuffer<uint8_t> _payload;
  45. public:
  46. Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) {
  47. ERR_FAIL_COND_V_MSG(p_payload && (uint32_t)_payload.space_left() < p_size, ERR_OUT_OF_MEMORY, "Buffer payload full! Dropping data.");
  48. ERR_FAIL_COND_V_MSG(p_info && _queued >= _packets.size(), ERR_OUT_OF_MEMORY, "Too many packets in queue! Dropping data.");
  49. // If p_info is nullptr, only the payload is written
  50. if (p_info) {
  51. ERR_FAIL_COND_V(_write_pos > _packets.size(), ERR_OUT_OF_MEMORY);
  52. _Packet p;
  53. p.size = p_size;
  54. p.info = *p_info;
  55. _packets.write[_write_pos] = p;
  56. _queued += 1;
  57. _write_pos++;
  58. if (_write_pos >= _packets.size()) {
  59. _write_pos = 0;
  60. }
  61. }
  62. // If p_payload is nullptr, only the packet information is written.
  63. if (p_payload) {
  64. _payload.write((const uint8_t *)p_payload, p_size);
  65. }
  66. return OK;
  67. }
  68. Error read_packet(uint8_t *r_payload, int p_bytes, T *r_info, int &r_read) {
  69. ERR_FAIL_COND_V(_queued < 1, ERR_UNAVAILABLE);
  70. _Packet p = _packets[_read_pos];
  71. _read_pos += 1;
  72. if (_read_pos >= _packets.size()) {
  73. _read_pos = 0;
  74. }
  75. _queued -= 1;
  76. ERR_FAIL_COND_V(_payload.data_left() < (int)p.size, ERR_BUG);
  77. ERR_FAIL_COND_V(p_bytes < (int)p.size, ERR_OUT_OF_MEMORY);
  78. r_read = p.size;
  79. memcpy(r_info, &p.info, sizeof(T));
  80. _payload.read(r_payload, p.size);
  81. return OK;
  82. }
  83. void resize(int p_buf_shift, int p_max_packets) {
  84. _payload.resize(p_buf_shift);
  85. _packets.resize(p_max_packets);
  86. _read_pos = 0;
  87. _write_pos = 0;
  88. _queued = 0;
  89. }
  90. int packets_left() const {
  91. return _queued;
  92. }
  93. int payload_space_left() const {
  94. return _payload.space_left();
  95. }
  96. int packets_space_left() const {
  97. return _packets.size() - _queued;
  98. }
  99. void clear() {
  100. _payload.resize(0);
  101. _packets.resize(0);
  102. _read_pos = 0;
  103. _write_pos = 0;
  104. _queued = 0;
  105. }
  106. PacketBuffer() {
  107. clear();
  108. }
  109. ~PacketBuffer() {
  110. clear();
  111. }
  112. };
  113. #endif // PACKET_BUFFER_H