mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-22 23:30:14 +00:00
Fix non-blocking TCP connect for remote servers and persist login info between sessions
This commit is contained in:
parent
a52e28191e
commit
bc9c7f09b1
3 changed files with 106 additions and 0 deletions
|
|
@ -52,6 +52,34 @@ bool TCPSocket::connect(const std::string& host, uint16_t port) {
|
|||
sockfd = INVALID_SOCK;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Non-blocking connect in progress — wait for it to complete
|
||||
fd_set writefds;
|
||||
FD_ZERO(&writefds);
|
||||
FD_SET(sockfd, &writefds);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 5;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
int selectResult = ::select(static_cast<int>(sockfd) + 1, nullptr, &writefds, nullptr, &tv);
|
||||
if (selectResult <= 0) {
|
||||
LOG_ERROR("Connection timed out to ", host, ":", port);
|
||||
net::closeSocket(sockfd);
|
||||
sockfd = INVALID_SOCK;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the connection actually succeeded
|
||||
int sockErr = 0;
|
||||
socklen_t errLen = sizeof(sockErr);
|
||||
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&sockErr), &errLen);
|
||||
if (sockErr != 0) {
|
||||
LOG_ERROR("Connection failed: ", net::errorString(sockErr));
|
||||
net::closeSocket(sockfd);
|
||||
sockfd = INVALID_SOCK;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
connected = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue