Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/scene_rpc_interface.h
10277 views
1
/**************************************************************************/
2
/* scene_rpc_interface.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_api.h"
35
36
class SceneMultiplayer;
37
class SceneCacheInterface;
38
class SceneReplicationInterface;
39
class Node;
40
41
class SceneRPCInterface : public RefCounted {
42
GDCLASS(SceneRPCInterface, RefCounted);
43
44
private:
45
struct RPCConfig {
46
StringName name;
47
MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
48
bool call_local = false;
49
MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
50
int channel = 0;
51
52
bool operator==(RPCConfig const &p_other) const {
53
return name == p_other.name;
54
}
55
};
56
57
struct RPCConfigCache {
58
HashMap<uint16_t, RPCConfig> configs;
59
HashMap<StringName, uint16_t> ids;
60
};
61
62
struct SortRPCConfig {
63
StringName::AlphCompare compare;
64
bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {
65
return compare(p_a.name, p_b.name);
66
}
67
};
68
69
enum NetworkNodeIdCompression {
70
NETWORK_NODE_ID_COMPRESSION_8 = 0,
71
NETWORK_NODE_ID_COMPRESSION_16,
72
NETWORK_NODE_ID_COMPRESSION_32,
73
};
74
75
enum NetworkNameIdCompression {
76
NETWORK_NAME_ID_COMPRESSION_8 = 0,
77
NETWORK_NAME_ID_COMPRESSION_16,
78
};
79
80
SceneMultiplayer *multiplayer = nullptr;
81
SceneCacheInterface *multiplayer_cache = nullptr;
82
SceneReplicationInterface *multiplayer_replicator = nullptr;
83
84
Vector<uint8_t> packet_cache;
85
86
HashMap<ObjectID, RPCConfigCache> rpc_cache;
87
88
#ifdef DEBUG_ENABLED
89
_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
90
#endif
91
92
protected:
93
void _process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset);
94
95
void _send_rpc(Node *p_from, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount);
96
Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);
97
98
void _parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache);
99
const RPCConfigCache &_get_node_config(const Node *p_node);
100
101
public:
102
Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
103
void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);
104
String get_rpc_md5(const Object *p_obj);
105
106
SceneRPCInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache, SceneReplicationInterface *p_replicator) {
107
multiplayer = p_multiplayer;
108
multiplayer_cache = p_cache;
109
multiplayer_replicator = p_replicator;
110
}
111
};
112
113