Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/scene_multiplayer.h
10277 views
1
/**************************************************************************/
2
/* scene_multiplayer.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 "scene_cache_interface.h"
34
#include "scene_replication_interface.h"
35
#include "scene_rpc_interface.h"
36
37
#include "scene/main/multiplayer_api.h"
38
39
class OfflineMultiplayerPeer : public MultiplayerPeer {
40
GDCLASS(OfflineMultiplayerPeer, MultiplayerPeer);
41
42
public:
43
virtual int get_available_packet_count() const override { return 0; }
44
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override {
45
*r_buffer = nullptr;
46
r_buffer_size = 0;
47
return OK;
48
}
49
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override { return OK; }
50
virtual int get_max_packet_size() const override { return 0; }
51
52
virtual void set_target_peer(int p_peer_id) override {}
53
virtual int get_packet_peer() const override { return 0; }
54
virtual TransferMode get_packet_mode() const override { return TRANSFER_MODE_RELIABLE; }
55
virtual int get_packet_channel() const override { return 0; }
56
virtual void disconnect_peer(int p_peer, bool p_force = false) override {}
57
virtual bool is_server() const override { return true; }
58
virtual void poll() override {}
59
virtual void close() override {}
60
virtual int get_unique_id() const override { return TARGET_PEER_SERVER; }
61
virtual ConnectionStatus get_connection_status() const override { return CONNECTION_CONNECTED; }
62
};
63
64
class SceneMultiplayer : public MultiplayerAPI {
65
GDCLASS(SceneMultiplayer, MultiplayerAPI);
66
67
public:
68
enum NetworkCommands {
69
NETWORK_COMMAND_REMOTE_CALL = 0,
70
NETWORK_COMMAND_SIMPLIFY_PATH,
71
NETWORK_COMMAND_CONFIRM_PATH,
72
NETWORK_COMMAND_RAW,
73
NETWORK_COMMAND_SPAWN,
74
NETWORK_COMMAND_DESPAWN,
75
NETWORK_COMMAND_SYNC,
76
NETWORK_COMMAND_SYS,
77
};
78
79
enum SysCommands {
80
SYS_COMMAND_AUTH,
81
SYS_COMMAND_ADD_PEER,
82
SYS_COMMAND_DEL_PEER,
83
SYS_COMMAND_RELAY,
84
};
85
86
enum {
87
SYS_CMD_SIZE = 6, // Command + sys command + peer_id (+ optional payload).
88
};
89
90
// For each command, the 4 MSB can contain custom flags, as defined by subsystems.
91
enum {
92
CMD_FLAG_0_SHIFT = 4,
93
CMD_FLAG_1_SHIFT = 5,
94
CMD_FLAG_2_SHIFT = 6,
95
CMD_FLAG_3_SHIFT = 7,
96
};
97
98
// This is the mask that will be used to extract the command.
99
enum {
100
CMD_MASK = 7, // 0x7 -> 0b00000111
101
};
102
103
private:
104
struct PendingPeer {
105
bool local = false;
106
bool remote = false;
107
uint64_t time = 0;
108
};
109
110
Ref<MultiplayerPeer> multiplayer_peer;
111
MultiplayerPeer::ConnectionStatus last_connection_status = MultiplayerPeer::CONNECTION_DISCONNECTED;
112
HashMap<int, PendingPeer> pending_peers; // true if locally finalized.
113
Callable auth_callback;
114
uint64_t auth_timeout = 3000;
115
HashSet<int> connected_peers;
116
int remote_sender_id = 0;
117
int remote_sender_override = 0;
118
119
Vector<uint8_t> packet_cache;
120
121
NodePath root_path;
122
bool allow_object_decoding = false;
123
bool server_relay = true;
124
Ref<StreamPeerBuffer> relay_buffer;
125
126
Ref<SceneCacheInterface> cache;
127
Ref<SceneReplicationInterface> replicator;
128
Ref<SceneRPCInterface> rpc;
129
130
#ifdef DEBUG_ENABLED
131
_FORCE_INLINE_ void _profile_bandwidth(const String &p_what, int p_value);
132
_FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len); // Also profiles.
133
#else
134
_FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len) {
135
return multiplayer_peer->put_packet(p_packet, p_packet_len);
136
}
137
#endif
138
139
protected:
140
static void _bind_methods();
141
142
void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
143
void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
144
void _process_sys(int p_from, const uint8_t *p_packet, int p_packet_len, MultiplayerPeer::TransferMode p_mode, int p_channel);
145
146
void _add_peer(int p_id);
147
void _admit_peer(int p_id);
148
void _del_peer(int p_id);
149
void _update_status();
150
151
public:
152
virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override;
153
virtual Ref<MultiplayerPeer> get_multiplayer_peer() override;
154
155
virtual Error poll() override;
156
virtual int get_unique_id() override;
157
virtual Vector<int> get_peer_ids() override;
158
virtual int get_remote_sender_id() override { return remote_sender_override ? remote_sender_override : remote_sender_id; }
159
160
virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override;
161
162
virtual Error object_configuration_add(Object *p_obj, Variant p_config) override;
163
virtual Error object_configuration_remove(Object *p_obj, Variant p_config) override;
164
165
void clear();
166
167
// Usually from object_configuration_add/remove
168
void set_root_path(const NodePath &p_path);
169
NodePath get_root_path() const;
170
171
void disconnect_peer(int p_id);
172
173
Error send_auth(int p_to, Vector<uint8_t> p_bytes);
174
Error complete_auth(int p_peer);
175
void set_auth_callback(Callable p_callback);
176
Callable get_auth_callback() const;
177
void set_auth_timeout(double p_timeout);
178
double get_auth_timeout() const;
179
Vector<int> get_authenticating_peer_ids();
180
181
Error send_command(int p_to, const uint8_t *p_packet, int p_packet_len); // Used internally to relay packets when needed.
182
Error send_bytes(Vector<uint8_t> p_data, int p_to = MultiplayerPeer::TARGET_PEER_BROADCAST, MultiplayerPeer::TransferMode p_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE, int p_channel = 0);
183
String get_rpc_md5(const Object *p_obj);
184
185
const HashSet<int> get_connected_peers() const { return connected_peers; }
186
187
void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }
188
void set_refuse_new_connections(bool p_refuse);
189
bool is_refusing_new_connections() const;
190
191
void set_allow_object_decoding(bool p_enable);
192
bool is_object_decoding_allowed() const;
193
194
void set_server_relay_enabled(bool p_enabled);
195
bool is_server_relay_enabled() const;
196
197
void set_max_sync_packet_size(int p_size);
198
int get_max_sync_packet_size() const;
199
200
void set_max_delta_packet_size(int p_size);
201
int get_max_delta_packet_size() const;
202
203
SceneMultiplayer();
204
~SceneMultiplayer();
205
};
206
207