packet_buffer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  31. #include "core/templates/ring_buffer.h"
  32. template <typename T>
  33. class PacketBuffer {
  34. private:
  35. typedef struct {
  36. uint32_t size;
  37. T info;
  38. } _Packet;
  39. Vector<_Packet> _packets;
  40. int _queued = 0;
  41. int _write_pos = 0;
  42. int _read_pos = 0;
  43. RingBuffer<uint8_t> _payload;
  44. public:
  45. Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) {
  46. ERR_FAIL_COND_V_MSG(p_payload && (uint32_t)_payload.space_left() < p_size, ERR_OUT_OF_MEMORY, "Buffer payload full! Dropping data.");
  47. ERR_FAIL_COND_V_MSG(p_info && _queued >= _packets.size(), ERR_OUT_OF_MEMORY, "Too many packets in queue! Dropping data.");
  48. // If p_info is nullptr, only the payload is written
  49. if (p_info) {
  50. ERR_FAIL_COND_V(_write_pos > _packets.size(), ERR_OUT_OF_MEMORY);
  51. _Packet p;
  52. p.size = p_size;
  53. p.info = *p_info;
  54. _packets.write[_write_pos] = p;
  55. _queued += 1;
  56. _write_pos++;
  57. if (_write_pos >= _packets.size()) {
  58. _write_pos = 0;
  59. }
  60. }
  61. // If p_payload is nullptr, only the packet information is written.
  62. if (p_payload) {
  63. _payload.write((const uint8_t *)p_payload, p_size);
  64. }
  65. return OK;
  66. }
  67. Error read_packet(uint8_t *r_payload, int p_bytes, T *r_info, int &r_read) {
  68. ERR_FAIL_COND_V(_queued < 1, ERR_UNAVAILABLE);
  69. _Packet p = _packets[_read_pos];
  70. _read_pos += 1;
  71. if (_read_pos >= _packets.size()) {
  72. _read_pos = 0;
  73. }
  74. _queued -= 1;
  75. ERR_FAIL_COND_V(_payload.data_left() < (int)p.size, ERR_BUG);
  76. ERR_FAIL_COND_V(p_bytes < (int)p.size, ERR_OUT_OF_MEMORY);
  77. r_read = p.size;
  78. memcpy(r_info, &p.info, sizeof(T));
  79. _payload.read(r_payload, p.size);
  80. return OK;
  81. }
  82. void resize(int p_buf_shift, int p_max_packets) {
  83. _payload.resize(p_buf_shift);
  84. _packets.resize(p_max_packets);
  85. _read_pos = 0;
  86. _write_pos = 0;
  87. _queued = 0;
  88. }
  89. int packets_left() const {
  90. return _queued;
  91. }
  92. int payload_space_left() const {
  93. return _payload.space_left();
  94. }
  95. int packets_space_left() const {
  96. return _packets.size() - _queued;
  97. }
  98. void clear() {
  99. _payload.resize(0);
  100. _packets.resize(0);
  101. _read_pos = 0;
  102. _write_pos = 0;
  103. _queued = 0;
  104. }
  105. PacketBuffer() {
  106. clear();
  107. }
  108. ~PacketBuffer() {
  109. clear();
  110. }
  111. };