mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 19:05:07 +00:00
LibCore: Mark Socket::{common_,}connect() virtual and add a on_write
This commit is contained in:
parent
f1578d7e9e
commit
7eb72c72e8
2 changed files with 18 additions and 16 deletions
|
@ -120,6 +120,19 @@ bool Socket::connect(const SocketAddress& address)
|
||||||
|
|
||||||
bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
|
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);
|
int rc = ::connect(fd(), addr, addrlen);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
if (errno == EINPROGRESS) {
|
if (errno == EINPROGRESS) {
|
||||||
|
@ -127,16 +140,7 @@ bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
|
||||||
dbg() << *this << " connection in progress (EINPROGRESS)";
|
dbg() << *this << " connection in progress (EINPROGRESS)";
|
||||||
#endif
|
#endif
|
||||||
m_notifier = Notifier::construct(fd(), Notifier::Event::Write, this);
|
m_notifier = Notifier::construct(fd(), Notifier::Event::Write, this);
|
||||||
m_notifier->on_ready_to_write = [this] {
|
m_notifier->on_ready_to_write = move(connected);
|
||||||
#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();
|
|
||||||
};
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
|
@ -147,10 +151,7 @@ bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
|
||||||
#ifdef CSOCKET_DEBUG
|
#ifdef CSOCKET_DEBUG
|
||||||
dbg() << *this << " connected ok!";
|
dbg() << *this << " connected ok!";
|
||||||
#endif
|
#endif
|
||||||
m_connected = true;
|
connected();
|
||||||
ensure_read_notifier();
|
|
||||||
if (on_connected)
|
|
||||||
on_connected();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ public:
|
||||||
|
|
||||||
Type type() const { return m_type; }
|
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&, int port);
|
||||||
bool connect(const SocketAddress&);
|
bool connect(const SocketAddress&);
|
||||||
|
|
||||||
|
@ -63,6 +63,7 @@ public:
|
||||||
|
|
||||||
Function<void()> on_connected;
|
Function<void()> on_connected;
|
||||||
Function<void()> on_ready_to_read;
|
Function<void()> on_ready_to_read;
|
||||||
|
Function<void()> on_ready_to_write;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Socket(Type, Object* parent);
|
Socket(Type, Object* parent);
|
||||||
|
@ -74,10 +75,10 @@ protected:
|
||||||
bool m_connected { false };
|
bool m_connected { false };
|
||||||
|
|
||||||
virtual void did_update_fd(int) override;
|
virtual void did_update_fd(int) override;
|
||||||
|
virtual bool common_connect(const struct sockaddr*, socklen_t);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool open(IODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
virtual bool open(IODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
||||||
bool common_connect(const struct sockaddr*, socklen_t);
|
|
||||||
void ensure_read_notifier();
|
void ensure_read_notifier();
|
||||||
|
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue