diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-09-22 21:46:46 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-22 21:46:46 +0200 |
commit | a1907011b258edbb86a9f27e5f1ba1430e281dfd (patch) | |
tree | ca3ce8dad452ad1c3da945cb23d1d40d9827ffe7 /Libraries/LibCore/CSocket.cpp | |
parent | 65409e8f04b0cdbc7df76d4e6edb4265ad5ded9c (diff) | |
download | serenity-a1907011b258edbb86a9f27e5f1ba1430e281dfd.zip |
CSocket: Don't create the read notifier until after we've connected
This makes it so that "on_connected" always gets called first.
Since accepted sockets are connected before construction, they have
to manually set CSocket::m_connected.
Diffstat (limited to 'Libraries/LibCore/CSocket.cpp')
-rw-r--r-- | Libraries/LibCore/CSocket.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Libraries/LibCore/CSocket.cpp b/Libraries/LibCore/CSocket.cpp index 8f7cc6067e..7ac939e780 100644 --- a/Libraries/LibCore/CSocket.cpp +++ b/Libraries/LibCore/CSocket.cpp @@ -89,6 +89,7 @@ bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen) m_notifier->on_ready_to_write = [this] { dbg() << *this << " connected!"; m_connected = true; + ensure_read_notifier(); m_notifier->set_event_mask(CNotifier::Event::None); if (on_connected) on_connected(); @@ -100,6 +101,7 @@ bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen) } dbg() << *this << " connected ok!"; m_connected = true; + ensure_read_notifier(); if (on_connected) on_connected(); return true; @@ -132,7 +134,18 @@ void CSocket::did_update_fd(int fd) m_read_notifier = nullptr; return; } - m_read_notifier = CNotifier::construct(fd, CNotifier::Event::Read, this); + if (m_connected) { + ensure_read_notifier(); + } else { + // I don't think it would be right if we updated the fd while not connected *but* while having a notifier.. + ASSERT(!m_read_notifier); + } +} + +void CSocket::ensure_read_notifier() +{ + ASSERT(m_connected); + m_read_notifier = CNotifier::construct(fd(), CNotifier::Event::Read, this); m_read_notifier->on_ready_to_read = [this] { if (on_ready_to_read) on_ready_to_read(); |