LibCore: Mark Socket::{common_,}connect() virtual and add a on_write

This commit is contained in:
AnotherTest 2020-04-19 12:57:13 +04:30 committed by Andreas Kling
parent f1578d7e9e
commit 7eb72c72e8
Notes: sideshowbarker 2024-07-19 07:05:11 +09:00
2 changed files with 18 additions and 16 deletions

View file

@ -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;
}

View file

@ -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<void()> on_connected;
Function<void()> on_ready_to_read;
Function<void()> 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 };