2018-05-16 19:19:33 +02:00
/*************************************************************************/
/* multiplayer_api.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2019-01-01 12:53:14 +01:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2018-05-16 19:19:33 +02:00
/* */
/* 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. */
/*************************************************************************/
2018-09-11 18:13:45 +02:00
# include "multiplayer_api.h"
2018-03-03 18:28:49 +01:00
# include "core/io/marshalls.h"
# include "scene/main/node.h"
2018-05-13 07:07:56 +02:00
_FORCE_INLINE_ bool _should_call_local ( MultiplayerAPI : : RPCMode mode , bool is_master , bool & r_skip_rpc ) {
switch ( mode ) {
case MultiplayerAPI : : RPC_MODE_DISABLED : {
2018-09-25 17:43:18 +02:00
// Do nothing.
2018-05-13 07:07:56 +02:00
} break ;
case MultiplayerAPI : : RPC_MODE_REMOTE : {
2018-09-25 17:43:18 +02:00
// Do nothing also. Remote cannot produce a local call.
2018-05-13 07:07:56 +02:00
} break ;
2018-09-25 17:43:18 +02:00
case MultiplayerAPI : : RPC_MODE_MASTERSYNC : {
if ( is_master )
r_skip_rpc = true ; // I am the master, so skip remote call.
2019-04-05 14:06:16 +02:00
FALLTHROUGH ;
}
2018-05-26 10:28:28 +02:00
case MultiplayerAPI : : RPC_MODE_REMOTESYNC :
2018-09-14 23:10:12 +02:00
case MultiplayerAPI : : RPC_MODE_PUPPETSYNC : {
2018-09-25 17:43:18 +02:00
// Call it, sync always results in a local call.
2018-05-13 07:07:56 +02:00
return true ;
} break ;
case MultiplayerAPI : : RPC_MODE_MASTER : {
if ( is_master )
2018-09-25 17:43:18 +02:00
r_skip_rpc = true ; // I am the master, so skip remote call.
2018-05-13 07:07:56 +02:00
return is_master ;
} break ;
2018-09-14 21:59:47 +02:00
case MultiplayerAPI : : RPC_MODE_PUPPET : {
2018-05-13 07:07:56 +02:00
return ! is_master ;
} break ;
}
return false ;
}
2019-04-05 18:18:08 +02:00
_FORCE_INLINE_ bool _can_call_mode ( Node * p_node , MultiplayerAPI : : RPCMode mode , int p_remote_id ) {
2018-05-13 07:07:56 +02:00
switch ( mode ) {
case MultiplayerAPI : : RPC_MODE_DISABLED : {
return false ;
} break ;
2018-09-14 23:10:12 +02:00
case MultiplayerAPI : : RPC_MODE_REMOTE :
case MultiplayerAPI : : RPC_MODE_REMOTESYNC : {
2018-05-13 07:07:56 +02:00
return true ;
} break ;
2018-05-26 10:28:28 +02:00
case MultiplayerAPI : : RPC_MODE_MASTERSYNC :
2018-05-13 07:07:56 +02:00
case MultiplayerAPI : : RPC_MODE_MASTER : {
2019-04-05 18:18:08 +02:00
return p_node - > is_network_master ( ) ;
2018-05-13 07:07:56 +02:00
} break ;
2018-09-14 21:59:47 +02:00
case MultiplayerAPI : : RPC_MODE_PUPPETSYNC :
case MultiplayerAPI : : RPC_MODE_PUPPET : {
2019-04-05 18:18:08 +02:00
return ! p_node - > is_network_master ( ) & & p_remote_id = = p_node - > get_network_master ( ) ;
2018-05-13 07:07:56 +02:00
} break ;
}
return false ;
}
2018-03-03 18:28:49 +01:00
void MultiplayerAPI : : poll ( ) {
if ( ! network_peer . is_valid ( ) | | network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED )
return ;
network_peer - > poll ( ) ;
2018-09-25 17:44:26 +02:00
if ( ! network_peer . is_valid ( ) ) // It's possible that polling might have resulted in a disconnection, so check here.
2018-03-03 18:28:49 +01:00
return ;
while ( network_peer - > get_available_packet_count ( ) ) {
int sender = network_peer - > get_packet_peer ( ) ;
const uint8_t * packet ;
int len ;
Error err = network_peer - > get_packet ( & packet , len ) ;
if ( err ! = OK ) {
ERR_PRINT ( " Error getting packet! " ) ;
}
rpc_sender_id = sender ;
_process_packet ( sender , packet , len ) ;
rpc_sender_id = 0 ;
if ( ! network_peer . is_valid ( ) ) {
2018-09-25 17:44:26 +02:00
break ; // It's also possible that a packet or RPC caused a disconnection, so also check here.
2018-03-03 18:28:49 +01:00
}
}
}
void MultiplayerAPI : : clear ( ) {
connected_peers . clear ( ) ;
path_get_cache . clear ( ) ;
path_send_cache . clear ( ) ;
2019-01-15 10:35:26 +01:00
packet_cache . clear ( ) ;
2018-03-03 18:28:49 +01:00
last_send_cache_id = 1 ;
}
void MultiplayerAPI : : set_root_node ( Node * p_node ) {
root_node = p_node ;
}
void MultiplayerAPI : : set_network_peer ( const Ref < NetworkedMultiplayerPeer > & p_peer ) {
2019-01-15 11:14:51 +01:00
if ( p_peer = = network_peer ) return ; // Nothing to do
2018-03-03 18:28:49 +01:00
if ( network_peer . is_valid ( ) ) {
2018-05-08 11:49:57 +02:00
network_peer - > disconnect ( " peer_connected " , this , " _add_peer " ) ;
network_peer - > disconnect ( " peer_disconnected " , this , " _del_peer " ) ;
network_peer - > disconnect ( " connection_succeeded " , this , " _connected_to_server " ) ;
network_peer - > disconnect ( " connection_failed " , this , " _connection_failed " ) ;
network_peer - > disconnect ( " server_disconnected " , this , " _server_disconnected " ) ;
2018-03-03 18:28:49 +01:00
clear ( ) ;
}
network_peer = p_peer ;
ERR_EXPLAIN ( " Supplied NetworkedNetworkPeer must be connecting or connected. " ) ;
ERR_FAIL_COND ( p_peer . is_valid ( ) & & p_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED ) ;
if ( network_peer . is_valid ( ) ) {
2018-05-08 11:49:57 +02:00
network_peer - > connect ( " peer_connected " , this , " _add_peer " ) ;
network_peer - > connect ( " peer_disconnected " , this , " _del_peer " ) ;
network_peer - > connect ( " connection_succeeded " , this , " _connected_to_server " ) ;
network_peer - > connect ( " connection_failed " , this , " _connection_failed " ) ;
network_peer - > connect ( " server_disconnected " , this , " _server_disconnected " ) ;
2018-03-03 18:28:49 +01:00
}
}
Ref < NetworkedMultiplayerPeer > MultiplayerAPI : : get_network_peer ( ) const {
return network_peer ;
}
void MultiplayerAPI : : _process_packet ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " 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 " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( root_node = = NULL ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND ( p_packet_len < 1 ) ;
2018-03-03 18:28:49 +01:00
uint8_t packet_type = p_packet [ 0 ] ;
switch ( packet_type ) {
case NETWORK_COMMAND_SIMPLIFY_PATH : {
_process_simplify_path ( p_from , p_packet , p_packet_len ) ;
} break ;
case NETWORK_COMMAND_CONFIRM_PATH : {
_process_confirm_path ( p_from , p_packet , p_packet_len ) ;
} break ;
case NETWORK_COMMAND_REMOTE_CALL :
case NETWORK_COMMAND_REMOTE_SET : {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( p_packet_len < 6 ) ;
Node * node = _process_get_node ( p_from , p_packet , p_packet_len ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Requested node was not found. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( node = = NULL ) ;
2018-09-25 17:44:26 +02:00
// Detect cstring end.
2018-03-03 18:28:49 +01:00
int len_end = 5 ;
for ( ; len_end < p_packet_len ; len_end + + ) {
if ( p_packet [ len_end ] = = 0 ) {
break ;
}
}
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( len_end > = p_packet_len ) ;
StringName name = String : : utf8 ( ( const char * ) & p_packet [ 5 ] ) ;
if ( packet_type = = NETWORK_COMMAND_REMOTE_CALL ) {
_process_rpc ( node , name , p_from , p_packet , p_packet_len , len_end + 1 ) ;
} else {
_process_rset ( node , name , p_from , p_packet , p_packet_len , len_end + 1 ) ;
}
} break ;
2018-05-11 21:34:45 +02:00
case NETWORK_COMMAND_RAW : {
_process_raw ( p_from , p_packet , p_packet_len ) ;
} break ;
2018-03-03 18:28:49 +01:00
}
}
Node * MultiplayerAPI : : _process_get_node ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
uint32_t target = decode_uint32 ( & p_packet [ 1 ] ) ;
Node * node = NULL ;
if ( target & 0x80000000 ) {
2018-09-25 17:44:26 +02:00
// Use full path (not cached yet).
2018-03-03 18:28:49 +01:00
int ofs = target & 0x7FFFFFFF ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size smaller than declared. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ofs > = p_packet_len , NULL ) ;
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_PRINTS ( " Failed to get path from RPC: " + String ( np ) ) ;
} else {
2018-09-25 17:44:26 +02:00
// Use cached path.
2018-03-03 18:28:49 +01:00
int id = target ;
Map < int , PathGetCache > : : Element * E = path_get_cache . find ( p_from ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Requests invalid peer cache. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! E , NULL ) ;
Map < int , PathGetCache : : NodeInfo > : : Element * F = E - > get ( ) . nodes . find ( id ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Unabled to find requested cached node. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! F , NULL ) ;
PathGetCache : : NodeInfo * ni = & F - > get ( ) ;
2018-09-25 17:44:26 +02:00
// Do proper caching later.
2018-03-03 18:28:49 +01:00
node = root_node - > get_node ( ni - > path ) ;
if ( ! node )
ERR_PRINTS ( " Failed to get cached path from RPC: " + String ( ni - > path ) ) ;
}
return node ;
}
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 ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( p_offset > = p_packet_len ) ;
2018-09-25 17:44:26 +02:00
// Check that remote can call the RPC on this node.
2018-05-13 07:07:56 +02:00
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 ) ;
}
2018-09-25 17:44:26 +02:00
2019-04-05 17:13:55 +02:00
bool can_call = _can_call_mode ( p_node , rpc_mode , p_from ) ;
ERR_EXPLAIN ( " RPC ' " + String ( p_name ) + " ' is not allowed on node " + p_node - > get_path ( ) + " from: " + itos ( p_from ) + " . Mode is " + itos ( ( int ) rpc_mode ) + " , master is " + itos ( p_node - > get_network_master ( ) ) + " . " ) ;
ERR_FAIL_COND ( ! can_call ) ;
2018-05-13 07:07:56 +02:00
2018-03-03 18:28:49 +01:00
int argc = p_packet [ p_offset ] ;
Vector < Variant > args ;
Vector < const Variant * > argp ;
args . resize ( argc ) ;
argp . resize ( argc ) ;
p_offset + + ;
for ( int i = 0 ; i < argc ; i + + ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( p_offset > = p_packet_len ) ;
2018-09-25 17:44:26 +02:00
2018-03-03 18:28:49 +01:00
int vlen ;
2019-03-26 16:52:42 +01:00
Error err = decode_variant ( args . write [ i ] , & p_packet [ p_offset ] , p_packet_len - p_offset , & vlen , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Unable to decode RPC argument. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( err ! = OK ) ;
2018-09-25 17:44:26 +02:00
2018-07-25 03:11:03 +02:00
argp . write [ i ] = & args [ i ] ;
2018-03-03 18:28:49 +01:00
p_offset + = vlen ;
}
Variant : : CallError ce ;
p_node - > call ( p_name , ( const Variant * * ) argp . ptr ( ) , argc , ce ) ;
if ( ce . error ! = Variant : : CallError : : CALL_OK ) {
String error = Variant : : get_call_error_text ( p_node , p_name , ( const Variant * * ) argp . ptr ( ) , argc , ce ) ;
error = " RPC - " + error ;
ERR_PRINTS ( error ) ;
}
}
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 ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-05-29 11:47:52 +02:00
ERR_FAIL_COND ( p_offset > = p_packet_len ) ;
2018-05-13 07:07:56 +02:00
2018-09-25 17:44:26 +02:00
// Check that remote can call the RSET on this node.
2018-05-13 07:07:56 +02:00
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 ) ;
}
2018-09-25 17:44:26 +02:00
2019-04-05 17:13:55 +02:00
bool can_call = _can_call_mode ( p_node , rset_mode , p_from ) ;
ERR_EXPLAIN ( " RSET ' " + String ( p_name ) + " ' is not allowed on node " + p_node - > get_path ( ) + " from: " + itos ( p_from ) + " . Mode is " + itos ( ( int ) rset_mode ) + " , master is " + itos ( p_node - > get_network_master ( ) ) + " . " ) ;
ERR_FAIL_COND ( ! can_call ) ;
2018-05-13 07:07:56 +02:00
2018-03-03 18:28:49 +01:00
Variant value ;
2019-03-26 16:52:42 +01:00
Error err = decode_variant ( value , & p_packet [ p_offset ] , p_packet_len - p_offset , NULL , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Unable to decode RSET value. " ) ;
ERR_FAIL_COND ( err ! = OK ) ;
2018-03-03 18:28:49 +01:00
bool valid ;
p_node - > set ( p_name , value , & valid ) ;
if ( ! valid ) {
String error = " Error setting remote property ' " + String ( p_name ) + " ', not found in object of type " + p_node - > get_class ( ) ;
ERR_PRINTS ( error ) ;
}
}
void MultiplayerAPI : : _process_simplify_path ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( p_packet_len < 5 ) ;
int id = decode_uint32 ( & p_packet [ 1 ] ) ;
String paths ;
paths . parse_utf8 ( ( const char * ) & p_packet [ 5 ] , p_packet_len - 5 ) ;
NodePath path = paths ;
if ( ! path_get_cache . has ( p_from ) ) {
path_get_cache [ p_from ] = PathGetCache ( ) ;
}
PathGetCache : : NodeInfo ni ;
ni . path = path ;
ni . instance = 0 ;
path_get_cache [ p_from ] . nodes [ id ] = ni ;
2018-09-25 17:44:26 +02:00
// Encode path to send ack.
2018-03-03 18:28:49 +01:00
CharString pname = String ( path ) . utf8 ( ) ;
int len = encode_cstring ( pname . get_data ( ) , NULL ) ;
Vector < uint8_t > packet ;
packet . resize ( 1 + len ) ;
2018-07-25 03:11:03 +02:00
packet . write [ 0 ] = NETWORK_COMMAND_CONFIRM_PATH ;
encode_cstring ( pname . get_data ( ) , & packet . write [ 1 ] ) ;
2018-03-03 18:28:49 +01:00
network_peer - > set_transfer_mode ( NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
network_peer - > set_target_peer ( p_from ) ;
network_peer - > put_packet ( packet . ptr ( ) , packet . size ( ) ) ;
}
void MultiplayerAPI : : _process_confirm_path ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND ( p_packet_len < 2 ) ;
2018-03-03 18:28:49 +01:00
String paths ;
paths . parse_utf8 ( ( const char * ) & p_packet [ 1 ] , p_packet_len - 1 ) ;
NodePath path = paths ;
PathSentCache * psc = path_send_cache . getptr ( path ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Tries to confirm a path which was not found in cache. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( ! psc ) ;
Map < int , bool > : : Element * E = psc - > confirmed_peers . find ( p_from ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Source peer was not found in cache for the given path. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( ! E ) ;
E - > get ( ) = true ;
}
bool MultiplayerAPI : : _send_confirm_path ( NodePath p_path , PathSentCache * psc , int p_target ) {
bool has_all_peers = true ;
2018-09-25 17:44:26 +02:00
List < int > peers_to_add ; // If one is missing, take note to add it.
2018-03-03 18:28:49 +01:00
for ( Set < int > : : Element * E = connected_peers . front ( ) ; E ; E = E - > next ( ) ) {
if ( p_target < 0 & & E - > get ( ) = = - p_target )
2018-09-25 17:44:26 +02:00
continue ; // Continue, excluded.
2018-03-03 18:28:49 +01:00
if ( p_target > 0 & & E - > get ( ) ! = p_target )
2018-09-25 17:44:26 +02:00
continue ; // Continue, not for this peer.
2018-03-03 18:28:49 +01:00
Map < int , bool > : : Element * F = psc - > confirmed_peers . find ( E - > get ( ) ) ;
2018-10-06 22:20:41 +02:00
if ( ! F | | ! F - > get ( ) ) {
2018-09-25 17:44:26 +02:00
// Path was not cached, or was cached but is unconfirmed.
2018-03-03 18:28:49 +01:00
if ( ! F ) {
2018-09-25 17:44:26 +02:00
// Not cached at all, take note.
2018-03-03 18:28:49 +01:00
peers_to_add . push_back ( E - > get ( ) ) ;
}
has_all_peers = false ;
}
}
2018-09-25 17:44:26 +02:00
// Those that need to be added, send a message for this.
2018-03-03 18:28:49 +01:00
for ( List < int > : : Element * E = peers_to_add . front ( ) ; E ; E = E - > next ( ) ) {
2018-09-25 17:44:26 +02:00
// Encode function name.
2018-03-03 18:28:49 +01:00
CharString pname = String ( p_path ) . utf8 ( ) ;
int len = encode_cstring ( pname . get_data ( ) , NULL ) ;
Vector < uint8_t > packet ;
packet . resize ( 1 + 4 + len ) ;
2018-07-25 03:11:03 +02:00
packet . write [ 0 ] = NETWORK_COMMAND_SIMPLIFY_PATH ;
encode_uint32 ( psc - > id , & packet . write [ 1 ] ) ;
encode_cstring ( pname . get_data ( ) , & packet . write [ 5 ] ) ;
2018-03-03 18:28:49 +01:00
2018-09-25 17:44:26 +02:00
network_peer - > set_target_peer ( E - > get ( ) ) ; // To all of you.
2018-03-03 18:28:49 +01:00
network_peer - > set_transfer_mode ( NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
network_peer - > put_packet ( packet . ptr ( ) , packet . size ( ) ) ;
2018-09-25 17:44:26 +02:00
psc - > confirmed_peers . insert ( E - > get ( ) , false ) ; // Insert into confirmed, but as false since it was not confirmed.
2018-03-03 18:28:49 +01:00
}
return has_all_peers ;
}
void MultiplayerAPI : : _send_rpc ( Node * p_from , int p_to , bool p_unreliable , bool p_set , const StringName & p_name , const Variant * * p_arg , int p_argcount ) {
if ( network_peer . is_null ( ) ) {
ERR_EXPLAIN ( " Attempt to remote call/set when networking is not active in SceneTree. " ) ;
ERR_FAIL ( ) ;
}
if ( network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_CONNECTING ) {
ERR_EXPLAIN ( " Attempt to remote call/set when networking is not connected yet in SceneTree. " ) ;
ERR_FAIL ( ) ;
}
if ( network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED ) {
ERR_EXPLAIN ( " Attempt to remote call/set when networking is disconnected. " ) ;
ERR_FAIL ( ) ;
}
if ( p_argcount > 255 ) {
ERR_EXPLAIN ( " Too many arguments >255. " ) ;
ERR_FAIL ( ) ;
}
if ( p_to ! = 0 & & ! connected_peers . has ( ABS ( p_to ) ) ) {
if ( p_to = = network_peer - > get_unique_id ( ) ) {
ERR_EXPLAIN ( " Attempt to remote call/set yourself! unique ID: " + itos ( network_peer - > get_unique_id ( ) ) ) ;
} else {
ERR_EXPLAIN ( " Attempt to remote call unexisting ID: " + itos ( p_to ) ) ;
}
ERR_FAIL ( ) ;
}
NodePath from_path = ( root_node - > get_path ( ) ) . rel_path_to ( p_from - > get_path ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Unable to send RPC. Relative path is empty. THIS IS LIKELY A BUG IN THE ENGINE! " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( from_path . is_empty ( ) ) ;
2018-09-25 17:44:26 +02:00
// See if the path is cached.
2018-03-03 18:28:49 +01:00
PathSentCache * psc = path_send_cache . getptr ( from_path ) ;
if ( ! psc ) {
2018-09-25 17:44:26 +02:00
// Path is not cached, create.
2018-03-03 18:28:49 +01:00
path_send_cache [ from_path ] = PathSentCache ( ) ;
psc = path_send_cache . getptr ( from_path ) ;
psc - > id = last_send_cache_id + + ;
}
2018-09-25 17:44:26 +02:00
// Create base packet, lots of hardcode because it must be tight.
2018-03-03 18:28:49 +01:00
int ofs = 0 ;
# define MAKE_ROOM(m_amount) \
if ( packet_cache . size ( ) < m_amount ) packet_cache . resize ( m_amount ) ;
2018-09-25 17:44:26 +02:00
// Encode type.
2018-03-03 18:28:49 +01:00
MAKE_ROOM ( 1 ) ;
2018-07-25 03:11:03 +02:00
packet_cache . write [ 0 ] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL ;
2018-03-03 18:28:49 +01:00
ofs + = 1 ;
2018-09-25 17:44:26 +02:00
// Encode ID.
2018-03-03 18:28:49 +01:00
MAKE_ROOM ( ofs + 4 ) ;
2018-07-25 03:11:03 +02:00
encode_uint32 ( psc - > id , & ( packet_cache . write [ ofs ] ) ) ;
2018-03-03 18:28:49 +01:00
ofs + = 4 ;
2018-09-25 17:44:26 +02:00
// Encode function name.
2018-03-03 18:28:49 +01:00
CharString name = String ( p_name ) . utf8 ( ) ;
int len = encode_cstring ( name . get_data ( ) , NULL ) ;
MAKE_ROOM ( ofs + len ) ;
2018-07-25 03:11:03 +02:00
encode_cstring ( name . get_data ( ) , & ( packet_cache . write [ ofs ] ) ) ;
2018-03-03 18:28:49 +01:00
ofs + = len ;
if ( p_set ) {
2018-09-25 17:44:26 +02:00
// Set argument.
2019-03-26 16:52:42 +01:00
Error err = encode_variant ( * p_arg [ 0 ] , NULL , len , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Unable to encode RSET value. THIS IS LIKELY A BUG IN THE ENGINE! " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( err ! = OK ) ;
MAKE_ROOM ( ofs + len ) ;
2019-03-26 16:52:42 +01:00
encode_variant ( * p_arg [ 0 ] , & ( packet_cache . write [ ofs ] ) , len , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-03-03 18:28:49 +01:00
ofs + = len ;
} else {
2018-09-25 17:44:26 +02:00
// Call arguments.
2018-03-03 18:28:49 +01:00
MAKE_ROOM ( ofs + 1 ) ;
2018-07-25 03:11:03 +02:00
packet_cache . write [ ofs ] = p_argcount ;
2018-03-03 18:28:49 +01:00
ofs + = 1 ;
for ( int i = 0 ; i < p_argcount ; i + + ) {
2019-03-26 16:52:42 +01:00
Error err = encode_variant ( * p_arg [ i ] , NULL , len , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE! " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( err ! = OK ) ;
MAKE_ROOM ( ofs + len ) ;
2019-03-26 16:52:42 +01:00
encode_variant ( * p_arg [ i ] , & ( packet_cache . write [ ofs ] ) , len , allow_object_decoding | | network_peer - > is_object_decoding_allowed ( ) ) ;
2018-03-03 18:28:49 +01:00
ofs + = len ;
}
}
2018-09-25 17:44:26 +02:00
// See if all peers have cached path (is so, call can be fast).
2018-03-03 18:28:49 +01:00
bool has_all_peers = _send_confirm_path ( from_path , psc , p_to ) ;
2018-09-25 17:44:26 +02:00
// Take chance and set transfer mode, since all send methods will use it.
2018-03-03 18:28:49 +01:00
network_peer - > set_transfer_mode ( p_unreliable ? NetworkedMultiplayerPeer : : TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
if ( has_all_peers ) {
2018-09-25 17:44:26 +02:00
// 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.
2018-03-03 18:28:49 +01:00
} else {
2018-09-25 17:44:26 +02:00
// Not all verified path, so send one by one.
2018-03-03 18:28:49 +01:00
2018-09-25 17:44:26 +02:00
// Append path at the end, since we will need it for some packets.
2018-03-03 18:28:49 +01:00
CharString pname = String ( from_path ) . utf8 ( ) ;
int path_len = encode_cstring ( pname . get_data ( ) , NULL ) ;
MAKE_ROOM ( ofs + path_len ) ;
2018-07-25 03:11:03 +02:00
encode_cstring ( pname . get_data ( ) , & ( packet_cache . write [ ofs ] ) ) ;
2018-03-03 18:28:49 +01:00
for ( Set < int > : : Element * E = connected_peers . front ( ) ; E ; E = E - > next ( ) ) {
if ( p_to < 0 & & E - > get ( ) = = - p_to )
2018-09-25 17:44:26 +02:00
continue ; // Continue, excluded.
2018-03-03 18:28:49 +01:00
if ( p_to > 0 & & E - > get ( ) ! = p_to )
2018-09-25 17:44:26 +02:00
continue ; // Continue, not for this peer.
2018-03-03 18:28:49 +01:00
Map < int , bool > : : Element * F = psc - > confirmed_peers . find ( E - > get ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_CONTINUE ( ! F ) ; // Should never happen.
2018-03-03 18:28:49 +01:00
2018-09-25 17:44:26 +02:00
network_peer - > set_target_peer ( E - > get ( ) ) ; // To this one specifically.
2018-03-03 18:28:49 +01:00
2018-10-03 19:40:37 +02:00
if ( F - > get ( ) ) {
2018-09-25 17:44:26 +02:00
// This one confirmed path, so use id.
2018-07-25 03:11:03 +02:00
encode_uint32 ( psc - > id , & ( packet_cache . write [ 1 ] ) ) ;
2018-03-03 18:28:49 +01:00
network_peer - > put_packet ( packet_cache . ptr ( ) , ofs ) ;
} else {
2018-09-25 17:44:26 +02:00
// 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.
2018-03-03 18:28:49 +01:00
network_peer - > put_packet ( packet_cache . ptr ( ) , ofs + path_len ) ;
}
}
}
}
2018-05-08 11:49:57 +02:00
void MultiplayerAPI : : _add_peer ( int p_id ) {
2018-03-03 18:28:49 +01:00
connected_peers . insert ( p_id ) ;
path_get_cache . insert ( p_id , PathGetCache ( ) ) ;
emit_signal ( " network_peer_connected " , p_id ) ;
}
2018-05-08 11:49:57 +02:00
void MultiplayerAPI : : _del_peer ( int p_id ) {
2018-03-03 18:28:49 +01:00
connected_peers . erase ( p_id ) ;
2018-09-25 17:44:26 +02:00
path_get_cache . erase ( p_id ) ; // I no longer need your cache, sorry.
2018-03-03 18:28:49 +01:00
emit_signal ( " network_peer_disconnected " , p_id ) ;
}
2018-05-08 11:49:57 +02:00
void MultiplayerAPI : : _connected_to_server ( ) {
2018-03-03 18:28:49 +01:00
emit_signal ( " connected_to_server " ) ;
}
2018-05-08 11:49:57 +02:00
void MultiplayerAPI : : _connection_failed ( ) {
2018-03-03 18:28:49 +01:00
emit_signal ( " connection_failed " ) ;
}
2018-05-08 11:49:57 +02:00
void MultiplayerAPI : : _server_disconnected ( ) {
2018-03-03 18:28:49 +01:00
emit_signal ( " 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 ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to call an RPC while no network peer is active. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( ! network_peer . is_valid ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to call an RPC on a node which is not inside SceneTree. " ) ;
ERR_FAIL_COND ( ! p_node - > is_inside_tree ( ) ) ;
ERR_EXPLAIN ( " Trying to call an RPC via a network peer which is not connected. " ) ;
ERR_FAIL_COND ( network_peer - > get_connection_status ( ) ! = NetworkedMultiplayerPeer : : CONNECTION_CONNECTED ) ;
2018-03-03 18:28:49 +01:00
int node_id = network_peer - > get_unique_id ( ) ;
bool skip_rpc = false ;
bool call_local_native = false ;
bool call_local_script = false ;
bool is_master = p_node - > is_network_master ( ) ;
if ( p_peer_id = = 0 | | p_peer_id = = node_id | | ( p_peer_id < 0 & & p_peer_id ! = - node_id ) ) {
2018-09-25 17:44:26 +02:00
// Check that send mode can use local call.
2018-03-03 18:28:49 +01:00
2018-05-13 07:07:56 +02:00
const Map < StringName , RPCMode > : : Element * E = p_node - > get_node_rpc_mode ( p_method ) ;
2018-03-03 18:28:49 +01:00
if ( E ) {
2018-05-13 07:07:56 +02:00
call_local_native = _should_call_local ( E - > get ( ) , is_master , skip_rpc ) ;
2018-03-03 18:28:49 +01:00
}
if ( call_local_native ) {
2018-09-25 17:44:26 +02:00
// Done below.
2018-03-03 18:28:49 +01:00
} else if ( p_node - > get_script_instance ( ) ) {
2018-09-25 17:44:26 +02:00
// Attempt with script.
2018-05-13 07:07:56 +02:00
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 ) ;
2018-03-03 18:28:49 +01:00
}
}
if ( ! skip_rpc ) {
_send_rpc ( p_node , p_peer_id , p_unreliable , false , p_method , p_arg , p_argcount ) ;
}
if ( call_local_native ) {
2019-01-12 19:31:19 +01:00
int temp_id = rpc_sender_id ;
rpc_sender_id = get_network_unique_id ( ) ;
2018-03-03 18:28:49 +01:00
Variant : : CallError ce ;
p_node - > call ( p_method , p_arg , p_argcount , ce ) ;
2019-01-12 19:31:19 +01:00
rpc_sender_id = temp_id ;
2018-03-03 18:28:49 +01:00
if ( ce . error ! = Variant : : 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_PRINTS ( error ) ;
return ;
}
}
if ( call_local_script ) {
2019-01-12 19:31:19 +01:00
int temp_id = rpc_sender_id ;
rpc_sender_id = get_network_unique_id ( ) ;
2018-03-03 18:28:49 +01:00
Variant : : CallError ce ;
ce . error = Variant : : CallError : : CALL_OK ;
p_node - > get_script_instance ( ) - > call ( p_method , p_arg , p_argcount , ce ) ;
2019-01-12 19:31:19 +01:00
rpc_sender_id = temp_id ;
2018-03-03 18:28:49 +01:00
if ( ce . error ! = Variant : : 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_PRINTS ( error ) ;
return ;
}
}
}
void MultiplayerAPI : : rsetp ( Node * p_node , int p_peer_id , bool p_unreliable , const StringName & p_property , const Variant & p_value ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to RSET while no network peer is active. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( ! network_peer . is_valid ( ) ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to RSET on a node which is not inside SceneTree. " ) ;
ERR_FAIL_COND ( ! p_node - > is_inside_tree ( ) ) ;
ERR_EXPLAIN ( " Trying to send an RSET via a network peer which is not connected. " ) ;
ERR_FAIL_COND ( network_peer - > get_connection_status ( ) ! = NetworkedMultiplayerPeer : : CONNECTION_CONNECTED ) ;
2018-03-03 18:28:49 +01:00
int node_id = network_peer - > get_unique_id ( ) ;
bool is_master = p_node - > is_network_master ( ) ;
bool skip_rset = false ;
if ( p_peer_id = = 0 | | p_peer_id = = node_id | | ( p_peer_id < 0 & & p_peer_id ! = - node_id ) ) {
2018-09-25 17:44:26 +02:00
// Check that send mode can use local call.
2018-03-03 18:28:49 +01:00
bool set_local = false ;
2018-05-13 07:07:56 +02:00
const Map < StringName , RPCMode > : : Element * E = p_node - > get_node_rset_mode ( p_property ) ;
2018-03-03 18:28:49 +01:00
if ( E ) {
2018-05-13 07:07:56 +02:00
set_local = _should_call_local ( E - > get ( ) , is_master , skip_rset ) ;
2018-03-03 18:28:49 +01:00
}
if ( set_local ) {
bool valid ;
2019-01-12 19:31:19 +01:00
int temp_id = rpc_sender_id ;
rpc_sender_id = get_network_unique_id ( ) ;
2018-03-03 18:28:49 +01:00
p_node - > set ( p_property , p_value , & valid ) ;
2019-01-12 19:31:19 +01:00
rpc_sender_id = temp_id ;
2018-03-03 18:28:49 +01:00
if ( ! valid ) {
String error = " rset() aborted in local set, property not found: - " + String ( p_property ) ;
ERR_PRINTS ( error ) ;
return ;
}
} else if ( p_node - > get_script_instance ( ) ) {
2018-09-25 17:44:26 +02:00
// Attempt with script.
2018-05-13 07:07:56 +02:00
RPCMode rpc_mode = p_node - > get_script_instance ( ) - > get_rset_mode ( p_property ) ;
2018-03-03 18:28:49 +01:00
2018-05-13 07:07:56 +02:00
set_local = _should_call_local ( rpc_mode , is_master , skip_rset ) ;
2018-03-03 18:28:49 +01:00
if ( set_local ) {
2019-01-12 19:31:19 +01:00
int temp_id = rpc_sender_id ;
2018-03-03 18:28:49 +01:00
2019-01-12 19:31:19 +01:00
rpc_sender_id = get_network_unique_id ( ) ;
2018-03-03 18:28:49 +01:00
bool valid = p_node - > get_script_instance ( ) - > set ( p_property , p_value ) ;
2019-01-12 19:31:19 +01:00
rpc_sender_id = temp_id ;
2018-03-03 18:28:49 +01:00
if ( ! valid ) {
String error = " rset() aborted in local script set, property not found: - " + String ( p_property ) ;
ERR_PRINTS ( error ) ;
return ;
}
}
}
}
if ( skip_rset )
return ;
const Variant * vptr = & p_value ;
_send_rpc ( p_node , p_peer_id , p_unreliable , true , p_property , & vptr , 1 ) ;
}
2018-07-08 09:47:22 +02:00
Error MultiplayerAPI : : send_bytes ( PoolVector < uint8_t > p_data , int p_to , NetworkedMultiplayerPeer : : TransferMode p_mode ) {
2018-05-11 21:34:45 +02:00
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to send an empty raw packet. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND_V ( p_data . size ( ) < 1 , ERR_INVALID_DATA ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to send a raw packet while no network peer is active. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , ERR_UNCONFIGURED ) ;
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Trying to send a raw packet via a network peer which is not connected. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND_V ( network_peer - > get_connection_status ( ) ! = NetworkedMultiplayerPeer : : CONNECTION_CONNECTED , ERR_UNCONFIGURED ) ;
MAKE_ROOM ( p_data . size ( ) + 1 ) ;
PoolVector < uint8_t > : : Read r = p_data . read ( ) ;
2018-07-25 03:11:03 +02:00
packet_cache . write [ 0 ] = NETWORK_COMMAND_RAW ;
memcpy ( & packet_cache . write [ 1 ] , & r [ 0 ] , p_data . size ( ) ) ;
2018-07-08 09:47:22 +02:00
2018-05-11 21:34:45 +02:00
network_peer - > set_target_peer ( p_to ) ;
2018-07-08 09:47:22 +02:00
network_peer - > set_transfer_mode ( p_mode ) ;
2018-05-11 21:34:45 +02:00
return network_peer - > put_packet ( packet_cache . ptr ( ) , p_data . size ( ) + 1 ) ;
}
void MultiplayerAPI : : _process_raw ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " Invalid packet received. Size too small. " ) ;
2018-05-11 21:34:45 +02:00
ERR_FAIL_COND ( p_packet_len < 2 ) ;
PoolVector < uint8_t > out ;
int len = p_packet_len - 1 ;
out . resize ( len ) ;
{
PoolVector < uint8_t > : : Write w = out . write ( ) ;
memcpy ( & w [ 0 ] , & p_packet [ 1 ] , len ) ;
}
emit_signal ( " network_peer_packet " , p_from , out ) ;
}
2018-03-03 18:28:49 +01:00
int MultiplayerAPI : : get_network_unique_id ( ) const {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " No network peer is assigned. Unable to get unique network ID. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , 0 ) ;
return network_peer - > get_unique_id ( ) ;
}
bool MultiplayerAPI : : is_network_server ( ) const {
2018-09-25 17:44:26 +02:00
// XXX Maybe fail silently? Maybe should actually return true to make development of both local and online multiplayer easier?
ERR_EXPLAIN ( " No network peer is assigned. I can't be a server. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , false ) ;
return network_peer - > is_server ( ) ;
}
void MultiplayerAPI : : set_refuse_new_network_connections ( bool p_refuse ) {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " No network peer is assigned. Unable to set 'refuse_new_connections'. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND ( ! network_peer . is_valid ( ) ) ;
network_peer - > set_refuse_new_connections ( p_refuse ) ;
}
bool MultiplayerAPI : : is_refusing_new_network_connections ( ) const {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " No network peer is assigned. Unable to get 'refuse_new_connections'. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , false ) ;
return network_peer - > is_refusing_new_connections ( ) ;
}
Vector < int > MultiplayerAPI : : get_network_connected_peers ( ) const {
2018-09-25 17:44:26 +02:00
ERR_EXPLAIN ( " No network peer is assigned. Assume no peers are connected. " ) ;
2018-03-03 18:28:49 +01:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , Vector < int > ( ) ) ;
Vector < int > ret ;
for ( Set < int > : : Element * E = connected_peers . front ( ) ; E ; E = E - > next ( ) ) {
ret . push_back ( E - > get ( ) ) ;
}
return ret ;
}
2019-03-26 16:52:42 +01:00
void MultiplayerAPI : : set_allow_object_decoding ( bool p_enable ) {
allow_object_decoding = p_enable ;
}
bool MultiplayerAPI : : is_object_decoding_allowed ( ) const {
return allow_object_decoding ;
}
2018-03-03 18:28:49 +01:00
void MultiplayerAPI : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " set_root_node " , " node " ) , & MultiplayerAPI : : set_root_node ) ;
2018-07-26 11:56:21 +02:00
ClassDB : : bind_method ( D_METHOD ( " send_bytes " , " bytes " , " id " , " mode " ) , & MultiplayerAPI : : send_bytes , DEFVAL ( NetworkedMultiplayerPeer : : TARGET_PEER_BROADCAST ) , DEFVAL ( NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ) ;
2018-03-03 18:28:49 +01:00
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 ) ;
2018-05-08 11:49:57 +02:00
ClassDB : : bind_method ( D_METHOD ( " _add_peer " , " id " ) , & MultiplayerAPI : : _add_peer ) ;
ClassDB : : bind_method ( D_METHOD ( " _del_peer " , " id " ) , & MultiplayerAPI : : _del_peer ) ;
2018-03-03 18:28:49 +01:00
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 ) ;
2018-05-08 11:49:57 +02:00
ClassDB : : bind_method ( D_METHOD ( " _connected_to_server " ) , & MultiplayerAPI : : _connected_to_server ) ;
ClassDB : : bind_method ( D_METHOD ( " _connection_failed " ) , & MultiplayerAPI : : _connection_failed ) ;
ClassDB : : bind_method ( D_METHOD ( " _server_disconnected " ) , & MultiplayerAPI : : _server_disconnected ) ;
2018-03-03 18:28:49 +01:00
ClassDB : : bind_method ( D_METHOD ( " get_network_connected_peers " ) , & MultiplayerAPI : : get_network_connected_peers ) ;
ClassDB : : bind_method ( D_METHOD ( " set_refuse_new_network_connections " , " refuse " ) , & MultiplayerAPI : : set_refuse_new_network_connections ) ;
ClassDB : : bind_method ( D_METHOD ( " is_refusing_new_network_connections " ) , & MultiplayerAPI : : is_refusing_new_network_connections ) ;
2019-03-26 16:52:42 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_allow_object_decoding " , " enable " ) , & MultiplayerAPI : : set_allow_object_decoding ) ;
ClassDB : : bind_method ( D_METHOD ( " is_object_decoding_allowed " ) , & MultiplayerAPI : : is_object_decoding_allowed ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " allow_object_decoding " ) , " set_allow_object_decoding " , " is_object_decoding_allowed " ) ;
2018-03-03 18:28:49 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " refuse_new_network_connections " ) , " set_refuse_new_network_connections " , " is_refusing_new_network_connections " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " network_peer " , PROPERTY_HINT_RESOURCE_TYPE , " NetworkedMultiplayerPeer " , 0 ) , " set_network_peer " , " get_network_peer " ) ;
ADD_SIGNAL ( MethodInfo ( " network_peer_connected " , PropertyInfo ( Variant : : INT , " id " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " network_peer_disconnected " , PropertyInfo ( Variant : : INT , " id " ) ) ) ;
2018-05-11 21:34:45 +02:00
ADD_SIGNAL ( MethodInfo ( " network_peer_packet " , PropertyInfo ( Variant : : INT , " id " ) , PropertyInfo ( Variant : : POOL_BYTE_ARRAY , " packet " ) ) ) ;
2018-03-03 18:28:49 +01:00
ADD_SIGNAL ( MethodInfo ( " connected_to_server " ) ) ;
ADD_SIGNAL ( MethodInfo ( " connection_failed " ) ) ;
ADD_SIGNAL ( MethodInfo ( " server_disconnected " ) ) ;
2018-05-13 07:07:56 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_DISABLED ) ;
BIND_ENUM_CONSTANT ( RPC_MODE_REMOTE ) ;
BIND_ENUM_CONSTANT ( RPC_MODE_MASTER ) ;
2018-09-14 21:59:47 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_PUPPET ) ;
2018-09-25 17:44:26 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_SLAVE ) ; // Deprecated.
2018-05-26 10:28:28 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_REMOTESYNC ) ;
2018-09-25 17:44:26 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_SYNC ) ; // Deprecated.
2018-05-26 10:28:28 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_MASTERSYNC ) ;
2018-09-14 21:59:47 +02:00
BIND_ENUM_CONSTANT ( RPC_MODE_PUPPETSYNC ) ;
2018-03-03 18:28:49 +01:00
}
2019-03-26 16:52:42 +01:00
MultiplayerAPI : : MultiplayerAPI ( ) :
allow_object_decoding ( false ) {
2019-01-15 10:35:26 +01:00
rpc_sender_id = 0 ;
root_node = NULL ;
2018-03-03 18:28:49 +01:00
clear ( ) ;
}
MultiplayerAPI : : ~ MultiplayerAPI ( ) {
clear ( ) ;
}