Skip to content

Commit

Permalink
fix bug defined in issue Redot-Engine#31
Browse files Browse the repository at this point in the history
  • Loading branch information
RadenTheFolf committed Oct 8, 2024
1 parent 0e98c9d commit 5613760
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
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 5613760

Please sign in to comment.