From 82446ea70175b05cddba60a962fe1443c137da6b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 Jul 2019 10:48:43 +0200 Subject: [PATCH] CSocket: Add an on_ready_to_read callback. This callback uses a CNotifier internally and will fire whenever there's something to be read from the socket. --- Libraries/LibCore/CSocket.cpp | 13 +++++++++++++ Libraries/LibCore/CSocket.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/Libraries/LibCore/CSocket.cpp b/Libraries/LibCore/CSocket.cpp index c49a6e2698..636dc179d5 100644 --- a/Libraries/LibCore/CSocket.cpp +++ b/Libraries/LibCore/CSocket.cpp @@ -137,3 +137,16 @@ bool CSocket::listen() set_error(errno); return rc == 0; } + +void CSocket::did_update_fd(int fd) +{ + if (fd < 0) { + m_read_notifier = nullptr; + return; + } + m_read_notifier = make(fd, CNotifier::Event::Read); + m_read_notifier->on_ready_to_read = [this] { + if (on_ready_to_read) + on_ready_to_read(); + }; +} diff --git a/Libraries/LibCore/CSocket.h b/Libraries/LibCore/CSocket.h index 20d059166b..7aac4008f9 100644 --- a/Libraries/LibCore/CSocket.h +++ b/Libraries/LibCore/CSocket.h @@ -39,6 +39,7 @@ public: int destination_port() const { return m_destination_port; } Function on_connected; + Function on_ready_to_read; protected: CSocket(Type, CObject* parent); @@ -49,8 +50,11 @@ protected: int m_destination_port { -1 }; bool m_connected { false }; + virtual void did_update_fd(int) override; + private: virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); } Type m_type { Type::Invalid }; OwnPtr m_notifier; + OwnPtr m_read_notifier; };