summaryrefslogtreecommitdiff
path: root/modules/websocket
diff options
context:
space:
mode:
Diffstat (limited to 'modules/websocket')
-rw-r--r--modules/websocket/doc_classes/WebSocketClient.xml48
-rw-r--r--modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml30
-rw-r--r--modules/websocket/doc_classes/WebSocketPeer.xml42
-rw-r--r--modules/websocket/doc_classes/WebSocketServer.xml83
-rw-r--r--modules/websocket/editor_debugger_server_websocket.cpp18
-rw-r--r--modules/websocket/editor_debugger_server_websocket.h11
-rw-r--r--modules/websocket/emws_client.cpp7
-rw-r--r--modules/websocket/emws_client.h1
-rw-r--r--modules/websocket/emws_peer.cpp31
-rw-r--r--modules/websocket/emws_peer.h5
-rw-r--r--modules/websocket/emws_server.h2
-rw-r--r--modules/websocket/library_godot_websocket.js14
-rw-r--r--modules/websocket/register_types.cpp2
-rw-r--r--modules/websocket/remote_debugger_peer_websocket.h6
-rw-r--r--modules/websocket/websocket_client.cpp27
-rw-r--r--modules/websocket/websocket_multiplayer_peer.cpp50
-rw-r--r--modules/websocket/websocket_multiplayer_peer.h15
-rw-r--r--modules/websocket/websocket_peer.cpp1
-rw-r--r--modules/websocket/websocket_peer.h1
-rw-r--r--modules/websocket/websocket_server.cpp37
-rw-r--r--modules/websocket/websocket_server.h8
-rw-r--r--modules/websocket/wsl_client.cpp35
-rw-r--r--modules/websocket/wsl_client.h2
-rw-r--r--modules/websocket/wsl_peer.cpp12
-rw-r--r--modules/websocket/wsl_peer.h4
-rw-r--r--modules/websocket/wsl_server.cpp43
-rw-r--r--modules/websocket/wsl_server.h8
27 files changed, 293 insertions, 250 deletions
diff --git a/modules/websocket/doc_classes/WebSocketClient.xml b/modules/websocket/doc_classes/WebSocketClient.xml
index d362bcc10f..b5202469f1 100644
--- a/modules/websocket/doc_classes/WebSocketClient.xml
+++ b/modules/websocket/doc_classes/WebSocketClient.xml
@@ -5,27 +5,22 @@
</brief_description>
<description>
This class implements a WebSocket client compatible with any RFC 6455-compliant 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]).
+ This client can be optionally used as a multiplayer peer for the [MultiplayerAPI].
+ After starting the client ([method connect_to_url]), you will need to [method MultiplayerPeer.poll] it at regular intervals (e.g. inside [method Node._process]).
You will receive appropriate signals when connecting, disconnecting, or when new data is available.
</description>
<tutorials>
</tutorials>
<methods>
<method name="connect_to_url">
- <return type="int" enum="Error">
- </return>
- <argument index="0" name="url" type="String">
- </argument>
- <argument index="1" name="protocols" type="PackedStringArray" default="PackedStringArray( )">
- </argument>
- <argument index="2" name="gd_mp_api" type="bool" default="false">
- </argument>
- <argument index="3" name="custom_headers" type="PackedStringArray" default="PackedStringArray( )">
- </argument>
+ <return type="int" enum="Error" />
+ <argument index="0" name="url" type="String" />
+ <argument index="1" name="protocols" type="PackedStringArray" default="PackedStringArray()" />
+ <argument index="2" name="gd_mp_api" type="bool" default="false" />
+ <argument index="3" name="custom_headers" type="PackedStringArray" default="PackedStringArray()" />
<description>
Connects to the given URL requesting one of the given [code]protocols[/code] as sub-protocol. If the list empty (default), no sub-protocol will be requested.
- If [code]true[/code] is passed as [code]gd_mp_api[/code], the client will behave like a network peer for the [MultiplayerAPI], connections to non-Godot servers will not work, and [signal data_received] will not be emitted.
+ If [code]true[/code] is passed as [code]gd_mp_api[/code], the client will behave like a multiplayer peer for the [MultiplayerAPI], connections to non-Godot servers will not work, and [signal data_received] will not be emitted.
If [code]false[/code] is passed instead (default), you must call [PacketPeer] functions ([code]put_packet[/code], [code]get_packet[/code], etc.) on the [WebSocketPeer] returned via [code]get_peer(1)[/code] and not on this object directly (e.g. [code]get_peer(1).put_packet(data)[/code]).
You can optionally pass a list of [code]custom_headers[/code] to be added to the handshake HTTP request.
[b]Note:[/b] To avoid mixed content warnings or errors in HTML5, you may have to use a [code]url[/code] that starts with [code]wss://[/code] (secure) instead of [code]ws://[/code]. When doing so, make sure to use the fully qualified domain name that matches the one defined in the server's SSL certificate. Do not connect directly via the IP address for [code]wss://[/code] connections, as it won't match with the SSL certificate.
@@ -33,26 +28,21 @@
</description>
</method>
<method name="disconnect_from_host">
- <return type="void">
- </return>
- <argument index="0" name="code" type="int" default="1000">
- </argument>
- <argument index="1" name="reason" type="String" default="&quot;&quot;">
- </argument>
+ <return type="void" />
+ <argument index="0" name="code" type="int" default="1000" />
+ <argument index="1" name="reason" type="String" default="&quot;&quot;" />
<description>
Disconnects this client from the connected host. See [method WebSocketPeer.close] for more information.
</description>
</method>
<method name="get_connected_host" qualifiers="const">
- <return type="String">
- </return>
+ <return type="String" />
<description>
Return the IP address of the currently connected host.
</description>
</method>
<method name="get_connected_port" qualifiers="const">
- <return type="int">
- </return>
+ <return type="int" />
<description>
Return the IP port of the currently connected host.
</description>
@@ -70,8 +60,7 @@
</members>
<signals>
<signal name="connection_closed">
- <argument index="0" name="was_clean_close" type="bool">
- </argument>
+ <argument index="0" name="was_clean_close" type="bool" />
<description>
Emitted when the connection to the server is closed. [code]was_clean_close[/code] will be [code]true[/code] if the connection was shutdown cleanly.
</description>
@@ -82,8 +71,7 @@
</description>
</signal>
<signal name="connection_established">
- <argument index="0" name="protocol" type="String">
- </argument>
+ <argument index="0" name="protocol" type="String" />
<description>
Emitted when a connection with the server is established, [code]protocol[/code] will contain the sub-protocol agreed with the server.
</description>
@@ -95,10 +83,8 @@
</description>
</signal>
<signal name="server_close_request">
- <argument index="0" name="code" type="int">
- </argument>
- <argument index="1" name="reason" type="String">
- </argument>
+ <argument index="0" name="code" type="int" />
+ <argument index="1" name="reason" type="String" />
<description>
Emitted when the server requests a clean close. You should keep polling until you get a [signal connection_closed] signal to achieve the clean close. See [method WebSocketPeer.close] for more details.
</description>
diff --git a/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml
index 0679acf78a..c7a0ca100f 100644
--- a/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml
+++ b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml
@@ -1,34 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<class name="WebSocketMultiplayerPeer" inherits="NetworkedMultiplayerPeer" version="4.0">
+<class name="WebSocketMultiplayerPeer" inherits="MultiplayerPeer" version="4.0">
<brief_description>
Base class for WebSocket server and client.
</brief_description>
<description>
- Base class for WebSocket server and client, allowing them to be used as network peer for the [MultiplayerAPI].
+ Base class for WebSocket server and client, allowing them to be used as multiplayer peer for the [MultiplayerAPI].
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_peer" qualifiers="const">
- <return type="WebSocketPeer">
- </return>
- <argument index="0" name="peer_id" type="int">
- </argument>
+ <return type="WebSocketPeer" />
+ <argument index="0" name="peer_id" type="int" />
<description>
Returns the [WebSocketPeer] associated to the given [code]peer_id[/code].
</description>
</method>
<method name="set_buffers">
- <return type="int" enum="Error">
- </return>
- <argument index="0" name="input_buffer_size_kb" type="int">
- </argument>
- <argument index="1" name="input_max_packets" type="int">
- </argument>
- <argument index="2" name="output_buffer_size_kb" type="int">
- </argument>
- <argument index="3" name="output_max_packets" type="int">
- </argument>
+ <return type="int" enum="Error" />
+ <argument index="0" name="input_buffer_size_kb" type="int" />
+ <argument index="1" name="input_max_packets" type="int" />
+ <argument index="2" name="output_buffer_size_kb" type="int" />
+ <argument index="3" name="output_max_packets" type="int" />
<description>
Configures the buffer sizes for this WebSocket peer. Default values can be specified in the Project Settings under [code]network/limits[/code]. For server, values are meant per connected peer.
The first two parameters define the size and queued packets limits of the input buffer, the last two of the output buffer.
@@ -39,12 +32,11 @@
</methods>
<members>
<member name="refuse_new_connections" type="bool" setter="set_refuse_new_connections" getter="is_refusing_new_connections" override="true" default="false" />
- <member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" override="true" enum="NetworkedMultiplayerPeer.TransferMode" default="2" />
+ <member name="transfer_mode" type="int" setter="set_transfer_mode" getter="get_transfer_mode" override="true" enum="TransferMode" default="2" />
</members>
<signals>
<signal name="peer_packet">
- <argument index="0" name="peer_source" type="int">
- </argument>
+ <argument index="0" name="peer_source" type="int" />
<description>
Emitted when a packet is received from a peer.
[b]Note:[/b] 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
index 5125956416..ab7ef6c4d0 100644
--- a/modules/websocket/doc_classes/WebSocketPeer.xml
+++ b/modules/websocket/doc_classes/WebSocketPeer.xml
@@ -11,12 +11,9 @@
</tutorials>
<methods>
<method name="close">
- <return type="void">
- </return>
- <argument index="0" name="code" type="int" default="1000">
- </argument>
- <argument index="1" name="reason" type="String" default="&quot;&quot;">
- </argument>
+ <return type="void" />
+ <argument index="0" name="code" type="int" default="1000" />
+ <argument index="1" name="reason" type="String" default="&quot;&quot;" />
<description>
Closes this WebSocket connection. [code]code[/code] is the status code for the closure (see RFC 6455 section 7.4 for a list of valid status codes). [code]reason[/code] is the human readable reason for closing the connection (can be any UTF-8 string that's smaller than 123 bytes).
[b]Note:[/b] To achieve a clean close, you will need to keep polling until either [signal WebSocketClient.connection_closed] or [signal WebSocketServer.client_disconnected] is received.
@@ -24,57 +21,54 @@
</description>
</method>
<method name="get_connected_host" qualifiers="const">
- <return type="String">
- </return>
+ <return type="String" />
<description>
Returns the IP address of the connected peer.
[b]Note:[/b] Not available in the HTML5 export.
</description>
</method>
<method name="get_connected_port" qualifiers="const">
- <return type="int">
- </return>
+ <return type="int" />
<description>
Returns the remote port of the connected peer.
[b]Note:[/b] Not available in the HTML5 export.
</description>
</method>
+ <method name="get_current_outbound_buffered_amount" qualifiers="const">
+ <return type="int" />
+ <description>
+ Returns the current amount of data in the outbound websocket buffer. [b]Note:[/b] HTML5 exports use WebSocket.bufferedAmount, while other platforms use an internal buffer.
+ </description>
+ </method>
<method name="get_write_mode" qualifiers="const">
- <return type="int" enum="WebSocketPeer.WriteMode">
- </return>
+ <return type="int" enum="WebSocketPeer.WriteMode" />
<description>
Gets the current selected write mode. See [enum WriteMode].
</description>
</method>
<method name="is_connected_to_host" qualifiers="const">
- <return type="bool">
- </return>
+ <return type="bool" />
<description>
Returns [code]true[/code] if this peer is currently connected.
</description>
</method>
<method name="set_no_delay">
- <return type="void">
- </return>
- <argument index="0" name="enabled" type="bool">
- </argument>
+ <return type="void" />
+ <argument index="0" name="enabled" type="bool" />
<description>
Disable Nagle's algorithm on the underling TCP socket (default). See [method StreamPeerTCP.set_no_delay] for more information.
[b]Note:[/b] Not available in the HTML5 export.
</description>
</method>
<method name="set_write_mode">
- <return type="void">
- </return>
- <argument index="0" name="mode" type="int" enum="WebSocketPeer.WriteMode">
- </argument>
+ <return type="void" />
+ <argument index="0" name="mode" type="int" enum="WebSocketPeer.WriteMode" />
<description>
Sets the socket to use the given [enum WriteMode].
</description>
</method>
<method name="was_string_packet" qualifiers="const">
- <return type="bool">
- </return>
+ <return type="bool" />
<description>
Returns [code]true[/code] if the last received packet was sent as a text payload. See [enum WriteMode].
</description>
diff --git a/modules/websocket/doc_classes/WebSocketServer.xml b/modules/websocket/doc_classes/WebSocketServer.xml
index f7805209e2..b66a1054ab 100644
--- a/modules/websocket/doc_classes/WebSocketServer.xml
+++ b/modules/websocket/doc_classes/WebSocketServer.xml
@@ -5,78 +5,62 @@
</brief_description>
<description>
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.
+ After starting the server ([method listen]), you will need to [method MultiplayerPeer.poll] it at regular intervals (e.g. inside [method Node._process]). When clients connect, disconnect, or send data, you will receive the appropriate signal.
[b]Note:[/b] Not available in HTML5 exports.
</description>
<tutorials>
</tutorials>
<methods>
<method name="disconnect_peer">
- <return type="void">
- </return>
- <argument index="0" name="id" type="int">
- </argument>
- <argument index="1" name="code" type="int" default="1000">
- </argument>
- <argument index="2" name="reason" type="String" default="&quot;&quot;">
- </argument>
+ <return type="void" />
+ <argument index="0" name="id" type="int" />
+ <argument index="1" name="code" type="int" default="1000" />
+ <argument index="2" name="reason" type="String" default="&quot;&quot;" />
<description>
Disconnects the peer identified by [code]id[/code] from the server. See [method WebSocketPeer.close] for more information.
</description>
</method>
<method name="get_peer_address" qualifiers="const">
- <return type="String">
- </return>
- <argument index="0" name="id" type="int">
- </argument>
+ <return type="String" />
+ <argument index="0" name="id" type="int" />
<description>
Returns the IP address of the given peer.
</description>
</method>
<method name="get_peer_port" qualifiers="const">
- <return type="int">
- </return>
- <argument index="0" name="id" type="int">
- </argument>
+ <return type="int" />
+ <argument index="0" name="id" type="int" />
<description>
Returns the remote port of the given peer.
</description>
</method>
<method name="has_peer" qualifiers="const">
- <return type="bool">
- </return>
- <argument index="0" name="id" type="int">
- </argument>
+ <return type="bool" />
+ <argument index="0" name="id" type="int" />
<description>
Returns [code]true[/code] if a peer with the given ID is connected.
</description>
</method>
<method name="is_listening" qualifiers="const">
- <return type="bool">
- </return>
+ <return type="bool" />
<description>
Returns [code]true[/code] if the server is actively listening on a port.
</description>
</method>
<method name="listen">
- <return type="int" enum="Error">
- </return>
- <argument index="0" name="port" type="int">
- </argument>
- <argument index="1" name="protocols" type="PackedStringArray" default="PackedStringArray( )">
- </argument>
- <argument index="2" name="gd_mp_api" type="bool" default="false">
- </argument>
+ <return type="int" enum="Error" />
+ <argument index="0" name="port" type="int" />
+ <argument index="1" name="protocols" type="PackedStringArray" default="PackedStringArray()" />
+ <argument index="2" name="gd_mp_api" type="bool" default="false" />
<description>
Starts listening on the given port.
You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested.
- If [code]true[/code] is passed as [code]gd_mp_api[/code], the server will behave like a network peer for the [MultiplayerAPI], connections from non-Godot clients will not work, and [signal data_received] will not be emitted.
+ If [code]true[/code] is passed as [code]gd_mp_api[/code], the server will behave like a multiplayer peer for the [MultiplayerAPI], connections from non-Godot clients will not work, and [signal data_received] will not be emitted.
If [code]false[/code] is passed instead (default), you must call [PacketPeer] functions ([code]put_packet[/code], [code]get_packet[/code], etc.), on the [WebSocketPeer] returned via [code]get_peer(id)[/code] to communicate with the peer with given [code]id[/code] (e.g. [code]get_peer(id).get_available_packet_count[/code]).
</description>
</method>
<method name="stop">
- <return type="void">
- </return>
+ <return type="void" />
<description>
Stops the server and clear its state.
</description>
@@ -89,6 +73,9 @@
<member name="ca_chain" type="X509Certificate" setter="set_ca_chain" getter="get_ca_chain">
When using SSL (see [member private_key] and [member ssl_certificate]), you can set this to a valid [X509Certificate] to be provided as additional CA chain information during the SSL handshake.
</member>
+ <member name="handshake_timeout" type="float" setter="set_handshake_timeout" getter="get_handshake_timeout" default="3.0">
+ The time in seconds before a pending client (i.e. a client that has not yet finished the HTTP handshake) is considered stale and forcefully disconnected.
+ </member>
<member name="private_key" type="CryptoKey" setter="set_private_key" getter="get_private_key">
When set to a valid [CryptoKey] (along with [member ssl_certificate]) will cause the server to require SSL instead of regular TCP (i.e. the [code]wss://[/code] protocol).
</member>
@@ -98,37 +85,31 @@
</members>
<signals>
<signal name="client_close_request">
- <argument index="0" name="id" type="int">
- </argument>
- <argument index="1" name="code" type="int">
- </argument>
- <argument index="2" name="reason" type="String">
- </argument>
+ <argument index="0" name="id" type="int" />
+ <argument index="1" name="code" type="int" />
+ <argument index="2" name="reason" type="String" />
<description>
Emitted when a client requests a clean close. You should keep polling until you get a [signal client_disconnected] signal with the same [code]id[/code] to achieve the clean close. See [method WebSocketPeer.close] for more details.
</description>
</signal>
<signal name="client_connected">
- <argument index="0" name="id" type="int">
- </argument>
- <argument index="1" name="protocol" type="String">
- </argument>
+ <argument index="0" name="id" type="int" />
+ <argument index="1" name="protocol" type="String" />
+ <argument index="2" name="resource_name" type="String" />
<description>
- Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.
+ Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client, and "resource_name" will be the resource name of the URI the peer used.
+ "resource_name" is a path (at the very least a single forward slash) and potentially a query string.
</description>
</signal>
<signal name="client_disconnected">
- <argument index="0" name="id" type="int">
- </argument>
- <argument index="1" name="was_clean_close" type="bool">
- </argument>
+ <argument index="0" name="id" type="int" />
+ <argument index="1" name="was_clean_close" type="bool" />
<description>
Emitted when a client disconnects. [code]was_clean_close[/code] will be [code]true[/code] if the connection was shutdown cleanly.
</description>
</signal>
<signal name="data_received">
- <argument index="0" name="id" type="int">
- </argument>
+ <argument index="0" name="id" type="int" />
<description>
Emitted when a new message is received.
[b]Note:[/b] This signal is [i]not[/i] emitted when used as high-level multiplayer peer.
diff --git a/modules/websocket/editor_debugger_server_websocket.cpp b/modules/websocket/editor_debugger_server_websocket.cpp
index b02d212c42..d248433d82 100644
--- a/modules/websocket/editor_debugger_server_websocket.cpp
+++ b/modules/websocket/editor_debugger_server_websocket.cpp
@@ -48,11 +48,19 @@ void EditorDebuggerServerWebSocket::poll() {
server->poll();
}
-Error EditorDebuggerServerWebSocket::start() {
- int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
- Vector<String> protocols;
- protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
- return server->listen(remote_port, protocols);
+Error EditorDebuggerServerWebSocket::start(const String &p_uri) {
+ int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
+ String bind_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
+ if (!p_uri.is_empty() && p_uri != "ws://") {
+ String scheme, path;
+ Error err = p_uri.parse_url(scheme, bind_host, bind_port, path);
+ ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);
+ }
+ server->set_bind_ip(bind_host);
+ Vector<String> compatible_protocols;
+ compatible_protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer.
+ return server->listen(bind_port, compatible_protocols);
}
void EditorDebuggerServerWebSocket::stop() {
diff --git a/modules/websocket/editor_debugger_server_websocket.h b/modules/websocket/editor_debugger_server_websocket.h
index 2f73b98c3d..14ab0109b2 100644
--- a/modules/websocket/editor_debugger_server_websocket.h
+++ b/modules/websocket/editor_debugger_server_websocket.h
@@ -28,12 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#ifndef SCRIPT_EDITOR_DEBUGGER_WEBSOCKET_H
-#define SCRIPT_EDITOR_DEBUGGER_WEBSOCKET_H
-
-#include "modules/websocket/websocket_server.h"
+#ifndef EDITOR_DEBUGGER_SERVER_WEBSOCKET_H
+#define EDITOR_DEBUGGER_SERVER_WEBSOCKET_H
#include "editor/debugger/editor_debugger_server.h"
+#include "modules/websocket/websocket_server.h"
class EditorDebuggerServerWebSocket : public EditorDebuggerServer {
GDCLASS(EditorDebuggerServerWebSocket, EditorDebuggerServer);
@@ -49,7 +48,7 @@ public:
void _peer_disconnected(int p_peer, bool p_was_clean);
void poll() override;
- Error start() override;
+ Error start(const String &p_uri) override;
void stop() override;
bool is_active() const override;
bool is_connection_available() const override;
@@ -59,4 +58,4 @@ public:
~EditorDebuggerServerWebSocket();
};
-#endif // SCRIPT_EDITOR_DEBUGGER_WEBSOCKET_H
+#endif // EDITOR_DEBUGGER_SERVER_WEBSOCKET_H
diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp
index 626498e1ae..5cd94e978f 100644
--- a/modules/websocket/emws_client.cpp
+++ b/modules/websocket/emws_client.cpp
@@ -79,7 +79,7 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
String str = "ws://";
if (p_custom_headers.size()) {
- WARN_PRINT_ONCE("Custom headers are not supported in in HTML5 platform.");
+ WARN_PRINT_ONCE("Custom headers are not supported in HTML5 platform.");
}
if (p_ssl) {
str = "wss://";
@@ -95,7 +95,7 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
return FAILED;
}
- static_cast<Ref<EMWSPeer>>(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size);
+ static_cast<Ref<EMWSPeer>>(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size, _out_buf_size);
return OK;
}
@@ -107,7 +107,7 @@ Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const {
return _peer;
}
-NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
+MultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
if (_peer->is_connected_to_host()) {
if (_is_connecting)
return CONNECTION_CONNECTING;
@@ -136,6 +136,7 @@ int EMWSClient::get_max_packet_size() const {
Error EMWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
_in_pkt_size = nearest_shift(p_in_packets - 1);
+ _out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
return OK;
}
diff --git a/modules/websocket/emws_client.h b/modules/websocket/emws_client.h
index ca2d7ed986..3b0b8395b9 100644
--- a/modules/websocket/emws_client.h
+++ b/modules/websocket/emws_client.h
@@ -45,6 +45,7 @@ private:
bool _is_connecting = false;
int _in_buf_size = DEF_BUF_SHIFT;
int _in_pkt_size = DEF_PKT_SHIFT;
+ int _out_buf_size = DEF_BUF_SHIFT;
static void _esws_on_connect(void *obj, char *proto);
static void _esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string);
diff --git a/modules/websocket/emws_peer.cpp b/modules/websocket/emws_peer.cpp
index 1ad3bdc825..d7263dcf43 100644
--- a/modules/websocket/emws_peer.cpp
+++ b/modules/websocket/emws_peer.cpp
@@ -33,10 +33,11 @@
#include "emws_peer.h"
#include "core/io/ip.h"
-void EMWSPeer::set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size) {
+void EMWSPeer::set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size) {
peer_sock = p_sock;
_in_buffer.resize(p_in_pkt_size, p_in_buf_size);
_packet_buffer.resize((1 << p_in_buf_size));
+ _out_buf_size = p_out_buf_size;
}
void EMWSPeer::set_write_mode(WriteMode p_mode) {
@@ -53,10 +54,13 @@ Error EMWSPeer::read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_strin
}
Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
+ ERR_FAIL_COND_V(_out_buf_size && ((uint64_t)godot_js_websocket_buffered_amount(peer_sock) >= (1ULL << _out_buf_size)), ERR_OUT_OF_MEMORY);
+
int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0;
+
godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin);
return OK;
-};
+}
Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
if (_in_buffer.packets_left() == 0)
@@ -70,19 +74,26 @@ Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
r_buffer_size = read;
return OK;
-};
+}
int EMWSPeer::get_available_packet_count() const {
return _in_buffer.packets_left();
-};
+}
+
+int EMWSPeer::get_current_outbound_buffered_amount() const {
+ if (peer_sock != -1) {
+ return godot_js_websocket_buffered_amount(peer_sock);
+ }
+ return 0;
+}
bool EMWSPeer::was_string_packet() const {
return _is_string;
-};
+}
bool EMWSPeer::is_connected_to_host() const {
return peer_sock != -1;
-};
+}
void EMWSPeer::close(int p_code, String p_reason) {
if (peer_sock != -1) {
@@ -91,7 +102,7 @@ void EMWSPeer::close(int p_code, String p_reason) {
_is_string = 0;
_in_buffer.clear();
peer_sock = -1;
-};
+}
IPAddress EMWSPeer::get_connected_host() const {
ERR_FAIL_V_MSG(IPAddress(), "Not supported in HTML5 export.");
@@ -99,7 +110,7 @@ IPAddress EMWSPeer::get_connected_host() const {
uint16_t EMWSPeer::get_connected_port() const {
ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
-};
+}
void EMWSPeer::set_no_delay(bool p_enabled) {
ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export.");
@@ -107,10 +118,10 @@ void EMWSPeer::set_no_delay(bool p_enabled) {
EMWSPeer::EMWSPeer() {
close();
-};
+}
EMWSPeer::~EMWSPeer() {
close();
-};
+}
#endif // JAVASCRIPT_ENABLED
diff --git a/modules/websocket/emws_peer.h b/modules/websocket/emws_peer.h
index 73e701720b..6e93ea31a2 100644
--- a/modules/websocket/emws_peer.h
+++ b/modules/websocket/emws_peer.h
@@ -48,6 +48,7 @@ typedef void (*WSOnError)(void *p_ref);
extern int godot_js_websocket_create(void *p_ref, const char *p_url, const char *p_proto, WSOnOpen p_on_open, WSOnMessage p_on_message, WSOnError p_on_error, WSOnClose p_on_close);
extern int godot_js_websocket_send(int p_id, const uint8_t *p_buf, int p_buf_len, int p_raw);
+extern int godot_js_websocket_buffered_amount(int p_id);
extern void godot_js_websocket_close(int p_id, int p_code, const char *p_reason);
extern void godot_js_websocket_destroy(int p_id);
}
@@ -62,14 +63,16 @@ private:
Vector<uint8_t> _packet_buffer;
PacketBuffer<uint8_t> _in_buffer;
uint8_t _is_string = 0;
+ int _out_buf_size = 0;
public:
Error read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_string);
- void set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size);
+ void set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size);
virtual int get_available_packet_count() const;
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
virtual int get_max_packet_size() const { return _packet_buffer.size(); };
+ virtual int get_current_outbound_buffered_amount() const;
virtual void close(int p_code = 1000, String p_reason = "");
virtual bool is_connected_to_host() const;
diff --git a/modules/websocket/emws_server.h b/modules/websocket/emws_server.h
index e7285ccb86..d36e3a3557 100644
--- a/modules/websocket/emws_server.h
+++ b/modules/websocket/emws_server.h
@@ -33,7 +33,7 @@
#ifdef JAVASCRIPT_ENABLED
-#include "core/object/reference.h"
+#include "core/object/ref_counted.h"
#include "emws_peer.h"
#include "websocket_server.h"
diff --git a/modules/websocket/library_godot_websocket.js b/modules/websocket/library_godot_websocket.js
index b182d1ecde..dd2fd1e94f 100644
--- a/modules/websocket/library_godot_websocket.js
+++ b/modules/websocket/library_godot_websocket.js
@@ -101,6 +101,15 @@ const GodotWebSocket = {
return 0;
},
+ // Get current bufferedAmount
+ bufferedAmount: function (p_id) {
+ const ref = IDHandler.get(p_id);
+ if (!ref) {
+ return 0; // Godot object is gone.
+ }
+ return ref.bufferedAmount;
+ },
+
create: function (socket, p_on_open, p_on_message, p_on_error, p_on_close) {
const id = IDHandler.add(socket);
socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open);
@@ -171,6 +180,11 @@ const GodotWebSocket = {
return GodotWebSocket.send(p_id, out);
},
+ godot_js_websocket_buffered_amount__sig: 'ii',
+ godot_js_websocket_buffered_amount: function (p_id) {
+ return GodotWebSocket.bufferedAmount(p_id);
+ },
+
godot_js_websocket_close__sig: 'viii',
godot_js_websocket_close: function (p_id, p_code, p_reason) {
const code = p_code;
diff --git a/modules/websocket/register_types.cpp b/modules/websocket/register_types.cpp
index 5a02509c4a..7c742b1b89 100644
--- a/modules/websocket/register_types.cpp
+++ b/modules/websocket/register_types.cpp
@@ -63,7 +63,7 @@ void register_websocket_types() {
WSLServer::make_default();
#endif
- ClassDB::register_virtual_class<WebSocketMultiplayerPeer>();
+ GDREGISTER_VIRTUAL_CLASS(WebSocketMultiplayerPeer);
ClassDB::register_custom_instance_class<WebSocketServer>();
ClassDB::register_custom_instance_class<WebSocketClient>();
ClassDB::register_custom_instance_class<WebSocketPeer>();
diff --git a/modules/websocket/remote_debugger_peer_websocket.h b/modules/websocket/remote_debugger_peer_websocket.h
index 03c60fb480..590d925dcc 100644
--- a/modules/websocket/remote_debugger_peer_websocket.h
+++ b/modules/websocket/remote_debugger_peer_websocket.h
@@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#ifndef SCRIPT_DEBUGGER_WEBSOCKET_H
-#define SCRIPT_DEBUGGER_WEBSOCKET_H
+#ifndef REMOTE_DEBUGGER_PEER_WEBSOCKET_H
+#define REMOTE_DEBUGGER_PEER_WEBSOCKET_H
#ifdef JAVASCRIPT_ENABLED
#include "modules/websocket/emws_client.h"
@@ -62,4 +62,4 @@ public:
RemoteDebuggerPeerWebSocket(Ref<WebSocketPeer> p_peer = Ref<WebSocketPeer>());
};
-#endif // SCRIPT_DEBUGGER_WEBSOCKET_H
+#endif // REMOTE_DEBUGGER_PEER_WEBSOCKET_H
diff --git a/modules/websocket/websocket_client.cpp b/modules/websocket/websocket_client.cpp
index 1e9183ebfa..f7a8944745 100644
--- a/modules/websocket/websocket_client.cpp
+++ b/modules/websocket/websocket_client.cpp
@@ -42,9 +42,9 @@ Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_proto
_is_multiplayer = gd_mp_api;
String host = p_url;
- String path = "/";
- String scheme = "";
- int port = 80;
+ String path;
+ String scheme;
+ int port = 0;
Error err = p_url.parse_url(scheme, host, port, path);
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
@@ -55,6 +55,9 @@ Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_proto
if (port == 0) {
port = ssl ? 443 : 80;
}
+ if (path.is_empty()) {
+ path = "/";
+ }
return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
}
@@ -83,7 +86,7 @@ void WebSocketClient::_on_peer_packet() {
if (_is_multiplayer) {
_process_multiplayer(get_peer(1), 1);
} else {
- emit_signal("data_received");
+ emit_signal(SNAME("data_received"));
}
}
@@ -91,27 +94,27 @@ void WebSocketClient::_on_connect(String p_protocol) {
if (_is_multiplayer) {
// need to wait for ID confirmation...
} else {
- emit_signal("connection_established", p_protocol);
+ emit_signal(SNAME("connection_established"), p_protocol);
}
}
void WebSocketClient::_on_close_request(int p_code, String p_reason) {
- emit_signal("server_close_request", p_code, p_reason);
+ emit_signal(SNAME("server_close_request"), p_code, p_reason);
}
void WebSocketClient::_on_disconnect(bool p_was_clean) {
if (_is_multiplayer) {
- emit_signal("connection_failed");
+ emit_signal(SNAME("connection_failed"));
} else {
- emit_signal("connection_closed", p_was_clean);
+ emit_signal(SNAME("connection_closed"), p_was_clean);
}
}
void WebSocketClient::_on_error() {
if (_is_multiplayer) {
- emit_signal("connection_failed");
+ emit_signal(SNAME("connection_failed"));
} else {
- emit_signal("connection_error");
+ emit_signal(SNAME("connection_error"));
}
}
@@ -123,12 +126,12 @@ void WebSocketClient::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
ClassDB::bind_method(D_METHOD("get_trusted_ssl_certificate"), &WebSocketClient::get_trusted_ssl_certificate);
ClassDB::bind_method(D_METHOD("set_trusted_ssl_certificate"), &WebSocketClient::set_trusted_ssl_certificate);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", PROPERTY_USAGE_NONE), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
ADD_SIGNAL(MethodInfo("data_received"));
ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
diff --git a/modules/websocket/websocket_multiplayer_peer.cpp b/modules/websocket/websocket_multiplayer_peer.cpp
index fa0ef7060f..7464cf2bf5 100644
--- a/modules/websocket/websocket_multiplayer_peer.cpp
+++ b/modules/websocket/websocket_multiplayer_peer.cpp
@@ -39,35 +39,15 @@ WebSocketMultiplayerPeer::~WebSocketMultiplayerPeer() {
_clear();
}
-int WebSocketMultiplayerPeer::_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_path().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 negative id is used for exclusion
- }
-
- return hash;
-}
-
void WebSocketMultiplayerPeer::_clear() {
_peer_map.clear();
if (_current_packet.data != nullptr) {
memfree(_current_packet.data);
}
- for (List<Packet>::Element *E = _incoming_packets.front(); E; E = E->next()) {
- memfree(E->get().data);
- E->get().data = nullptr;
+ for (Packet &E : _incoming_packets) {
+ memfree(E.data);
+ E.data = nullptr;
}
_incoming_packets.clear();
@@ -123,15 +103,23 @@ Error WebSocketMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer
}
//
-// NetworkedMultiplayerPeer
+// MultiplayerPeer
//
-void WebSocketMultiplayerPeer::set_transfer_mode(TransferMode p_mode) {
+void WebSocketMultiplayerPeer::set_transfer_channel(int p_channel) {
+ // Websocket does not have channels.
+}
+
+int WebSocketMultiplayerPeer::get_transfer_channel() const {
+ return 0;
+}
+
+void WebSocketMultiplayerPeer::set_transfer_mode(Multiplayer::TransferMode p_mode) {
// Websocket uses TCP, reliable
}
-NetworkedMultiplayerPeer::TransferMode WebSocketMultiplayerPeer::get_transfer_mode() const {
+Multiplayer::TransferMode WebSocketMultiplayerPeer::get_transfer_mode() const {
// Websocket uses TCP, reliable
- return TRANSFER_MODE_RELIABLE;
+ return Multiplayer::TRANSFER_MODE_RELIABLE;
}
void WebSocketMultiplayerPeer::set_target_peer(int p_target_peer) {
@@ -215,7 +203,7 @@ void WebSocketMultiplayerPeer::_store_pkt(int32_t p_source, int32_t p_dest, cons
packet.destination = p_dest;
memcpy(packet.data, &p_data[PROTO_SIZE], p_data_size);
_incoming_packets.push_back(packet);
- emit_signal("peer_packet", p_source);
+ emit_signal(SNAME("peer_packet"), p_source);
}
Error WebSocketMultiplayerPeer::_server_relay(int32_t p_from, int32_t p_to, const uint8_t *p_buffer, uint32_t p_buffer_size) {
@@ -306,15 +294,15 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u
switch (type) {
case SYS_ADD: // Add peer
_peer_map[id] = Ref<WebSocketPeer>();
- emit_signal("peer_connected", id);
+ emit_signal(SNAME("peer_connected"), id);
if (id == 1) { // We just connected to the server
- emit_signal("connection_succeeded");
+ emit_signal(SNAME("connection_succeeded"));
}
break;
case SYS_DEL: // Remove peer
_peer_map.erase(id);
- emit_signal("peer_disconnected", id);
+ emit_signal(SNAME("peer_disconnected"), id);
break;
case SYS_ID: // Hello, server assigned ID
_peer_id = id;
diff --git a/modules/websocket/websocket_multiplayer_peer.h b/modules/websocket/websocket_multiplayer_peer.h
index 48a6607d89..d97a599fe9 100644
--- a/modules/websocket/websocket_multiplayer_peer.h
+++ b/modules/websocket/websocket_multiplayer_peer.h
@@ -32,12 +32,12 @@
#define WEBSOCKET_MULTIPLAYER_PEER_H
#include "core/error/error_list.h"
-#include "core/io/networked_multiplayer_peer.h"
+#include "core/multiplayer/multiplayer_peer.h"
#include "core/templates/list.h"
#include "websocket_peer.h"
-class WebSocketMultiplayerPeer : public NetworkedMultiplayerPeer {
- GDCLASS(WebSocketMultiplayerPeer, NetworkedMultiplayerPeer);
+class WebSocketMultiplayerPeer : public MultiplayerPeer {
+ GDCLASS(WebSocketMultiplayerPeer, MultiplayerPeer);
private:
Vector<uint8_t> _make_pkt(uint8_t p_type, int32_t p_from, int32_t p_to, const uint8_t *p_data, uint32_t p_data_size);
@@ -75,12 +75,13 @@ protected:
void _send_add(int32_t p_peer_id);
void _send_sys(Ref<WebSocketPeer> p_peer, uint8_t p_type, int32_t p_peer_id);
void _send_del(int32_t p_peer_id);
- int _gen_unique_id() const;
public:
- /* NetworkedMultiplayerPeer */
- void set_transfer_mode(TransferMode p_mode) override;
- TransferMode get_transfer_mode() const override;
+ /* MultiplayerPeer */
+ void set_transfer_channel(int p_channel) override;
+ int get_transfer_channel() const override;
+ void set_transfer_mode(Multiplayer::TransferMode p_mode) override;
+ Multiplayer::TransferMode get_transfer_mode() const override;
void set_target_peer(int p_target_peer) override;
int get_packet_peer() const override;
int get_unique_id() const override;
diff --git a/modules/websocket/websocket_peer.cpp b/modules/websocket/websocket_peer.cpp
index e77fdcfed2..ee13040821 100644
--- a/modules/websocket/websocket_peer.cpp
+++ b/modules/websocket/websocket_peer.cpp
@@ -47,6 +47,7 @@ void WebSocketPeer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketPeer::get_connected_host);
ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketPeer::get_connected_port);
ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &WebSocketPeer::set_no_delay);
+ ClassDB::bind_method(D_METHOD("get_current_outbound_buffered_amount"), &WebSocketPeer::get_current_outbound_buffered_amount);
BIND_ENUM_CONSTANT(WRITE_MODE_TEXT);
BIND_ENUM_CONSTANT(WRITE_MODE_BINARY);
diff --git a/modules/websocket/websocket_peer.h b/modules/websocket/websocket_peer.h
index e9bb20f21f..517b8600d6 100644
--- a/modules/websocket/websocket_peer.h
+++ b/modules/websocket/websocket_peer.h
@@ -59,6 +59,7 @@ public:
virtual uint16_t get_connected_port() const = 0;
virtual bool was_string_packet() const = 0;
virtual void set_no_delay(bool p_enabled) = 0;
+ virtual int get_current_outbound_buffered_amount() const = 0;
WebSocketPeer();
~WebSocketPeer();
diff --git a/modules/websocket/websocket_server.cpp b/modules/websocket/websocket_server.cpp
index 7cf68b835c..fb838109f3 100644
--- a/modules/websocket/websocket_server.cpp
+++ b/modules/websocket/websocket_server.cpp
@@ -55,19 +55,23 @@ void WebSocketServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_private_key"), &WebSocketServer::get_private_key);
ClassDB::bind_method(D_METHOD("set_private_key"), &WebSocketServer::set_private_key);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "private_key", PROPERTY_HINT_RESOURCE_TYPE, "CryptoKey", 0), "set_private_key", "get_private_key");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "private_key", PROPERTY_HINT_RESOURCE_TYPE, "CryptoKey", PROPERTY_USAGE_NONE), "set_private_key", "get_private_key");
ClassDB::bind_method(D_METHOD("get_ssl_certificate"), &WebSocketServer::get_ssl_certificate);
ClassDB::bind_method(D_METHOD("set_ssl_certificate"), &WebSocketServer::set_ssl_certificate);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_ssl_certificate", "get_ssl_certificate");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", PROPERTY_USAGE_NONE), "set_ssl_certificate", "get_ssl_certificate");
ClassDB::bind_method(D_METHOD("get_ca_chain"), &WebSocketServer::get_ca_chain);
ClassDB::bind_method(D_METHOD("set_ca_chain"), &WebSocketServer::set_ca_chain);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ca_chain", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_ca_chain", "get_ca_chain");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ca_chain", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", PROPERTY_USAGE_NONE), "set_ca_chain", "get_ca_chain");
+
+ ClassDB::bind_method(D_METHOD("get_handshake_timeout"), &WebSocketServer::get_handshake_timeout);
+ ClassDB::bind_method(D_METHOD("set_handshake_timeout", "timeout"), &WebSocketServer::set_handshake_timeout);
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handshake_timeout"), "set_handshake_timeout", "get_handshake_timeout");
ADD_SIGNAL(MethodInfo("client_close_request", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
ADD_SIGNAL(MethodInfo("client_disconnected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::BOOL, "was_clean_close")));
- ADD_SIGNAL(MethodInfo("client_connected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "protocol")));
+ ADD_SIGNAL(MethodInfo("client_connected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "protocol"), PropertyInfo(Variant::STRING, "resource_name")));
ADD_SIGNAL(MethodInfo("data_received", PropertyInfo(Variant::INT, "id")));
}
@@ -108,7 +112,16 @@ void WebSocketServer::set_ca_chain(Ref<X509Certificate> p_ca_chain) {
ca_chain = p_ca_chain;
}
-NetworkedMultiplayerPeer::ConnectionStatus WebSocketServer::get_connection_status() const {
+float WebSocketServer::get_handshake_timeout() const {
+ return handshake_timeout / 1000.0;
+}
+
+void WebSocketServer::set_handshake_timeout(float p_timeout) {
+ ERR_FAIL_COND(p_timeout <= 0.0);
+ handshake_timeout = p_timeout * 1000;
+}
+
+MultiplayerPeer::ConnectionStatus WebSocketServer::get_connection_status() const {
if (is_listening()) {
return CONNECTION_CONNECTED;
}
@@ -124,17 +137,17 @@ void WebSocketServer::_on_peer_packet(int32_t p_peer_id) {
if (_is_multiplayer) {
_process_multiplayer(get_peer(p_peer_id), p_peer_id);
} else {
- emit_signal("data_received", p_peer_id);
+ emit_signal(SNAME("data_received"), p_peer_id);
}
}
-void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol) {
+void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol, String p_resource_name) {
if (_is_multiplayer) {
// Send add to clients
_send_add(p_peer_id);
- emit_signal("peer_connected", p_peer_id);
+ emit_signal(SNAME("peer_connected"), p_peer_id);
} else {
- emit_signal("client_connected", p_peer_id, p_protocol);
+ emit_signal(SNAME("client_connected"), p_peer_id, p_protocol, p_resource_name);
}
}
@@ -142,12 +155,12 @@ void WebSocketServer::_on_disconnect(int32_t p_peer_id, bool p_was_clean) {
if (_is_multiplayer) {
// Send delete to clients
_send_del(p_peer_id);
- emit_signal("peer_disconnected", p_peer_id);
+ emit_signal(SNAME("peer_disconnected"), p_peer_id);
} else {
- emit_signal("client_disconnected", p_peer_id, p_was_clean);
+ emit_signal(SNAME("client_disconnected"), p_peer_id, p_was_clean);
}
}
void WebSocketServer::_on_close_request(int32_t p_peer_id, int p_code, String p_reason) {
- emit_signal("client_close_request", p_peer_id, p_code, p_reason);
+ emit_signal(SNAME("client_close_request"), p_peer_id, p_code, p_reason);
}
diff --git a/modules/websocket/websocket_server.h b/modules/websocket/websocket_server.h
index 10da51fce5..c4d651471f 100644
--- a/modules/websocket/websocket_server.h
+++ b/modules/websocket/websocket_server.h
@@ -32,7 +32,7 @@
#define WEBSOCKET_H
#include "core/crypto/crypto.h"
-#include "core/object/reference.h"
+#include "core/object/ref_counted.h"
#include "websocket_multiplayer_peer.h"
#include "websocket_peer.h"
@@ -48,6 +48,7 @@ protected:
Ref<CryptoKey> private_key;
Ref<X509Certificate> ssl_cert;
Ref<X509Certificate> ca_chain;
+ uint32_t handshake_timeout = 3000;
public:
virtual Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false) = 0;
@@ -62,7 +63,7 @@ public:
virtual void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = "") = 0;
void _on_peer_packet(int32_t p_peer_id);
- void _on_connect(int32_t p_peer_id, String p_protocol);
+ void _on_connect(int32_t p_peer_id, String p_protocol, String p_resource_name);
void _on_disconnect(int32_t p_peer_id, bool p_was_clean);
void _on_close_request(int32_t p_peer_id, int p_code, String p_reason);
@@ -78,6 +79,9 @@ public:
Ref<X509Certificate> get_ca_chain() const;
void set_ca_chain(Ref<X509Certificate> p_ca_chain);
+ float get_handshake_timeout() const;
+ void set_handshake_timeout(float p_timeout);
+
WebSocketServer();
~WebSocketServer();
};
diff --git a/modules/websocket/wsl_client.cpp b/modules/websocket/wsl_client.cpp
index 111d2178d6..26c0176ea4 100644
--- a/modules/websocket/wsl_client.cpp
+++ b/modules/websocket/wsl_client.cpp
@@ -158,24 +158,31 @@ bool WSLClient::_verify_headers(String &r_protocol) {
Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) {
ERR_FAIL_COND_V(_connection.is_valid(), ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V(p_path.is_empty(), ERR_INVALID_PARAMETER);
_peer = Ref<WSLPeer>(memnew(WSLPeer));
- IPAddress addr;
- if (!p_host.is_valid_ip_address()) {
- addr = IP::get_singleton()->resolve_hostname(p_host);
+ if (p_host.is_valid_ip_address()) {
+ ip_candidates.clear();
+ ip_candidates.push_back(IPAddress(p_host));
} else {
- addr = p_host;
+ ip_candidates = IP::get_singleton()->resolve_hostname_addresses(p_host);
}
- ERR_FAIL_COND_V(!addr.is_valid(), ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V(ip_candidates.is_empty(), ERR_INVALID_PARAMETER);
String port = "";
if ((p_port != 80 && !p_ssl) || (p_port != 443 && p_ssl)) {
port = ":" + itos(p_port);
}
- Error err = _tcp->connect_to_host(addr, p_port);
+ Error err = ERR_BUG; // Should be at least one entry.
+ while (ip_candidates.size() > 0) {
+ err = _tcp->connect_to_host(ip_candidates.pop_front(), p_port);
+ if (err == OK) {
+ break;
+ }
+ }
if (err != OK) {
_tcp->disconnect_from_host();
_on_error();
@@ -184,6 +191,7 @@ Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
_connection = _tcp;
_use_ssl = p_ssl;
_host = p_host;
+ _port = p_port;
// Strip edges from protocols.
_protocols.resize(p_protocols.size());
String *pw = _protocols.ptrw();
@@ -243,6 +251,7 @@ void WSLClient::poll() {
_on_error();
break;
case StreamPeerTCP::STATUS_CONNECTED: {
+ ip_candidates.clear();
Ref<StreamPeerSSL> ssl;
if (_use_ssl) {
if (_connection == _tcp) {
@@ -273,6 +282,12 @@ void WSLClient::poll() {
_do_handshake();
} break;
case StreamPeerTCP::STATUS_ERROR:
+ while (ip_candidates.size() > 0) {
+ _tcp->disconnect_from_host();
+ if (_tcp->connect_to_host(ip_candidates.pop_front(), _port) == OK) {
+ return;
+ }
+ }
disconnect_from_host();
_on_error();
break;
@@ -287,7 +302,7 @@ Ref<WebSocketPeer> WSLClient::get_peer(int p_peer_id) const {
return _peer;
}
-NetworkedMultiplayerPeer::ConnectionStatus WSLClient::get_connection_status() const {
+MultiplayerPeer::ConnectionStatus WSLClient::get_connection_status() const {
if (_peer->is_connected_to_host()) {
return CONNECTION_CONNECTED;
}
@@ -314,6 +329,8 @@ void WSLClient::disconnect_from_host(int p_code, String p_reason) {
memset(_resp_buf, 0, sizeof(_resp_buf));
_resp_pos = 0;
+
+ ip_candidates.clear();
}
IPAddress WSLClient::get_connected_host() const {
@@ -337,8 +354,8 @@ Error WSLClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer
}
WSLClient::WSLClient() {
- _peer.instance();
- _tcp.instance();
+ _peer.instantiate();
+ _tcp.instantiate();
disconnect_from_host();
}
diff --git a/modules/websocket/wsl_client.h b/modules/websocket/wsl_client.h
index 849639ee8b..3972977910 100644
--- a/modules/websocket/wsl_client.h
+++ b/modules/websocket/wsl_client.h
@@ -63,6 +63,8 @@ private:
String _key;
String _host;
+ int _port;
+ Array ip_candidates;
Vector<String> _protocols;
bool _use_ssl = false;
diff --git a/modules/websocket/wsl_peer.cpp b/modules/websocket/wsl_peer.cpp
index 1dbadfed74..7f027e1c0d 100644
--- a/modules/websocket/wsl_peer.cpp
+++ b/modules/websocket/wsl_peer.cpp
@@ -205,7 +205,9 @@ void WSLPeer::make_context(PeerData *p_data, unsigned int p_in_buf_size, unsigne
ERR_FAIL_COND(p_data == nullptr);
_in_buffer.resize(p_in_pkt_size, p_in_buf_size);
- _packet_buffer.resize((1 << MAX(p_in_buf_size, p_out_buf_size)));
+ _packet_buffer.resize(1 << p_in_buf_size);
+ _out_buf_size = p_out_buf_size;
+ _out_pkt_size = p_out_pkt_size;
_data = p_data;
_data->peer = this;
@@ -239,6 +241,8 @@ void WSLPeer::poll() {
Error WSLPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
+ ERR_FAIL_COND_V(_out_pkt_size && (wslay_event_get_queued_msg_count(_data->ctx) >= (1ULL << _out_pkt_size)), ERR_OUT_OF_MEMORY);
+ ERR_FAIL_COND_V(_out_buf_size && (wslay_event_get_queued_msg_length(_data->ctx) >= (1ULL << _out_buf_size)), ERR_OUT_OF_MEMORY);
struct wslay_event_msg msg; // Should I use fragmented?
msg.opcode = write_mode == WRITE_MODE_TEXT ? WSLAY_TEXT_FRAME : WSLAY_BINARY_FRAME;
@@ -280,6 +284,12 @@ int WSLPeer::get_available_packet_count() const {
return _in_buffer.packets_left();
}
+int WSLPeer::get_current_outbound_buffered_amount() const {
+ ERR_FAIL_COND_V(!_data, 0);
+
+ return wslay_event_get_queued_msg_length(_data->ctx);
+}
+
bool WSLPeer::was_string_packet() const {
return _is_string;
}
diff --git a/modules/websocket/wsl_peer.h b/modules/websocket/wsl_peer.h
index f1ea98d384..260d4b183d 100644
--- a/modules/websocket/wsl_peer.h
+++ b/modules/websocket/wsl_peer.h
@@ -77,6 +77,9 @@ private:
WriteMode write_mode = WRITE_MODE_BINARY;
+ int _out_buf_size = 0;
+ int _out_pkt_size = 0;
+
public:
int close_code = -1;
String close_reason;
@@ -86,6 +89,7 @@ public:
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
virtual int get_max_packet_size() const { return _packet_buffer.size(); };
+ virtual int get_current_outbound_buffered_amount() const;
virtual void close_now();
virtual void close(int p_code = 1000, String p_reason = "");
diff --git a/modules/websocket/wsl_server.cpp b/modules/websocket/wsl_server.cpp
index dc5b23c31e..7402bbb46e 100644
--- a/modules/websocket/wsl_server.cpp
+++ b/modules/websocket/wsl_server.cpp
@@ -34,7 +34,7 @@
#include "core/config/project_settings.h"
#include "core/os/os.h"
-bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
+bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols, String &r_resource_name) {
Vector<String> psa = String((char *)req_buf).split("\r\n");
int len = psa.size();
ERR_FAIL_COND_V_MSG(len < 4, false, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
@@ -45,6 +45,7 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
// Wrong protocol
ERR_FAIL_COND_V_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", false, "Invalid method or HTTP version.");
+ r_resource_name = req[1];
Map<String, String> headers;
for (int i = 1; i < len; i++) {
Vector<String> header = psa[i].split(":", false, 1);
@@ -95,28 +96,33 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
return true;
}
-Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) {
- if (OS::get_singleton()->get_ticks_msec() - time > WSL_SERVER_TIMEOUT) {
+Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uint64_t p_timeout, String &r_resource_name) {
+ if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) {
+ print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", p_timeout * 0.001));
return ERR_TIMEOUT;
}
+
if (use_ssl) {
Ref<StreamPeerSSL> ssl = static_cast<Ref<StreamPeerSSL>>(connection);
if (ssl.is_null()) {
- return FAILED;
+ ERR_FAIL_V_MSG(ERR_BUG, "Couldn't get StreamPeerSSL for WebSocket handshake.");
}
ssl->poll();
if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) {
return ERR_BUSY;
} else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
+ print_verbose(vformat("WebSocket SSL connection error during handshake (StreamPeerSSL status code %d).", ssl->get_status()));
return FAILED;
}
}
+
if (!has_request) {
int read = 0;
while (true) {
- ERR_FAIL_COND_V_MSG(req_pos >= WSL_MAX_HEADER_SIZE, ERR_OUT_OF_MEMORY, "Response headers too big.");
+ ERR_FAIL_COND_V_MSG(req_pos >= WSL_MAX_HEADER_SIZE, ERR_OUT_OF_MEMORY, "WebSocket response headers are too big.");
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
if (err != OK) { // Got an error
+ print_verbose(vformat("WebSocket error while getting partial data (StreamPeer error code %d).", err));
return FAILED;
} else if (read != 1) { // Busy, wait next poll
return ERR_BUSY;
@@ -125,7 +131,7 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) {
int l = req_pos;
if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
r[l - 3] = '\0';
- if (!_parse_request(p_protocols)) {
+ if (!_parse_request(p_protocols, r_resource_name)) {
return FAILED;
}
String s = "HTTP/1.1 101 Switching Protocols\r\n";
@@ -143,17 +149,21 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) {
req_pos += 1;
}
}
+
if (has_request && response_sent < response.size() - 1) {
int sent = 0;
Error err = connection->put_partial_data((const uint8_t *)response.get_data() + response_sent, response.size() - response_sent - 1, sent);
if (err != OK) {
+ print_verbose(vformat("WebSocket error while putting partial data (StreamPeer error code %d).", err));
return err;
}
response_sent += sent;
}
+
if (response_sent < response.size() - 1) {
return ERR_BUSY;
}
+
return OK;
}
@@ -180,15 +190,16 @@ void WSLServer::poll() {
remove_ids.push_back(E->key());
}
}
- for (List<int>::Element *E = remove_ids.front(); E; E = E->next()) {
- _peer_map.erase(E->get());
+ for (int &E : remove_ids) {
+ _peer_map.erase(E);
}
remove_ids.clear();
List<Ref<PendingPeer>> remove_peers;
- for (List<Ref<PendingPeer>>::Element *E = _pending.front(); E; E = E->next()) {
- Ref<PendingPeer> ppeer = E->get();
- Error err = ppeer->do_handshake(_protocols);
+ for (const Ref<PendingPeer> &E : _pending) {
+ String resource_name;
+ Ref<PendingPeer> ppeer = E;
+ Error err = ppeer->do_handshake(_protocols, handshake_timeout, resource_name);
if (err == ERR_BUSY) {
continue;
} else if (err != OK) {
@@ -196,7 +207,7 @@ void WSLServer::poll() {
continue;
}
// Creating new peer
- int32_t id = _gen_unique_id();
+ int32_t id = generate_unique_id();
WSLPeer::PeerData *data = memnew(struct WSLPeer::PeerData);
data->obj = this;
@@ -211,10 +222,10 @@ void WSLServer::poll() {
_peer_map[id] = ws_peer;
remove_peers.push_back(ppeer);
- _on_connect(id, ppeer->protocol);
+ _on_connect(id, ppeer->protocol, resource_name);
}
- for (List<Ref<PendingPeer>>::Element *E = remove_peers.front(); E; E = E->next()) {
- _pending.erase(E->get());
+ for (const Ref<PendingPeer> &E : remove_peers) {
+ _pending.erase(E);
}
remove_peers.clear();
@@ -301,7 +312,7 @@ Error WSLServer::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer
}
WSLServer::WSLServer() {
- _server.instance();
+ _server.instantiate();
}
WSLServer::~WSLServer() {
diff --git a/modules/websocket/wsl_server.h b/modules/websocket/wsl_server.h
index c2cf9df58b..93c8bd9604 100644
--- a/modules/websocket/wsl_server.h
+++ b/modules/websocket/wsl_server.h
@@ -40,15 +40,13 @@
#include "core/io/stream_peer_tcp.h"
#include "core/io/tcp_server.h"
-#define WSL_SERVER_TIMEOUT 1000
-
class WSLServer : public WebSocketServer {
GDCIIMPL(WSLServer, WebSocketServer);
private:
- class PendingPeer : public Reference {
+ class PendingPeer : public RefCounted {
private:
- bool _parse_request(const Vector<String> p_protocols);
+ bool _parse_request(const Vector<String> p_protocols, String &r_resource_name);
public:
Ref<StreamPeerTCP> tcp;
@@ -64,7 +62,7 @@ private:
CharString response;
int response_sent = 0;
- Error do_handshake(const Vector<String> p_protocols);
+ Error do_handshake(const Vector<String> p_protocols, uint64_t p_timeout, String &r_resource_name);
};
int _in_buf_size = DEF_BUF_SHIFT;