diff --git a/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml index 8548d21323d..103dbd771b8 100644 --- a/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml +++ b/modules/websocket/doc_classes/WebSocketMultiplayerPeer.xml @@ -18,6 +18,24 @@ Returns the [WebSocketPeer] associated to the given [code]peer_id[/code]. + + + + + + + + + + + + + Configure the buffers sizes for this WebSocket peer. Default values can be specified in 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. + Buffer sizes are expressed in KiB, so [code]4 = 2^12 = 4096 bytes[/code]. All parameters will be rounded up to the nearest power of two. + NOTE: HTML5 exports only use the input buffer since the output one is managed by browsers. + + diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp index aafae8f1c23..409cc9f6994 100644 --- a/modules/websocket/emws_client.cpp +++ b/modules/websocket/emws_client.cpp @@ -205,6 +205,12 @@ int EMWSClient::get_max_packet_size() const { return (1 << _in_buf_size) - PROTO_SIZE; } +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); + return OK; +} + EMWSClient::EMWSClient() { _in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10; _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1); diff --git a/modules/websocket/emws_client.h b/modules/websocket/emws_client.h index 9bc29c19139..1811d05eea0 100644 --- a/modules/websocket/emws_client.h +++ b/modules/websocket/emws_client.h @@ -49,6 +49,7 @@ private: public: bool _is_connecting; + Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector p_protocol = PoolVector()); Ref get_peer(int p_peer_id) const; void disconnect_from_host(int p_code = 1000, String p_reason = ""); diff --git a/modules/websocket/emws_server.cpp b/modules/websocket/emws_server.cpp index 0eef1c4ba9e..c4bb459ad08 100644 --- a/modules/websocket/emws_server.cpp +++ b/modules/websocket/emws_server.cpp @@ -79,6 +79,10 @@ int EMWSServer::get_max_packet_size() const { return 0; } +Error EMWSServer::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) { + return OK; +} + EMWSServer::EMWSServer() { } diff --git a/modules/websocket/emws_server.h b/modules/websocket/emws_server.h index bb101cd155a..a5e5b4090ec 100644 --- a/modules/websocket/emws_server.h +++ b/modules/websocket/emws_server.h @@ -42,6 +42,7 @@ class EMWSServer : public WebSocketServer { GDCIIMPL(EMWSServer, WebSocketServer); public: + Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); Error listen(int p_port, PoolVector p_protocols = PoolVector(), bool gd_mp_api = false); void stop(); bool is_listening() const; diff --git a/modules/websocket/lws_client.cpp b/modules/websocket/lws_client.cpp index 08df76293b1..f139a4ef668 100644 --- a/modules/websocket/lws_client.cpp +++ b/modules/websocket/lws_client.cpp @@ -215,6 +215,17 @@ uint16_t LWSClient::get_connected_port() const { return 1025; }; +Error LWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) { + ERR_EXPLAIN("Buffers sizes can only be set before listening or connecting"); + ERR_FAIL_COND_V(context != NULL, FAILED); + + _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; + _out_pkt_size = nearest_shift(p_out_packets - 1); + return OK; +} + LWSClient::LWSClient() { _in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10; _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1); diff --git a/modules/websocket/lws_client.h b/modules/websocket/lws_client.h index b3a1237550b..df4aabec7f1 100644 --- a/modules/websocket/lws_client.h +++ b/modules/websocket/lws_client.h @@ -51,6 +51,7 @@ private: int _out_pkt_size; public: + Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector p_protocol = PoolVector()); int get_max_packet_size() const; Ref get_peer(int p_peer_id) const; diff --git a/modules/websocket/lws_server.cpp b/modules/websocket/lws_server.cpp index 61ccdca74a2..482c02dc60b 100644 --- a/modules/websocket/lws_server.cpp +++ b/modules/websocket/lws_server.cpp @@ -197,6 +197,17 @@ void LWSServer::disconnect_peer(int p_peer_id, int p_code, String p_reason) { get_peer(p_peer_id)->close(p_code, p_reason); } +Error LWSServer::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) { + ERR_EXPLAIN("Buffers sizes can only be set before listening or connecting"); + ERR_FAIL_COND_V(context != NULL, FAILED); + + _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; + _out_pkt_size = nearest_shift(p_out_packets - 1); + return OK; +} + LWSServer::LWSServer() { _in_buf_size = nearest_shift((int)GLOBAL_GET(WSS_IN_BUF) - 1) + 10; _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_IN_PKT) - 1); diff --git a/modules/websocket/lws_server.h b/modules/websocket/lws_server.h index 5096ee0f887..b331852d26b 100644 --- a/modules/websocket/lws_server.h +++ b/modules/websocket/lws_server.h @@ -52,6 +52,7 @@ private: int _out_pkt_size; public: + Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); Error listen(int p_port, PoolVector p_protocols = PoolVector(), bool gd_mp_api = false); void stop(); bool is_listening() const; diff --git a/modules/websocket/websocket_client.h b/modules/websocket/websocket_client.h index c464d97c7f9..7ddb9468a5c 100644 --- a/modules/websocket/websocket_client.h +++ b/modules/websocket/websocket_client.h @@ -67,6 +67,8 @@ public: void _on_disconnect(bool p_was_clean); void _on_error(); + virtual Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) = 0; + WebSocketClient(); ~WebSocketClient(); }; diff --git a/modules/websocket/websocket_multiplayer_peer.cpp b/modules/websocket/websocket_multiplayer_peer.cpp index 6aab8a7e813..23cbf916ebc 100644 --- a/modules/websocket/websocket_multiplayer_peer.cpp +++ b/modules/websocket/websocket_multiplayer_peer.cpp @@ -87,6 +87,7 @@ void WebSocketMultiplayerPeer::_clear() { void WebSocketMultiplayerPeer::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_buffers", "input_buffer_size_kb", "input_max_packets", "output_buffer_size_kb", "output_max_packets"), &WebSocketMultiplayerPeer::set_buffers); ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebSocketMultiplayerPeer::get_peer); ADD_SIGNAL(MethodInfo("peer_packet", PropertyInfo(Variant::INT, "peer_source"))); diff --git a/modules/websocket/websocket_multiplayer_peer.h b/modules/websocket/websocket_multiplayer_peer.h index b050449ee07..089bc25fe9f 100644 --- a/modules/websocket/websocket_multiplayer_peer.h +++ b/modules/websocket/websocket_multiplayer_peer.h @@ -97,6 +97,7 @@ public: virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); /* WebSocketPeer */ + virtual Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) = 0; virtual Ref get_peer(int p_peer_id) const = 0; void _process_multiplayer(Ref p_peer, uint32_t p_peer_id); diff --git a/modules/websocket/websocket_server.h b/modules/websocket/websocket_server.h index 7a94c4047b2..83c0c104195 100644 --- a/modules/websocket/websocket_server.h +++ b/modules/websocket/websocket_server.h @@ -62,6 +62,8 @@ public: 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); + virtual Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) = 0; + WebSocketServer(); ~WebSocketServer(); };