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.
This commit is contained in:
Andreas Kling 2019-07-27 10:48:43 +02:00
parent 8f4fba95c0
commit 82446ea701
Notes: sideshowbarker 2024-07-19 13:02:11 +09:00
2 changed files with 17 additions and 0 deletions

View file

@ -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<CNotifier>(fd, CNotifier::Event::Read);
m_read_notifier->on_ready_to_read = [this] {
if (on_ready_to_read)
on_ready_to_read();
};
}

View file

@ -39,6 +39,7 @@ public:
int destination_port() const { return m_destination_port; }
Function<void()> on_connected;
Function<void()> 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<CNotifier> m_notifier;
OwnPtr<CNotifier> m_read_notifier;
};