summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2022-02-01 03:15:44 +0330
committerAndreas Kling <kling@serenityos.org>2022-02-06 13:10:10 +0100
commit33a4048f55e9311c7d4f9639cdfd93a0bff95cba (patch)
tree1981edbdf301ce4a429f2508fdd71df4a8e1752f /Userland/Libraries/LibCore
parent07f444439ce0071f0f005d72065c2b76f8d68841 (diff)
downloadserenity-33a4048f55e9311c7d4f9639cdfd93a0bff95cba.zip
LibCore: Add Core::Stream::Socket::set_notifications_enabled()
This is equivalent to Core::Socket::set_notifications_enabled(), and serves to disable the on_ready_to_read() notifications, as the sockets often implement these using the event loop, this method can help avoid waking the event loop and spamming useless calls to on_ready_to_read().
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/Stream.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h
index 0e04e5153f..f8c4dad675 100644
--- a/Userland/Libraries/LibCore/Stream.h
+++ b/Userland/Libraries/LibCore/Stream.h
@@ -106,6 +106,11 @@ public:
// an exec call happens.
virtual ErrorOr<void> set_close_on_exec(bool enabled) = 0;
+ /// Disables any listening mechanisms that this socket uses.
+ /// Can be called with 'false' when `on_ready_to_read` notifications are no longer needed.
+ /// Conversely, set_notifications_enabled(true) will re-enable notifications.
+ virtual void set_notifications_enabled(bool) { }
+
Function<void()> on_ready_to_read;
protected:
@@ -284,6 +289,11 @@ public:
virtual void close() override { m_helper.close(); };
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
+ virtual void set_notifications_enabled(bool enabled) override
+ {
+ if (auto notifier = m_helper.notifier())
+ notifier->set_enabled(enabled);
+ }
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
@@ -354,6 +364,11 @@ public:
virtual void close() override { m_helper.close(); }
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
+ virtual void set_notifications_enabled(bool enabled) override
+ {
+ if (auto notifier = m_helper.notifier())
+ notifier->set_enabled(enabled);
+ }
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
@@ -410,6 +425,11 @@ public:
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
+ virtual void set_notifications_enabled(bool enabled) override
+ {
+ if (auto notifier = m_helper.notifier())
+ notifier->set_enabled(enabled);
+ }
ErrorOr<int> receive_fd(int flags);
ErrorOr<void> send_fd(int fd);
@@ -787,6 +807,7 @@ public:
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.stream().can_read_without_blocking(timeout); }
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.stream().set_blocking(enabled); }
virtual ErrorOr<void> 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); }
virtual ErrorOr<size_t> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
virtual ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }