Skip to content

Commit

Permalink
Fixes integration with wslay to prevent accepting packets when in_buf…
Browse files Browse the repository at this point in the history
…fer full
  • Loading branch information
RadenTheFolf authored and Spartan322 committed Oct 12, 2024
1 parent 92e51fc commit dc534d1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/websocket/doc_classes/WebSocketPeer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@
<member name="max_queued_packets" type="int" setter="set_max_queued_packets" getter="get_max_queued_packets" default="2048">
The maximum amount of packets that will be allowed in the queues (both inbound and outbound).
</member>
<member name="min_buffer_free_space" type="int" setter="set_min_buffer_free_space" getter="get_min_buffer_free_space" default="4096">
The minimum amount of free space allowed to be in the buffer before reading more messages.
</member>
<member name="outbound_buffer_size" type="int" setter="set_outbound_buffer_size" getter="get_outbound_buffer_size" default="65535">
The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the outbound packets).
</member>
Expand Down
4 changes: 4 additions & 0 deletions modules/websocket/packet_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class PacketBuffer {
return _queued;
}

int space_left() const {
return _payload.space_left();
}

void clear() {
_payload.resize(0);
_packets.resize(0);
Expand Down
12 changes: 12 additions & 0 deletions modules/websocket/websocket_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ void WebSocketPeer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_max_queued_packets", "buffer_size"), &WebSocketPeer::set_max_queued_packets);
ClassDB::bind_method(D_METHOD("get_max_queued_packets"), &WebSocketPeer::get_max_queued_packets);

ClassDB::bind_method(D_METHOD("set_min_buffer_free_space", "min_buffer_free_space"), &WebSocketPeer::set_min_buffer_free_space);
ClassDB::bind_method(D_METHOD("get_min_buffer_free_space"), &WebSocketPeer::get_min_buffer_free_space);

ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "supported_protocols"), "set_supported_protocols", "get_supported_protocols");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "handshake_headers"), "set_handshake_headers", "get_handshake_headers");

ADD_PROPERTY(PropertyInfo(Variant::INT, "inbound_buffer_size"), "set_inbound_buffer_size", "get_inbound_buffer_size");
ADD_PROPERTY(PropertyInfo(Variant::INT, "outbound_buffer_size"), "set_outbound_buffer_size", "get_outbound_buffer_size");

ADD_PROPERTY(PropertyInfo(Variant::INT, "max_queued_packets"), "set_max_queued_packets", "get_max_queued_packets");
ADD_PROPERTY(PropertyInfo(Variant::INT, "min_buffer_free_space"), "set_min_buffer_free_space", "get_min_buffer_free_space");

BIND_ENUM_CONSTANT(WRITE_MODE_TEXT);
BIND_ENUM_CONSTANT(WRITE_MODE_BINARY);
Expand Down Expand Up @@ -151,3 +155,11 @@ void WebSocketPeer::set_max_queued_packets(int p_max_queued_packets) {
int WebSocketPeer::get_max_queued_packets() const {
return max_queued_packets;
}

void WebSocketPeer::set_min_buffer_free_space(int p_min_buffer_free_space) {
min_buffer_free_space = p_min_buffer_free_space;
}

int WebSocketPeer::get_min_buffer_free_space() const {
return min_buffer_free_space;
}
4 changes: 4 additions & 0 deletions modules/websocket/websocket_peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class WebSocketPeer : public PacketPeer {
int outbound_buffer_size = DEFAULT_BUFFER_SIZE;
int inbound_buffer_size = DEFAULT_BUFFER_SIZE;
int max_queued_packets = 2048;
int min_buffer_free_space = 4096;

public:
static WebSocketPeer *create(bool p_notify_postinitialize = true) {
Expand Down Expand Up @@ -117,6 +118,9 @@ class WebSocketPeer : public PacketPeer {
void set_max_queued_packets(int p_max_queued_packets);
int get_max_queued_packets() const;

void set_min_buffer_free_space(int p_min_buffer_free_space);
int get_min_buffer_free_space() const;

WebSocketPeer();
~WebSocketPeer();
};
Expand Down
4 changes: 4 additions & 0 deletions modules/websocket/wsl_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ Error WSLPeer::connect_to_url(const String &p_url, Ref<TLSOptions> p_options) {
ssize_t WSLPeer::_wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data, size_t len, int flags, void *user_data) {
WSLPeer *peer = (WSLPeer *)user_data;
Ref<StreamPeer> conn = peer->connection;
if (peer->in_buffer.space_left() < peer->min_buffer_free_space) {
return WSLAY_ERR_NOMEM;
}

if (conn.is_null()) {
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
return -1;
Expand Down

0 comments on commit dc534d1

Please sign in to comment.