packet_buffer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/ring_buffer.h"
  33. template <class T>
  34. class PacketBuffer {
  35. private:
  36. typedef struct {
  37. uint32_t size;
  38. T info;
  39. } _Packet;
  40. RingBuffer<_Packet> _packets;
  41. RingBuffer<uint8_t> _payload;
  42. public:
  43. Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) {
  44. #ifdef TOOLS_ENABLED
  45. // Verbose buffer warnings
  46. if (p_payload && _payload.space_left() < (int32_t)p_size) {
  47. ERR_PRINT("Buffer payload full! Dropping data.");
  48. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  49. }
  50. if (p_info && _packets.space_left() < 1) {
  51. ERR_PRINT("Too many packets in queue! Dropping data.");
  52. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  53. }
  54. #else
  55. ERR_FAIL_COND_V(p_payload && (uint32_t)_payload.space_left() < p_size, ERR_OUT_OF_MEMORY);
  56. ERR_FAIL_COND_V(p_info && _packets.space_left() < 1, ERR_OUT_OF_MEMORY);
  57. #endif
  58. // If p_info is NULL, only the payload is written
  59. if (p_info) {
  60. _Packet p;
  61. p.size = p_size;
  62. memcpy(&p.info, p_info, sizeof(T));
  63. _packets.write(p);
  64. }
  65. // If p_payload is NULL, only the packet information is written.
  66. if (p_payload) {
  67. _payload.write((const uint8_t *)p_payload, p_size);
  68. }
  69. return OK;
  70. }
  71. Error read_packet(uint8_t *r_payload, int p_bytes, T *r_info, int &r_read) {
  72. ERR_FAIL_COND_V(_packets.data_left() < 1, ERR_UNAVAILABLE);
  73. _Packet p;
  74. _packets.read(&p, 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 discard_payload(int p_size) {
  83. _packets.decrease_write(p_size);
  84. }
  85. void resize(int p_pkt_shift, int p_buf_shift) {
  86. _packets.resize(p_pkt_shift);
  87. _payload.resize(p_buf_shift);
  88. }
  89. int packets_left() const {
  90. return _packets.data_left();
  91. }
  92. void clear() {
  93. _payload.resize(0);
  94. _packets.resize(0);
  95. }
  96. PacketBuffer() {
  97. clear();
  98. }
  99. ~PacketBuffer() {
  100. clear();
  101. }
  102. };
  103. #endif // PACKET_BUFFER_H