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