From f4d841077de51d1eaf0b6cca3ecc15bfc96ab669 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Wed, 2 Feb 2022 19:21:37 +0330 Subject: [PATCH] LibCore: Make sure BufferedSocket takes the buffer into account pending_bytes() and can_read_without_blocking() should also take the buffered data into account, otherwise we'll end up pretending that the socket cannot be read from while it has buffered data. --- Userland/Libraries/LibCore/Stream.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index f8c4dad675..710dc27213 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -684,6 +684,11 @@ public: return m_buffer.size(); } + size_t buffered_data_size() const + { + return m_buffered_size; + } + void clear_buffer() { m_buffered_size = 0; @@ -803,8 +808,11 @@ public: virtual bool is_eof() const override { return m_helper.is_eof(); } virtual bool is_open() const override { return m_helper.stream().is_open(); } virtual void close() override { m_helper.stream().close(); } - virtual ErrorOr pending_bytes() const override { return m_helper.stream().pending_bytes(); } - virtual ErrorOr can_read_without_blocking(int timeout = 0) const override { return m_helper.stream().can_read_without_blocking(timeout); } + virtual ErrorOr pending_bytes() const override + { + return TRY(m_helper.stream().pending_bytes()) + m_helper.buffered_data_size(); + } + virtual ErrorOr can_read_without_blocking(int timeout = 0) const override { return m_helper.buffered_data_size() > 0 || TRY(m_helper.stream().can_read_without_blocking(timeout)); } virtual ErrorOr set_blocking(bool enabled) override { return m_helper.stream().set_blocking(enabled); } virtual ErrorOr set_close_on_exec(bool enabled) override { return m_helper.stream().set_close_on_exec(enabled); } virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); }