diff options
Diffstat (limited to 'scene/main/scene_main_loop.cpp')
| -rw-r--r-- | scene/main/scene_main_loop.cpp | 355 |
1 files changed, 271 insertions, 84 deletions
diff --git a/scene/main/scene_main_loop.cpp b/scene/main/scene_main_loop.cpp index 6af2b9b16..4c7b7c139 100644 --- a/scene/main/scene_main_loop.cpp +++ b/scene/main/scene_main_loop.cpp @@ -44,7 +44,7 @@ #include "scene/resources/packed_scene.h" #include "scene/resources/material.h" #include "scene/resources/mesh.h" - +#include "io/marshalls.h" void SceneTreeTimer::_bind_methods() { @@ -1657,25 +1657,45 @@ Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec) { return stt; } -void SceneTree::_network_peer_connected(const StringName& p_id) { +void SceneTree::_network_peer_connected(int p_id) { connected_peers.insert(p_id); path_get_cache.insert(p_id,PathGetCache()); + + emit_signal("network_peer_connected",p_id); } -void SceneTree::_network_peer_disconnected(const StringName& p_id) { +void SceneTree::_network_peer_disconnected(int p_id) { connected_peers.erase(p_id); path_get_cache.erase(p_id); //I no longer need your cache, sorry emit_signal("network_peer_disconnected",p_id); } +void SceneTree::_connected_to_server() { + + emit_signal("connected_to_server"); +} + +void SceneTree::_connection_failed() { + + emit_signal("connection_failed"); +} + +void SceneTree::_server_disconnected() { + + emit_signal("server_disconnected"); +} + void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer>& p_network_peer) { if (network_peer.is_valid()) { network_peer->disconnect("peer_connected",this,"_network_peer_connected"); network_peer->disconnect("peer_disconnected",this,"_network_peer_disconnected"); + 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"); connected_peers.clear(); path_get_cache.clear(); path_send_cache.clear(); @@ -1690,6 +1710,9 @@ void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer>& p_network_ if (network_peer.is_valid()) { network_peer->connect("peer_connected",this,"_network_peer_connected"); network_peer->connect("peer_disconnected",this,"_network_peer_disconnected"); + 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"); } } @@ -1700,7 +1723,27 @@ bool SceneTree::is_network_server() const { } -void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const StringName& p_name, const Variant** p_arg, int p_argcount) { +int SceneTree::get_network_unique_id() const { + + ERR_FAIL_COND_V(!network_peer.is_valid(),0); + return network_peer->get_unique_id(); +} + +void SceneTree::set_refuse_new_network_connections(bool p_refuse) { + ERR_FAIL_COND(!network_peer.is_valid()); + network_peer->set_refuse_new_connections(p_refuse); +} + +bool SceneTree::is_refusing_new_network_connections() const { + + ERR_FAIL_COND_V(!network_peer.is_valid(),false); + + return network_peer->is_refusing_new_connections(); + +} + + +void SceneTree::_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."); @@ -1717,28 +1760,26 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St ERR_FAIL(); } - NodePath from_path = p_from->get_path(); - ERR_FAIL_COND(from_path.is_empty()); + if (p_argcount>255) { + ERR_EXPLAIN("Too many arguments >255."); + ERR_FAIL(); + } - //create base packet - Array message; + if (p_to!=0 && !connected_peers.has(ABS(p_to))) { + if (p_to==get_network_unique_id()) { + ERR_EXPLAIN("Attempt to remote call/set yourself! unique ID: "+itos(get_network_unique_id())); + } else { + ERR_EXPLAIN("Attempt to remote call unexisting ID: "+itos(p_to)); - message.resize(3+p_argcount); //alloc size for args + } - //set message type - if (p_set) { - message[0]=NETWORK_COMMAND_REMOTE_SET; - } else { - message[0]=NETWORK_COMMAND_REMOTE_CALL; + ERR_FAIL(); } - //set message name - message[2]=p_name; + NodePath from_path = p_from->get_path(); + ERR_FAIL_COND(from_path.is_empty()); + - //set message args - for(int i=0;i<p_argcount;i++) { - message[3+i]=*p_arg[i]; - } //see if the path is cached PathSentCache *psc = path_send_cache.getptr(from_path); @@ -1750,14 +1791,67 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St } + + //create base packet, lots of harcode 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 type + MAKE_ROOM(1); + packet_cache[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]); + 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]); + ofs+=len; + + if (p_set) { + //set argument + 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); + ofs+=len; + + } else { + //call arguments + MAKE_ROOM(ofs+1); + packet_cache[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); + ofs+=len; + } + + } + //see if all peers have cached path (is so, call can be fast) bool has_all_peers=true; - List<StringName> peers_to_add; //if one is missing, take note to add it + List<int> peers_to_add; //if one is missing, take note to add it + + for (Set<int>::Element *E=connected_peers.front();E;E=E->next()) { - for (Set<StringName>::Element *E=connected_peers.front();E;E=E->next()) { + if (p_to<0 && E->get()==-p_to) + continue; //continue, excluded - Map<StringName,bool>::Element *F = psc->confirmed_peers.find(E->get()); + 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()); if (!F || F->get()==false) { //path was not cached, or was cached but is unconfirmed @@ -1767,87 +1861,114 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St } has_all_peers=false; - break; } } //those that need to be added, send a message for this - for (List<StringName>::Element *E=peers_to_add.front();E;E=E->next()) { + for (List<int>::Element *E=peers_to_add.front();E;E=E->next()) { - Array add_path_message; - add_path_message.resize(3); - add_path_message[0]=NETWORK_COMMAND_SIMPLIFY_PATH; - add_path_message[1]=from_path; - add_path_message[2]=psc->id; + //encode function name + CharString pname = String(from_path).utf8(); + int len = encode_cstring(pname.get_data(),NULL); + + 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]); network_peer->set_target_peer(E->get()); //to all of you - network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_ORDERED); - network_peer->put_var(add_path_message); //a message with love + network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); + network_peer->put_packet(packet.ptr(),packet.size()); psc->confirmed_peers.insert(E->get(),false); //insert into confirmed, but as false since it was not confirmed } //take chance and set transfer mode, since all send methods will use it - network_peer->set_transfer_mode(p_reliable ? NetworkedMultiplayerPeer::TRANSFER_MODE_ORDERED : NetworkedMultiplayerPeer::TRANSFER_MODE_UNRELIABLE); + network_peer->set_transfer_mode(p_unreliable ? NetworkedMultiplayerPeer::TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); if (has_all_peers) { //they all have verified paths, so send fast - message[1]=psc->id; - - network_peer->set_target_peer(StringName()); //to all of you - network_peer->put_var(message); //a message with love + network_peer->set_target_peer(p_to); //to all of you + network_peer->put_packet(packet_cache.ptr(),ofs); //a message with love } else { //not all verified path, so send one by one - for (Set<StringName>::Element *E=connected_peers.front();E;E=E->next()) { - Map<StringName,bool>::Element *F = psc->confirmed_peers.find(E->get()); + //apend 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(),NULL); + MAKE_ROOM(ofs+path_len); + encode_cstring(pname.get_data(),&packet_cache[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()==true) { //this one confirmed path, so use id - message[1]=psc->id; + encode_uint32(psc->id,&packet_cache[1]); + network_peer->put_packet(packet_cache.ptr(),ofs); } else { //this one did not confirm path yet, so use entire path (sorry!) - message[1]=from_path; + encode_uint32(0x80000000|ofs,&packet_cache[1]); //offset to path and flag + network_peer->put_packet(packet_cache.ptr(),ofs+path_len); } - network_peer->put_var(message); } } } -void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_packet) { +void SceneTree::_network_process_packet(int p_from, const uint8_t* p_packet, int p_packet_len) { - ERR_FAIL_COND(p_packet.empty()); + ERR_FAIL_COND(p_packet_len<5); - int packet_type = p_packet[0]; + uint8_t packet_type = p_packet[0]; switch(packet_type) { case NETWORK_COMMAND_REMOTE_CALL: case NETWORK_COMMAND_REMOTE_SET: { - ERR_FAIL_COND(p_packet.size()<3); - Variant target = p_packet[1]; - Node* node=NULL; + ERR_FAIL_COND(p_packet_len<5); + uint32_t target = decode_uint32(&p_packet[1]); + + + Node *node=NULL; + + if (target&0x80000000) { + + int ofs = target&0x7FFFFFFF; + ERR_FAIL_COND(ofs>=p_packet_len); + + String paths; + paths.parse_utf8((const char*)&p_packet[ofs],p_packet_len-ofs); + + NodePath np = paths; - if (target.get_type()==Variant::NODE_PATH) { - NodePath np = target; node = get_root()->get_node(np); if (node==NULL) { ERR_EXPLAIN("Failed to get path from RPC: "+String(np)); ERR_FAIL_COND(node==NULL); } - } else if (target.get_type()==Variant::INT) { + } else { int id = target; - Map<StringName,PathGetCache>::Element *E=path_get_cache.find(p_from); + Map<int,PathGetCache>::Element *E=path_get_cache.find(p_from); ERR_FAIL_COND(!E); Map<int,PathGetCache::NodeInfo>::Element *F=E->get().nodes.find(id); @@ -1863,23 +1984,51 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_ } - } else { - ERR_FAIL(); } - StringName name = p_packet[2]; + ERR_FAIL_COND(p_packet_len<6); + + //detect cstring end + int len_end=5; + for(;len_end<p_packet_len;len_end++) { + if (p_packet[len_end]==0) { + break; + } + } + + ERR_FAIL_COND(len_end>=p_packet_len); + + StringName name = String::utf8((const char*)&p_packet[5]); + + + if (packet_type==NETWORK_COMMAND_REMOTE_CALL) { - int argc = p_packet.size()-3; + if (!node->can_call_rpc(name)) + return; + + int ofs = len_end+1; + + ERR_FAIL_COND(ofs>=p_packet_len); + + int argc = p_packet[ofs]; Vector<Variant> args; Vector<const Variant*> argp; args.resize(argc); argp.resize(argc); + ofs++; + for(int i=0;i<argc;i++) { - args[i]=p_packet[3+i]; - argp[i]=&args[i]; + + ERR_FAIL_COND(ofs>=p_packet_len); + int vlen; + Error err = decode_variant(args[i],&p_packet[ofs],p_packet_len-ofs,&vlen); + ERR_FAIL_COND(err!=OK); + //args[i]=p_packet[3+i]; + argp[i]=&args[i]; + ofs+=vlen; } Variant::CallError ce; @@ -1887,14 +2036,22 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_ node->call(name,argp.ptr(),argc,ce); if (ce.error!=Variant::CallError::CALL_OK) { String error = Variant::get_call_error_text(node,name,argp.ptr(),argc,ce); + error="RPC - "+error; ERR_PRINTS(error); } } else { + if (!node->can_call_rset(name)) + return; + + int ofs = len_end+1; + + ERR_FAIL_COND(ofs>=p_packet_len); + + Variant value; + decode_variant(value,&p_packet[ofs],p_packet_len-ofs); - ERR_FAIL_COND(p_packet.size()!=4); - Variant value = p_packet[3]; bool valid; node->set(name,value,&valid); @@ -1907,9 +2064,13 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_ } break; case NETWORK_COMMAND_SIMPLIFY_PATH: { - ERR_FAIL_COND(p_packet.size()!=3); - NodePath path = p_packet[1]; - int id = p_packet[2]; + 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(); @@ -1921,24 +2082,36 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_ path_get_cache[p_from].nodes[id]=ni; - network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_ORDERED); - network_peer->set_target_peer(p_from); - Array message; - message.resize(2); - message[0]=NETWORK_COMMAND_CONFIRM_PATH; - message[1]=path; + { + //send ack + + //encode path + CharString pname = String(path).utf8(); + int len = encode_cstring(pname.get_data(),NULL); + + Vector<uint8_t> packet; - network_peer->put_var(message); + packet.resize(1+len); + packet[0]=NETWORK_COMMAND_CONFIRM_PATH; + encode_cstring(pname.get_data(),&packet[1]); + + network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); + network_peer->set_target_peer(p_from); + network_peer->put_packet(packet.ptr(),packet.size()); + } } break; case NETWORK_COMMAND_CONFIRM_PATH: { - ERR_FAIL_COND(p_packet.size()!=2); - NodePath path = p_packet[1]; + + String paths; + paths.parse_utf8((const char*)&p_packet[1],p_packet_len-1); + + NodePath path = paths; PathSentCache *psc = path_send_cache.getptr(path); ERR_FAIL_COND(!psc); - Map<StringName,bool>::Element *E=psc->confirmed_peers.find(p_from); + Map<int,bool>::Element *E=psc->confirmed_peers.find(p_from); ERR_FAIL_COND(!E); E->get()=true; } break; @@ -1948,25 +2121,30 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_ void SceneTree::_network_poll() { - if (!network_peer.is_valid()) + if (!network_peer.is_valid() || network_peer->get_connection_status()==NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) return; network_peer->poll(); + if (!network_peer.is_valid()) //it's possible that polling might have resulted in a disconnection, so check here + return; + while(network_peer->get_available_packet_count()) { - StringName sender = network_peer->get_packet_peer(); - Variant packet; - Error err = network_peer->get_var(packet); + 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!"); } - if (packet.get_type()!=Variant::ARRAY) { - ERR_PRINT("Error getting packet! (not an array)"); - } + _network_process_packet(sender,packet,len); - _network_process_packet(sender,packet); + if (!network_peer.is_valid()) { + break; //it's also possible that a packet or RPC caused a disconnection, so also check here + } } @@ -2042,9 +2220,15 @@ void SceneTree::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_network_peer","peer:NetworkedMultiplayerPeer"),&SceneTree::set_network_peer); - ObjectTypeDB::bind_method(_MD("is_network_server","is_network_server"),&SceneTree::is_network_server); + ObjectTypeDB::bind_method(_MD("is_network_server"),&SceneTree::is_network_server); + ObjectTypeDB::bind_method(_MD("get_network_unique_id"),&SceneTree::get_network_unique_id); + ObjectTypeDB::bind_method(_MD("set_refuse_new_network_connections","refuse"),&SceneTree::set_refuse_new_network_connections); + ObjectTypeDB::bind_method(_MD("is_refusing_new_network_connections"),&SceneTree::is_refusing_new_network_connections); ObjectTypeDB::bind_method(_MD("_network_peer_connected"),&SceneTree::_network_peer_connected); ObjectTypeDB::bind_method(_MD("_network_peer_disconnected"),&SceneTree::_network_peer_disconnected); + ObjectTypeDB::bind_method(_MD("_connected_to_server"),&SceneTree::_connected_to_server); + ObjectTypeDB::bind_method(_MD("_connection_failed"),&SceneTree::_connection_failed); + ObjectTypeDB::bind_method(_MD("_server_disconnected"),&SceneTree::_server_disconnected); ADD_SIGNAL( MethodInfo("tree_changed") ); ADD_SIGNAL( MethodInfo("node_removed",PropertyInfo( Variant::OBJECT, "node") ) ); @@ -2055,8 +2239,11 @@ void SceneTree::_bind_methods() { ADD_SIGNAL( MethodInfo("fixed_frame")); ADD_SIGNAL( MethodInfo("files_dropped",PropertyInfo(Variant::STRING_ARRAY,"files"),PropertyInfo(Variant::INT,"screen")) ); - ADD_SIGNAL( MethodInfo("network_peer_connected",PropertyInfo(Variant::STRING,"id"))); - ADD_SIGNAL( MethodInfo("network_peer_disconnected",PropertyInfo(Variant::STRING,"id"))); + ADD_SIGNAL( MethodInfo("network_peer_connected",PropertyInfo(Variant::INT,"id"))); + ADD_SIGNAL( MethodInfo("network_peer_disconnected",PropertyInfo(Variant::INT,"id"))); + ADD_SIGNAL( MethodInfo("connected_to_server")); + ADD_SIGNAL( MethodInfo("connection_failed")); + ADD_SIGNAL( MethodInfo("server_disconnected")); BIND_CONSTANT( GROUP_CALL_DEFAULT ); BIND_CONSTANT( GROUP_CALL_REVERSE ); |
