packet_buffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************************/
  2. /* packet_buffer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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/os/copymem.h"
  33. #include "core/ring_buffer.h"
  34. template <class T>
  35. class PacketBuffer {
  36. private:
  37. typedef struct {
  38. uint32_t size;
  39. T info;
  40. } _Packet;
  41. RingBuffer<_Packet> _packets;
  42. RingBuffer<uint8_t> _payload;
  43. public:
  44. Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) {
  45. #ifdef TOOLS_ENABLED
  46. // Verbose buffer warnings
  47. if (p_payload && _payload.space_left() < p_size) {
  48. ERR_PRINT("Buffer payload full! Dropping data.");
  49. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  50. }
  51. if (p_info && _packets.space_left() < 1) {
  52. ERR_PRINT("Too many packets in queue! Dropping data.");
  53. ERR_FAIL_V(ERR_OUT_OF_MEMORY);
  54. }
  55. #else
  56. ERR_FAIL_COND_V(p_payload && _payload.space_left() < p_size, ERR_OUT_OF_MEMORY);
  57. ERR_FAIL_COND_V(p_info && _packets.space_left() < 1, ERR_OUT_OF_MEMORY);
  58. #endif
  59. // If p_info is NULL, only the payload is written
  60. if (p_info) {
  61. _Packet p;
  62. p.size = p_size;
  63. copymem(&p.info, p_info, sizeof(T));
  64. _packets.write(p);
  65. }
  66. // If p_payload is NULL, only the packet information is written.
  67. if (p_payload) {
  68. _payload.write((const uint8_t *)p_payload, p_size);
  69. }
  70. return OK;
  71. }
  72. Error read_packet(uint8_t *r_payload, int p_bytes, T *r_info, int &r_read) {
  73. ERR_FAIL_COND_V(_packets.data_left() < 1, ERR_UNAVAILABLE);
  74. _Packet p;
  75. _packets.read(&p, 1);
  76. ERR_FAIL_COND_V(_payload.data_left() < p.size, ERR_BUG);
  77. ERR_FAIL_COND_V(p_bytes < p.size, ERR_OUT_OF_MEMORY);
  78. r_read = p.size;
  79. copymem(r_info, &p.info, sizeof(T));
  80. _payload.read(r_payload, p.size);
  81. return OK;
  82. }
  83. void discard_payload(int p_size) {
  84. _packets.decrease_write(p_size);
  85. }
  86. void resize(int p_pkt_shift, int p_buf_shift) {
  87. _packets.resize(p_pkt_shift);
  88. _payload.resize(p_buf_shift);
  89. }
  90. int packets_left() const {
  91. return _packets.data_left();
  92. }
  93. void clear() {
  94. _payload.resize(0);
  95. _packets.resize(0);
  96. }
  97. PacketBuffer() {
  98. clear();
  99. }
  100. ~PacketBuffer() {
  101. clear();
  102. }
  103. };
  104. #endif // PACKET_BUFFER_H