add cancellation support while connecting, to speed up WebSocket::stop
This commit is contained in:
@ -23,6 +23,7 @@
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@ -32,7 +33,6 @@
|
||||
# include <linux/tcp.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
void closeSocket(int fd)
|
||||
@ -49,7 +49,8 @@ namespace ix
|
||||
{
|
||||
bool SocketConnect::connectToAddress(const struct addrinfo *address,
|
||||
int& sockfd,
|
||||
std::string& errMsg)
|
||||
std::string& errMsg,
|
||||
CancellationRequest isCancellationRequested)
|
||||
{
|
||||
sockfd = -1;
|
||||
|
||||
@ -75,10 +76,30 @@ namespace ix
|
||||
return false;
|
||||
}
|
||||
|
||||
// 10 seconds timeout, each time we wait for 50ms with select -> 200 attempts
|
||||
int maxRetries = 200;
|
||||
// std::cout << "I WAS HERE A" << std::endl;
|
||||
|
||||
//
|
||||
// If during a connection attempt the request remains idle for longer
|
||||
// than the timeout interval, the request is considered to have timed
|
||||
// out. The default timeout interval is 60 seconds.
|
||||
//
|
||||
// See https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1414063-timeoutinterval?language=objc
|
||||
//
|
||||
// 60 seconds timeout, each time we wait for 50ms with select -> 1200 attempts
|
||||
//
|
||||
int selectTimeOut = 50 * 1000; // In micro-seconds => 50ms
|
||||
int maxRetries = 60 * 1000 * 1000 / selectTimeOut;
|
||||
|
||||
for (int i = 0; i < maxRetries; ++i)
|
||||
{
|
||||
if (isCancellationRequested())
|
||||
{
|
||||
closeSocket(fd);
|
||||
sockfd = -1;
|
||||
errMsg = "Cancelled";
|
||||
return false;
|
||||
}
|
||||
|
||||
fd_set wfds;
|
||||
FD_ZERO(&wfds);
|
||||
FD_SET(fd, &wfds);
|
||||
@ -86,7 +107,7 @@ namespace ix
|
||||
// 50ms timeout
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 1000 * 50;
|
||||
timeout.tv_usec = selectTimeOut;
|
||||
|
||||
select(fd + 1, nullptr, &wfds, nullptr, &timeout);
|
||||
|
||||
@ -94,8 +115,8 @@ namespace ix
|
||||
if (!FD_ISSET(fd, &wfds)) continue;
|
||||
|
||||
// Something was written to the socket
|
||||
int optval = -1;
|
||||
socklen_t optlen;
|
||||
int optval;
|
||||
socklen_t optlen = sizeof(optval);
|
||||
|
||||
// getsockopt() puts the errno value for connect into optval so 0
|
||||
// means no-error.
|
||||
@ -123,7 +144,8 @@ namespace ix
|
||||
|
||||
int SocketConnect::connect(const std::string& hostname,
|
||||
int port,
|
||||
std::string& errMsg)
|
||||
std::string& errMsg,
|
||||
CancellationRequest isCancellationRequested)
|
||||
{
|
||||
//
|
||||
// First do DNS resolution
|
||||
@ -155,7 +177,7 @@ namespace ix
|
||||
//
|
||||
// Second try to connect to the remote host
|
||||
//
|
||||
success = connectToAddress(address, sockfd, errMsg);
|
||||
success = connectToAddress(address, sockfd, errMsg, isCancellationRequested);
|
||||
if (success)
|
||||
{
|
||||
break;
|
||||
|
Reference in New Issue
Block a user