Skip to content

Commit 7c53eb8

Browse files
fix: socket readiness fixes for windows, doesnt get stuck in reconnect loops any more (#1461)
1 parent 281c844 commit 7c53eb8

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

include/dpp/sslconnection.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ class DPP_EXPORT ssl_connection
237237

238238
virtual void on_buffer_drained();
239239

240+
/**
241+
* @brief Start connecting to a TCP socket.
242+
* This simply calls connect() and checks for error return, as the timeout is now handled in the main
243+
* IO events for the ssl_connection class.
244+
*
245+
* @param sockfd socket descriptor
246+
* @param addr address to connect to
247+
* @param addrlen address length
248+
* @param timeout_ms timeout in milliseconds
249+
* @return int -1 on error, 0 on success just like POSIX connect()
250+
* @throw dpp::connection_exception on failure
251+
*/
252+
int start_connecting(dpp::socket sockfd, const struct sockaddr *addr, socklen_t addrlen);
253+
240254
public:
241255
/**
242256
* @brief For low-level debugging, calling this function will

src/dpp/discordclient.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ void discord_client::error(uint32_t errorcode)
363363
if (i != errortext.end()) {
364364
error = i->second;
365365
}
366-
log(dpp::ll_warning, "OOF! Error from underlying websocket: " + std::to_string(errorcode) + ": " + error);
367-
this->close();
366+
throw dpp::connection_exception("OOF! Error from underlying websocket: " + std::to_string(errorcode) + ": " + error);
368367
}
369368

370369
void discord_client::log(dpp::loglevel severity, const std::string &msg) const

src/dpp/sslconnection.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,7 @@ bool set_nonblocking(dpp::socket sockfd, bool non_blocking)
129129
return true;
130130
}
131131

132-
/**
133-
* @brief Start connecting to a TCP socket.
134-
* This simply calls connect() and checks for error return, as the timeout is now handled in the main
135-
* IO events for the ssl_connection class.
136-
*
137-
* @param sockfd socket descriptor
138-
* @param addr address to connect to
139-
* @param addrlen address length
140-
* @param timeout_ms timeout in milliseconds
141-
* @return int -1 on error, 0 on success just like POSIX connect()
142-
* @throw dpp::connection_exception on failure
143-
*/
144-
int start_connecting(dpp::socket sockfd, const struct sockaddr *addr, socklen_t addrlen) {
132+
int ssl_connection::start_connecting(dpp::socket sockfd, const struct sockaddr *addr, socklen_t addrlen) {
145133
if (!set_nonblocking(sockfd, true)) {
146134
throw dpp::connection_exception(err_nonblocking_failure, "Can't switch socket to non-blocking mode!");
147135
}
@@ -152,14 +140,24 @@ int start_connecting(dpp::socket sockfd, const struct sockaddr *addr, socklen_t
152140
ULONG non_blocking = 1;
153141
ioctlsocket(sockfd, FIONBIO, &non_blocking);
154142
int rc = WSAConnect(sockfd, addr, addrlen, nullptr, nullptr, nullptr, nullptr);
155-
int err = EWOULDBLOCK;
143+
int err = ((rc == SOCKET_ERROR) ? WSAGetLastError() : 0);
156144
#else
157145
/* Standard POSIX connection behaviour */
158146
int rc = (::connect(sockfd, addr, addrlen));
159147
int err = errno;
160148
#endif
161-
if (rc == -1 && err != EWOULDBLOCK && err != EINPROGRESS) {
149+
if (rc == -1
150+
#ifdef _WIN32
151+
&& err != WSAEWOULDBLOCK
152+
#endif
153+
&& err != EWOULDBLOCK && err != EINPROGRESS) {
162154
throw connection_exception(err_connect_failure, strerror(errno));
155+
} else if (rc == 0) {
156+
/* We are ready RIGHT NOW, connection already succeeded */
157+
socket_events ev;
158+
ev.fd = sockfd;
159+
ev.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
160+
on_write(sockfd, ev);
163161
}
164162
return 0;
165163
}
@@ -215,7 +213,7 @@ ssl_connection::ssl_connection(cluster* creator, const std::string &_hostname, c
215213
try {
216214
ssl_connection::connect();
217215
}
218-
catch (std::exception&) {
216+
catch (const std::exception&) {
219217
cleanup();
220218
throw;
221219
}

0 commit comments

Comments
 (0)