|
@@ -32,7 +32,8 @@
|
|
|
|
|
|
#include "core/debugger/engine_debugger.h"
|
|
|
#include "core/io/marshalls.h"
|
|
|
-#include "core/io/multiplayer_replicator.h"
|
|
|
+#include "core/multiplayer/multiplayer_replicator.h"
|
|
|
+#include "core/multiplayer/rpc_manager.h"
|
|
|
#include "scene/main/node.h"
|
|
|
|
|
|
#include <stdint.h>
|
|
@@ -41,71 +42,18 @@
|
|
|
#include "core/os/os.h"
|
|
|
#endif
|
|
|
|
|
|
-String _get_rpc_md5(const Node *p_node) {
|
|
|
- String rpc_list;
|
|
|
- const Vector<MultiplayerAPI::RPCConfig> node_config = p_node->get_node_rpc_methods();
|
|
|
- for (int i = 0; i < node_config.size(); i++) {
|
|
|
- rpc_list += String(node_config[i].name);
|
|
|
- }
|
|
|
- if (p_node->get_script_instance()) {
|
|
|
- const Vector<MultiplayerAPI::RPCConfig> script_config = p_node->get_script_instance()->get_rpc_methods();
|
|
|
- for (int i = 0; i < script_config.size(); i++) {
|
|
|
- rpc_list += String(script_config[i].name);
|
|
|
- }
|
|
|
- }
|
|
|
- return rpc_list.md5_text();
|
|
|
-}
|
|
|
-
|
|
|
-const MultiplayerAPI::RPCConfig _get_rpc_config(const Node *p_node, const StringName &p_method, uint16_t &r_id) {
|
|
|
- const Vector<MultiplayerAPI::RPCConfig> node_config = p_node->get_node_rpc_methods();
|
|
|
- for (int i = 0; i < node_config.size(); i++) {
|
|
|
- if (node_config[i].name == p_method) {
|
|
|
- r_id = ((uint16_t)i) | (1 << 15);
|
|
|
- return node_config[i];
|
|
|
- }
|
|
|
- }
|
|
|
- if (p_node->get_script_instance()) {
|
|
|
- const Vector<MultiplayerAPI::RPCConfig> script_config = p_node->get_script_instance()->get_rpc_methods();
|
|
|
- for (int i = 0; i < script_config.size(); i++) {
|
|
|
- if (script_config[i].name == p_method) {
|
|
|
- r_id = (uint16_t)i;
|
|
|
- return script_config[i];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return MultiplayerAPI::RPCConfig();
|
|
|
-}
|
|
|
-
|
|
|
-const MultiplayerAPI::RPCConfig _get_rpc_config_by_id(Node *p_node, uint16_t p_id) {
|
|
|
- Vector<MultiplayerAPI::RPCConfig> config;
|
|
|
- uint16_t id = p_id;
|
|
|
- if (id & (1 << 15)) {
|
|
|
- id = id & ~(1 << 15);
|
|
|
- config = p_node->get_node_rpc_methods();
|
|
|
- } else if (p_node->get_script_instance()) {
|
|
|
- config = p_node->get_script_instance()->get_rpc_methods();
|
|
|
- }
|
|
|
- if (id < config.size()) {
|
|
|
- return config[id];
|
|
|
- }
|
|
|
- return MultiplayerAPI::RPCConfig();
|
|
|
-}
|
|
|
-
|
|
|
-_FORCE_INLINE_ bool _can_call_mode(Node *p_node, MultiplayerAPI::RPCMode mode, int p_remote_id) {
|
|
|
- switch (mode) {
|
|
|
- case MultiplayerAPI::RPC_MODE_DISABLED: {
|
|
|
- return false;
|
|
|
- } break;
|
|
|
- case MultiplayerAPI::RPC_MODE_ANY: {
|
|
|
- return true;
|
|
|
- } break;
|
|
|
- case MultiplayerAPI::RPC_MODE_AUTHORITY: {
|
|
|
- return !p_node->is_network_authority() && p_remote_id == p_node->get_network_authority();
|
|
|
- } break;
|
|
|
+#ifdef DEBUG_ENABLED
|
|
|
+void MultiplayerAPI::profile_bandwidth(const String &p_inout, int p_size) {
|
|
|
+ if (EngineDebugger::is_profiling("multiplayer")) {
|
|
|
+ Array values;
|
|
|
+ values.push_back("bandwidth");
|
|
|
+ values.push_back(p_inout);
|
|
|
+ values.push_back(OS::get_singleton()->get_ticks_msec());
|
|
|
+ values.push_back(p_size);
|
|
|
+ EngineDebugger::profiler_add_frame_data("multiplayer", values);
|
|
|
}
|
|
|
-
|
|
|
- return false;
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
void MultiplayerAPI::poll() {
|
|
|
if (!network_peer.is_valid() || network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED) {
|
|
@@ -129,9 +77,9 @@ void MultiplayerAPI::poll() {
|
|
|
break; // Something is wrong!
|
|
|
}
|
|
|
|
|
|
- rpc_sender_id = sender;
|
|
|
+ remote_sender_id = sender;
|
|
|
_process_packet(sender, packet, len);
|
|
|
- rpc_sender_id = 0;
|
|
|
+ remote_sender_id = 0;
|
|
|
|
|
|
if (!network_peer.is_valid()) {
|
|
|
break; // It's also possible that a packet or RPC caused a disconnection, so also check here.
|
|
@@ -191,49 +139,16 @@ Ref<MultiplayerPeer> MultiplayerAPI::get_network_peer() const {
|
|
|
return network_peer;
|
|
|
}
|
|
|
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
-void _profile_node_data(const String &p_what, ObjectID p_id) {
|
|
|
- if (EngineDebugger::is_profiling("multiplayer")) {
|
|
|
- Array values;
|
|
|
- values.push_back("node");
|
|
|
- values.push_back(p_id);
|
|
|
- values.push_back(p_what);
|
|
|
- EngineDebugger::profiler_add_frame_data("multiplayer", values);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void _profile_bandwidth_data(const String &p_inout, int p_size) {
|
|
|
- if (EngineDebugger::is_profiling("multiplayer")) {
|
|
|
- Array values;
|
|
|
- values.push_back("bandwidth");
|
|
|
- values.push_back(p_inout);
|
|
|
- values.push_back(OS::get_singleton()->get_ticks_msec());
|
|
|
- values.push_back(p_size);
|
|
|
- EngineDebugger::profiler_add_frame_data("multiplayer", values);
|
|
|
- }
|
|
|
-}
|
|
|
-#endif
|
|
|
-
|
|
|
-// Returns the packet size stripping the node path added when the node is not yet cached.
|
|
|
-int get_packet_len(uint32_t p_node_target, int p_packet_len) {
|
|
|
- if (p_node_target & 0x80000000) {
|
|
|
- int ofs = p_node_target & 0x7FFFFFFF;
|
|
|
- return p_packet_len - (p_packet_len - ofs);
|
|
|
- } else {
|
|
|
- return p_packet_len;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
|
|
|
ERR_FAIL_COND_MSG(root_node == nullptr, "Multiplayer root node was not initialized. If you are using custom multiplayer, remember to set the root node via MultiplayerAPI.set_root_node before using it.");
|
|
|
ERR_FAIL_COND_MSG(p_packet_len < 1, "Invalid packet received. Size too small.");
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
- _profile_bandwidth_data("in", p_packet_len);
|
|
|
+ profile_bandwidth("in", p_packet_len);
|
|
|
#endif
|
|
|
|
|
|
// Extract the `packet_type` from the LSB three bits:
|
|
|
- uint8_t packet_type = p_packet[0] & 7;
|
|
|
+ uint8_t packet_type = p_packet[0] & CMD_MASK;
|
|
|
|
|
|
switch (packet_type) {
|
|
|
case NETWORK_COMMAND_SIMPLIFY_PATH: {
|
|
@@ -245,76 +160,7 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_
|
|
|
} break;
|
|
|
|
|
|
case NETWORK_COMMAND_REMOTE_CALL: {
|
|
|
- // Extract packet meta
|
|
|
- int packet_min_size = 1;
|
|
|
- int name_id_offset = 1;
|
|
|
- ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
|
|
|
- // Compute the meta size, which depends on the compression level.
|
|
|
- int node_id_compression = (p_packet[0] & 24) >> NODE_ID_COMPRESSION_SHIFT;
|
|
|
- int name_id_compression = (p_packet[0] & 32) >> NAME_ID_COMPRESSION_SHIFT;
|
|
|
-
|
|
|
- switch (node_id_compression) {
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_8:
|
|
|
- packet_min_size += 1;
|
|
|
- name_id_offset += 1;
|
|
|
- break;
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_16:
|
|
|
- packet_min_size += 2;
|
|
|
- name_id_offset += 2;
|
|
|
- break;
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_32:
|
|
|
- packet_min_size += 4;
|
|
|
- name_id_offset += 4;
|
|
|
- break;
|
|
|
- default:
|
|
|
- ERR_FAIL_MSG("Was not possible to extract the node id compression mode.");
|
|
|
- }
|
|
|
- switch (name_id_compression) {
|
|
|
- case NETWORK_NAME_ID_COMPRESSION_8:
|
|
|
- packet_min_size += 1;
|
|
|
- break;
|
|
|
- case NETWORK_NAME_ID_COMPRESSION_16:
|
|
|
- packet_min_size += 2;
|
|
|
- break;
|
|
|
- default:
|
|
|
- ERR_FAIL_MSG("Was not possible to extract the name id compression mode.");
|
|
|
- }
|
|
|
- ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
|
|
|
-
|
|
|
- uint32_t node_target = 0;
|
|
|
- switch (node_id_compression) {
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_8:
|
|
|
- node_target = p_packet[1];
|
|
|
- break;
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_16:
|
|
|
- node_target = decode_uint16(p_packet + 1);
|
|
|
- break;
|
|
|
- case NETWORK_NODE_ID_COMPRESSION_32:
|
|
|
- node_target = decode_uint32(p_packet + 1);
|
|
|
- break;
|
|
|
- default:
|
|
|
- // Unreachable, checked before.
|
|
|
- CRASH_NOW();
|
|
|
- }
|
|
|
-
|
|
|
- Node *node = _process_get_node(p_from, p_packet, node_target, p_packet_len);
|
|
|
- ERR_FAIL_COND_MSG(node == nullptr, "Invalid packet received. Requested node was not found.");
|
|
|
-
|
|
|
- uint16_t name_id = 0;
|
|
|
- switch (name_id_compression) {
|
|
|
- case NETWORK_NAME_ID_COMPRESSION_8:
|
|
|
- name_id = p_packet[name_id_offset];
|
|
|
- break;
|
|
|
- case NETWORK_NAME_ID_COMPRESSION_16:
|
|
|
- name_id = decode_uint16(p_packet + name_id_offset);
|
|
|
- break;
|
|
|
- default:
|
|
|
- // Unreachable, checked before.
|
|
|
- CRASH_NOW();
|
|
|
- }
|
|
|
-
|
|
|
- const int packet_len = get_packet_len(node_target, p_packet_len);
|
|
|
- _process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size);
|
|
|
+ rpc_manager->process_rpc(p_from, p_packet, p_packet_len);
|
|
|
} break;
|
|
|
|
|
|
case NETWORK_COMMAND_RAW: {
|
|
@@ -332,101 +178,6 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) {
|
|
|
- Node *node = nullptr;
|
|
|
-
|
|
|
- if (p_node_target & 0x80000000) {
|
|
|
- // Use full path (not cached yet).
|
|
|
- int ofs = p_node_target & 0x7FFFFFFF;
|
|
|
-
|
|
|
- ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, nullptr, "Invalid packet received. Size smaller than declared.");
|
|
|
-
|
|
|
- String paths;
|
|
|
- paths.parse_utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
|
|
|
-
|
|
|
- NodePath np = paths;
|
|
|
-
|
|
|
- node = root_node->get_node(np);
|
|
|
-
|
|
|
- if (!node) {
|
|
|
- ERR_PRINT("Failed to get path from RPC: " + String(np) + ".");
|
|
|
- }
|
|
|
- return node;
|
|
|
- } else {
|
|
|
- // Use cached path.
|
|
|
- return get_cached_node(p_from, p_node_target);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void MultiplayerAPI::_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) {
|
|
|
- ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small.");
|
|
|
-
|
|
|
- // Check that remote can call the RPC on this node.
|
|
|
- const RPCConfig config = _get_rpc_config_by_id(p_node, p_rpc_method_id);
|
|
|
- ERR_FAIL_COND(config.name == StringName());
|
|
|
-
|
|
|
- bool can_call = _can_call_mode(p_node, config.rpc_mode, p_from);
|
|
|
- ERR_FAIL_COND_MSG(!can_call, "RPC '" + String(config.name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)config.rpc_mode) + ", authority is " + itos(p_node->get_network_authority()) + ".");
|
|
|
-
|
|
|
- int argc = 0;
|
|
|
- bool byte_only = false;
|
|
|
-
|
|
|
- const bool byte_only_or_no_args = ((p_packet[0] & 64) >> BYTE_ONLY_OR_NO_ARGS_SHIFT) == 1;
|
|
|
- if (byte_only_or_no_args) {
|
|
|
- if (p_offset < p_packet_len) {
|
|
|
- // This packet contains only bytes.
|
|
|
- argc = 1;
|
|
|
- byte_only = true;
|
|
|
- } else {
|
|
|
- // This rpc calls a method without parameters.
|
|
|
- }
|
|
|
- } else {
|
|
|
- // Normal variant, takes the argument count from the packet.
|
|
|
- ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
|
|
|
- argc = p_packet[p_offset];
|
|
|
- p_offset += 1;
|
|
|
- }
|
|
|
-
|
|
|
- Vector<Variant> args;
|
|
|
- Vector<const Variant *> argp;
|
|
|
- args.resize(argc);
|
|
|
- argp.resize(argc);
|
|
|
-
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
- _profile_node_data("in_rpc", p_node->get_instance_id());
|
|
|
-#endif
|
|
|
-
|
|
|
- if (byte_only) {
|
|
|
- Vector<uint8_t> pure_data;
|
|
|
- const int len = p_packet_len - p_offset;
|
|
|
- pure_data.resize(len);
|
|
|
- memcpy(pure_data.ptrw(), &p_packet[p_offset], len);
|
|
|
- args.write[0] = pure_data;
|
|
|
- argp.write[0] = &args[0];
|
|
|
- p_offset += len;
|
|
|
- } else {
|
|
|
- for (int i = 0; i < argc; i++) {
|
|
|
- ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
|
|
|
-
|
|
|
- int vlen;
|
|
|
- Error err = decode_and_decompress_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
|
|
|
- ERR_FAIL_COND_MSG(err != OK, "Invalid packet received. Unable to decode RPC argument.");
|
|
|
-
|
|
|
- argp.write[i] = &args[i];
|
|
|
- p_offset += vlen;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Callable::CallError ce;
|
|
|
-
|
|
|
- p_node->call(config.name, (const Variant **)argp.ptr(), argc, ce);
|
|
|
- if (ce.error != Callable::CallError::CALL_OK) {
|
|
|
- String error = Variant::get_call_error_text(p_node, config.name, (const Variant **)argp.ptr(), argc, ce);
|
|
|
- error = "RPC - " + error;
|
|
|
- ERR_PRINT(error);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
|
|
|
ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small.");
|
|
|
int ofs = 1;
|
|
@@ -449,7 +200,7 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet,
|
|
|
|
|
|
Node *node = root_node->get_node(path);
|
|
|
ERR_FAIL_COND(node == nullptr);
|
|
|
- const bool valid_rpc_checksum = _get_rpc_md5(node) == methods_md5;
|
|
|
+ const bool valid_rpc_checksum = rpc_manager->get_rpc_md5(node) == methods_md5;
|
|
|
if (valid_rpc_checksum == false) {
|
|
|
ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
|
|
|
}
|
|
@@ -471,7 +222,7 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet,
|
|
|
encode_cstring(pname.get_data(), &packet.write[2]);
|
|
|
|
|
|
network_peer->set_transfer_channel(0);
|
|
|
- network_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
|
|
|
+ network_peer->set_transfer_mode(Multiplayer::TRANSFER_MODE_RELIABLE);
|
|
|
network_peer->set_target_peer(p_from);
|
|
|
network_peer->put_packet(packet.ptr(), packet.size());
|
|
|
}
|
|
@@ -532,7 +283,7 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC
|
|
|
const int path_len = encode_cstring(path.get_data(), nullptr);
|
|
|
|
|
|
// Extract MD5 from rpc methods list.
|
|
|
- const String methods_md5 = _get_rpc_md5(p_node);
|
|
|
+ const String methods_md5 = rpc_manager->get_rpc_md5(p_node);
|
|
|
const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
|
|
|
|
|
|
Vector<uint8_t> packet;
|
|
@@ -551,7 +302,7 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC
|
|
|
for (int &E : peers_to_add) {
|
|
|
network_peer->set_target_peer(E); // To all of you.
|
|
|
network_peer->set_transfer_channel(0);
|
|
|
- network_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
|
|
|
+ network_peer->set_transfer_mode(Multiplayer::TRANSFER_MODE_RELIABLE);
|
|
|
network_peer->put_packet(packet.ptr(), packet.size());
|
|
|
|
|
|
psc->confirmed_peers.insert(E, false); // Insert into confirmed, but as false since it was not confirmed.
|
|
@@ -716,188 +467,6 @@ Error MultiplayerAPI::decode_and_decompress_variant(Variant &r_variant, const ui
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
-void MultiplayerAPI::_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) {
|
|
|
- ERR_FAIL_COND_MSG(network_peer.is_null(), "Attempt to remote call/set when networking is not active in SceneTree.");
|
|
|
-
|
|
|
- ERR_FAIL_COND_MSG(network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_CONNECTING, "Attempt to remote call/set when networking is not connected yet in SceneTree.");
|
|
|
-
|
|
|
- ERR_FAIL_COND_MSG(network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to remote call/set when networking is disconnected.");
|
|
|
-
|
|
|
- ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments >255.");
|
|
|
-
|
|
|
- if (p_to != 0 && !connected_peers.has(ABS(p_to))) {
|
|
|
- ERR_FAIL_COND_MSG(p_to == network_peer->get_unique_id(), "Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id()) + ".");
|
|
|
-
|
|
|
- ERR_FAIL_MSG("Attempt to remote call unexisting ID: " + itos(p_to) + ".");
|
|
|
- }
|
|
|
-
|
|
|
- NodePath from_path = (root_node->get_path()).rel_path_to(p_from->get_path());
|
|
|
- ERR_FAIL_COND_MSG(from_path.is_empty(), "Unable to send RPC. Relative path is empty. THIS IS LIKELY A BUG IN THE ENGINE!");
|
|
|
-
|
|
|
- // See if the path is cached.
|
|
|
- PathSentCache *psc = path_send_cache.getptr(from_path);
|
|
|
- if (!psc) {
|
|
|
- // Path is not cached, create.
|
|
|
- path_send_cache[from_path] = PathSentCache();
|
|
|
- psc = path_send_cache.getptr(from_path);
|
|
|
- psc->id = last_send_cache_id++;
|
|
|
- }
|
|
|
-
|
|
|
- // See if all peers have cached path (if so, call can be fast).
|
|
|
- const bool has_all_peers = _send_confirm_path(p_from, from_path, psc, p_to);
|
|
|
-
|
|
|
- // Create base packet, lots of hardcode because it must be tight.
|
|
|
-
|
|
|
- int ofs = 0;
|
|
|
-
|
|
|
-#define MAKE_ROOM(m_amount) \
|
|
|
- if (packet_cache.size() < m_amount) \
|
|
|
- packet_cache.resize(m_amount);
|
|
|
-
|
|
|
- // Encode meta.
|
|
|
- // The meta is composed by a single byte that contains (starting from the least significant bit):
|
|
|
- // - `NetworkCommands` in the first three bits.
|
|
|
- // - `NetworkNodeIdCompression` in the next 2 bits.
|
|
|
- // - `NetworkNameIdCompression` in the next 1 bit.
|
|
|
- // - `byte_only_or_no_args` in the next 1 bit.
|
|
|
- // - So we still have the last bit free!
|
|
|
- uint8_t command_type = NETWORK_COMMAND_REMOTE_CALL;
|
|
|
- uint8_t node_id_compression = UINT8_MAX;
|
|
|
- uint8_t name_id_compression = UINT8_MAX;
|
|
|
- bool byte_only_or_no_args = false;
|
|
|
-
|
|
|
- MAKE_ROOM(1);
|
|
|
- // The meta is composed along the way, so just set 0 for now.
|
|
|
- packet_cache.write[0] = 0;
|
|
|
- ofs += 1;
|
|
|
-
|
|
|
- // Encode Node ID.
|
|
|
- if (has_all_peers) {
|
|
|
- // Compress the node ID only if all the target peers already know it.
|
|
|
- if (psc->id >= 0 && psc->id <= 255) {
|
|
|
- // We can encode the id in 1 byte
|
|
|
- node_id_compression = NETWORK_NODE_ID_COMPRESSION_8;
|
|
|
- MAKE_ROOM(ofs + 1);
|
|
|
- packet_cache.write[ofs] = static_cast<uint8_t>(psc->id);
|
|
|
- ofs += 1;
|
|
|
- } else if (psc->id >= 0 && psc->id <= 65535) {
|
|
|
- // We can encode the id in 2 bytes
|
|
|
- node_id_compression = NETWORK_NODE_ID_COMPRESSION_16;
|
|
|
- MAKE_ROOM(ofs + 2);
|
|
|
- encode_uint16(static_cast<uint16_t>(psc->id), &(packet_cache.write[ofs]));
|
|
|
- ofs += 2;
|
|
|
- } else {
|
|
|
- // Too big, let's use 4 bytes.
|
|
|
- node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
|
|
|
- MAKE_ROOM(ofs + 4);
|
|
|
- encode_uint32(psc->id, &(packet_cache.write[ofs]));
|
|
|
- ofs += 4;
|
|
|
- }
|
|
|
- } else {
|
|
|
- // The targets don't know the node yet, so we need to use 32 bits int.
|
|
|
- node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
|
|
|
- MAKE_ROOM(ofs + 4);
|
|
|
- encode_uint32(psc->id, &(packet_cache.write[ofs]));
|
|
|
- ofs += 4;
|
|
|
- }
|
|
|
-
|
|
|
- // Encode method ID
|
|
|
- if (p_rpc_id <= UINT8_MAX) {
|
|
|
- // The ID fits in 1 byte
|
|
|
- name_id_compression = NETWORK_NAME_ID_COMPRESSION_8;
|
|
|
- MAKE_ROOM(ofs + 1);
|
|
|
- packet_cache.write[ofs] = static_cast<uint8_t>(p_rpc_id);
|
|
|
- ofs += 1;
|
|
|
- } else {
|
|
|
- // The ID is larger, let's use 2 bytes
|
|
|
- name_id_compression = NETWORK_NAME_ID_COMPRESSION_16;
|
|
|
- MAKE_ROOM(ofs + 2);
|
|
|
- encode_uint16(p_rpc_id, &(packet_cache.write[ofs]));
|
|
|
- ofs += 2;
|
|
|
- }
|
|
|
-
|
|
|
- if (p_argcount == 0) {
|
|
|
- byte_only_or_no_args = true;
|
|
|
- } else if (p_argcount == 1 && p_arg[0]->get_type() == Variant::PACKED_BYTE_ARRAY) {
|
|
|
- byte_only_or_no_args = true;
|
|
|
- // Special optimization when only the byte vector is sent.
|
|
|
- const Vector<uint8_t> data = *p_arg[0];
|
|
|
- MAKE_ROOM(ofs + data.size());
|
|
|
- memcpy(&(packet_cache.write[ofs]), data.ptr(), sizeof(uint8_t) * data.size());
|
|
|
- ofs += data.size();
|
|
|
- } else {
|
|
|
- // Arguments
|
|
|
- MAKE_ROOM(ofs + 1);
|
|
|
- packet_cache.write[ofs] = p_argcount;
|
|
|
- ofs += 1;
|
|
|
- for (int i = 0; i < p_argcount; i++) {
|
|
|
- int len(0);
|
|
|
- Error err = encode_and_compress_variant(*p_arg[i], nullptr, len);
|
|
|
- ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE!");
|
|
|
- MAKE_ROOM(ofs + len);
|
|
|
- encode_and_compress_variant(*p_arg[i], &(packet_cache.write[ofs]), len);
|
|
|
- ofs += len;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- ERR_FAIL_COND(command_type > 7);
|
|
|
- ERR_FAIL_COND(node_id_compression > 3);
|
|
|
- ERR_FAIL_COND(name_id_compression > 1);
|
|
|
-
|
|
|
- // We can now set the meta
|
|
|
- packet_cache.write[0] = command_type + (node_id_compression << NODE_ID_COMPRESSION_SHIFT) + (name_id_compression << NAME_ID_COMPRESSION_SHIFT) + ((byte_only_or_no_args ? 1 : 0) << BYTE_ONLY_OR_NO_ARGS_SHIFT);
|
|
|
-
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
- _profile_bandwidth_data("out", ofs);
|
|
|
-#endif
|
|
|
-
|
|
|
- // Take chance and set transfer mode, since all send methods will use it.
|
|
|
- network_peer->set_transfer_channel(p_config.channel);
|
|
|
- network_peer->set_transfer_mode(p_config.transfer_mode);
|
|
|
-
|
|
|
- if (has_all_peers) {
|
|
|
- // They all have verified paths, so send fast.
|
|
|
- network_peer->set_target_peer(p_to); // To all of you.
|
|
|
- network_peer->put_packet(packet_cache.ptr(), ofs); // A message with love.
|
|
|
- } else {
|
|
|
- // Unreachable because the node ID is never compressed if the peers doesn't know it.
|
|
|
- CRASH_COND(node_id_compression != NETWORK_NODE_ID_COMPRESSION_32);
|
|
|
-
|
|
|
- // Not all verified path, so send one by one.
|
|
|
-
|
|
|
- // Append path at the end, since we will need it for some packets.
|
|
|
- CharString pname = String(from_path).utf8();
|
|
|
- int path_len = encode_cstring(pname.get_data(), nullptr);
|
|
|
- MAKE_ROOM(ofs + path_len);
|
|
|
- encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
|
|
|
-
|
|
|
- for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
|
|
|
- if (p_to < 0 && E->get() == -p_to) {
|
|
|
- continue; // Continue, excluded.
|
|
|
- }
|
|
|
-
|
|
|
- if (p_to > 0 && E->get() != p_to) {
|
|
|
- continue; // Continue, not for this peer.
|
|
|
- }
|
|
|
-
|
|
|
- Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
|
|
|
- ERR_CONTINUE(!F); // Should never happen.
|
|
|
-
|
|
|
- network_peer->set_target_peer(E->get()); // To this one specifically.
|
|
|
-
|
|
|
- if (F->get()) {
|
|
|
- // This one confirmed path, so use id.
|
|
|
- encode_uint32(psc->id, &(packet_cache.write[1]));
|
|
|
- network_peer->put_packet(packet_cache.ptr(), ofs);
|
|
|
- } else {
|
|
|
- // This one did not confirm path yet, so use entire path (sorry!).
|
|
|
- encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); // Offset to path and flag.
|
|
|
- network_peer->put_packet(packet_cache.ptr(), ofs + path_len);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void MultiplayerAPI::_add_peer(int p_id) {
|
|
|
connected_peers.insert(p_id);
|
|
|
path_get_cache.insert(p_id, PathGetCache());
|
|
@@ -934,72 +503,15 @@ void MultiplayerAPI::_server_disconnected() {
|
|
|
emit_signal(SNAME("server_disconnected"));
|
|
|
}
|
|
|
|
|
|
-void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) {
|
|
|
- ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to call an RPC while no network peer is active.");
|
|
|
- ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to call an RPC on a node which is not inside SceneTree.");
|
|
|
- ERR_FAIL_COND_MSG(network_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, "Trying to call an RPC via a network peer which is not connected.");
|
|
|
-
|
|
|
- int node_id = network_peer->get_unique_id();
|
|
|
- bool call_local_native = false;
|
|
|
- bool call_local_script = false;
|
|
|
- uint16_t rpc_id = UINT16_MAX;
|
|
|
- const RPCConfig config = _get_rpc_config(p_node, p_method, rpc_id);
|
|
|
- ERR_FAIL_COND_MSG(config.name == StringName(),
|
|
|
- vformat("Unable to get the RPC configuration for the function \"%s\" at path: \"%s\". This happens when the method is not marked for RPCs.", p_method, p_node->get_path()));
|
|
|
- if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
|
|
|
- if (rpc_id & (1 << 15)) {
|
|
|
- call_local_native = config.sync;
|
|
|
- } else {
|
|
|
- call_local_script = config.sync;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (p_peer_id != node_id) {
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
- _profile_node_data("out_rpc", p_node->get_instance_id());
|
|
|
-#endif
|
|
|
-
|
|
|
- _send_rpc(p_node, p_peer_id, rpc_id, config, p_method, p_arg, p_argcount);
|
|
|
- }
|
|
|
-
|
|
|
- if (call_local_native) {
|
|
|
- int temp_id = rpc_sender_id;
|
|
|
- rpc_sender_id = get_network_unique_id();
|
|
|
- Callable::CallError ce;
|
|
|
- p_node->call(p_method, p_arg, p_argcount, ce);
|
|
|
- rpc_sender_id = temp_id;
|
|
|
- if (ce.error != Callable::CallError::CALL_OK) {
|
|
|
- String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
|
|
|
- error = "rpc() aborted in local call: - " + error + ".";
|
|
|
- ERR_PRINT(error);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (call_local_script) {
|
|
|
- int temp_id = rpc_sender_id;
|
|
|
- rpc_sender_id = get_network_unique_id();
|
|
|
- Callable::CallError ce;
|
|
|
- ce.error = Callable::CallError::CALL_OK;
|
|
|
- p_node->get_script_instance()->call(p_method, p_arg, p_argcount, ce);
|
|
|
- rpc_sender_id = temp_id;
|
|
|
- if (ce.error != Callable::CallError::CALL_OK) {
|
|
|
- String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
|
|
|
- error = "rpc() aborted in script local call: - " + error + ".";
|
|
|
- ERR_PRINT(error);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- ERR_FAIL_COND_MSG(p_peer_id == node_id && !config.sync, "RPC '" + p_method + "' on yourself is not allowed by selected mode.");
|
|
|
-}
|
|
|
-
|
|
|
-Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, MultiplayerPeer::TransferMode p_mode, int p_channel) {
|
|
|
+Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, Multiplayer::TransferMode p_mode, int p_channel) {
|
|
|
ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet.");
|
|
|
ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no network peer is active.");
|
|
|
ERR_FAIL_COND_V_MSG(network_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED, "Trying to send a raw packet via a network peer which is not connected.");
|
|
|
|
|
|
- MAKE_ROOM(p_data.size() + 1);
|
|
|
+ if (packet_cache.size() < p_data.size() + 1) {
|
|
|
+ packet_cache.resize(p_data.size() + 1);
|
|
|
+ }
|
|
|
+
|
|
|
const uint8_t *r = p_data.ptr();
|
|
|
packet_cache.write[0] = NETWORK_COMMAND_RAW;
|
|
|
memcpy(&packet_cache.write[1], &r[0], p_data.size());
|
|
@@ -1024,6 +536,14 @@ void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_pac
|
|
|
emit_signal(SNAME("network_peer_packet"), p_from, out);
|
|
|
}
|
|
|
|
|
|
+bool MultiplayerAPI::is_cache_confirmed(NodePath p_path, int p_peer) {
|
|
|
+ const PathSentCache *psc = path_send_cache.getptr(p_path);
|
|
|
+ ERR_FAIL_COND_V(!psc, false);
|
|
|
+ const Map<int, bool>::Element *F = psc->confirmed_peers.find(p_peer);
|
|
|
+ ERR_FAIL_COND_V(!F, false); // Should never happen.
|
|
|
+ return F->get();
|
|
|
+}
|
|
|
+
|
|
|
bool MultiplayerAPI::send_confirm_path(Node *p_node, NodePath p_path, int p_peer_id, int &r_id) {
|
|
|
// See if the path is cached.
|
|
|
PathSentCache *psc = path_send_cache.getptr(p_path);
|
|
@@ -1092,23 +612,23 @@ bool MultiplayerAPI::is_object_decoding_allowed() const {
|
|
|
return allow_object_decoding;
|
|
|
}
|
|
|
|
|
|
-MultiplayerReplicator *MultiplayerAPI::get_replicator() const {
|
|
|
- return replicator;
|
|
|
-}
|
|
|
-
|
|
|
void MultiplayerAPI::scene_enter_exit_notify(const String &p_scene, Node *p_node, bool p_enter) {
|
|
|
replicator->scene_enter_exit_notify(p_scene, p_node, p_enter);
|
|
|
}
|
|
|
|
|
|
+void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
|
|
|
+ rpc_manager->rpcp(p_node, p_peer_id, p_method, p_arg, p_argcount);
|
|
|
+}
|
|
|
+
|
|
|
void MultiplayerAPI::_bind_methods() {
|
|
|
ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
|
|
|
ClassDB::bind_method(D_METHOD("get_root_node"), &MultiplayerAPI::get_root_node);
|
|
|
- ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode", "channel"), &MultiplayerAPI::send_bytes, DEFVAL(MultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(MultiplayerPeer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
|
|
|
+ ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode", "channel"), &MultiplayerAPI::send_bytes, DEFVAL(MultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(Multiplayer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
|
|
|
ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer);
|
|
|
ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer);
|
|
|
ClassDB::bind_method(D_METHOD("get_network_unique_id"), &MultiplayerAPI::get_network_unique_id);
|
|
|
ClassDB::bind_method(D_METHOD("is_network_server"), &MultiplayerAPI::is_network_server);
|
|
|
- ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &MultiplayerAPI::get_rpc_sender_id);
|
|
|
+ ClassDB::bind_method(D_METHOD("get_remote_sender_id"), &MultiplayerAPI::get_remote_sender_id);
|
|
|
ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &MultiplayerAPI::set_network_peer);
|
|
|
ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear);
|
|
@@ -1133,18 +653,16 @@ void MultiplayerAPI::_bind_methods() {
|
|
|
ADD_SIGNAL(MethodInfo("connected_to_server"));
|
|
|
ADD_SIGNAL(MethodInfo("connection_failed"));
|
|
|
ADD_SIGNAL(MethodInfo("server_disconnected"));
|
|
|
-
|
|
|
- BIND_ENUM_CONSTANT(RPC_MODE_DISABLED);
|
|
|
- BIND_ENUM_CONSTANT(RPC_MODE_ANY);
|
|
|
- BIND_ENUM_CONSTANT(RPC_MODE_AUTHORITY);
|
|
|
}
|
|
|
|
|
|
MultiplayerAPI::MultiplayerAPI() {
|
|
|
replicator = memnew(MultiplayerReplicator(this));
|
|
|
+ rpc_manager = memnew(RPCManager(this));
|
|
|
clear();
|
|
|
}
|
|
|
|
|
|
MultiplayerAPI::~MultiplayerAPI() {
|
|
|
clear();
|
|
|
memdelete(replicator);
|
|
|
+ memdelete(rpc_manager);
|
|
|
}
|