Debian Codegraph @

scene/main/multiplayer_api.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* multiplayer_api.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 "multiplayer_api.h"
32
33 #include "core/io/marshalls.h"
34 StringName MultiplayerAPI::default_interface;
35
36 void MultiplayerAPI::set_default_interface(const StringName &p_interface) {
37 ERR_FAIL_COND_MSG(!ClassDB::is_parent_class(p_interface, MultiplayerAPI::get_class_static()), vformat("Can't make %s the default multiplayer interface since it does not extend MultiplayerAPI.", p_interface));
38 default_interface = StringName(p_interface, true);
39 }
40
41 StringName MultiplayerAPI::get_default_interface() {
42 return default_interface;
43 }
44
45 Ref<MultiplayerAPI> MultiplayerAPI::create_default_interface() {
46 if (default_interface != StringName()) {
47 return Ref<MultiplayerAPI>(Object::cast_to<MultiplayerAPI>(ClassDB::instantiate(default_interface)));
48 }
49 return Ref<MultiplayerAPI>(memnew(MultiplayerAPIExtension));
50 }
51
52 // The variant is compressed and encoded; The first byte contains all the meta
53 // information and the format is:
54 // - The first LSB 6 bits are used for the variant type.
55 // - The next two bits are used to store the encoding mode.
56 // - Boolean values uses the encoding mode to store the value.
57 #define VARIANT_META_TYPE_MASK 0x3F
58 #define VARIANT_META_EMODE_MASK 0xC0
59 #define VARIANT_META_BOOL_MASK 0x80
60 #define ENCODE_8 0 << 6
61 #define ENCODE_16 1 << 6
62 #define ENCODE_32 2 << 6
63 #define ENCODE_64 3 << 6
64 Error MultiplayerAPI::encode_and_compress_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_allow_object_decoding) {
65 // Unreachable because `VARIANT_MAX` == 38 and `ENCODE_VARIANT_MASK` == 77
66 CRASH_COND(p_variant.get_type() > VARIANT_META_TYPE_MASK);
67
68 uint8_t *buf = r_buffer;
69 r_len = 0;
70 uint8_t encode_mode = 0;
71
72 switch (p_variant.get_type()) {
73 case Variant::BOOL: {
74 if (buf) {
75 // We don't use encode_mode for booleans, so we can use it to store the value.
76 buf[0] = (p_variant.operator bool()) ? (1 << 7) : 0;
77 buf[0] |= p_variant.get_type();
78 }
79 r_len += 1;
80 } break;
81 case Variant::INT: {
82 if (buf) {
83 // Reserve the first byte for the meta.
84 buf += 1;
85 }
86 r_len += 1;
87 int64_t val = p_variant;
88 if (val <= (int64_t)INT8_MAX && val >= (int64_t)INT8_MIN) {
89 // Use 8 bit
90 encode_mode = ENCODE_8;
91 if (buf) {
92 buf[0] = val;
93 }
94 r_len += 1;
95 } else if (val <= (int64_t)INT16_MAX && val >= (int64_t)INT16_MIN) {
96 // Use 16 bit
97 encode_mode = ENCODE_16;
98 if (buf) {
99 encode_uint16(val, buf);
100 }
101 r_len += 2;
102 } else if (val <= (int64_t)INT32_MAX && val >= (int64_t)INT32_MIN) {
103 // Use 32 bit
104 encode_mode = ENCODE_32;
105 if (buf) {
106 encode_uint32(val, buf);
107 }
108 r_len += 4;
109 } else {
110 // Use 64 bit
111 encode_mode = ENCODE_64;
112 if (buf) {
113 encode_uint64(val, buf);
114 }
115 r_len += 8;
116 }
117 // Store the meta
118 if (buf) {
119 buf -= 1;
120 buf[0] = encode_mode | p_variant.get_type();
121 }
122 } break;
123 default:
124 // Any other case is not yet compressed.
125 Error err = encode_variant(p_variant, r_buffer, r_len, p_allow_object_decoding);
126 if (err != OK) {
127 return err;
128 }
129 if (r_buffer) {
130 // The first byte is not used by the marshaling, so store the type
131 // so we know how to decompress and decode this variant.
132 r_buffer[0] = p_variant.get_type();
133 }
134 }
135
136 return OK;
137 }
138
139 Error MultiplayerAPI::decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_object_decoding) {
140 const uint8_t *buf = p_buffer;
141 int len = p_len;
142
143 ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
144 uint8_t type = buf[0] & VARIANT_META_TYPE_MASK;
145 uint8_t encode_mode = buf[0] & VARIANT_META_EMODE_MASK;
146
147 ERR_FAIL_COND_V(type >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
148
149 switch (type) {
150 case Variant::BOOL: {
151 bool val = (buf[0] & VARIANT_META_BOOL_MASK) > 0;
152 r_variant = val;
153 if (r_len) {
154 *r_len = 1;
155 }
156 } break;
157 case Variant::INT: {
158 buf += 1;
159 len -= 1;
160 if (r_len) {
161 *r_len = 1;
162 }
163 if (encode_mode == ENCODE_8) {
164 // 8 bits.
165 ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
166 int8_t val = buf[0];
167 r_variant = val;
168 if (r_len) {
169 (*r_len) += 1;
170 }
171 } else if (encode_mode == ENCODE_16) {
172 // 16 bits.
173 ERR_FAIL_COND_V(len < 2, ERR_INVALID_DATA);
174 int16_t val = decode_uint16(buf);
175 r_variant = val;
176 if (r_len) {
177 (*r_len) += 2;
178 }
179 } else if (encode_mode == ENCODE_32) {
180 // 32 bits.
181 ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
182 int32_t val = decode_uint32(buf);
183 r_variant = val;
184 if (r_len) {
185 (*r_len) += 4;
186 }
187 } else {
188 // 64 bits.
189 ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
190 int64_t val = decode_uint64(buf);
191 r_variant = val;
192 if (r_len) {
193 (*r_len) += 8;
194 }
195 }
196 } break;
197 default:
198 Error err = decode_variant(r_variant, p_buffer, p_len, r_len, p_allow_object_decoding);
199 if (err != OK) {
200 return err;
201 }
202 }
203
204 return OK;
205 }
206
207 Error MultiplayerAPI::encode_and_compress_variants(const Variant **p_variants, int p_count, uint8_t *p_buffer, int &r_len, bool *r_raw, bool p_allow_object_decoding) {
208 r_len = 0;
209 int size = 0;
210
211 if (p_count == 0) {
212 if (r_raw) {
213 *r_raw = true;
214 }
215 return OK;
216 }
217
218 // Try raw encoding optimization.
219 if (r_raw && p_count == 1) {
220 *r_raw = false;
221 const Variant &v = *(p_variants[0]);
222 if (v.get_type() == Variant::PACKED_BYTE_ARRAY) {
223 *r_raw = true;
224 const PackedByteArray pba = v;
225 if (p_buffer) {
226 memcpy(p_buffer, pba.ptr(), pba.size());
227 }
228 r_len += pba.size();
229 } else {
230 encode_and_compress_variant(v, p_buffer, size, p_allow_object_decoding);
231 r_len += size;
232 }
233 return OK;
234 }
235
236 // Regular encoding.
237 for (int i = 0; i < p_count; i++) {
238 const Variant &v = *(p_variants[i]);
239 encode_and_compress_variant(v, p_buffer ? p_buffer + r_len : nullptr, size, p_allow_object_decoding);
240 r_len += size;
241 }
242 return OK;
243 }
244
245 Error MultiplayerAPI::decode_and_decompress_variants(Vector<Variant> &r_variants, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw, bool p_allow_object_decoding) {
246 r_len = 0;
247 int argc = r_variants.size();
248 if (argc == 0 && p_raw) {
249 return OK;
250 }
251 ERR_FAIL_COND_V(p_raw && argc != 1, ERR_INVALID_DATA);
252 if (p_raw) {
253 r_len = p_len;
254 PackedByteArray pba;
255 pba.resize(p_len);
256 memcpy(pba.ptrw(), p_buffer, p_len);
257 r_variants.write[0] = pba;
258 return OK;
259 }
260
261 for (int i = 0; i < argc; i++) {
262 ERR_FAIL_COND_V_MSG(r_len >= p_len, ERR_INVALID_DATA, "Invalid packet received. Size too small.");
263
264 int vlen;
265 Error err = MultiplayerAPI::decode_and_decompress_variant(r_variants.write[i], &p_buffer[r_len], p_len - r_len, &vlen, p_allow_object_decoding);
266 ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid packet received. Unable to decode state variable.");
267 r_len += vlen;
268 }
269 return OK;
270 }
271
272 Error MultiplayerAPI::_rpc_bind(int p_peer, Object *p_object, const StringName &p_method, Array p_args) {
273 Vector<Variant> args;
274 Vector<const Variant *> argsp;
275 args.resize(p_args.size());
276 argsp.resize(p_args.size());
277 Variant *ptr = args.ptrw();
278 const Variant **pptr = argsp.ptrw();
279 for (int i = 0; i < p_args.size(); i++) {
280 ptr[i] = p_args[i];
281 pptr[i] = &ptr[i];
282 }
283 return rpcp(p_object, p_peer, p_method, argsp.size() ? argsp.ptrw() : nullptr, argsp.size());
284 }
285
286 void MultiplayerAPI::_bind_methods() {
287 ClassDB::bind_method(D_METHOD("has_multiplayer_peer"), &MultiplayerAPI::has_multiplayer_peer);
288 ClassDB::bind_method(D_METHOD("get_multiplayer_peer"), &MultiplayerAPI::get_multiplayer_peer);
289 ClassDB::bind_method(D_METHOD("set_multiplayer_peer", "peer"), &MultiplayerAPI::set_multiplayer_peer);
290 ClassDB::bind_method(D_METHOD("get_unique_id"), &MultiplayerAPI::get_unique_id);
291 ClassDB::bind_method(D_METHOD("is_server"), &MultiplayerAPI::is_server);
292 ClassDB::bind_method(D_METHOD("get_remote_sender_id"), &MultiplayerAPI::get_remote_sender_id);
293 ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
294 ClassDB::bind_method(D_METHOD("rpc", "peer", "object", "method", "arguments"), &MultiplayerAPI::_rpc_bind, DEFVAL(Array()));
295 ClassDB::bind_method(D_METHOD("object_configuration_add", "object", "configuration"), &MultiplayerAPI::object_configuration_add);
296 ClassDB::bind_method(D_METHOD("object_configuration_remove", "object", "configuration"), &MultiplayerAPI::object_configuration_remove);
297
298 ClassDB::bind_method(D_METHOD("get_peers"), &MultiplayerAPI::get_peer_ids);
299
300 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer_peer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerPeer", PROPERTY_USAGE_NONE), "set_multiplayer_peer", "get_multiplayer_peer");
301
302 ClassDB::bind_static_method("MultiplayerAPI", D_METHOD("set_default_interface", "interface_name"), &MultiplayerAPI::set_default_interface);
303 ClassDB::bind_static_method("MultiplayerAPI", D_METHOD("get_default_interface"), &MultiplayerAPI::get_default_interface);
304 ClassDB::bind_static_method("MultiplayerAPI", D_METHOD("create_default_interface"), &MultiplayerAPI::create_default_interface);
305
306 ADD_SIGNAL(MethodInfo("peer_connected", PropertyInfo(Variant::INT, "id")));
307 ADD_SIGNAL(MethodInfo("peer_disconnected", PropertyInfo(Variant::INT, "id")));
308 ADD_SIGNAL(MethodInfo("connected_to_server"));
309 ADD_SIGNAL(MethodInfo("connection_failed"));
310 ADD_SIGNAL(MethodInfo("server_disconnected"));
311
312 BIND_ENUM_CONSTANT(RPC_MODE_DISABLED);
313 BIND_ENUM_CONSTANT(RPC_MODE_ANY_PEER);
314 BIND_ENUM_CONSTANT(RPC_MODE_AUTHORITY);
315 }
316
317 /// MultiplayerAPIExtension
318
319 Error MultiplayerAPIExtension::poll() {
320 Error err = OK;
321 GDVIRTUAL_CALL(_poll, err);
322 return err;
323 }
324
325 void MultiplayerAPIExtension::set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) {
326 GDVIRTUAL_CALL(_set_multiplayer_peer, p_peer);
327 }
328
329 Ref<MultiplayerPeer> MultiplayerAPIExtension::get_multiplayer_peer() {
330 Ref<MultiplayerPeer> peer;
331 GDVIRTUAL_CALL(_get_multiplayer_peer, peer);
332 return peer;
333 }
334
335 int MultiplayerAPIExtension::get_unique_id() {
336 int id = 1;
337 GDVIRTUAL_CALL(_get_unique_id, id);
338 return id;
339 }
340
341 Vector<int> MultiplayerAPIExtension::get_peer_ids() {
342 Vector<int> ids;
343 GDVIRTUAL_CALL(_get_peer_ids, ids);
344 return ids;
345 }
346
347 Error MultiplayerAPIExtension::rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
348 if (!GDVIRTUAL_IS_OVERRIDDEN(_rpc)) {
349 return ERR_UNAVAILABLE;
350 }
351 Array args;
352 for (int i = 0; i < p_argcount; i++) {
353 args.push_back(*p_arg[i]);
354 }
355 Error ret = FAILED;
356 GDVIRTUAL_CALL(_rpc, p_peer_id, p_obj, p_method, args, ret);
357 return ret;
358 }
359
360 int MultiplayerAPIExtension::get_remote_sender_id() {
361 int id = 0;
362 GDVIRTUAL_CALL(_get_remote_sender_id, id);
363 return id;
364 }
365
366 Error MultiplayerAPIExtension::object_configuration_add(Object *p_object, Variant p_config) {
367 Error err = ERR_UNAVAILABLE;
368 GDVIRTUAL_CALL(_object_configuration_add, p_object, p_config, err);
369 return err;
370 }
371
372 Error MultiplayerAPIExtension::object_configuration_remove(Object *p_object, Variant p_config) {
373 Error err = ERR_UNAVAILABLE;
374 GDVIRTUAL_CALL(_object_configuration_remove, p_object, p_config, err);
375 return err;
376 }
377
378 void MultiplayerAPIExtension::_bind_methods() {
379 GDVIRTUAL_BIND(_poll);
380 GDVIRTUAL_BIND(_set_multiplayer_peer, "multiplayer_peer");
381 GDVIRTUAL_BIND(_get_multiplayer_peer);
382 GDVIRTUAL_BIND(_get_unique_id);
383 GDVIRTUAL_BIND(_get_peer_ids);
384 GDVIRTUAL_BIND(_rpc, "peer", "object", "method", "args");
385 GDVIRTUAL_BIND(_get_remote_sender_id);
386 GDVIRTUAL_BIND(_object_configuration_add, "object", "configuration");
387 GDVIRTUAL_BIND(_object_configuration_remove, "object", "configuration");
388 }