diff --git a/Libraries/LibCore/Socket.cpp b/Libraries/LibCore/Socket.cpp index 11446da741..f1b261f852 100644 --- a/Libraries/LibCore/Socket.cpp +++ b/Libraries/LibCore/Socket.cpp @@ -120,6 +120,19 @@ bool Socket::connect(const SocketAddress& address) bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen) { + auto connected = [this] { +#ifdef CSOCKET_DEBUG + dbg() << *this << " connected!"; +#endif + if (!m_connected) { + m_connected = true; + ensure_read_notifier(); + if (on_connected) + on_connected(); + } + if (on_ready_to_write) + on_ready_to_write(); + }; int rc = ::connect(fd(), addr, addrlen); if (rc < 0) { if (errno == EINPROGRESS) { @@ -127,16 +140,7 @@ bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen) dbg() << *this << " connection in progress (EINPROGRESS)"; #endif m_notifier = Notifier::construct(fd(), Notifier::Event::Write, this); - m_notifier->on_ready_to_write = [this] { -#ifdef CSOCKET_DEBUG - dbg() << *this << " connected!"; -#endif - m_connected = true; - ensure_read_notifier(); - m_notifier->set_event_mask(Notifier::Event::None); - if (on_connected) - on_connected(); - }; + m_notifier->on_ready_to_write = move(connected); return true; } int saved_errno = errno; @@ -147,10 +151,7 @@ bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen) #ifdef CSOCKET_DEBUG dbg() << *this << " connected ok!"; #endif - m_connected = true; - ensure_read_notifier(); - if (on_connected) - on_connected(); + connected(); return true; } diff --git a/Libraries/LibCore/Socket.h b/Libraries/LibCore/Socket.h index 48bd428748..3985dc8bde 100644 --- a/Libraries/LibCore/Socket.h +++ b/Libraries/LibCore/Socket.h @@ -45,7 +45,7 @@ public: Type type() const { return m_type; } - bool connect(const String& hostname, int port); + virtual bool connect(const String& hostname, int port); bool connect(const SocketAddress&, int port); bool connect(const SocketAddress&); @@ -63,6 +63,7 @@ public: Function on_connected; Function on_ready_to_read; + Function on_ready_to_write; protected: Socket(Type, Object* parent); @@ -74,10 +75,10 @@ protected: bool m_connected { false }; virtual void did_update_fd(int) override; + virtual bool common_connect(const struct sockaddr*, socklen_t); private: virtual bool open(IODevice::OpenMode) override { ASSERT_NOT_REACHED(); } - bool common_connect(const struct sockaddr*, socklen_t); void ensure_read_notifier(); Type m_type { Type::Invalid };