Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/multiplayer_api.h
9896 views
1
/**************************************************************************/
2
/* multiplayer_api.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/object/ref_counted.h"
34
#include "scene/main/multiplayer_peer.h"
35
36
class MultiplayerAPI : public RefCounted {
37
GDCLASS(MultiplayerAPI, RefCounted);
38
39
private:
40
static StringName default_interface;
41
42
protected:
43
static void _bind_methods();
44
Error _rpc_bind(int p_peer, Object *p_obj, const StringName &p_method, Array args = Array());
45
46
public:
47
enum RPCMode {
48
RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default)
49
RPC_MODE_ANY_PEER, // Any peer can call this RPC
50
RPC_MODE_AUTHORITY, // Only the node's multiplayer authority (server by default) can call this RPC
51
};
52
53
static Ref<MultiplayerAPI> create_default_interface();
54
static void set_default_interface(const StringName &p_interface);
55
static StringName get_default_interface();
56
57
static Error encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len, bool p_allow_object_decoding);
58
static Error decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_object_decoding);
59
static Error encode_and_compress_variants(const Variant **p_variants, int p_count, uint8_t *p_buffer, int &r_len, bool *r_raw = nullptr, bool p_allow_object_decoding = false);
60
static Error decode_and_decompress_variants(Vector<Variant> &r_variants, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw = false, bool p_allow_object_decoding = false);
61
62
virtual Error poll() = 0;
63
virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) = 0;
64
virtual Ref<MultiplayerPeer> get_multiplayer_peer() = 0;
65
virtual int get_unique_id() = 0;
66
virtual Vector<int> get_peer_ids() = 0;
67
68
virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) = 0;
69
virtual int get_remote_sender_id() = 0;
70
71
virtual Error object_configuration_add(Object *p_object, Variant p_config) = 0;
72
virtual Error object_configuration_remove(Object *p_object, Variant p_config) = 0;
73
74
bool has_multiplayer_peer() { return get_multiplayer_peer().is_valid(); }
75
bool is_server() { return get_unique_id() == MultiplayerPeer::TARGET_PEER_SERVER; }
76
77
virtual ~MultiplayerAPI() {}
78
};
79
80
VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
81
82
class MultiplayerAPIExtension : public MultiplayerAPI {
83
GDCLASS(MultiplayerAPIExtension, MultiplayerAPI);
84
85
protected:
86
static void _bind_methods();
87
88
public:
89
virtual Error poll() override;
90
virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override;
91
virtual Ref<MultiplayerPeer> get_multiplayer_peer() override;
92
virtual int get_unique_id() override;
93
virtual Vector<int> get_peer_ids() override;
94
95
virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override;
96
virtual int get_remote_sender_id() override;
97
98
virtual Error object_configuration_add(Object *p_object, Variant p_config) override;
99
virtual Error object_configuration_remove(Object *p_object, Variant p_config) override;
100
101
// Extensions
102
GDVIRTUAL0R(Error, _poll);
103
GDVIRTUAL1(_set_multiplayer_peer, Ref<MultiplayerPeer>);
104
GDVIRTUAL0R(Ref<MultiplayerPeer>, _get_multiplayer_peer);
105
GDVIRTUAL0RC(int, _get_unique_id);
106
GDVIRTUAL0RC(PackedInt32Array, _get_peer_ids);
107
GDVIRTUAL4R(Error, _rpc, int, Object *, StringName, Array);
108
GDVIRTUAL0RC(int, _get_remote_sender_id);
109
GDVIRTUAL2R(Error, _object_configuration_add, Object *, Variant);
110
GDVIRTUAL2R(Error, _object_configuration_remove, Object *, Variant);
111
};
112
113