1 | /**************************************************************************/ |
2 | /* packet_peer.cpp */ |
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 | #include "packet_peer.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/io/marshalls.h" |
35 | |
36 | /* helpers / binders */ |
37 | |
38 | void PacketPeer::set_encode_buffer_max_size(int p_max_size) { |
39 | ERR_FAIL_COND_MSG(p_max_size < 1024, "Max encode buffer must be at least 1024 bytes" ); |
40 | ERR_FAIL_COND_MSG(p_max_size > 256 * 1024 * 1024, "Max encode buffer cannot exceed 256 MiB" ); |
41 | encode_buffer_max_size = next_power_of_2(p_max_size); |
42 | encode_buffer.clear(); |
43 | } |
44 | |
45 | int PacketPeer::get_encode_buffer_max_size() const { |
46 | return encode_buffer_max_size; |
47 | } |
48 | |
49 | Error PacketPeer::get_packet_buffer(Vector<uint8_t> &r_buffer) { |
50 | const uint8_t *buffer; |
51 | int buffer_size; |
52 | Error err = get_packet(&buffer, buffer_size); |
53 | if (err) { |
54 | return err; |
55 | } |
56 | |
57 | r_buffer.resize(buffer_size); |
58 | if (buffer_size == 0) { |
59 | return OK; |
60 | } |
61 | |
62 | uint8_t *w = r_buffer.ptrw(); |
63 | for (int i = 0; i < buffer_size; i++) { |
64 | w[i] = buffer[i]; |
65 | } |
66 | |
67 | return OK; |
68 | } |
69 | |
70 | Error PacketPeer::put_packet_buffer(const Vector<uint8_t> &p_buffer) { |
71 | int len = p_buffer.size(); |
72 | if (len == 0) { |
73 | return OK; |
74 | } |
75 | |
76 | const uint8_t *r = p_buffer.ptr(); |
77 | return put_packet(&r[0], len); |
78 | } |
79 | |
80 | Error PacketPeer::get_var(Variant &r_variant, bool p_allow_objects) { |
81 | const uint8_t *buffer; |
82 | int buffer_size; |
83 | Error err = get_packet(&buffer, buffer_size); |
84 | if (err) { |
85 | return err; |
86 | } |
87 | |
88 | return decode_variant(r_variant, buffer, buffer_size, nullptr, p_allow_objects); |
89 | } |
90 | |
91 | Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) { |
92 | int len; |
93 | Error err = encode_variant(p_packet, nullptr, len, p_full_objects); // compute len first |
94 | if (err) { |
95 | return err; |
96 | } |
97 | |
98 | if (len == 0) { |
99 | return OK; |
100 | } |
101 | |
102 | ERR_FAIL_COND_V_MSG(len > encode_buffer_max_size, ERR_OUT_OF_MEMORY, "Failed to encode variant, encode size is bigger then encode_buffer_max_size. Consider raising it via 'set_encode_buffer_max_size'." ); |
103 | |
104 | if (unlikely(encode_buffer.size() < len)) { |
105 | encode_buffer.resize(0); // Avoid realloc |
106 | encode_buffer.resize(next_power_of_2(len)); |
107 | } |
108 | |
109 | uint8_t *w = encode_buffer.ptrw(); |
110 | err = encode_variant(p_packet, w, len, p_full_objects); |
111 | ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to encode Variant." ); |
112 | |
113 | return put_packet(w, len); |
114 | } |
115 | |
116 | Variant PacketPeer::_bnd_get_var(bool p_allow_objects) { |
117 | Variant var; |
118 | Error err = get_var(var, p_allow_objects); |
119 | |
120 | ERR_FAIL_COND_V(err != OK, Variant()); |
121 | return var; |
122 | } |
123 | |
124 | Error PacketPeer::_put_packet(const Vector<uint8_t> &p_buffer) { |
125 | return put_packet_buffer(p_buffer); |
126 | } |
127 | |
128 | Vector<uint8_t> PacketPeer::_get_packet() { |
129 | Vector<uint8_t> raw; |
130 | last_get_error = get_packet_buffer(raw); |
131 | return raw; |
132 | } |
133 | |
134 | Error PacketPeer::_get_packet_error() const { |
135 | return last_get_error; |
136 | } |
137 | |
138 | void PacketPeer::_bind_methods() { |
139 | ClassDB::bind_method(D_METHOD("get_var" , "allow_objects" ), &PacketPeer::_bnd_get_var, DEFVAL(false)); |
140 | ClassDB::bind_method(D_METHOD("put_var" , "var" , "full_objects" ), &PacketPeer::put_var, DEFVAL(false)); |
141 | |
142 | ClassDB::bind_method(D_METHOD("get_packet" ), &PacketPeer::_get_packet); |
143 | ClassDB::bind_method(D_METHOD("put_packet" , "buffer" ), &PacketPeer::_put_packet); |
144 | ClassDB::bind_method(D_METHOD("get_packet_error" ), &PacketPeer::_get_packet_error); |
145 | ClassDB::bind_method(D_METHOD("get_available_packet_count" ), &PacketPeer::get_available_packet_count); |
146 | |
147 | ClassDB::bind_method(D_METHOD("get_encode_buffer_max_size" ), &PacketPeer::get_encode_buffer_max_size); |
148 | ClassDB::bind_method(D_METHOD("set_encode_buffer_max_size" , "max_size" ), &PacketPeer::set_encode_buffer_max_size); |
149 | |
150 | ADD_PROPERTY(PropertyInfo(Variant::INT, "encode_buffer_max_size" ), "set_encode_buffer_max_size" , "get_encode_buffer_max_size" ); |
151 | } |
152 | |
153 | /***************/ |
154 | |
155 | Error PacketPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { |
156 | Error err; |
157 | if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) { |
158 | return err; |
159 | } |
160 | WARN_PRINT_ONCE("PacketPeerExtension::_get_packet_native is unimplemented!" ); |
161 | return FAILED; |
162 | } |
163 | |
164 | Error PacketPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) { |
165 | Error err; |
166 | if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) { |
167 | return err; |
168 | } |
169 | WARN_PRINT_ONCE("PacketPeerExtension::_put_packet_native is unimplemented!" ); |
170 | return FAILED; |
171 | } |
172 | |
173 | void PacketPeerExtension::_bind_methods() { |
174 | GDVIRTUAL_BIND(_get_packet, "r_buffer" , "r_buffer_size" ); |
175 | GDVIRTUAL_BIND(_put_packet, "p_buffer" , "p_buffer_size" ); |
176 | GDVIRTUAL_BIND(_get_available_packet_count); |
177 | GDVIRTUAL_BIND(_get_max_packet_size); |
178 | } |
179 | |
180 | /***************/ |
181 | |
182 | void PacketPeerStream::_bind_methods() { |
183 | ClassDB::bind_method(D_METHOD("set_stream_peer" , "peer" ), &PacketPeerStream::set_stream_peer); |
184 | ClassDB::bind_method(D_METHOD("get_stream_peer" ), &PacketPeerStream::get_stream_peer); |
185 | ClassDB::bind_method(D_METHOD("set_input_buffer_max_size" , "max_size_bytes" ), &PacketPeerStream::set_input_buffer_max_size); |
186 | ClassDB::bind_method(D_METHOD("set_output_buffer_max_size" , "max_size_bytes" ), &PacketPeerStream::set_output_buffer_max_size); |
187 | ClassDB::bind_method(D_METHOD("get_input_buffer_max_size" ), &PacketPeerStream::get_input_buffer_max_size); |
188 | ClassDB::bind_method(D_METHOD("get_output_buffer_max_size" ), &PacketPeerStream::get_output_buffer_max_size); |
189 | |
190 | ADD_PROPERTY(PropertyInfo(Variant::INT, "input_buffer_max_size" ), "set_input_buffer_max_size" , "get_input_buffer_max_size" ); |
191 | ADD_PROPERTY(PropertyInfo(Variant::INT, "output_buffer_max_size" ), "set_output_buffer_max_size" , "get_output_buffer_max_size" ); |
192 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream_peer" , PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer" , PROPERTY_USAGE_NONE), "set_stream_peer" , "get_stream_peer" ); |
193 | } |
194 | |
195 | Error PacketPeerStream::_poll_buffer() const { |
196 | ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); |
197 | |
198 | int read = 0; |
199 | ERR_FAIL_COND_V(input_buffer.size() < ring_buffer.space_left(), ERR_UNAVAILABLE); |
200 | Error err = peer->get_partial_data(input_buffer.ptrw(), ring_buffer.space_left(), read); |
201 | if (err) { |
202 | return err; |
203 | } |
204 | if (read == 0) { |
205 | return OK; |
206 | } |
207 | |
208 | int w = ring_buffer.write(&input_buffer[0], read); |
209 | ERR_FAIL_COND_V(w != read, ERR_BUG); |
210 | |
211 | return OK; |
212 | } |
213 | |
214 | int PacketPeerStream::get_available_packet_count() const { |
215 | _poll_buffer(); |
216 | |
217 | uint32_t remaining = ring_buffer.data_left(); |
218 | |
219 | int ofs = 0; |
220 | int count = 0; |
221 | |
222 | while (remaining >= 4) { |
223 | uint8_t lbuf[4]; |
224 | ring_buffer.copy(lbuf, ofs, 4); |
225 | uint32_t len = decode_uint32(lbuf); |
226 | remaining -= 4; |
227 | ofs += 4; |
228 | if (len > remaining) { |
229 | break; |
230 | } |
231 | remaining -= len; |
232 | ofs += len; |
233 | count++; |
234 | } |
235 | |
236 | return count; |
237 | } |
238 | |
239 | Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { |
240 | ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); |
241 | _poll_buffer(); |
242 | |
243 | int remaining = ring_buffer.data_left(); |
244 | ERR_FAIL_COND_V(remaining < 4, ERR_UNAVAILABLE); |
245 | uint8_t lbuf[4]; |
246 | ring_buffer.copy(lbuf, 0, 4); |
247 | remaining -= 4; |
248 | uint32_t len = decode_uint32(lbuf); |
249 | ERR_FAIL_COND_V(remaining < (int)len, ERR_UNAVAILABLE); |
250 | |
251 | ERR_FAIL_COND_V(input_buffer.size() < (int)len, ERR_UNAVAILABLE); |
252 | ring_buffer.read(lbuf, 4); //get rid of first 4 bytes |
253 | ring_buffer.read(input_buffer.ptrw(), len); // read packet |
254 | |
255 | *r_buffer = &input_buffer[0]; |
256 | r_buffer_size = len; |
257 | return OK; |
258 | } |
259 | |
260 | Error PacketPeerStream::put_packet(const uint8_t *p_buffer, int p_buffer_size) { |
261 | ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); |
262 | Error err = _poll_buffer(); //won't hurt to poll here too |
263 | |
264 | if (err) { |
265 | return err; |
266 | } |
267 | |
268 | if (p_buffer_size == 0) { |
269 | return OK; |
270 | } |
271 | |
272 | ERR_FAIL_COND_V(p_buffer_size < 0, ERR_INVALID_PARAMETER); |
273 | ERR_FAIL_COND_V(p_buffer_size + 4 > output_buffer.size(), ERR_INVALID_PARAMETER); |
274 | |
275 | encode_uint32(p_buffer_size, output_buffer.ptrw()); |
276 | uint8_t *dst = &output_buffer.write[4]; |
277 | for (int i = 0; i < p_buffer_size; i++) { |
278 | dst[i] = p_buffer[i]; |
279 | } |
280 | |
281 | return peer->put_data(&output_buffer[0], p_buffer_size + 4); |
282 | } |
283 | |
284 | int PacketPeerStream::get_max_packet_size() const { |
285 | return output_buffer.size(); |
286 | } |
287 | |
288 | void PacketPeerStream::set_stream_peer(const Ref<StreamPeer> &p_peer) { |
289 | if (p_peer.ptr() != peer.ptr()) { |
290 | ring_buffer.advance_read(ring_buffer.data_left()); // Reset the ring buffer. |
291 | } |
292 | |
293 | peer = p_peer; |
294 | } |
295 | |
296 | Ref<StreamPeer> PacketPeerStream::get_stream_peer() const { |
297 | return peer; |
298 | } |
299 | |
300 | void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { |
301 | ERR_FAIL_COND_MSG(p_max_size < 0, "Max size of input buffer size cannot be smaller than 0." ); |
302 | //warning may lose packets |
303 | ERR_FAIL_COND_MSG(ring_buffer.data_left(), "Buffer in use, resizing would cause loss of data." ); |
304 | ring_buffer.resize(nearest_shift(next_power_of_2(p_max_size + 4)) - 1); |
305 | input_buffer.resize(next_power_of_2(p_max_size + 4)); |
306 | } |
307 | |
308 | int PacketPeerStream::get_input_buffer_max_size() const { |
309 | return input_buffer.size() - 4; |
310 | } |
311 | |
312 | void PacketPeerStream::set_output_buffer_max_size(int p_max_size) { |
313 | output_buffer.resize(next_power_of_2(p_max_size + 4)); |
314 | } |
315 | |
316 | int PacketPeerStream::get_output_buffer_max_size() const { |
317 | return output_buffer.size() - 4; |
318 | } |
319 | |
320 | PacketPeerStream::PacketPeerStream() { |
321 | int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2" ); |
322 | |
323 | ring_buffer.resize(rbsize); |
324 | input_buffer.resize(1 << rbsize); |
325 | output_buffer.resize(1 << rbsize); |
326 | } |
327 | |