瀏覽代碼

Brand new networked multiplayer

Juan Linietsky 9 年之前
父節點
當前提交
1add52b55e

+ 13 - 4
core/io/networked_multiplayer_peer.cpp

@@ -5,14 +5,16 @@ void NetworkedMultiplayerPeer::_bind_methods() {
 
 	ObjectTypeDB::bind_method(_MD("set_transfer_mode","mode"), &NetworkedMultiplayerPeer::set_transfer_mode );
 	ObjectTypeDB::bind_method(_MD("set_target_peer","id"), &NetworkedMultiplayerPeer::set_target_peer );
-	ObjectTypeDB::bind_method(_MD("set_channel","id"), &NetworkedMultiplayerPeer::set_channel );
 
 	ObjectTypeDB::bind_method(_MD("get_packet_peer"), &NetworkedMultiplayerPeer::get_packet_peer );
-	ObjectTypeDB::bind_method(_MD("get_packet_channel"), &NetworkedMultiplayerPeer::get_packet_channel );
 
 	ObjectTypeDB::bind_method(_MD("poll"), &NetworkedMultiplayerPeer::poll );
 
 	ObjectTypeDB::bind_method(_MD("get_connection_status"), &NetworkedMultiplayerPeer::get_connection_status );
+	ObjectTypeDB::bind_method(_MD("get_unique_id"), &NetworkedMultiplayerPeer::get_unique_id );
+
+	ObjectTypeDB::bind_method(_MD("set_refuse_new_connections","enable"), &NetworkedMultiplayerPeer::set_refuse_new_connections );
+	ObjectTypeDB::bind_method(_MD("is_refusing_new_connections"), &NetworkedMultiplayerPeer::is_refusing_new_connections );
 
 	BIND_CONSTANT( TRANSFER_MODE_UNRELIABLE );
 	BIND_CONSTANT( TRANSFER_MODE_RELIABLE );
@@ -22,8 +24,15 @@ void NetworkedMultiplayerPeer::_bind_methods() {
 	BIND_CONSTANT( CONNECTION_CONNECTING );
 	BIND_CONSTANT( CONNECTION_CONNECTED );
 
-	ADD_SIGNAL( MethodInfo("peer_connected",PropertyInfo(Variant::STRING,"id")));
-	ADD_SIGNAL( MethodInfo("peer_disconnected",PropertyInfo(Variant::STRING,"id")));
+	BIND_CONSTANT( TARGET_PEER_BROADCAST );
+	BIND_CONSTANT( TARGET_PEER_SERVER );
+
+
+	ADD_SIGNAL( MethodInfo("peer_connected",PropertyInfo(Variant::INT,"id")));
+	ADD_SIGNAL( MethodInfo("peer_disconnected",PropertyInfo(Variant::INT,"id")));
+	ADD_SIGNAL( MethodInfo("server_disconnected"));
+	ADD_SIGNAL( MethodInfo("connection_succeeded") );
+	ADD_SIGNAL( MethodInfo("connection_failed") );
 }
 
 NetworkedMultiplayerPeer::NetworkedMultiplayerPeer() {

+ 12 - 5
core/io/networked_multiplayer_peer.h

@@ -11,6 +11,10 @@ protected:
 	static void _bind_methods();
 public:
 
+	enum {
+		TARGET_PEER_BROADCAST=0,
+		TARGET_PEER_SERVER=1
+	};
 	enum TransferMode {
 		TRANSFER_MODE_UNRELIABLE,
 		TRANSFER_MODE_RELIABLE,
@@ -25,17 +29,20 @@ public:
 
 
 	virtual void set_transfer_mode(TransferMode p_mode)=0;
-	virtual void set_target_peer(const StringName& p_peer_id)=0;
-	virtual void set_channel(int p_channel)=0;
-
+	virtual void set_target_peer(int p_peer_id)=0;
 
-	virtual StringName get_packet_peer() const=0;
-	virtual int get_packet_channel() const=0;
+	virtual int get_packet_peer() const=0;
 
 	virtual bool is_server() const=0;
 
 	virtual void poll()=0;
 
+	virtual int get_unique_id() const=0;
+
+	virtual void set_refuse_new_connections(bool p_enable)=0;
+	virtual bool is_refusing_new_connections() const=0;
+
+
 	virtual ConnectionStatus get_connection_status() const=0;
 
 	NetworkedMultiplayerPeer();

+ 17 - 1
core/script_language.h

@@ -113,7 +113,7 @@ public:
 	virtual bool get_property_default_value(const StringName& p_property,Variant& r_value) const=0;
 
 	virtual void update_exports() {} //editor tool
-	virtual void get_method_list(List<MethodInfo> *p_list) const=0;
+	virtual void get_script_method_list(List<MethodInfo> *p_list) const=0;
 
 
 	Script() {}
@@ -121,6 +121,8 @@ public:
 
 class ScriptInstance {
 public:
+
+
 	virtual bool set(const StringName& p_name, const Variant& p_value)=0;
 	virtual bool get(const StringName& p_name, Variant &r_ret) const=0;
 	virtual void get_property_list(List<PropertyInfo> *p_properties) const=0;
@@ -148,6 +150,17 @@ public:
 
 	virtual bool is_placeholder() const { return false; }
 
+	enum RPCMode {
+		RPC_MODE_DISABLED,
+		RPC_MODE_REMOTE,
+		RPC_MODE_SYNC,
+		RPC_MODE_MASTER,
+		RPC_MODE_SLAVE,
+	};
+
+	virtual RPCMode get_rpc_mode(const StringName& p_method) const=0;
+	virtual RPCMode get_rset_mode(const StringName& p_variable) const=0;
+
 	virtual ScriptLanguage *get_language()=0;
 	virtual ~ScriptInstance();
 };
@@ -279,6 +292,9 @@ public:
 
 	virtual bool is_placeholder() const { return true; }
 
+	virtual RPCMode get_rpc_mode(const StringName& p_method) const { return RPC_MODE_DISABLED; }
+	virtual RPCMode get_rset_mode(const StringName& p_variable) const { return RPC_MODE_DISABLED; }
+
 	PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script,Object *p_owner);
 	~PlaceHolderScriptInstance();
 

+ 317 - 65
modules/enet/networked_multiplayer_enet.cpp

@@ -1,48 +1,38 @@
 #include "networked_multiplayer_enet.h"
+#include "os/os.h"
+#include "io/marshalls.h"
 
 void NetworkedMultiplayerENet::set_transfer_mode(TransferMode p_mode) {
 
 	transfer_mode=p_mode;
 }
 
-void NetworkedMultiplayerENet::set_target_peer(const StringName &p_peer){
+void NetworkedMultiplayerENet::set_target_peer(int p_peer){
 
 	target_peer=p_peer;
 }
-void NetworkedMultiplayerENet::set_channel(int p_channel){
-
-	send_channel=p_channel;
-}
 
+int NetworkedMultiplayerENet::get_packet_peer() const{
 
-StringName NetworkedMultiplayerENet::get_packet_peer() const{
-
-	ERR_FAIL_COND_V(!active,StringName());
-	ERR_FAIL_COND_V(incoming_packets.size()==0,StringName());
+	ERR_FAIL_COND_V(!active,1);
+	ERR_FAIL_COND_V(incoming_packets.size()==0,1);
 
 	return incoming_packets.front()->get().from;
 
 }
-int NetworkedMultiplayerENet::get_packet_channel() const{
-
-	ERR_FAIL_COND_V(!active,0);
-	ERR_FAIL_COND_V(incoming_packets.size()==0,0);
-	return incoming_packets.front()->get().from_channel;
-}
-
 
-Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth){
+Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_in_bandwidth, int p_out_bandwidth){
 
 	ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE);
 
 	ENetAddress address;
 	address.host = ENET_HOST_ANY;
-	/* Bind the server to port 1234. */
-	address.port = 1234;
+
+	address.port = p_port;
 
 	host = enet_host_create (& address /* the address to bind the server host to */,
 				     p_max_clients      /* allow up to 32 clients and/or outgoing connections */,
-				     p_max_channels      /* allow up to 2 channels to be used, 0 and 1 */,
+				     2      /* allow up to 2 channels to be used, 0 and 1 */,
 				     p_in_bandwidth      /* assume any amount of incoming bandwidth */,
 				     p_out_bandwidth      /* assume any amount of outgoing bandwidth */);
 
@@ -50,16 +40,18 @@ Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int
 
 	active=true;
 	server=true;
+	refuse_connections=false;
+	unique_id=1;
 	connection_status=CONNECTION_CONNECTED;
 	return OK;
 }
-Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip,int p_port, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth){
+Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth){
 
 	ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE);
 
 	host = enet_host_create (NULL /* create a client host */,
 		    1 /* only allow 1 outgoing connection */,
-		    p_max_channels /* allow up 2 channels to be used, 0 and 1 */,
+		    2 /* allow up 2 channels to be used, 0 and 1 */,
 		    p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
 		    p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
 
@@ -70,8 +62,13 @@ Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip,int p_port,
 	address.host=p_ip.host;
 	address.port=p_port;
 
+	//enet_address_set_host (& address, "localhost");
+	//address.port = p_port;
+
+	unique_id=_gen_unique_id();
+
 	/* Initiate the connection, allocating the two channels 0 and 1. */
-	ENetPeer *peer = enet_host_connect (host, & address, p_max_channels, 0);
+	ENetPeer *peer = enet_host_connect (host, & address, 2, unique_id);
 
 	if (peer == NULL) {
 		enet_host_destroy(host);
@@ -83,6 +80,7 @@ Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip,int p_port,
 	connection_status=CONNECTION_CONNECTING;
 	active=true;
 	server=false;
+	refuse_connections=false;
 
 	return OK;
 }
@@ -95,18 +93,41 @@ void NetworkedMultiplayerENet::poll(){
 
 	ENetEvent event;
 	/* Wait up to 1000 milliseconds for an event. */
-	while (enet_host_service (host, & event, 1000) > 0)
-	{
+	while (true) {
+
+		if (!host || !active) //might have been disconnected while emitting a notification
+			return;
+
+		int ret = enet_host_service (host, & event, 1);
+
+		if (ret<0) {
+			//error, do something?
+			break;
+		} else if (ret==0) {
+			break;
+		}
+
 		switch (event.type)
 		{
 			case ENET_EVENT_TYPE_CONNECT: {
 				/* Store any relevant client information here. */
 
+				if (server && refuse_connections) {
+					enet_peer_reset(event.peer);
+					break;
+				}
+
 				IP_Address ip;
 				ip.host=event.peer -> address.host;
 
-				StringName *new_id = memnew( StringName );
-				*new_id = String(ip) +":"+ itos(event.peer -> address.port);
+				int *new_id = memnew( int );
+				*new_id = event.data;
+
+				if (*new_id==0) { //data zero is sent by server (enet won't let you configure this). Server is always 1
+					*new_id=1;
+				}
+
+				event.peer->data=new_id;
 
 				peer_map[*new_id]=event.peer;
 
@@ -114,29 +135,170 @@ void NetworkedMultiplayerENet::poll(){
 
 				emit_signal("peer_connected",*new_id);
 
+				if (server) {
+					//someone connected, let it know of all the peers available
+					for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) {
+
+						if (E->key()==*new_id)
+							continue;
+						//send existing peers to new peer
+						ENetPacket * packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
+						encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]);
+						encode_uint32(E->key(),&packet->data[4]);
+						enet_peer_send(event.peer,1,packet);
+						//send the new peer to existing peers
+						packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
+						encode_uint32(SYSMSG_ADD_PEER,&packet->data[0]);
+						encode_uint32(*new_id,&packet->data[4]);
+						enet_peer_send(E->get(),1,packet);
+					}
+				} else {
+
+					emit_signal("connection_succeeded");
+				}
+
 			} break;
 			case ENET_EVENT_TYPE_DISCONNECT: {
 
 				/* Reset the peer's client information. */
 
-				StringName *id = (StringName*)event.peer -> data;
+				int *id = (int*)event.peer -> data;
+
+
+
+				if (!id) {
+					if (!server) {
+						emit_signal("connection_failed");
+					}
+				} else {
+
+					if (server) {
+						//someone disconnected, let it know to everyone else
+						for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) {
+
+							if (E->key()==*id)
+								continue;
+							//send the new peer to existing peers
+							ENetPacket* packet = enet_packet_create (NULL,8,ENET_PACKET_FLAG_RELIABLE);
+							encode_uint32(SYSMSG_REMOVE_PEER,&packet->data[0]);
+							encode_uint32(*id,&packet->data[4]);
+							enet_peer_send(E->get(),1,packet);
+						}
+					} else if (!server) {
+						emit_signal("server_disconnected");
+						close_connection();
+						return;
+					}
+
+					emit_signal("peer_disconnected",*id);
+					peer_map.erase(*id);
+					memdelete( id );
+
+				}
 
-				emit_signal("peer_disconnected",*id);
 
-				peer_map.erase(*id);
-				memdelete( id );
 			} break;
 			case ENET_EVENT_TYPE_RECEIVE: {
 
-				Packet packet;
-				packet.packet = event.packet;
 
-				StringName *id = (StringName*)event.peer -> data;
-				packet.from_channel=event.channelID;
-				packet.from=*id;
+				if (event.channelID==1) {
+					//some config message
+					ERR_CONTINUE( event.packet->dataLength < 8);
+
+					int msg = decode_uint32(&event.packet->data[0]);
+					int id = decode_uint32(&event.packet->data[4]);
+
+					switch(msg) {
+						case SYSMSG_ADD_PEER: {
+
+							peer_map[id]=NULL;
+							emit_signal("peer_connected",id);
+
+						} break;
+						case SYSMSG_REMOVE_PEER: {
+
+							peer_map.erase(id);
+							emit_signal("peer_disconnected",id);
+						} break;
+					}
+
+					enet_packet_destroy(event.packet);
+				} else if (event.channelID==0){
+
+					Packet packet;
+					packet.packet = event.packet;
+
+					int *id = (int*)event.peer -> data;
+
+					ERR_CONTINUE(event.packet->dataLength<12)
+
+
+					uint32_t source = decode_uint32(&event.packet->data[0]);
+					int target = decode_uint32(&event.packet->data[4]);
+					uint32_t flags = decode_uint32(&event.packet->data[8]);
+
+					packet.from=source;
+
+					if (server) {
+
+						packet.from=*id;
+
+						if (target==0) {
+							//re-send the everyone but sender :|
+
+							incoming_packets.push_back(packet);
+							//and make copies for sending
+							for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) {
+
+								if (uint32_t(E->key())==source) //do not resend to self
+									continue;
+
+								ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags);
+
+								enet_peer_send(E->get(),0,packet2);
+							}
+
+						} else if (target<0) {
+							//to all but one
+
+							//and make copies for sending
+							for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) {
+
+								if (uint32_t(E->key())==source || E->key()==-target) //do not resend to self, also do not send to excluded
+									continue;
+
+								ENetPacket* packet2 = enet_packet_create (packet.packet->data,packet.packet->dataLength,flags);
+
+								enet_peer_send(E->get(),0,packet2);
+							}
+
+							if (-target != 1) {
+								//server is not excluded
+								incoming_packets.push_back(packet);
+							} else {
+								//server is excluded, erase packet
+								enet_packet_destroy(packet.packet);
+							}
+
+						} else if (target==1) {
+							//to myself and only myself
+							incoming_packets.push_back(packet);
+						} else {
+							//to someone else, specifically
+							ERR_CONTINUE(!peer_map.has(source));
+							enet_peer_send(peer_map[source],0,packet.packet);
+						}
+					} else {
+
+						incoming_packets.push_back(packet);
+					}
+
+
+					//destroy packet later..
+				} else {
+					ERR_CONTINUE(true);
+				}
 
-				incoming_packets.push_back(packet);
-				//destroy packet later..
 
 			}break;
 			case ENET_EVENT_TYPE_NONE: {
@@ -154,14 +316,29 @@ bool NetworkedMultiplayerENet::is_server() const {
 
 void NetworkedMultiplayerENet::close_connection() {
 
-	ERR_FAIL_COND(!active);
+	if (!active)
+		return;
 
 	_pop_current_packet();
 
+	bool peers_disconnected=false;
+	for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) {
+		if (E->get()) {
+			enet_peer_disconnect_now(E->get(),unique_id);
+			peers_disconnected=true;
+		}
+	}
+
+	if (peers_disconnected) {
+		enet_host_flush(host);
+		OS::get_singleton()->delay_usec(100); //wait 100ms for disconnection packets to send
+
+	}
+
 	enet_host_destroy(host);
 	active=false;
 	incoming_packets.clear();
-
+	unique_id=1; //server is 1
 	connection_status=CONNECTION_DISCONNECTED;
 }
 
@@ -178,26 +355,18 @@ Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer,int &r_buffe
 	current_packet = incoming_packets.front()->get();
 	incoming_packets.pop_front();
 
-	r_buffer=(const uint8_t**)&current_packet.packet->data;
+	*r_buffer=(const uint8_t*)(&current_packet.packet->data[12]);
 	r_buffer_size=current_packet.packet->dataLength;
 
 	return OK;
 }
 Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_size){
 
-	ERR_FAIL_COND_V(incoming_packets.size()==0,ERR_UNAVAILABLE);
-
-	Map<StringName,ENetPeer*>::Element *E=NULL;
-
-	if (target_peer!=StringName()) {
-		peer_map.find(target_peer);
-		if (!E) {
-			ERR_EXPLAIN("Invalid Target Peer: "+String(target_peer));
-			ERR_FAIL_V(ERR_INVALID_PARAMETER);
-		}
-	}
+	ERR_FAIL_COND_V(!active,ERR_UNCONFIGURED);
+	ERR_FAIL_COND_V(connection_status!=CONNECTION_CONNECTED,ERR_UNCONFIGURED);
 
 	int packet_flags=0;
+
 	switch(transfer_mode) {
 		case TRANSFER_MODE_UNRELIABLE: {
 			packet_flags=ENET_PACKET_FLAG_UNSEQUENCED;
@@ -210,13 +379,53 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_
 		} break;
 	}
 
-	/* Create a reliable packet of size 7 containing "packet\0" */
-	ENetPacket * packet = enet_packet_create (p_buffer,p_buffer_size,packet_flags);
+	Map<int,ENetPeer*>::Element *E=NULL;
+
+	if (target_peer!=0) {
+
+		E = peer_map.find(ABS(target_peer));
+		if (!E) {
+			ERR_EXPLAIN("Invalid Target Peer: "+itos(target_peer));
+			ERR_FAIL_V(ERR_INVALID_PARAMETER);
+		}
+	}
+
+	ENetPacket * packet = enet_packet_create (NULL,p_buffer_size+12,packet_flags);
+	encode_uint32(unique_id,&packet->data[0]); //source ID
+	encode_uint32(target_peer,&packet->data[4]); //dest ID
+	encode_uint32(packet_flags,&packet->data[8]); //dest ID
+	copymem(&packet->data[12],p_buffer,p_buffer_size);
+
+	if (server) {
+
+		if (target_peer==0) {
+			enet_host_broadcast(host,0,packet);
+		} else if (target_peer<0) {
+			//send to all but one
+			//and make copies for sending
+
+			int exclude=-target_peer;
+
+			for (Map<int,ENetPeer*>::Element *F=peer_map.front();F;F=F->next()) {
+
+				if (F->key()==exclude) // exclude packet
+					continue;
 
-	if (target_peer==StringName()) {
-		enet_host_broadcast(host,send_channel,packet);
+				ENetPacket* packet2 = enet_packet_create (packet->data,packet->dataLength,packet_flags);
+
+				enet_peer_send(F->get(),0,packet2);
+			}
+
+			enet_packet_destroy(packet); //original packet no longer needed
+		} else {
+			enet_peer_send (E->get(), 0, packet);
+
+		}
 	} else {
-		enet_peer_send (E->get(), send_channel, packet);
+
+		ERR_FAIL_COND_V(!peer_map.has(1),ERR_BUG);
+		enet_peer_send (peer_map[1], 0, packet); //send to server for broadcast..
+
 	}
 
 	enet_host_flush(host);
@@ -234,7 +443,7 @@ void NetworkedMultiplayerENet::_pop_current_packet() const {
 	if (current_packet.packet) {
 		enet_packet_destroy(current_packet.packet);
 		current_packet.packet=NULL;
-		current_packet.from=StringName();
+		current_packet.from=0;
 	}
 
 }
@@ -244,11 +453,54 @@ NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connect
 	return connection_status;
 }
 
+uint32_t NetworkedMultiplayerENet::_gen_unique_id() const {
+
+	uint32_t hash = 0;
+
+	while (hash==0 || hash==1) {
+
+		hash = hash_djb2_one_32(
+					(uint32_t)OS::get_singleton()->get_ticks_usec() );
+		hash = hash_djb2_one_32(
+					(uint32_t)OS::get_singleton()->get_unix_time(), hash );
+		hash = hash_djb2_one_32(
+					(uint32_t)OS::get_singleton()->get_data_dir().hash64(), hash );
+		//hash = hash_djb2_one_32(
+		//			(uint32_t)OS::get_singleton()->get_unique_ID().hash64(), hash );
+		hash = hash_djb2_one_32(
+					(uint32_t)((uint64_t)this), hash ); //rely on aslr heap
+		hash = hash_djb2_one_32(
+					(uint32_t)((uint64_t)&hash), hash ); //rely on aslr stack
+
+		hash=hash&0x7FFFFFFF; // make it compatible with unsigned, since negatie id is used for exclusion
+	}
+
+	return hash;
+}
+
+int NetworkedMultiplayerENet::get_unique_id() const {
+
+	ERR_FAIL_COND_V(!active,0);
+	return unique_id;
+}
+
+void NetworkedMultiplayerENet::set_refuse_new_connections(bool p_enable) {
+
+	refuse_connections=p_enable;
+}
+
+bool NetworkedMultiplayerENet::is_refusing_new_connections() const {
+
+	return refuse_connections;
+}
+
+
 void NetworkedMultiplayerENet::_bind_methods() {
 
-	ObjectTypeDB::bind_method(_MD("create_server","port","max_clients","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_server,DEFVAL(32),DEFVAL(1),DEFVAL(0),DEFVAL(0));
-	ObjectTypeDB::bind_method(_MD("create_client","ip","port","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_client,DEFVAL(1),DEFVAL(0),DEFVAL(0));
-	ObjectTypeDB::bind_method(_MD("disconnect"),&NetworkedMultiplayerENet::disconnect);
+	ObjectTypeDB::bind_method(_MD("create_server","port","max_clients","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_server,DEFVAL(32),DEFVAL(0),DEFVAL(0));
+	ObjectTypeDB::bind_method(_MD("create_client","ip","port","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_client,DEFVAL(0),DEFVAL(0));
+	ObjectTypeDB::bind_method(_MD("close_connection"),&NetworkedMultiplayerENet::close_connection);
+
 
 }
 
@@ -257,7 +509,9 @@ NetworkedMultiplayerENet::NetworkedMultiplayerENet(){
 
 	active=false;
 	server=false;
-	send_channel=0;
+	refuse_connections=false;
+	unique_id=0;
+	target_peer=0;
 	current_packet.packet=NULL;
 	transfer_mode=TRANSFER_MODE_ORDERED;
 	connection_status=CONNECTION_DISCONNECTED;
@@ -265,7 +519,5 @@ NetworkedMultiplayerENet::NetworkedMultiplayerENet(){
 
 NetworkedMultiplayerENet::~NetworkedMultiplayerENet(){
 
-	if (active) {
-		close_connection();
-	}
+	close_connection();
 }

+ 22 - 11
modules/enet/networked_multiplayer_enet.h

@@ -8,32 +8,40 @@ class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
 
 	OBJ_TYPE(NetworkedMultiplayerENet,NetworkedMultiplayerPeer)
 
+	enum {
+		SYSMSG_ADD_PEER,
+		SYSMSG_REMOVE_PEER
+	};
+
 	bool active;
 	bool server;
 
-	int send_channel;
-	StringName target_peer;
+	uint32_t unique_id;
+
+	int target_peer;
 	TransferMode transfer_mode;
 
 	ENetEvent event;
 	ENetPeer *peer;
 	ENetHost *host;
 
+	bool refuse_connections;
+
 	ConnectionStatus connection_status;
 
-	Map<StringName,ENetPeer*> peer_map;
+	Map<int,ENetPeer*> peer_map;
 
 	struct Packet {
 
 		ENetPacket *packet;
-		int from_channel;
-		StringName from;
+		int from;
 	};
 
 	mutable List<Packet> incoming_packets;
 
 	mutable Packet current_packet;
 
+	uint32_t _gen_unique_id() const;
 	void _pop_current_packet() const;
 
 protected:
@@ -41,16 +49,14 @@ protected:
 public:
 
 	virtual void set_transfer_mode(TransferMode p_mode);
-	virtual void set_target_peer(const StringName& p_peer);
-	virtual void set_channel(int p_channel);
+	virtual void set_target_peer(int p_peer);
 
 
-	virtual StringName get_packet_peer() const;
-	virtual int get_packet_channel() const;
+	virtual int get_packet_peer() const;
 
 
-	Error create_server(int p_port, int p_max_clients=32, int p_max_channels=1, int p_in_bandwidth=0, int p_out_bandwidth=0);
-	Error create_client(const IP_Address& p_ip,int p_port, int p_max_channels=1, int p_in_bandwidth=0, int p_out_bandwidth=0);
+	Error create_server(int p_port, int p_max_peers=32, int p_in_bandwidth=0, int p_out_bandwidth=0);
+	Error create_client(const IP_Address& p_ip, int p_port, int p_in_bandwidth=0, int p_out_bandwidth=0);
 
 	void close_connection();
 
@@ -66,6 +72,11 @@ public:
 
 	virtual ConnectionStatus get_connection_status() const;
 
+	virtual void set_refuse_new_connections(bool p_enable);
+	virtual bool is_refusing_new_connections() const;
+
+	virtual int get_unique_id() const;
+
 	NetworkedMultiplayerENet();
 	~NetworkedMultiplayerENet();
 };

+ 1 - 0
modules/enet/protocol.c

@@ -9,6 +9,7 @@
 #include "enet/time.h"
 #include "enet/enet.h"
 
+
 static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
 {
     0,

+ 5 - 1
modules/gdscript/gd_compiler.cpp

@@ -1297,8 +1297,10 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
 		gdfunc = p_script->member_functions[func_name];
 	//}
 
-	if (p_func)
+	if (p_func) {
 		gdfunc->_static=p_func->_static;
+		gdfunc->rpc_mode=p_func->rpc_mode;
+	}
 
 #ifdef TOOLS_ENABLED
 	gdfunc->arg_names=argnames;
@@ -1625,6 +1627,8 @@ Error GDCompiler::_parse_class(GDScript *p_script, GDScript *p_owner, const GDPa
 		minfo.index = p_script->member_indices.size();
 		minfo.setter = p_class->variables[i].setter;
 		minfo.getter = p_class->variables[i].getter;
+		minfo.rpc_mode=p_class->variables[i].rpc_mode;
+
 		p_script->member_indices[name]=minfo;
 		p_script->members.insert(name);
 

+ 25 - 3
modules/gdscript/gd_editor.cpp

@@ -1463,7 +1463,7 @@ static void _make_function_hint(const GDParser::FunctionNode* p_func,int p_argid
 }
 
 
-static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const StringName& p_method,const GDCompletionIdentifier& id, int p_argidx, Set<String>& result, String& arghint) {
+static void _find_type_arguments(GDCompletionContext& context,const GDParser::Node*p_node,int p_line,const StringName& p_method,const GDCompletionIdentifier& id, int p_argidx, Set<String>& result, String& arghint) {
 
 
 	//print_line("find type arguments?");
@@ -1700,9 +1700,31 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
 				if (p_argidx==0) {
 					List<MethodInfo> sigs;
 					ObjectTypeDB::get_signal_list(id.obj_type,&sigs);
+
+					if (id.script.is_valid()) {
+						id.script->get_script_signal_list(&sigs);
+					} else if (id.value.get_type()==Variant::OBJECT) {
+						Object *obj = id.value;
+						if (obj && !obj->get_script().is_null()) {
+							Ref<Script> scr=obj->get_script();
+							if (scr.is_valid()) {
+								scr->get_script_signal_list(&sigs);
+							}
+						}
+					}
+
 					for (List<MethodInfo>::Element *E=sigs.front();E;E=E->next()) {
 						result.insert("\""+E->get().name+"\"");
 					}
+
+				} else if (p_argidx==2){
+
+
+					if (context._class) {
+						for(int i=0;i<context._class->functions.size();i++) {
+							result.insert("\""+context._class->functions[i]->name+"\"");
+						}
+					}
 				}
 				/*if (p_argidx==2) {
 
@@ -1944,7 +1966,7 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
 						if (!context._class->owner)
 							ci.value=context.base;
 
-						_find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
+						_find_type_arguments(context,p_node,p_line,id->name,ci,p_argidx,result,arghint);
 						//guess type..
 						/*
 						List<MethodInfo> methods;
@@ -1967,7 +1989,7 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
 			GDCompletionIdentifier ci;
 			if (_guess_expression_type(context,op->arguments[0],p_line,ci)) {
 
-				_find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
+				_find_type_arguments(context,p_node,p_line,id->name,ci,p_argidx,result,arghint);
 				return;
 			}
 

+ 1 - 0
modules/gdscript/gd_function.cpp

@@ -1309,6 +1309,7 @@ GDFunction::GDFunction() : function_list(this) {
 
 	_stack_size=0;
 	_call_size=0;
+	rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
 	name="<anonymous>";
 #ifdef DEBUG_ENABLED
 	_func_cname=NULL;

+ 12 - 0
modules/gdscript/gd_function.h

@@ -7,6 +7,7 @@
 #include "variant.h"
 #include "string_db.h"
 #include "reference.h"
+#include "script_language.h"
 
 class GDInstance;
 class GDScript;
@@ -64,6 +65,14 @@ public:
 		ADDR_TYPE_NIL=8
 	};
 
+	enum RPCMode {
+		RPC_DISABLED,
+		RPC_ENABLED,
+		RPC_SYNC,
+		RPC_SYNC_MASTER,
+		RPC_SYNC_SLAVE
+	};
+
     struct StackDebug {
 
 	int line;
@@ -91,6 +100,8 @@ friend class GDCompiler;
 	int _call_size;
 	int _initial_line;
 	bool _static;
+	ScriptInstance::RPCMode rpc_mode;
+
 	GDScript *_script;
 
 	StringName name;
@@ -185,6 +196,7 @@ public:
 
 	Variant call(GDInstance *p_instance,const Variant **p_args, int p_argcount,Variant::CallError& r_err,CallState *p_state=NULL);
 
+	_FORCE_INLINE_ ScriptInstance::RPCMode get_rpc_mode() const { return rpc_mode; }
 	GDFunction();
 	~GDFunction();
 };

+ 92 - 7
modules/gdscript/gd_parser.cpp

@@ -2075,6 +2075,7 @@ void GDParser::_parse_class(ClassNode *p_class) {
 		if (error_set)
 			return;
 
+
 		if (indent_level>tab_level.back()->get()) {
 			p_class->end_line=tokenizer->get_token_line();
 			return; //go back a level
@@ -2371,6 +2372,9 @@ void GDParser::_parse_class(ClassNode *p_class) {
 				function->_static=_static;
 				function->line=fnline;
 
+				function->rpc_mode=rpc_mode;
+				rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
+
 
 				if (_static)
 					p_class->static_functions.push_back(function);
@@ -2842,25 +2846,101 @@ void GDParser::_parse_class(ClassNode *p_class) {
 
 				}
 
-				if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
+				if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_ONREADY && tokenizer->get_token()!=GDTokenizer::TK_PR_REMOTE && tokenizer->get_token()!=GDTokenizer::TK_PR_MASTER && tokenizer->get_token()!=GDTokenizer::TK_PR_SLAVE && tokenizer->get_token()!=GDTokenizer::TK_PR_SYNC) {
 
 					current_export=PropertyInfo();
-					_set_error("Expected 'var'.");
+					_set_error("Expected 'var', 'onready', 'remote', 'master', 'slave' or 'sync'.");
 					return;
 				}
 
-			}; //fallthrough to var
+				continue;
+			} break;
 			case GDTokenizer::TK_PR_ONREADY: {
 
-				if (token==GDTokenizer::TK_PR_ONREADY) {
-					//may be fallthrough from export, ignore if so
-					tokenizer->advance();
+				//may be fallthrough from export, ignore if so
+				tokenizer->advance();
+				if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
+					_set_error("Expected 'var'.");
+					return;
+				}
+
+				continue;
+			} break;
+			case GDTokenizer::TK_PR_REMOTE: {
+
+				//may be fallthrough from export, ignore if so
+				tokenizer->advance();
+				if (current_export.type)  {
 					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
 						_set_error("Expected 'var'.");
 						return;
 					}
+
+				} else {
+					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
+						_set_error("Expected 'var' or 'func'.");
+						return;
+					}
 				}
-			}; //fallthrough to var
+				rpc_mode=ScriptInstance::RPC_MODE_REMOTE;
+
+				continue;
+			} break;
+			case GDTokenizer::TK_PR_MASTER: {
+
+				//may be fallthrough from export, ignore if so
+				tokenizer->advance();
+				if (current_export.type)  {
+					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
+						_set_error("Expected 'var'.");
+						return;
+					}
+
+				} else {
+					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
+						_set_error("Expected 'var' or 'func'.");
+						return;
+					}
+				}
+
+				rpc_mode=ScriptInstance::RPC_MODE_MASTER;
+				continue;
+			} break;
+			case GDTokenizer::TK_PR_SLAVE: {
+
+				//may be fallthrough from export, ignore if so
+				tokenizer->advance();
+				if (current_export.type)  {
+					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
+						_set_error("Expected 'var'.");
+						return;
+					}
+
+				} else {
+					if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
+						_set_error("Expected 'var' or 'func'.");
+						return;
+					}
+				}
+
+				rpc_mode=ScriptInstance::RPC_MODE_SLAVE;
+				continue;
+			} break;
+			case GDTokenizer::TK_PR_SYNC: {
+
+				//may be fallthrough from export, ignore if so
+				tokenizer->advance();
+				if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
+					if (current_export.type)
+						_set_error("Expected 'var'.");
+					else
+						_set_error("Expected 'var' or 'func'.");
+					return;
+				}
+
+				rpc_mode=ScriptInstance::RPC_MODE_SYNC;
+				continue;
+			} break;
 			case GDTokenizer::TK_PR_VAR: {
 				//variale declaration and (eventual) initialization
 
@@ -2884,8 +2964,12 @@ void GDParser::_parse_class(ClassNode *p_class) {
 				member.expression=NULL;
 				member._export.name=member.identifier;
 				member.line=tokenizer->get_token_line();
+				member.rpc_mode=rpc_mode;
+
 				tokenizer->advance();
 
+				rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
+
 				if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
 
 #ifdef DEBUG_ENABLED
@@ -3228,6 +3312,7 @@ void GDParser::clear() {
 	current_class=NULL;
 
 	completion_found=false;
+	rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
 
 	current_function=NULL;
 

+ 7 - 1
modules/gdscript/gd_parser.h

@@ -33,6 +33,7 @@
 #include "gd_functions.h"
 #include "map.h"
 #include "object.h"
+#include "script_language.h"
 
 class GDParser {
 public:
@@ -88,6 +89,7 @@ public:
 			StringName getter;
 			int line;
 			Node *expression;
+			ScriptInstance::RPCMode rpc_mode;
 		};
 		struct Constant {
 			StringName identifier;
@@ -119,12 +121,13 @@ public:
 	struct FunctionNode : public Node {
 
 		bool _static;
+		ScriptInstance::RPCMode rpc_mode;
 		StringName name;
 		Vector<StringName> arguments;
 		Vector<Node*> default_values;
 		BlockNode *body;
 
-		FunctionNode() { type=TYPE_FUNCTION; _static=false; }
+		FunctionNode() { type=TYPE_FUNCTION; _static=false; rpc_mode=ScriptInstance::RPC_MODE_DISABLED; }
 
 	};
 
@@ -429,6 +432,9 @@ private:
 
 	PropertyInfo current_export;
 
+	ScriptInstance::RPCMode rpc_mode;
+
+
 	void _set_error(const String& p_error, int p_line=-1, int p_column=-1);
 	bool _recover_from_completion();
 

+ 45 - 1
modules/gdscript/gd_script.cpp

@@ -250,7 +250,7 @@ void GDScript::_update_placeholder(PlaceHolderScriptInstance *p_placeholder) {
 #endif
 
 
-void GDScript::get_method_list(List<MethodInfo> *p_list) const {
+void GDScript::get_script_method_list(List<MethodInfo> *p_list) const {
 
 	for (const Map<StringName,GDFunction*>::Element *E=member_functions.front();E;E=E->next()) {
 		MethodInfo mi;
@@ -1300,6 +1300,46 @@ ScriptLanguage *GDInstance::get_language() {
 	return GDScriptLanguage::get_singleton();
 }
 
+GDInstance::RPCMode GDInstance::get_rpc_mode(const StringName& p_method) const {
+
+	const GDScript *cscript = script.ptr();
+
+	while(cscript) {
+		const Map<StringName,GDFunction*>::Element *E=cscript->member_functions.find(p_method);
+		if (E) {
+
+			if (E->get()->get_rpc_mode()!=RPC_MODE_DISABLED) {
+				return E->get()->get_rpc_mode();
+			}
+
+		}
+		cscript=cscript->_base;
+	}
+
+	return RPC_MODE_DISABLED;
+}
+
+GDInstance::RPCMode GDInstance::get_rset_mode(const StringName& p_variable) const {
+
+	const GDScript *cscript = script.ptr();
+
+	while(cscript) {
+		const Map<StringName,GDScript::MemberInfo>::Element *E=cscript->member_indices.find(p_variable);
+		if (E) {
+
+			if (E->get().rpc_mode) {
+				return E->get().rpc_mode;
+			}
+
+		}
+		cscript=cscript->_base;
+	}
+
+	return RPC_MODE_DISABLED;
+}
+
+
+
 void GDInstance::reload_members() {
 
 #ifdef DEBUG_ENABLED
@@ -1811,6 +1851,10 @@ void GDScriptLanguage::get_reserved_words(List<String> *p_words) const  {
 		"pass",
 		"return",
 		"while",
+		"remote",
+		"sync",
+		"master",
+		"slave",
 		0};
 
 

+ 53 - 48
modules/gdscript/gd_script.h

@@ -64,6 +64,7 @@ class GDScript : public Script {
 		int index;
 		StringName setter;
 		StringName getter;
+		ScriptInstance::RPCMode rpc_mode;
 	};
 
 friend class GDInstance;
@@ -181,7 +182,7 @@ public:
 
 	bool get_property_default_value(const StringName& p_property,Variant& r_value) const;
 
-	virtual void get_method_list(List<MethodInfo> *p_list) const;
+	virtual void get_script_method_list(List<MethodInfo> *p_list) const;
 	virtual bool has_method(const StringName& p_method) const;
 	virtual MethodInfo get_method_info(const StringName& p_method) const;
 
@@ -236,6 +237,10 @@ public:
 
 	void reload_members();
 
+	virtual RPCMode get_rpc_mode(const StringName& p_method) const;
+	virtual RPCMode get_rset_mode(const StringName& p_variable) const;
+
+
 	GDInstance();
 	~GDInstance();
 
@@ -250,23 +255,23 @@ class GDScriptLanguage : public ScriptLanguage {
 	Map<StringName,int> globals;
 
 
-    struct CallLevel {
+	struct CallLevel {
 
-        Variant *stack;
-        GDFunction *function;
-        GDInstance *instance;
-        int *ip;
-        int *line;
+		Variant *stack;
+		GDFunction *function;
+		GDInstance *instance;
+		int *ip;
+		int *line;
 
-    };
+	};
 
 
-    int _debug_parse_err_line;
-    String _debug_parse_err_file;
-    String _debug_error;
-    int _debug_call_stack_pos;
-    int _debug_max_call_stack;
-    CallLevel *_call_stack;
+	int _debug_parse_err_line;
+	String _debug_parse_err_file;
+	String _debug_error;
+	int _debug_call_stack_pos;
+	int _debug_max_call_stack;
+	CallLevel *_call_stack;
 
 	void _add_global(const StringName& p_name,const Variant& p_value);
 
@@ -288,54 +293,54 @@ public:
 
 	int calls;
 
-    bool debug_break(const String& p_error,bool p_allow_continue=true);
-    bool debug_break_parse(const String& p_file, int p_line,const String& p_error);
+	bool debug_break(const String& p_error,bool p_allow_continue=true);
+	bool debug_break_parse(const String& p_file, int p_line,const String& p_error);
 
-    _FORCE_INLINE_ void enter_function(GDInstance *p_instance,GDFunction *p_function, Variant *p_stack, int *p_ip, int *p_line) {
+	_FORCE_INLINE_ void enter_function(GDInstance *p_instance,GDFunction *p_function, Variant *p_stack, int *p_ip, int *p_line) {
 
-        if (Thread::get_main_ID()!=Thread::get_caller_ID())
-            return; //no support for other threads than main for now
+		if (Thread::get_main_ID()!=Thread::get_caller_ID())
+			return; //no support for other threads than main for now
 
-        if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
-            ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() +1 );
+		if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
+			ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() +1 );
 
-        if (_debug_call_stack_pos >= _debug_max_call_stack) {
-            //stack overflow
-            _debug_error="Stack Overflow (Stack Size: "+itos(_debug_max_call_stack)+")";
-            ScriptDebugger::get_singleton()->debug(this);
-            return;
-	}
+		if (_debug_call_stack_pos >= _debug_max_call_stack) {
+			//stack overflow
+			_debug_error="Stack Overflow (Stack Size: "+itos(_debug_max_call_stack)+")";
+			ScriptDebugger::get_singleton()->debug(this);
+			return;
+		}
 
-        _call_stack[_debug_call_stack_pos].stack=p_stack;
-        _call_stack[_debug_call_stack_pos].instance=p_instance;
-        _call_stack[_debug_call_stack_pos].function=p_function;
-        _call_stack[_debug_call_stack_pos].ip=p_ip;
-        _call_stack[_debug_call_stack_pos].line=p_line;
-        _debug_call_stack_pos++;
-    }
+		_call_stack[_debug_call_stack_pos].stack=p_stack;
+		_call_stack[_debug_call_stack_pos].instance=p_instance;
+		_call_stack[_debug_call_stack_pos].function=p_function;
+		_call_stack[_debug_call_stack_pos].ip=p_ip;
+		_call_stack[_debug_call_stack_pos].line=p_line;
+		_debug_call_stack_pos++;
+	}
 
-    _FORCE_INLINE_ void exit_function() {
+	_FORCE_INLINE_ void exit_function() {
 
-        if (Thread::get_main_ID()!=Thread::get_caller_ID())
-            return; //no support for other threads than main for now
+		if (Thread::get_main_ID()!=Thread::get_caller_ID())
+			return; //no support for other threads than main for now
 
-        if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
-	    ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() -1 );
+		if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
+			ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() -1 );
 
-        if (_debug_call_stack_pos==0) {
+		if (_debug_call_stack_pos==0) {
 
-            _debug_error="Stack Underflow (Engine Bug)";
-            ScriptDebugger::get_singleton()->debug(this);
-            return;
-        }
+			_debug_error="Stack Underflow (Engine Bug)";
+			ScriptDebugger::get_singleton()->debug(this);
+			return;
+		}
 
-        _debug_call_stack_pos--;
-    }
+		_debug_call_stack_pos--;
+	}
 
 
 	virtual Vector<StackInfo> debug_get_current_stack_info() {
-	    if (Thread::get_main_ID()!=Thread::get_caller_ID())
-		return Vector<StackInfo>();
+		if (Thread::get_main_ID()!=Thread::get_caller_ID())
+			return Vector<StackInfo>();
 
 		Vector<StackInfo> csi;
 		csi.resize(_debug_call_stack_pos);

+ 9 - 1
modules/gdscript/gd_tokenizer.cpp

@@ -100,6 +100,10 @@ const char* GDTokenizer::token_names[TK_MAX]={
 "yield",
 "signal",
 "breakpoint",
+"rpc",
+"sync",
+"master",
+"slave",
 "'['",
 "']'",
 "'{'",
@@ -865,6 +869,10 @@ void GDTokenizerText::_advance() {
 								{TK_PR_YIELD,"yield"},
 								{TK_PR_SIGNAL,"signal"},
 								{TK_PR_BREAKPOINT,"breakpoint"},
+								{TK_PR_REMOTE,"remote"},
+								{TK_PR_MASTER,"master"},
+								{TK_PR_SLAVE,"slave"},
+								{TK_PR_SYNC,"sync"},
 								{TK_PR_CONST,"const"},
 								//controlflow
 								{TK_CF_IF,"if"},
@@ -1047,7 +1055,7 @@ void GDTokenizerText::advance(int p_amount) {
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////
 
-#define BYTECODE_VERSION 10
+#define BYTECODE_VERSION 11
 
 Error GDTokenizerBuffer::set_code_buffer(const Vector<uint8_t> & p_buffer) {
 

+ 4 - 0
modules/gdscript/gd_tokenizer.h

@@ -107,6 +107,10 @@ public:
 		TK_PR_YIELD,
 		TK_PR_SIGNAL,
 		TK_PR_BREAKPOINT,
+		TK_PR_REMOTE,
+		TK_PR_SYNC,
+		TK_PR_MASTER,
+		TK_PR_SLAVE,
 		TK_BRACKET_OPEN,
 		TK_BRACKET_CLOSE,
 		TK_CURLY_BRACKET_OPEN,

+ 11 - 2
modules/visual_script/visual_script.cpp

@@ -992,7 +992,7 @@ bool VisualScript::get_property_default_value(const StringName& p_property,Varia
 	r_value=variables[ script_variable_remap[p_property] ].default_value;
 	return true;
 }
-void VisualScript::get_method_list(List<MethodInfo> *p_list) const {
+void VisualScript::get_script_method_list(List<MethodInfo> *p_list) const {
 
 	for (Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
 
@@ -1767,7 +1767,7 @@ Variant VisualScriptInstance::_call_internal(const StringName& p_method, void* p
 }
 
 
-Variant VisualScriptInstance::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error){
+Variant VisualScriptInstance::call(const StringName& p_method, const Variant** p_args, int p_argcount, Variant::CallError &r_error){
 
 	r_error.error=Variant::CallError::CALL_OK; //ok by default
 
@@ -1871,6 +1871,15 @@ Ref<Script> VisualScriptInstance::get_script() const{
 	return script;
 }
 
+ScriptInstance::RPCMode VisualScriptInstance::get_rpc_mode(const StringName& p_method) const {
+
+	return RPC_MODE_DISABLED;
+}
+ScriptInstance::RPCMode VisualScriptInstance::get_rset_mode(const StringName& p_variable) const {
+
+	return RPC_MODE_DISABLED;
+}
+
 
 void VisualScriptInstance::create(const Ref<VisualScript>& p_script,Object *p_owner) {
 

+ 4 - 1
modules/visual_script/visual_script.h

@@ -320,7 +320,7 @@ public:
 	virtual void get_script_signal_list(List<MethodInfo> *r_signals) const;
 
 	virtual bool get_property_default_value(const StringName& p_property,Variant& r_value) const;
-	virtual void get_method_list(List<MethodInfo> *p_list) const;
+	virtual void get_script_method_list(List<MethodInfo> *p_list) const;
 
 	virtual bool has_method(const StringName& p_method) const;
 	virtual MethodInfo get_method_info(const StringName& p_method) const;
@@ -413,6 +413,9 @@ public:
 
 	virtual ScriptLanguage *get_language();
 
+	virtual RPCMode get_rpc_mode(const StringName& p_method) const;
+	virtual RPCMode get_rset_mode(const StringName& p_variable) const;
+
 	VisualScriptInstance();
 	~VisualScriptInstance();
 };

+ 3 - 3
modules/visual_script/visual_script_func_nodes.cpp

@@ -1951,7 +1951,7 @@ PropertyInfo VisualScriptScriptCall::get_input_value_port_info(int p_idx) const{
 			return PropertyInfo();
 
 		List<MethodInfo> functions;
-		script->get_method_list(&functions);
+		script->get_script_method_list(&functions);
 		for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
 			if (E->get().name==function) {
 				if (p_idx<0 || p_idx>=E->get().arguments.size())
@@ -2118,7 +2118,7 @@ void VisualScriptScriptCall::_validate_property(PropertyInfo& property) const {
 			Ref<VisualScript> vs = get_visual_script();
 			if (vs.is_valid()) {
 
-				vs->get_method_list(&methods);
+				vs->get_script_method_list(&methods);
 
 			}
 		} else {
@@ -2130,7 +2130,7 @@ void VisualScriptScriptCall::_validate_property(PropertyInfo& property) const {
 			if (!script.is_valid())
 				return;
 
-			script->get_method_list(&methods);
+			script->get_script_method_list(&methods);
 
 		}
 

+ 506 - 50
scene/main/node.cpp

@@ -37,6 +37,7 @@
 
 VARIANT_ENUM_CAST(Node::PauseMode);
 VARIANT_ENUM_CAST(Node::NetworkMode);
+VARIANT_ENUM_CAST(Node::RPCMode);
 
 
 
@@ -494,41 +495,57 @@ bool Node::is_network_master() const {
 	return false;
 }
 
-void Node::allow_remote_call(const StringName& p_method) {
 
-	data.allowed_remote_calls.insert(p_method);
-}
 
-void Node::disallow_remote_call(const StringName& p_method){
+void Node::_propagate_network_owner(Node*p_owner) {
 
-	data.allowed_remote_calls.erase(p_method);
+	if (data.network_mode!=NETWORK_MODE_INHERIT)
+		return;
+	data.network_owner=p_owner;
+	for(int i=0;i<data.children.size();i++) {
 
+		data.children[i]->_propagate_network_owner(p_owner);
+	}
 }
 
-void Node::allow_remote_set(const StringName& p_property){
+/***** RPC CONFIG ********/
 
-	data.allowed_remote_set.insert(p_property);
+void Node::rpc_config(const StringName& p_method,RPCMode p_mode) {
 
+	if (p_mode==RPC_MODE_DISABLED) {
+		data.rpc_methods.erase(p_method);
+	} else {
+		data.rpc_methods[p_method]=p_mode;
+	};
 }
-void Node::disallow_remote_set(const StringName& p_property){
 
-	data.allowed_remote_set.erase(p_property);
+void Node::rset_config(const StringName& p_property,RPCMode p_mode) {
+
+	if (p_mode==RPC_MODE_DISABLED) {
+		data.rpc_properties.erase(p_property);
+	} else {
+		data.rpc_properties[p_property]=p_mode;
+	};
 }
 
+/***** RPC FUNCTIONS ********/
 
-void Node::_propagate_network_owner(Node*p_owner) {
+void Node::rpc(const StringName& p_method,VARIANT_ARG_DECLARE) {
 
-	if (data.network_mode!=NETWORK_MODE_INHERIT)
-		return;
-	data.network_owner=p_owner;
-	for(int i=0;i<data.children.size();i++) {
+	VARIANT_ARGPTRS;
 
-		data.children[i]->_propagate_network_owner(p_owner);
+	int argc=0;
+	for(int i=0;i<VARIANT_ARG_MAX;i++) {
+		if (argptr[i]->get_type()==Variant::NIL)
+			break;
+		argc++;
 	}
+
+	rpcp(0,false,p_method,argptr,argc);
 }
 
-void Node::remote_call_reliable(const StringName& p_method,VARIANT_ARG_DECLARE) {
 
+void Node::rpc_id(int p_peer_id,const StringName& p_method,VARIANT_ARG_DECLARE) {
 
 	VARIANT_ARGPTRS;
 
@@ -539,16 +556,26 @@ void Node::remote_call_reliable(const StringName& p_method,VARIANT_ARG_DECLARE)
 		argc++;
 	}
 
-	remote_call_reliablep(p_method,argptr,argc);
+	rpcp(p_peer_id,false,p_method,argptr,argc);
 }
 
-void Node::remote_call_reliablep(const StringName& p_method,const Variant** p_arg,int p_argcount){
 
-	ERR_FAIL_COND(!is_inside_tree());
-	get_tree()->_remote_call(this,true,false,p_method,p_arg,p_argcount);
+void Node::rpc_unreliable(const StringName& p_method,VARIANT_ARG_DECLARE) {
+
+	VARIANT_ARGPTRS;
+
+	int argc=0;
+	for(int i=0;i<VARIANT_ARG_MAX;i++) {
+		if (argptr[i]->get_type()==Variant::NIL)
+			break;
+		argc++;
+	}
+
+	rpcp(0,true,p_method,argptr,argc);
 }
 
-void Node::remote_call_unreliable(const StringName& p_method,VARIANT_ARG_DECLARE){
+
+void Node::rpc_unreliable_id(int p_peer_id,const StringName& p_method,VARIANT_ARG_DECLARE) {
 
 	VARIANT_ARGPTRS;
 
@@ -559,35 +586,67 @@ void Node::remote_call_unreliable(const StringName& p_method,VARIANT_ARG_DECLARE
 		argc++;
 	}
 
-	remote_call_unreliablep(p_method,argptr,argc);
-
+	rpcp(p_peer_id,true,p_method,argptr,argc);
 }
 
-void Node::remote_call_unreliablep(const StringName& p_method,const Variant** p_arg,int p_argcount){
 
-	ERR_FAIL_COND(!is_inside_tree());
+Variant Node::_rpc_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
 
-	get_tree()->_remote_call(this,false,false,p_method,p_arg,p_argcount);
-}
+	if (p_argcount<1) {
+		r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+		r_error.argument=1;
+		return Variant();
+	}
 
-void Node::remote_set_reliable(const StringName& p_property,const Variant& p_value) {
+	if (p_args[0]->get_type()!=Variant::STRING) {
+		r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+		r_error.argument=0;
+		r_error.expected=Variant::STRING;
+		return Variant();
+	}
 
+	StringName method = *p_args[0];
 
-	ERR_FAIL_COND(!is_inside_tree());
-	const Variant *ptr=&p_value;
+	rpcp(0,false,method,&p_args[1],p_argcount-1);
 
-	get_tree()->_remote_call(this,true,true,p_property,&ptr,1);
+	r_error.error=Variant::CallError::CALL_OK;
+	return Variant();
 }
 
-void Node::remote_set_unreliable(const StringName& p_property,const Variant& p_value){
 
-	ERR_FAIL_COND(!is_inside_tree());
-	const Variant *ptr=&p_value;
+Variant Node::_rpc_id_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
+
+	if (p_argcount<2) {
+		r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+		r_error.argument=2;
+		return Variant();
+	}
 
-	get_tree()->_remote_call(this,false,true,p_property,&ptr,1);
+	if (p_args[0]->get_type()!=Variant::INT) {
+		r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+		r_error.argument=0;
+		r_error.expected=Variant::INT;
+		return Variant();
+	}
+
+	if (p_args[1]->get_type()!=Variant::STRING) {
+		r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+		r_error.argument=1;
+		r_error.expected=Variant::STRING;
+		return Variant();
+	}
+
+	int peer_id = *p_args[0];
+	StringName method = *p_args[1];
+
+	rpcp(peer_id,false,method,&p_args[2],p_argcount-2);
+
+	r_error.error=Variant::CallError::CALL_OK;
+	return Variant();
 }
 
-Variant Node::_remote_call_reliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
+
+Variant Node::_rpc_unreliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
 
 	if (p_argcount<1) {
 		r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
@@ -604,12 +663,46 @@ Variant Node::_remote_call_reliable_bind(const Variant** p_args, int p_argcount,
 
 	StringName method = *p_args[0];
 
-	remote_call_reliablep(method,&p_args[1],p_argcount-1);
+	rpcp(0,true,method,&p_args[1],p_argcount-1);
 
+	r_error.error=Variant::CallError::CALL_OK;
+	return Variant();
 }
 
 
-Variant Node::_remote_call_unreliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
+Variant Node::_rpc_unreliable_id_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
+
+	if (p_argcount<2) {
+		r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+		r_error.argument=2;
+		return Variant();
+	}
+
+	if (p_args[0]->get_type()!=Variant::INT) {
+		r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+		r_error.argument=0;
+		r_error.expected=Variant::INT;
+		return Variant();
+	}
+
+	if (p_args[1]->get_type()!=Variant::STRING) {
+		r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+		r_error.argument=1;
+		r_error.expected=Variant::STRING;
+		return Variant();
+	}
+
+	int peer_id = *p_args[0];
+	StringName method = *p_args[1];
+
+	rpcp(peer_id,true,method,&p_args[2],p_argcount-2);
+
+	r_error.error=Variant::CallError::CALL_OK;
+	return Variant();
+}
+
+#if 0
+Variant Node::_rpc_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
 
 	if (p_argcount<1) {
 		r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
@@ -626,8 +719,356 @@ Variant Node::_remote_call_unreliable_bind(const Variant** p_args, int p_argcoun
 
 	StringName method = *p_args[0];
 
-	remote_call_unreliablep(method,&p_args[1],p_argcount-1);
+	rpcp(method,&p_args[1],p_argcount-1);
 
+	r_error.error=Variant::CallError::CALL_OK;
+	return Variant();
+}
+
+#endif
+void Node::rpcp(int p_peer_id,bool p_unreliable,const StringName& p_method,const Variant** p_arg,int p_argcount) {
+
+	ERR_FAIL_COND(!is_inside_tree());
+
+	bool skip_rpc=false;
+
+	if (p_peer_id==0 || p_peer_id==get_tree()->get_network_unique_id() || (p_peer_id<0 && p_peer_id!=-get_tree()->get_network_unique_id())) {
+		//check that send mode can use local call
+
+
+		bool call_local=false;
+
+
+
+		Map<StringName,RPCMode>::Element *E = data.rpc_methods.find(p_method);
+		if (E) {
+
+			switch(E->get()) {
+
+				case RPC_MODE_DISABLED: {
+					//do nothing
+				} break;
+				case RPC_MODE_REMOTE: {
+					//do nothing also, no need to call local
+				} break;
+				case RPC_MODE_SYNC: {
+					//call it, sync always results in call
+					call_local=true;
+				} break;
+				case RPC_MODE_MASTER: {
+					call_local=is_network_master();
+					if (call_local) {
+						skip_rpc=true; //no other master so..
+					}
+				} break;
+				case RPC_MODE_SLAVE: {
+					call_local=!is_network_master();
+				} break;
+
+			}
+		}
+
+
+		if (call_local) {
+			Variant::CallError ce;
+			call(p_method,p_arg,p_argcount,ce);
+			if (ce.error!=Variant::CallError::CALL_OK) {
+				String error = Variant::get_call_error_text(this,p_method,p_arg,p_argcount,ce);
+				error="rpc() aborted in local call:  - "+error;
+				ERR_PRINTS(error);
+				return;
+			}
+		} else if (get_script_instance()){
+			//attempt with script
+			ScriptInstance::RPCMode rpc_mode = get_script_instance()->get_rpc_mode(p_method);
+
+			switch(rpc_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
+					call_local=true;
+				} break;
+				case ScriptInstance::RPC_MODE_MASTER: {
+					call_local=is_network_master();
+					if (call_local) {
+						skip_rpc=true; //no other master so..
+					}
+				} break;
+				case ScriptInstance::RPC_MODE_SLAVE: {
+					call_local=!is_network_master();
+				} break;
+			}
+
+			if (call_local) {
+				Variant::CallError ce;
+				ce.error=Variant::CallError::CALL_OK;
+				get_script_instance()->call(p_method,p_arg,p_argcount,ce);
+				if (ce.error!=Variant::CallError::CALL_OK) {
+					String error = Variant::get_call_error_text(this,p_method,p_arg,p_argcount,ce);
+					error="rpc() aborted in script local call:  - "+error;
+					ERR_PRINTS(error);
+					return;
+				}
+			}
+		}
+	}
+
+	if (skip_rpc)
+		return;
+
+
+	get_tree()->_rpc(this,p_peer_id,p_unreliable,false,p_method,p_arg,p_argcount);
+
+}
+
+
+/******** RSET *********/
+
+
+void Node::rsetp(int p_peer_id,bool p_unreliable,const StringName& p_property,const Variant& p_value) {
+
+	ERR_FAIL_COND(!is_inside_tree());
+
+	bool skip_rset=false;
+
+	if (p_peer_id==0 || p_peer_id==get_tree()->get_network_unique_id() || (p_peer_id<0 && p_peer_id!=-get_tree()->get_network_unique_id())) {
+		//check that send mode can use local call
+
+
+		bool set_local=false;
+
+		Map<StringName,RPCMode>::Element *E = data.rpc_properties.find(p_property);
+		if (E) {
+
+			switch(E->get()) {
+
+				case RPC_MODE_DISABLED: {
+					//do nothing
+				} break;
+				case RPC_MODE_REMOTE: {
+					//do nothing also, no need to call local
+				} break;
+				case RPC_MODE_SYNC: {
+					//call it, sync always results in call
+					set_local=true;
+				} break;
+				case RPC_MODE_MASTER: {
+					set_local=is_network_master();
+					if (set_local) {
+						skip_rset=true;
+					}
+
+				} break;
+				case RPC_MODE_SLAVE: {
+					set_local=!is_network_master();
+				} break;
+
+			}
+		}
+
+
+		if (set_local) {
+			bool valid;
+			set(p_property,p_value,&valid);
+
+			if (!valid) {
+				String error="rset() aborted in local set, property not found:  - "+String(p_property);
+				ERR_PRINTS(error);
+				return;
+			}
+		} else if (get_script_instance()){
+			//attempt with script
+			ScriptInstance::RPCMode rpc_mode = get_script_instance()->get_rset_mode(p_property);
+
+			switch(rpc_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
+					set_local=true;
+				} break;
+				case ScriptInstance::RPC_MODE_MASTER: {
+					set_local=is_network_master();
+					if (set_local) {
+						skip_rset=true;
+					}
+				} break;
+				case ScriptInstance::RPC_MODE_SLAVE: {
+					set_local=!is_network_master();
+				} break;
+			}
+
+			if (set_local) {
+
+				bool valid = get_script_instance()->set(p_property,p_value);
+
+				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;
+
+	get_tree()->_rpc(this,p_peer_id,p_unreliable,true,p_property,&vptr,1);
+
+}
+
+
+
+void Node::rset(const StringName& p_property,const Variant& p_value) {
+
+	rsetp(0,false,p_property,p_value);
+
+}
+
+void Node::rset_id(int p_peer_id,const StringName& p_property,const Variant& p_value) {
+
+	rsetp(p_peer_id,false,p_property,p_value);
+
+}
+
+void Node::rset_unreliable(const StringName& p_property,const Variant& p_value) {
+
+	rsetp(0,true,p_property,p_value);
+
+}
+
+void Node::rset_unreliable_id(int p_peer_id,const StringName& p_property,const Variant& p_value) {
+
+	rsetp(p_peer_id,true,p_property,p_value);
+
+}
+
+//////////// end of rpc
+
+bool Node::can_call_rpc(const StringName& p_method) const {
+
+	const Map<StringName,RPCMode>::Element *E = data.rpc_methods.find(p_method);
+	if (E) {
+
+		switch(E->get()) {
+
+			case RPC_MODE_DISABLED: {
+				return false;
+			} break;
+			case RPC_MODE_REMOTE: {
+				return true;
+			} break;
+			case RPC_MODE_SYNC: {
+				return true;
+			} break;
+			case RPC_MODE_MASTER: {
+				return is_network_master();
+			} break;
+			case RPC_MODE_SLAVE: {
+				return !is_network_master();
+			} break;
+		}
+	}
+
+
+	if (get_script_instance()){
+		//attempt with script
+		ScriptInstance::RPCMode rpc_mode = get_script_instance()->get_rpc_mode(p_method);
+
+		switch(rpc_mode) {
+
+			case ScriptInstance::RPC_MODE_DISABLED: {
+				return false;
+			} break;
+			case ScriptInstance::RPC_MODE_REMOTE: {
+				return true;
+			} break;
+			case ScriptInstance::RPC_MODE_SYNC: {
+				return true;
+			} break;
+			case ScriptInstance::RPC_MODE_MASTER: {
+				return is_network_master();
+			} break;
+			case ScriptInstance::RPC_MODE_SLAVE: {
+				return !is_network_master();
+			} break;
+		}
+
+	}
+
+	ERR_PRINTS("RPC on unauthorized method attempted: "+String(p_method)+" on base: "+String(Variant(this)));
+	return false;
+}
+
+bool Node::can_call_rset(const StringName& p_property) const {
+
+	const Map<StringName,RPCMode>::Element *E = data.rpc_properties.find(p_property);
+	if (E) {
+
+		switch(E->get()) {
+
+			case RPC_MODE_DISABLED: {
+				return false;
+			} break;
+			case RPC_MODE_REMOTE: {
+				return true;
+			} break;
+			case RPC_MODE_SYNC: {
+				return true;
+			} break;
+			case RPC_MODE_MASTER: {
+				return is_network_master();
+			} break;
+			case RPC_MODE_SLAVE: {
+				return !is_network_master();
+			} break;
+		}
+	}
+
+
+	if (get_script_instance()){
+		//attempt with script
+		ScriptInstance::RPCMode rpc_mode = get_script_instance()->get_rset_mode(p_property);
+
+		switch(rpc_mode) {
+
+			case ScriptInstance::RPC_MODE_DISABLED: {
+				return false;
+			} break;
+			case ScriptInstance::RPC_MODE_REMOTE: {
+				return true;
+			} break;
+			case ScriptInstance::RPC_MODE_SYNC: {
+				return true;
+			} break;
+			case ScriptInstance::RPC_MODE_MASTER: {
+				return is_network_master();
+			} break;
+			case ScriptInstance::RPC_MODE_SLAVE: {
+				return !is_network_master();
+			} break;
+		}
+
+	}
+
+	ERR_PRINTS("RSET on unauthorized property attempted: "+String(p_property)+" on base: "+String(Variant(this)));
+
+	return false;
 }
 
 
@@ -2444,11 +2885,9 @@ void Node::_bind_methods() {
 
 	ObjectTypeDB::bind_method(_MD("is_network_master"),&Node::is_network_master);
 
-	ObjectTypeDB::bind_method(_MD("allow_remote_call","method"),&Node::allow_remote_call);
-	ObjectTypeDB::bind_method(_MD("disallow_remote_call","method"),&Node::disallow_remote_call);
+	ObjectTypeDB::bind_method(_MD("rpc_config","method","mode"),&Node::rpc_config);
+	ObjectTypeDB::bind_method(_MD("rset_config","property","mode"),&Node::rset_config);
 
-	ObjectTypeDB::bind_method(_MD("allow_remote_set","method"),&Node::allow_remote_set);
-	ObjectTypeDB::bind_method(_MD("disallow_remote_set","method"),&Node::disallow_remote_set);
 
 #ifdef TOOLS_ENABLED
 	ObjectTypeDB::bind_method(_MD("_set_import_path","import_path"),&Node::set_import_path);
@@ -2459,19 +2898,29 @@ void Node::_bind_methods() {
 
 	{
 		MethodInfo mi;
-		mi.name="remote_call_reliable";
+
 		mi.arguments.push_back( PropertyInfo( Variant::STRING, "method"));
 
 
-		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"remote_call_reliable",&Node::_remote_call_reliable_bind,mi);
+		mi.name="rpc";
+		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"rpc",&Node::_rpc_bind,mi);
+		mi.name="rpc_unreliable";
+		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"rpc_unreliable",&Node::_rpc_unreliable_bind,mi);
+
+		mi.arguments.push_front( PropertyInfo( Variant::INT, "peer_") );
+
+		mi.name="rpc_id";
+		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"rpc_id",&Node::_rpc_id_bind,mi);
+		mi.name="rpc_unreliable_id";
+		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"rpc_unreliable_id",&Node::_rpc_unreliable_id_bind,mi);
 
-		mi.name="remote_call_unreliable";
-		ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"remote_call_unreliable",&Node::_remote_call_unreliable_bind,mi);
 
 	}
 
-	ObjectTypeDB::bind_method(_MD("remote_set_reliable","property","value:Variant"),&Node::remote_set_reliable);
-	ObjectTypeDB::bind_method(_MD("remote_set_unreliable","property","value:Variant"),&Node::remote_set_unreliable);
+	ObjectTypeDB::bind_method(_MD("rset","property","value:Variant"),&Node::rset);
+	ObjectTypeDB::bind_method(_MD("rset_id","peer_id","property","value:Variant"),&Node::rset_id);
+	ObjectTypeDB::bind_method(_MD("rset_unreliable","property","value:Variant"),&Node::rset_unreliable);
+	ObjectTypeDB::bind_method(_MD("rset_unreliable_id","peer_id","property","value:Variant"),&Node::rset_unreliable_id);
 
 
 	BIND_CONSTANT( NOTIFICATION_ENTER_TREE );
@@ -2491,7 +2940,15 @@ void Node::_bind_methods() {
 	BIND_CONSTANT( NOTIFICATION_PATH_CHANGED);
 
 
+	BIND_CONSTANT( NETWORK_MODE_INHERIT );
+	BIND_CONSTANT( NETWORK_MODE_MASTER );
+	BIND_CONSTANT( NETWORK_MODE_SLAVE );
 
+	BIND_CONSTANT( RPC_MODE_DISABLED );
+	BIND_CONSTANT( RPC_MODE_REMOTE );
+	BIND_CONSTANT( RPC_MODE_SYNC );
+	BIND_CONSTANT( RPC_MODE_MASTER );
+	BIND_CONSTANT( RPC_MODE_SLAVE );
 
 	BIND_CONSTANT( PAUSE_MODE_INHERIT );
 	BIND_CONSTANT( PAUSE_MODE_STOP );
@@ -2506,7 +2963,6 @@ void Node::_bind_methods() {
 	//ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/input" ), _SCS("set_process_input"),_SCS("is_processing_input" ) );
 	//ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/unhandled_input" ), _SCS("set_process_unhandled_input"),_SCS("is_processing_unhandled_input" ) );
 	ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "process/pause_mode",PROPERTY_HINT_ENUM,"Inherit,Stop,Process" ), _SCS("set_pause_mode"),_SCS("get_pause_mode" ) );
-	ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "process/network_mode",PROPERTY_HINT_ENUM,"Inherit,Master,Slave" ), _SCS("set_network_mode"),_SCS("get_network_mode" ) );
 	ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "editor/display_folded",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR ), _SCS("set_display_folded"),_SCS("is_displayed_folded" ) );
 
 	BIND_VMETHOD( MethodInfo("_process",PropertyInfo(Variant::REAL,"delta")) );

+ 31 - 14
scene/main/node.h

@@ -60,6 +60,15 @@ public:
 		NETWORK_MODE_SLAVE
 	};
 
+	enum RPCMode {
+
+		RPC_MODE_DISABLED, //no rpc for this method, calls to this will be blocked (default)
+		RPC_MODE_REMOTE, // using rpc() on it will call method / set property in all other peers
+		RPC_MODE_SYNC, // using rpc() on it will call method / set property in all other peers and locally
+		RPC_MODE_MASTER, // usinc rpc() on it will call method on wherever the master is, be it local or remote
+		RPC_MODE_SLAVE, // usinc rpc() on it will call method for all slaves, be it local or remote
+	};
+
 	struct Comparator {
 
 		bool operator()(const Node* p_a, const Node* p_b) const { return p_b->is_greater_than(p_a); }
@@ -75,6 +84,7 @@ private:
 	};
 
 
+
 	struct Data {
 
 		String filename;
@@ -108,8 +118,8 @@ private:
 
 		NetworkMode network_mode;
 		Node *network_owner;
-		Set<StringName> allowed_remote_calls;
-		Set<StringName> allowed_remote_set;
+		Map<StringName,RPCMode> rpc_methods;
+		Map<StringName,RPCMode> rpc_properties;
 
 
 		// variables used to properly sort the node when processing, ignored otherwise
@@ -160,8 +170,10 @@ private:
 	Array _get_children() const;
 	Array _get_groups() const;
 
-	Variant _remote_call_reliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
-	Variant _remote_call_unreliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
+	Variant _rpc_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
+	Variant _rpc_unreliable_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
+	Variant _rpc_id_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
+	Variant _rpc_unreliable_id_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
 
 friend class SceneTree;
 
@@ -358,20 +370,25 @@ public:
 	NetworkMode get_network_mode() const;
 	bool is_network_master() const;
 
-	void allow_remote_call(const StringName& p_method);
-	void disallow_remote_call(const StringName& p_method);
+	void rpc_config(const StringName& p_method,RPCMode p_mode); // config a local method for RPC
+	void rset_config(const StringName& p_property,RPCMode p_mode); // config a local property for RPC
+
+	void rpc(const StringName& p_method,VARIANT_ARG_LIST); //rpc call, honors RPCMode
+	void rpc_unreliable(const StringName& p_method,VARIANT_ARG_LIST); //rpc call, honors RPCMode
+	void rpc_id(int p_peer_id,const StringName& p_method,VARIANT_ARG_LIST); //rpc call, honors RPCMode
+	void rpc_unreliable_id(int p_peer_id,const StringName& p_method,VARIANT_ARG_LIST); //rpc call, honors RPCMode
 
-	void allow_remote_set(const StringName& p_property);
-	void disallow_remote_set(const StringName& p_property);
+	void rpcp(int p_peer_id,bool p_unreliable,const StringName& p_method,const Variant** p_arg,int p_argcount);
 
-	void remote_call_reliable(const StringName& p_method,VARIANT_ARG_DECLARE);
-	void remote_call_reliablep(const StringName& p_method,const Variant** p_arg,int p_argcount);
+	void rset(const StringName& p_property, const Variant& p_value); //remote set call, honors RPCMode
+	void rset_unreliable(const StringName& p_property,const Variant& p_value); //remote set call, honors RPCMode
+	void rset_id(int p_peer_id,const StringName& p_property,const Variant& p_value); //remote set call, honors RPCMode
+	void rset_unreliable_id(int p_peer_id,const StringName& p_property,const Variant& p_value); //remote set call, honors RPCMode
 
-	void remote_call_unreliable(const StringName& p_method,VARIANT_ARG_DECLARE);
-	void remote_call_unreliablep(const StringName& p_method,const Variant** p_arg,int p_argcount);
+	void rsetp(int p_peer_id,bool p_unreliable,const StringName& p_property,const Variant& p_value);
 
-	void remote_set_reliable(const StringName& p_property,const Variant& p_value);
-	void remote_set_unreliable(const StringName& p_property,const Variant& p_value);
+	bool can_call_rpc(const StringName& p_method) const;
+	bool can_call_rset(const StringName& p_property) const;
 
 
 	Node();

+ 97 - 19
scene/main/scene_main_loop.cpp

@@ -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.");
@@ -1753,11 +1796,17 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St
 	//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()) {
+
+		if (p_to<0 && E->get()==-p_to)
+			continue; //continue, excluded
 
-	for (Set<StringName>::Element *E=connected_peers.front();E;E=E->next()) {
+		if (p_to>0 && E->get()!=p_to)
+			continue; //continue, not for this peer
 
-		Map<StringName,bool>::Element *F = psc->confirmed_peers.find(E->get());
+		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
@@ -1773,7 +1822,7 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St
 
 	//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);
@@ -1789,20 +1838,26 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St
 	}
 
 	//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_ORDERED);
 
 	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->set_target_peer(p_to); //to all of you
 		network_peer->put_var(message); //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()) {
+		for (Set<int>::Element *E=connected_peers.front();E;E=E->next()) {
 
-			Map<StringName,bool>::Element *F = psc->confirmed_peers.find(E->get());
+			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
@@ -1821,7 +1876,7 @@ void SceneTree::_remote_call(Node* p_from, bool p_reliable, bool p_set, const St
 }
 
 
-void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_packet) {
+void SceneTree::_network_process_packet(int p_from, const Array& p_packet) {
 
 	ERR_FAIL_COND(p_packet.empty());
 
@@ -1847,7 +1902,7 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_
 
 				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);
@@ -1871,6 +1926,9 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_
 
 			if (packet_type==NETWORK_COMMAND_REMOTE_CALL) {
 
+				if (!node->can_call_rpc(name))
+					return;
+
 				int argc = p_packet.size()-3;
 				Vector<Variant> args;
 				Vector<const Variant*> argp;
@@ -1887,11 +1945,15 @@ 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;
+
 
 				ERR_FAIL_COND(p_packet.size()!=4);
 				Variant value = p_packet[3];
@@ -1938,7 +2000,7 @@ void SceneTree::_network_process_packet(const StringName& p_from,const Array& p_
 			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,14 +2010,17 @@ 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();
+		int sender = network_peer->get_packet_peer();
 		Variant packet;
 		Error err = network_peer->get_var(packet);
 		if (err!=OK) {
@@ -1967,6 +2032,10 @@ void SceneTree::_network_poll() {
 		}
 
 		_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 +2111,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 +2130,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 );

+ 16 - 7
scene/main/scene_main_loop.h

@@ -188,13 +188,17 @@ private:
 
 	Ref<NetworkedMultiplayerPeer> network_peer;
 
-	Set<StringName> connected_peers;
-	void _network_peer_connected(const StringName& p_id);
-	void _network_peer_disconnected(const StringName& p_id);
+	Set<int> connected_peers;
+	void _network_peer_connected(int p_id);
+	void _network_peer_disconnected(int p_id);
+
+	void _connected_to_server();
+	void _connection_failed();
+	void _server_disconnected();
 
 	//path sent caches
 	struct PathSentCache {
-		Map<StringName,bool> confirmed_peers;
+		Map<int,bool> confirmed_peers;
 		int id;
 	};
 
@@ -211,9 +215,9 @@ private:
 		Map<int,NodeInfo> nodes;
 	};
 
-	Map<StringName,PathGetCache> path_get_cache;
+	Map<int,PathGetCache> path_get_cache;
 
-	void _network_process_packet(const StringName &p_from, const Array& p_packet);
+	void _network_process_packet(int p_from, const Array& p_packet);
 	void _network_poll();
 
 	static SceneTree *singleton;
@@ -221,7 +225,8 @@ friend class Node;
 
 
 
-	void _remote_call(Node* p_from,bool p_reliable,bool p_set,const StringName& p_name,const Variant** p_arg,int p_argcount);
+
+	void _rpc(Node* p_from,int p_to,bool p_unreliable,bool p_set,const StringName& p_name,const Variant** p_arg,int p_argcount);
 
 	void tree_changed();
 	void node_removed(Node *p_node);
@@ -418,6 +423,10 @@ public:
 
 	void set_network_peer(const Ref<NetworkedMultiplayerPeer>& p_network_peer);
 	bool is_network_server() const;
+	int get_network_unique_id() const;
+
+	void set_refuse_new_network_connections(bool p_refuse);
+	bool is_refusing_new_network_connections() const;
 
 	SceneTree();
 	~SceneTree();