From 234914c42ceebfb3c8d05b9071248cdd993b673b Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 8 May 2018 10:51:04 +0200 Subject: [PATCH 1/4] Rename multiplayer_api to just multiplayer. Only the class name retain the MultiplayerAPI name --- scene/main/node.cpp | 32 ++++++++++---------- scene/main/node.h | 8 ++--- scene/main/scene_tree.cpp | 62 +++++++++++++++++++-------------------- scene/main/scene_tree.h | 6 ++-- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 67ee2462528..e6ea4e4b4a7 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -477,7 +477,7 @@ bool Node::is_network_master() const { ERR_FAIL_COND_V(!is_inside_tree(), false); - return get_multiplayer_api()->get_network_unique_id() == data.network_master; + return get_multiplayer()->get_network_unique_id() == data.network_master; } /***** RPC CONFIG ********/ @@ -668,12 +668,12 @@ Variant Node::_rpc_unreliable_id_bind(const Variant **p_args, int p_argcount, Va 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()); - get_multiplayer_api()->rpcp(this, p_peer_id, p_unreliable, p_method, p_arg, p_argcount); + get_multiplayer()->rpcp(this, p_peer_id, p_unreliable, p_method, p_arg, p_argcount); } void Node::rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) { ERR_FAIL_COND(!is_inside_tree()); - get_multiplayer_api()->rsetp(this, p_peer_id, p_unreliable, p_property, p_value); + get_multiplayer()->rsetp(this, p_peer_id, p_unreliable, p_property, p_value); } /******** RSET *********/ @@ -698,21 +698,21 @@ void Node::rset_unreliable_id(int p_peer_id, const StringName &p_property, const } //////////// end of rpc -Ref Node::get_multiplayer_api() const { - if (multiplayer_api.is_valid()) - return multiplayer_api; +Ref Node::get_multiplayer() const { + if (multiplayer.is_valid()) + return multiplayer; if (!is_inside_tree()) return Ref(); - return get_tree()->get_multiplayer_api(); + return get_tree()->get_multiplayer(); } -Ref Node::get_custom_multiplayer_api() const { - return multiplayer_api; +Ref Node::get_custom_multiplayer() const { + return multiplayer; } -void Node::set_custom_multiplayer_api(Ref p_multiplayer_api) { +void Node::set_custom_multiplayer(Ref p_multiplayer) { - multiplayer_api = p_multiplayer_api; + multiplayer = p_multiplayer; } const Map::Element *Node::get_node_rpc_mode(const StringName &p_method) { @@ -2748,9 +2748,9 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("is_network_master"), &Node::is_network_master); - ClassDB::bind_method(D_METHOD("get_multiplayer_api"), &Node::get_multiplayer_api); - ClassDB::bind_method(D_METHOD("get_custom_multiplayer_api"), &Node::get_custom_multiplayer_api); - ClassDB::bind_method(D_METHOD("set_custom_multiplayer_api", "api"), &Node::set_custom_multiplayer_api); + ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer); + ClassDB::bind_method(D_METHOD("get_custom_multiplayer"), &Node::get_custom_multiplayer); + ClassDB::bind_method(D_METHOD("set_custom_multiplayer", "api"), &Node::set_custom_multiplayer); ClassDB::bind_method(D_METHOD("rpc_config", "method", "mode"), &Node::rpc_config); ClassDB::bind_method(D_METHOD("rset_config", "property", "mode"), &Node::rset_config); @@ -2830,8 +2830,8 @@ void Node::_bind_methods() { ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "", 0), "set_name", "get_name"); ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "filename", PROPERTY_HINT_NONE, "", 0), "set_filename", "get_filename"); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_owner", "get_owner"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "multiplayer_api", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "", "get_multiplayer_api"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "custom_multiplayer_api", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_custom_multiplayer_api", "get_custom_multiplayer_api"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "", "get_multiplayer"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "custom_multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_custom_multiplayer", "get_custom_multiplayer"); BIND_VMETHOD(MethodInfo("_process", PropertyInfo(Variant::REAL, "delta"))); BIND_VMETHOD(MethodInfo("_physics_process", PropertyInfo(Variant::REAL, "delta"))); diff --git a/scene/main/node.h b/scene/main/node.h index b5a956116d0..4ff1247e141 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -151,7 +151,7 @@ private: NAME_CASING_SNAKE_CASE }; - Ref multiplayer_api; + Ref multiplayer; void _print_tree_pretty(const String prefix, const bool last); void _print_tree(const Node *p_node); @@ -419,9 +419,9 @@ public: void rpcp(int p_peer_id, bool p_unreliable, 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); - Ref get_multiplayer_api() const; - Ref get_custom_multiplayer_api() const; - void set_custom_multiplayer_api(Ref p_multiplayer_api); + Ref get_multiplayer() const; + Ref get_custom_multiplayer() const; + void set_custom_multiplayer(Ref p_multiplayer); const Map::Element *get_node_rpc_mode(const StringName &p_method); const Map::Element *get_node_rset_mode(const StringName &p_property); diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index 011087b4877..607dbebf6cb 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -485,7 +485,7 @@ bool SceneTree::idle(float p_time) { idle_process_time = p_time; - multiplayer_api->poll(); + multiplayer->poll(); emit_signal("idle_frame"); @@ -1668,70 +1668,70 @@ void SceneTree::_server_disconnected() { emit_signal("server_disconnected"); } -Ref SceneTree::get_multiplayer_api() const { - return multiplayer_api; +Ref SceneTree::get_multiplayer() const { + return multiplayer; } -void SceneTree::set_multiplayer_api(Ref p_multiplayer_api) { - ERR_FAIL_COND(!p_multiplayer_api.is_valid()); +void SceneTree::set_multiplayer(Ref p_multiplayer) { + ERR_FAIL_COND(!p_multiplayer.is_valid()); - if (multiplayer_api.is_valid()) { - multiplayer_api->disconnect("network_peer_connected", this, "_network_peer_connected"); - multiplayer_api->disconnect("network_peer_disconnected", this, "_network_peer_disconnected"); - multiplayer_api->disconnect("connected_to_server", this, "_connected_to_server"); - multiplayer_api->disconnect("connection_failed", this, "_connection_failed"); - multiplayer_api->disconnect("server_disconnected", this, "_server_disconnected"); + if (multiplayer.is_valid()) { + multiplayer->disconnect("network_peer_connected", this, "_network_peer_connected"); + multiplayer->disconnect("network_peer_disconnected", this, "_network_peer_disconnected"); + multiplayer->disconnect("connected_to_server", this, "_connected_to_server"); + multiplayer->disconnect("connection_failed", this, "_connection_failed"); + multiplayer->disconnect("server_disconnected", this, "_server_disconnected"); } - multiplayer_api = p_multiplayer_api; - multiplayer_api->set_root_node(root); + multiplayer = p_multiplayer; + multiplayer->set_root_node(root); - multiplayer_api->connect("network_peer_connected", this, "_network_peer_connected"); - multiplayer_api->connect("network_peer_disconnected", this, "_network_peer_disconnected"); - multiplayer_api->connect("connected_to_server", this, "_connected_to_server"); - multiplayer_api->connect("connection_failed", this, "_connection_failed"); - multiplayer_api->connect("server_disconnected", this, "_server_disconnected"); + multiplayer->connect("network_peer_connected", this, "_network_peer_connected"); + multiplayer->connect("network_peer_disconnected", this, "_network_peer_disconnected"); + multiplayer->connect("connected_to_server", this, "_connected_to_server"); + multiplayer->connect("connection_failed", this, "_connection_failed"); + multiplayer->connect("server_disconnected", this, "_server_disconnected"); } void SceneTree::set_network_peer(const Ref &p_network_peer) { - multiplayer_api->set_network_peer(p_network_peer); + multiplayer->set_network_peer(p_network_peer); } Ref SceneTree::get_network_peer() const { - return multiplayer_api->get_network_peer(); + return multiplayer->get_network_peer(); } bool SceneTree::is_network_server() const { - return multiplayer_api->is_network_server(); + return multiplayer->is_network_server(); } bool SceneTree::has_network_peer() const { - return multiplayer_api->has_network_peer(); + return multiplayer->has_network_peer(); } int SceneTree::get_network_unique_id() const { - return multiplayer_api->get_network_unique_id(); + return multiplayer->get_network_unique_id(); } Vector SceneTree::get_network_connected_peers() const { - return multiplayer_api->get_network_connected_peers(); + return multiplayer->get_network_connected_peers(); } int SceneTree::get_rpc_sender_id() const { - return multiplayer_api->get_rpc_sender_id(); + return multiplayer->get_rpc_sender_id(); } void SceneTree::set_refuse_new_network_connections(bool p_refuse) { - multiplayer_api->set_refuse_new_network_connections(p_refuse); + multiplayer->set_refuse_new_network_connections(p_refuse); } bool SceneTree::is_refusing_new_network_connections() const { - return multiplayer_api->is_refusing_new_network_connections(); + return multiplayer->is_refusing_new_network_connections(); } void SceneTree::_bind_methods() { @@ -1800,8 +1800,8 @@ void SceneTree::_bind_methods() { ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene); - ClassDB::bind_method(D_METHOD("set_multiplayer_api", "multiplayer_api"), &SceneTree::set_multiplayer_api); - ClassDB::bind_method(D_METHOD("get_multiplayer_api"), &SceneTree::get_multiplayer_api); + ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer"), &SceneTree::set_multiplayer); + ClassDB::bind_method(D_METHOD("get_multiplayer"), &SceneTree::get_multiplayer); ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &SceneTree::set_network_peer); ClassDB::bind_method(D_METHOD("get_network_peer"), &SceneTree::get_network_peer); ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server); @@ -1829,7 +1829,7 @@ void SceneTree::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_current_scene", "get_current_scene"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "", "get_root"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer_api", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer_api", "get_multiplayer_api"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer", "get_multiplayer"); ADD_SIGNAL(MethodInfo("tree_changed")); ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node"))); @@ -1934,7 +1934,7 @@ SceneTree::SceneTree() { root->set_world(Ref(memnew(World))); // Initialize network state - set_multiplayer_api(Ref(memnew(MultiplayerAPI))); + set_multiplayer(Ref(memnew(MultiplayerAPI))); //root->set_world_2d( Ref( memnew( World2D ))); root->set_as_audio_listener(true); diff --git a/scene/main/scene_tree.h b/scene/main/scene_tree.h index 9c06e4ded34..6e0156546e4 100644 --- a/scene/main/scene_tree.h +++ b/scene/main/scene_tree.h @@ -185,7 +185,7 @@ private: ///network/// - Ref multiplayer_api; + Ref multiplayer; void _network_peer_connected(int p_id); void _network_peer_disconnected(int p_id); @@ -410,8 +410,8 @@ public: //network API - Ref get_multiplayer_api() const; - void set_multiplayer_api(Ref p_multiplayer_api); + Ref get_multiplayer() const; + void set_multiplayer(Ref p_multiplayer); void set_network_peer(const Ref &p_network_peer); Ref get_network_peer() const; bool is_network_server() const; From e391b493d5630d90e7e41c142900dc65ebb438bd Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 8 May 2018 11:49:57 +0200 Subject: [PATCH 2/4] Internal methods in MultiplayerAPI start with _ --- core/io/multiplayer_api.cpp | 40 ++++++++++++++++++------------------- core/io/multiplayer_api.h | 10 +++++----- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index cbe7f87d928..0ede7e87cb3 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -47,11 +47,11 @@ void MultiplayerAPI::set_root_node(Node *p_node) { void MultiplayerAPI::set_network_peer(const Ref &p_peer) { if (network_peer.is_valid()) { - network_peer->disconnect("peer_connected", this, "add_peer"); - network_peer->disconnect("peer_disconnected", this, "del_peer"); - network_peer->disconnect("connection_succeeded", this, "connected_to_server"); - network_peer->disconnect("connection_failed", this, "connection_failed"); - network_peer->disconnect("server_disconnected", this, "server_disconnected"); + network_peer->disconnect("peer_connected", this, "_add_peer"); + network_peer->disconnect("peer_disconnected", this, "_del_peer"); + network_peer->disconnect("connection_succeeded", this, "_connected_to_server"); + network_peer->disconnect("connection_failed", this, "_connection_failed"); + network_peer->disconnect("server_disconnected", this, "_server_disconnected"); clear(); } @@ -61,11 +61,11 @@ void MultiplayerAPI::set_network_peer(const Ref &p_pee ERR_FAIL_COND(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED); if (network_peer.is_valid()) { - network_peer->connect("peer_connected", this, "add_peer"); - network_peer->connect("peer_disconnected", this, "del_peer"); - network_peer->connect("connection_succeeded", this, "connected_to_server"); - network_peer->connect("connection_failed", this, "connection_failed"); - network_peer->connect("server_disconnected", this, "server_disconnected"); + network_peer->connect("peer_connected", this, "_add_peer"); + network_peer->connect("peer_disconnected", this, "_del_peer"); + network_peer->connect("connection_succeeded", this, "_connected_to_server"); + network_peer->connect("connection_failed", this, "_connection_failed"); + network_peer->connect("server_disconnected", this, "_server_disconnected"); } } @@ -458,29 +458,29 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p } } -void MultiplayerAPI::add_peer(int p_id) { +void MultiplayerAPI::_add_peer(int p_id) { connected_peers.insert(p_id); path_get_cache.insert(p_id, PathGetCache()); emit_signal("network_peer_connected", p_id); } -void MultiplayerAPI::del_peer(int p_id) { +void MultiplayerAPI::_del_peer(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 MultiplayerAPI::connected_to_server() { +void MultiplayerAPI::_connected_to_server() { emit_signal("connected_to_server"); } -void MultiplayerAPI::connection_failed() { +void MultiplayerAPI::_connection_failed() { emit_signal("connection_failed"); } -void MultiplayerAPI::server_disconnected() { +void MultiplayerAPI::_server_disconnected() { emit_signal("server_disconnected"); } @@ -691,15 +691,15 @@ void MultiplayerAPI::_bind_methods() { ClassDB::bind_method(D_METHOD("get_network_unique_id"), &MultiplayerAPI::get_network_unique_id); ClassDB::bind_method(D_METHOD("is_network_server"), &MultiplayerAPI::is_network_server); ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &MultiplayerAPI::get_rpc_sender_id); - ClassDB::bind_method(D_METHOD("add_peer", "id"), &MultiplayerAPI::add_peer); - ClassDB::bind_method(D_METHOD("del_peer", "id"), &MultiplayerAPI::del_peer); + ClassDB::bind_method(D_METHOD("_add_peer", "id"), &MultiplayerAPI::_add_peer); + ClassDB::bind_method(D_METHOD("_del_peer", "id"), &MultiplayerAPI::_del_peer); ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &MultiplayerAPI::set_network_peer); ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll); ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear); - ClassDB::bind_method(D_METHOD("connected_to_server"), &MultiplayerAPI::connected_to_server); - ClassDB::bind_method(D_METHOD("connection_failed"), &MultiplayerAPI::connection_failed); - ClassDB::bind_method(D_METHOD("server_disconnected"), &MultiplayerAPI::server_disconnected); + ClassDB::bind_method(D_METHOD("_connected_to_server"), &MultiplayerAPI::_connected_to_server); + ClassDB::bind_method(D_METHOD("_connection_failed"), &MultiplayerAPI::_connection_failed); + ClassDB::bind_method(D_METHOD("_server_disconnected"), &MultiplayerAPI::_server_disconnected); ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &MultiplayerAPI::get_network_connected_peers); ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &MultiplayerAPI::set_refuse_new_network_connections); ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &MultiplayerAPI::is_refusing_new_network_connections); diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h index e7c6ffbea63..cc60890cedd 100644 --- a/core/io/multiplayer_api.h +++ b/core/io/multiplayer_api.h @@ -66,11 +66,11 @@ public: // Called by Node.rset void rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value); - void add_peer(int p_id); - void del_peer(int p_id); - void connected_to_server(); - void connection_failed(); - void server_disconnected(); + void _add_peer(int p_id); + void _del_peer(int p_id); + void _connected_to_server(); + void _connection_failed(); + void _server_disconnected(); bool has_network_peer() const { return network_peer.is_valid(); } Vector get_network_connected_peers() const; From cbb744c4e09c44cd44ab4a21bc3a814c0913f6fe Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 8 May 2018 11:33:25 +0200 Subject: [PATCH 3/4] Document MultiplayerAPI class --- doc/classes/MultiplayerAPI.xml | 57 ++++++++++++++-------------------- doc/classes/Node.xml | 6 ++-- doc/classes/SceneTree.xml | 3 +- 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/doc/classes/MultiplayerAPI.xml b/doc/classes/MultiplayerAPI.xml index 591d715174b..b2ed712554e 100644 --- a/doc/classes/MultiplayerAPI.xml +++ b/doc/classes/MultiplayerAPI.xml @@ -1,88 +1,68 @@ + High Level Multiplayer API. + This class implements most of the logic behind the high level multiplayer API. + By default, [SceneTree] has a reference to this class that is used to provide multiplayer capabilities (i.e. RPC/RSET) across the whole scene. + It is possible to override the MultiplayerAPI instance used by specific Nodes by setting the [member Node.custom_multiplayer] property, effectively allowing to run both client and server in the same scene. - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Clears the current MultiplayerAPI network state (you shouldn't call this unless you know what you are doing). + Returns the peer IDs of all connected peers of this MultiplayerAPI's [member network_peer]. + Returns the unique peer ID of this MultiplayerAPI's [member network_peer]. + Returns the sender's peer ID for the RPC currently being executed. + NOTE: If not inside an RPC this method will return 0. + Returns [code]true[/code] if there is a [member network_peer] set. + Returns [code]true[/code] if this MultiplayerAPI's [member network_peer] is in server mode (listening for connections). - - - - - - + Method used for polling the MultiplayerAPI. + You only need to worry about this if you are using [memeber Node.custom_multplayer] override. + SceneTree will poll the default MultiplayerAPI for you. @@ -91,38 +71,47 @@ + Sets the base root node to use for RPCs. Instead of an absolute path, a relative path will be used to find the node upon which the RPC should be executed. + This effectively allows to have different branches of the scene tree to be managed by different MultiplayerAPI, allowing for example to run both client and server in the same scene. + The peer object to handle the RPC system (effectively enabling networking when set). Depending on the peer itself, the MultiplayerAPI will become a network server (check with [method is_network_server()]) and will set root node's network mode to master (see NETWORK_MODE_* constants in [Node]), or it will become a regular peer with root node set to slave. All child nodes are set to inherit the network mode by default. Handling of networking-related events (connection, disconnection, new clients) is done by connecting to MultiplayerAPI's signals. + If [code]true[/code] the MultiplayerAPI's [member network_peer] refuses new incoming connections. + Emitted whenever this MultiplayerAPI's [member network_peer] successfully connected to a server. Only emitted on clients. + Emitted whenever this MultiplayerAPI's [member network_peer] fails to establish a connection to a server. Only emitted on clients. + Emitted whenever this MultiplayerAPI's [member network_peer] connects with a new peer. ID is the peer ID of the new peer. Clients get notified when other clients connect to the same server. Upon connecting to a server, a client also receives this signal for the server (with ID being 1). + Emitted whenever this MultiplayerAPI's [member network_peer] disconnects from a peer. Clients get notified when other clients disconnect from the same server. + Emitted whenever this MultiplayerAPI's [member network_peer] disconnected from server. Only emitted on clients. diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index d43885a9d9f..c11b8aacaaa 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -762,12 +762,14 @@ - + + The override to the default [MultiplayerAPI]. Set to null to use the default SceneTree one. When a scene is instanced from a file, its topmost node contains the filename from which it was loaded. - + + The [MultiplayerAPI] instance associated with this node. Either the [member custom_multiplayer], or the default SceneTree one (if inside tree). The name of the node. This name is unique among the siblings (other child nodes from the same parent). When set to an existing name, the node will be automatically renamed diff --git a/doc/classes/SceneTree.xml b/doc/classes/SceneTree.xml index dec0cbbcc30..55063bbe243 100644 --- a/doc/classes/SceneTree.xml +++ b/doc/classes/SceneTree.xml @@ -266,7 +266,8 @@ The root of the edited scene. - + + The default [MultiplayerAPI] instance for this SceneTree. The peer object to handle the RPC system (effectively enabling networking when set). Depending on the peer itself, the SceneTree will become a network server (check with [method is_network_server()]) and will set root node's network mode to master (see NETWORK_MODE_* constants in [Node]), or it will become a regular peer with root node set to slave. All child nodes are set to inherit the network mode by default. Handling of networking-related events (connection, disconnection, new clients) is done by connecting to SceneTree's signals. From dd546dc5b86e0bb9c397919fd8bfcf8e8460bc38 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 8 May 2018 14:40:08 +0200 Subject: [PATCH 4/4] Document websocket module, further document enet --- doc/classes/WebSocketClient.xml | 57 --------- doc/classes/WebSocketPeer.xml | 51 -------- doc/classes/WebSocketServer.xml | 69 ----------- .../doc_classes/NetworkedMultiplayerENet.xml | 37 +++++- modules/enet/networked_multiplayer_enet.cpp | 4 +- modules/websocket/config.py | 11 ++ .../websocket/doc_classes/WebSocketClient.xml | 70 +++++++++++ .../doc_classes}/WebSocketMultiplayerPeer.xml | 4 + .../websocket/doc_classes/WebSocketPeer.xml | 75 ++++++++++++ .../websocket/doc_classes/WebSocketServer.xml | 109 ++++++++++++++++++ modules/websocket/websocket_server.cpp | 6 +- 11 files changed, 309 insertions(+), 184 deletions(-) delete mode 100644 doc/classes/WebSocketClient.xml delete mode 100644 doc/classes/WebSocketPeer.xml delete mode 100644 doc/classes/WebSocketServer.xml create mode 100644 modules/websocket/doc_classes/WebSocketClient.xml rename {doc/classes => modules/websocket/doc_classes}/WebSocketMultiplayerPeer.xml (64%) create mode 100644 modules/websocket/doc_classes/WebSocketPeer.xml create mode 100644 modules/websocket/doc_classes/WebSocketServer.xml diff --git a/doc/classes/WebSocketClient.xml b/doc/classes/WebSocketClient.xml deleted file mode 100644 index 496baa57873..00000000000 --- a/doc/classes/WebSocketClient.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/doc/classes/WebSocketPeer.xml b/doc/classes/WebSocketPeer.xml deleted file mode 100644 index 276249e65a8..00000000000 --- a/doc/classes/WebSocketPeer.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/doc/classes/WebSocketServer.xml b/doc/classes/WebSocketServer.xml deleted file mode 100644 index 9a7108952f8..00000000000 --- a/doc/classes/WebSocketServer.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/modules/enet/doc_classes/NetworkedMultiplayerENet.xml b/modules/enet/doc_classes/NetworkedMultiplayerENet.xml index 7bee63019bd..4b2a9df8c48 100644 --- a/modules/enet/doc_classes/NetworkedMultiplayerENet.xml +++ b/modules/enet/doc_classes/NetworkedMultiplayerENet.xml @@ -16,6 +16,8 @@ + + Closes the connection. Ignored if no connection is currently established. If this is a server it tries to notify all clients before forcibly disconnecting them. If this is a client it simply closes the connection to the server. @@ -23,7 +25,7 @@ - + @@ -31,8 +33,10 @@ + + - Create client that connects to a server at address [code]ip[/code] using specified [code]port[/code]. The given IP needs to be in IPv4 or IPv6 address format, for example: [code]192.168.1.1[/code]. The [code]port[/code] is the port the server is listening on. The [code]in_bandwidth[/code] and [code]out_bandwidth[/code] parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returns [code]OK[/code] if a client was created, [code]ERR_ALREADY_IN_USE[/code] if this NetworkedMultiplayerEnet instance already has an open connection (in which case you need to call [method close_connection] first) or [code]ERR_CANT_CREATE[/code] if the client could not be created. + Create client that connects to a server at [code]address[/code] using specified [code]port[/code]. The given address needs to be either a fully qualified domain nome (e.g. [code]www.example.com[/code]) or an IP address in IPv4 or IPv6 format (e.g. [code]192.168.1.1[/code]). The [code]port[/code] is the port the server is listening on. The [code]in_bandwidth[/code] and [code]out_bandwidth[/code] parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returns [code]OK[/code] if a client was created, [code]ERR_ALREADY_IN_USE[/code] if this NetworkedMultiplayerEnet instance already has an open connection (in which case you need to call [method close_connection] first) or [code]ERR_CANT_CREATE[/code] if the client could not be created. If [code]client_port[/code] is specified, the client will also listen to the given port, this is useful in some NAT traveral technique. @@ -50,6 +54,35 @@ Create server that listens to connections via [code]port[/code]. The port needs to be an available, unused port between 0 and 65535. Note that ports below 1024 are privileged and may require elevated permissions depending on the platform. To change the interface the server listens on, use [method set_bind_ip]. The default IP is the wildcard [code]*[/code], which listens on all available interfaces. [code]max_clients[/code] is the maximum number of clients that are allowed at once, any number up to 4096 may be used, although the achievable number of simultaneous clients may be far lower and depends on the application. For additional details on the bandwidth parameters, see [method create_client]. Returns [code]OK[/code] if a server was created, [code]ERR_ALREADY_IN_USE[/code] if this NetworkedMultiplayerEnet instance already has an open connection (in which case you need to call [method close_connection] first) or [code]ERR_CANT_CREATE[/code] if the server could not be created. + + + + + + + + + Disconnect the given peer. If "now" is set to true, the connection will be closed immediately without flushing queued messages. + + + + + + + + + Returns the IP address of the given peer. + + + + + + + + + Returns the remote port of the given peer. + + diff --git a/modules/enet/networked_multiplayer_enet.cpp b/modules/enet/networked_multiplayer_enet.cpp index bd76c766a01..e948df27428 100644 --- a/modules/enet/networked_multiplayer_enet.cpp +++ b/modules/enet/networked_multiplayer_enet.cpp @@ -768,8 +768,8 @@ void NetworkedMultiplayerENet::_bind_methods() { ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode); ClassDB::bind_method(D_METHOD("get_compression_mode"), &NetworkedMultiplayerENet::get_compression_mode); ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &NetworkedMultiplayerENet::set_bind_ip); - ClassDB::bind_method(D_METHOD("get_peer_address"), &NetworkedMultiplayerENet::get_peer_address); - ClassDB::bind_method(D_METHOD("get_peer_port"), &NetworkedMultiplayerENet::get_peer_port); + ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &NetworkedMultiplayerENet::get_peer_address); + ClassDB::bind_method(D_METHOD("get_peer_port", "id"), &NetworkedMultiplayerENet::get_peer_port); ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_mode", PROPERTY_HINT_ENUM, "None,Range Coder,FastLZ,ZLib,ZStd"), "set_compression_mode", "get_compression_mode"); diff --git a/modules/websocket/config.py b/modules/websocket/config.py index fb920482f5f..399ca88fc1b 100644 --- a/modules/websocket/config.py +++ b/modules/websocket/config.py @@ -5,3 +5,14 @@ def can_build(platform): def configure(env): pass + +def get_doc_classes(): + return [ + "WebSocketClient", + "WebSocketMultiplayerPeer", + "WebSocketPeer", + "WebSocketServer" + ] + +def get_doc_path(): + return "doc_classes" diff --git a/modules/websocket/doc_classes/WebSocketClient.xml b/modules/websocket/doc_classes/WebSocketClient.xml new file mode 100644 index 00000000000..2e11e1a44cf --- /dev/null +++ b/modules/websocket/doc_classes/WebSocketClient.xml @@ -0,0 +1,70 @@ + + + + A WebSocket client implementation + + + This class implements a WebSocket client compatible with any RFC 6455 complaint WebSocket server. + This client can be optionally used as a network peer for the [MultiplayerAPI]. + After starting the client ([method connect_to_url]), you will need to [method NetworkedMultiplayerPeer.poll] it at regular intervals (e.g. inside [method Node._process]). + You will received appropriate signals when connecting, disconnecting, or when new data is available. + + + + + + + + + + + + + + + + + Connect to the given URL requesting one of the given [code]protocols[/code] as sub-protocol. + If [code]true[/code] is passed as [code]gd_mp_api[/code], the client will behave like a network peer for the [MultiplayerAPI]. Note: connnections to non Godot servers will not work, and [signal data_received] will not be emitted when this option is true. + + + + + + + Disconnect from the server if currently connected. + + + + + + Enable or disable SSL certificate verification. Note: You must specify the certificates to be used in the project settings for it to work when exported. + + + + + + Emitted when the connection to the server is closed. + + + + + Emitted when the connection to the server fails. + + + + + + + Emitted when a connection with the server is established, [code]protocol[/code] will contain the sub-protocol agreed with the server. + + + + + Emitted when a WebSocket message is received. Note: This signal is NOT emitted when used as high level multiplayer peer. + + + + + + diff --git a/doc/classes/WebSocketMultiplayerPeer.xml b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml similarity index 64% rename from doc/classes/WebSocketMultiplayerPeer.xml rename to modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml index a1014350f5e..1a841f85ed5 100644 --- a/doc/classes/WebSocketMultiplayerPeer.xml +++ b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml @@ -1,8 +1,10 @@ + Base class for WebSocket server and client. + Base class for WebSocket server and client, allowing them to be used as network peer for the [MultiplayerAPI]. @@ -15,6 +17,7 @@ + Returns the [WebSocketPeer] associated to the given [code]peer_id[/code]. @@ -23,6 +26,7 @@ + Emitted when a packet is received from a peer. Note: this signal is only emitted when the client or server is configured to use Godot multiplayer API. diff --git a/modules/websocket/doc_classes/WebSocketPeer.xml b/modules/websocket/doc_classes/WebSocketPeer.xml new file mode 100644 index 00000000000..85a08e0c0b1 --- /dev/null +++ b/modules/websocket/doc_classes/WebSocketPeer.xml @@ -0,0 +1,75 @@ + + + + A class representing a specific WebSocket connection. + + + This class represent a specific WebSocket connection, you can do lower level operations with it. + You can choose to write to the socket in binary or text mode, and you can recognize the mode used for writing by the other peer. + + + + + + + + + + + Close this WebSocket connection, actively disconnecting the peer. + + + + + + + Returns the IP Address of the connected peer. (Not available in HTML5 export) + + + + + + + Returns the remote port of the connected peer. (Not available in HTML5 export) + + + + + + + Get the current selected write mode. See [enum WriteMode]. + + + + + + + Returns [code]true[/code] if this peer is currently connected. + + + + + + + + + Sets the socket to use the given [enum WriteMode]. + + + + + + + Returns [code]true[/code] if the last received packet was sent as a text payload. See [enum WriteMode] + + + + + + Specify that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed). + + + Specify that WebSockets messages should be transferred as binary payload (any byte combination is allowed). + + + diff --git a/modules/websocket/doc_classes/WebSocketServer.xml b/modules/websocket/doc_classes/WebSocketServer.xml new file mode 100644 index 00000000000..a1061e446b7 --- /dev/null +++ b/modules/websocket/doc_classes/WebSocketServer.xml @@ -0,0 +1,109 @@ + + + + A WebSocket server implementation + + + This class implements a WebSocket server that can also support the high level multiplayer API. + After starting the server ([method listen]), you will need to [method NetworkedMultiplayerPeer.poll] it at regular intervals (e.g. inside [method Node._process]). When clients connect, disconnect, or send data, you will receive the appropriate signal. + Note: This class will not work in HTML5 exports due to browser restrictions. + + + + + + + + + + + + + Disconnects the given peer. + + + + + + + + + Returns the IP address of the given peer. + + + + + + + + + Returns the remote port of the given peer. + + + + + + + + + Returns [code]true[/code] if a peer with the given ID is connected. + + + + + + + Returns [code]true[/code] if the server is actively listening on a port. + + + + + + + + + + + + + Start listening on the given port. + You can specify the desired subprotocols via the "protocols" array. If the list empty (default), "binary" will be used. + You can use this server as a network peer for [MultiplayerAPI] by passing true as "gd_mp_api". Note: [signal data_received] will not be fired and clients other than Godot will not work in this case. + + + + + + + Stop the server and clear its state. + + + + + + + + + + + Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client. + + + + + + + Emitted when a client disconnects. + + + + + + + Emitted when a new message is received. Note: This signal is NOT emitted when used as high level multiplayer peer. + + + + + + diff --git a/modules/websocket/websocket_server.cpp b/modules/websocket/websocket_server.cpp index 2693b26e47d..53dd7b51b78 100644 --- a/modules/websocket/websocket_server.cpp +++ b/modules/websocket/websocket_server.cpp @@ -44,9 +44,9 @@ void WebSocketServer::_bind_methods() { ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(PoolVector()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop); ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer); - ClassDB::bind_method(D_METHOD("get_peer_address"), &WebSocketServer::get_peer_address); - ClassDB::bind_method(D_METHOD("get_peer_port"), &WebSocketServer::get_peer_port); - ClassDB::bind_method(D_METHOD("disconnect_peer"), &WebSocketServer::disconnect_peer); + ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketServer::get_peer_address); + ClassDB::bind_method(D_METHOD("get_peer_port", "id"), &WebSocketServer::get_peer_port); + ClassDB::bind_method(D_METHOD("disconnect_peer", "id"), &WebSocketServer::disconnect_peer); ADD_SIGNAL(MethodInfo("client_disconnected", PropertyInfo(Variant::INT, "id"))); ADD_SIGNAL(MethodInfo("client_connected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "protocol")));