mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +00:00
CSocket: Share code between connect() overloads
Both overloads should know how to set up a notifier callback in case we get EINPROGRESS from connect(). It might be even better to merge the connect() overloads into a single function..
This commit is contained in:
parent
1427c20f6a
commit
99970d7d4b
2 changed files with 23 additions and 27 deletions
|
@ -63,8 +63,25 @@ bool CSocket::connect(const CSocketAddress& address, int port)
|
||||||
m_destination_address = address;
|
m_destination_address = address;
|
||||||
m_destination_port = port;
|
m_destination_port = port;
|
||||||
|
|
||||||
fflush(stdout);
|
return common_connect((struct sockaddr*)&addr, sizeof(addr));
|
||||||
int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
|
}
|
||||||
|
|
||||||
|
bool CSocket::connect(const CSocketAddress& address)
|
||||||
|
{
|
||||||
|
ASSERT(!is_connected());
|
||||||
|
ASSERT(address.type() == CSocketAddress::Type::Local);
|
||||||
|
dbg() << *this << " connecting to " << address << "...";
|
||||||
|
|
||||||
|
sockaddr_un saddr;
|
||||||
|
saddr.sun_family = AF_LOCAL;
|
||||||
|
strcpy(saddr.sun_path, address.to_string().characters());
|
||||||
|
|
||||||
|
return common_connect((const sockaddr*)&saddr, sizeof(saddr));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
|
||||||
|
{
|
||||||
|
int rc = ::connect(fd(), addr, addrlen);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
if (errno == EINPROGRESS) {
|
if (errno == EINPROGRESS) {
|
||||||
dbg() << *this << " connection in progress (EINPROGRESS)";
|
dbg() << *this << " connection in progress (EINPROGRESS)";
|
||||||
|
@ -78,33 +95,10 @@ bool CSocket::connect(const CSocketAddress& address, int port)
|
||||||
};
|
};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
perror("connect");
|
perror("CSocket::common_connect: connect");
|
||||||
exit(1);
|
|
||||||
} else {
|
|
||||||
dbg() << *this << " connected ok!";
|
|
||||||
m_connected = true;
|
|
||||||
if (on_connected)
|
|
||||||
on_connected();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CSocket::connect(const CSocketAddress& address)
|
|
||||||
{
|
|
||||||
ASSERT(!is_connected());
|
|
||||||
ASSERT(address.type() == CSocketAddress::Type::Local);
|
|
||||||
dbg() << *this << " connecting to " << address << "...";
|
|
||||||
|
|
||||||
sockaddr_un saddr;
|
|
||||||
saddr.sun_family = AF_LOCAL;
|
|
||||||
strcpy(saddr.sun_path, address.to_string().characters());
|
|
||||||
|
|
||||||
int rc = ::connect(fd(), (const sockaddr*)&saddr, sizeof(saddr));
|
|
||||||
if (rc < 0) {
|
|
||||||
perror("connect");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
dbg() << *this << " connected ok!";
|
||||||
m_connected = true;
|
m_connected = true;
|
||||||
if (on_connected)
|
if (on_connected)
|
||||||
on_connected();
|
on_connected();
|
||||||
|
|
|
@ -50,6 +50,8 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
||||||
|
bool common_connect(const struct sockaddr*, socklen_t);
|
||||||
|
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
OwnPtr<CNotifier> m_notifier;
|
OwnPtr<CNotifier> m_notifier;
|
||||||
OwnPtr<CNotifier> m_read_notifier;
|
OwnPtr<CNotifier> m_read_notifier;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue