summaryrefslogtreecommitdiffstats
path: root/core/io/multiplayer_api.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/io/multiplayer_api.cpp')
-rw-r--r--core/io/multiplayer_api.cpp229
1 files changed, 145 insertions, 84 deletions
diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp
index 456d29520f..8e67f1c97a 100644
--- a/core/io/multiplayer_api.cpp
+++ b/core/io/multiplayer_api.cpp
@@ -1,7 +1,92 @@
+/*************************************************************************/
+/* multiplayer_api.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
#include "core/io/multiplayer_api.h"
#include "core/io/marshalls.h"
#include "scene/main/node.h"
+_FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_master, bool &r_skip_rpc) {
+
+ switch (mode) {
+
+ case MultiplayerAPI::RPC_MODE_DISABLED: {
+ //do nothing
+ } break;
+ case MultiplayerAPI::RPC_MODE_REMOTE: {
+ //do nothing also, no need to call local
+ } break;
+ case MultiplayerAPI::RPC_MODE_REMOTESYNC:
+ case MultiplayerAPI::RPC_MODE_MASTERSYNC:
+ case MultiplayerAPI::RPC_MODE_SLAVESYNC:
+ case MultiplayerAPI::RPC_MODE_SYNC: {
+ //call it, sync always results in call
+ return true;
+ } break;
+ case MultiplayerAPI::RPC_MODE_MASTER: {
+ if (is_master)
+ r_skip_rpc = true; //no other master so..
+ return is_master;
+ } break;
+ case MultiplayerAPI::RPC_MODE_SLAVE: {
+ return !is_master;
+ } break;
+ }
+ return false;
+}
+
+_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_REMOTE: {
+ return true;
+ } break;
+ case MultiplayerAPI::RPC_MODE_REMOTESYNC:
+ case MultiplayerAPI::RPC_MODE_SYNC: {
+ return true;
+ } break;
+ case MultiplayerAPI::RPC_MODE_MASTERSYNC:
+ case MultiplayerAPI::RPC_MODE_MASTER: {
+ return p_node->is_network_master();
+ } break;
+ case MultiplayerAPI::RPC_MODE_SLAVESYNC:
+ case MultiplayerAPI::RPC_MODE_SLAVE: {
+ return !p_node->is_network_master() && p_remote_id == p_node->get_network_master();
+ } break;
+ }
+
+ return false;
+}
+
void MultiplayerAPI::poll() {
if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED)
@@ -172,11 +257,19 @@ Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, int
}
void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
- if (!p_node->can_call_rpc(p_name, p_from))
- return;
ERR_FAIL_COND(p_offset >= p_packet_len);
+ // Check that remote can call the RPC on this node
+ RPCMode rpc_mode = RPC_MODE_DISABLED;
+ const Map<StringName, RPCMode>::Element *E = p_node->get_node_rpc_mode(p_name);
+ if (E) {
+ rpc_mode = E->get();
+ } else if (p_node->get_script_instance()) {
+ rpc_mode = p_node->get_script_instance()->get_rpc_mode(p_name);
+ }
+ ERR_FAIL_COND(!_can_call_mode(p_node, rpc_mode, p_from));
+
int argc = p_packet[p_offset];
Vector<Variant> args;
Vector<const Variant *> argp;
@@ -189,10 +282,10 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_
ERR_FAIL_COND(p_offset >= p_packet_len);
int vlen;
- Error err = decode_variant(args[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
+ Error err = decode_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
ERR_FAIL_COND(err != OK);
//args[i]=p_packet[3+i];
- argp[i] = &args[i];
+ argp.write[i] = &args[i];
p_offset += vlen;
}
@@ -208,11 +301,18 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_
void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
- if (!p_node->can_call_rset(p_name, p_from))
- return;
-
ERR_FAIL_COND(p_offset >= p_packet_len);
+ // Check that remote can call the RSET on this node
+ RPCMode rset_mode = RPC_MODE_DISABLED;
+ const Map<StringName, RPCMode>::Element *E = p_node->get_node_rset_mode(p_name);
+ if (E) {
+ rset_mode = E->get();
+ } else if (p_node->get_script_instance()) {
+ rset_mode = p_node->get_script_instance()->get_rset_mode(p_name);
+ }
+ ERR_FAIL_COND(!_can_call_mode(p_node, rset_mode, p_from));
+
Variant value;
decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset);
@@ -254,8 +354,8 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet,
Vector<uint8_t> packet;
packet.resize(1 + len);
- packet[0] = NETWORK_COMMAND_CONFIRM_PATH;
- encode_cstring(pname.get_data(), &packet[1]);
+ packet.write[0] = NETWORK_COMMAND_CONFIRM_PATH;
+ encode_cstring(pname.get_data(), &packet.write[1]);
network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
network_peer->set_target_peer(p_from);
@@ -315,9 +415,9 @@ bool MultiplayerAPI::_send_confirm_path(NodePath p_path, PathSentCache *psc, int
Vector<uint8_t> packet;
packet.resize(1 + 4 + len);
- packet[0] = NETWORK_COMMAND_SIMPLIFY_PATH;
- encode_uint32(psc->id, &packet[1]);
- encode_cstring(pname.get_data(), &packet[5]);
+ packet.write[0] = NETWORK_COMMAND_SIMPLIFY_PATH;
+ encode_uint32(psc->id, &packet.write[1]);
+ encode_cstring(pname.get_data(), &packet.write[5]);
network_peer->set_target_peer(E->get()); //to all of you
network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
@@ -382,19 +482,19 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p
//encode type
MAKE_ROOM(1);
- packet_cache[0] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL;
+ packet_cache.write[0] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL;
ofs += 1;
//encode ID
MAKE_ROOM(ofs + 4);
- encode_uint32(psc->id, &(packet_cache[ofs]));
+ encode_uint32(psc->id, &(packet_cache.write[ofs]));
ofs += 4;
//encode function name
CharString name = String(p_name).utf8();
int len = encode_cstring(name.get_data(), NULL);
MAKE_ROOM(ofs + len);
- encode_cstring(name.get_data(), &(packet_cache[ofs]));
+ encode_cstring(name.get_data(), &(packet_cache.write[ofs]));
ofs += len;
if (p_set) {
@@ -402,19 +502,19 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p
Error err = encode_variant(*p_arg[0], NULL, len);
ERR_FAIL_COND(err != OK);
MAKE_ROOM(ofs + len);
- encode_variant(*p_arg[0], &(packet_cache[ofs]), len);
+ encode_variant(*p_arg[0], &(packet_cache.write[ofs]), len);
ofs += len;
} else {
//call arguments
MAKE_ROOM(ofs + 1);
- packet_cache[ofs] = p_argcount;
+ packet_cache.write[ofs] = p_argcount;
ofs += 1;
for (int i = 0; i < p_argcount; i++) {
Error err = encode_variant(*p_arg[i], NULL, len);
ERR_FAIL_COND(err != OK);
MAKE_ROOM(ofs + len);
- encode_variant(*p_arg[i], &(packet_cache[ofs]), len);
+ encode_variant(*p_arg[i], &(packet_cache.write[ofs]), len);
ofs += len;
}
}
@@ -437,7 +537,7 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p
CharString pname = String(from_path).utf8();
int path_len = encode_cstring(pname.get_data(), NULL);
MAKE_ROOM(ofs + path_len);
- encode_cstring(pname.get_data(), &(packet_cache[ofs]));
+ encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
@@ -454,11 +554,11 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p
if (F->get() == true) {
//this one confirmed path, so use id
- encode_uint32(psc->id, &(packet_cache[1]));
+ 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[1])); //offset to path and flag
+ encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); //offset to path and flag
network_peer->put_packet(packet_cache.ptr(), ofs + path_len);
}
}
@@ -492,57 +592,6 @@ void MultiplayerAPI::_server_disconnected() {
emit_signal("server_disconnected");
}
-bool _should_call_native(Node::RPCMode mode, bool is_master, bool &r_skip_rpc) {
-
- switch (mode) {
-
- case Node::RPC_MODE_DISABLED: {
- //do nothing
- } break;
- case Node::RPC_MODE_REMOTE: {
- //do nothing also, no need to call local
- } break;
- case Node::RPC_MODE_SYNC: {
- //call it, sync always results in call
- return true;
- } break;
- case Node::RPC_MODE_MASTER: {
- if (is_master)
- r_skip_rpc = true; //no other master so..
- return is_master;
- } break;
- case Node::RPC_MODE_SLAVE: {
- return !is_master;
- } break;
- }
- return false;
-}
-
-bool _should_call_script(ScriptInstance::RPCMode mode, bool is_master, bool &r_skip_rpc) {
- switch (mode) {
-
- case ScriptInstance::RPC_MODE_DISABLED: {
- //do nothing
- } break;
- case ScriptInstance::RPC_MODE_REMOTE: {
- //do nothing also, no need to call local
- } break;
- case ScriptInstance::RPC_MODE_SYNC: {
- //call it, sync always results in call
- return true;
- } break;
- case ScriptInstance::RPC_MODE_MASTER: {
- if (is_master)
- r_skip_rpc = true; //no other master so..
- return is_master;
- } break;
- case ScriptInstance::RPC_MODE_SLAVE: {
- return !is_master;
- } break;
- }
- return false;
-}
-
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(!p_node->is_inside_tree());
@@ -557,17 +606,17 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const
if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
//check that send mode can use local call
- const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rpc_mode(p_method);
+ const Map<StringName, RPCMode>::Element *E = p_node->get_node_rpc_mode(p_method);
if (E) {
- call_local_native = _should_call_native(E->get(), is_master, skip_rpc);
+ call_local_native = _should_call_local(E->get(), is_master, skip_rpc);
}
if (call_local_native) {
// done below
} else if (p_node->get_script_instance()) {
//attempt with script
- ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rpc_mode(p_method);
- call_local_script = _should_call_script(rpc_mode, is_master, skip_rpc);
+ RPCMode rpc_mode = p_node->get_script_instance()->get_rpc_mode(p_method);
+ call_local_script = _should_call_local(rpc_mode, is_master, skip_rpc);
}
}
@@ -613,10 +662,10 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const
bool set_local = false;
- const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rset_mode(p_property);
+ const Map<StringName, RPCMode>::Element *E = p_node->get_node_rset_mode(p_property);
if (E) {
- set_local = _should_call_native(E->get(), is_master, skip_rset);
+ set_local = _should_call_local(E->get(), is_master, skip_rset);
}
if (set_local) {
@@ -630,9 +679,9 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const
}
} else if (p_node->get_script_instance()) {
//attempt with script
- ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rset_mode(p_property);
+ RPCMode rpc_mode = p_node->get_script_instance()->get_rset_mode(p_property);
- set_local = _should_call_script(rpc_mode, is_master, skip_rset);
+ set_local = _should_call_local(rpc_mode, is_master, skip_rset);
if (set_local) {
@@ -655,7 +704,7 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const
_send_rpc(p_node, p_peer_id, p_unreliable, true, p_property, &vptr, 1);
}
-Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to) {
+Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to, NetworkedMultiplayerPeer::TransferMode p_mode) {
ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
ERR_FAIL_COND_V(!network_peer.is_valid(), ERR_UNCONFIGURED);
@@ -663,9 +712,12 @@ Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to) {
MAKE_ROOM(p_data.size() + 1);
PoolVector<uint8_t>::Read r = p_data.read();
- packet_cache[0] = NETWORK_COMMAND_RAW;
- memcpy(&packet_cache[1], &r[0], p_data.size());
+ packet_cache.write[0] = NETWORK_COMMAND_RAW;
+ memcpy(&packet_cache.write[1], &r[0], p_data.size());
+
network_peer->set_target_peer(p_to);
+ network_peer->set_transfer_mode(p_mode);
+
return network_peer->put_packet(packet_cache.ptr(), p_data.size() + 1);
}
@@ -721,7 +773,7 @@ Vector<int> MultiplayerAPI::get_network_connected_peers() const {
void MultiplayerAPI::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
- ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST));
+ ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE));
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);
@@ -748,6 +800,15 @@ 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_REMOTE);
+ BIND_ENUM_CONSTANT(RPC_MODE_SYNC);
+ BIND_ENUM_CONSTANT(RPC_MODE_MASTER);
+ BIND_ENUM_CONSTANT(RPC_MODE_SLAVE);
+ BIND_ENUM_CONSTANT(RPC_MODE_REMOTESYNC);
+ BIND_ENUM_CONSTANT(RPC_MODE_MASTERSYNC);
+ BIND_ENUM_CONSTANT(RPC_MODE_SLAVESYNC);
}
MultiplayerAPI::MultiplayerAPI() {