Debian Codegraph @

modules/websocket/packet_buffer.h

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