Compare commits
4 Commits
feature/no
...
v1.5.0
Author | SHA1 | Date | |
---|---|---|---|
753fc845ac | |||
5dbc00bbfe | |||
14ec8522ef | |||
0c2d1c22bc |
@ -199,6 +199,8 @@ make install # will install to /usr/local on Unix, on macOS it is a good idea to
|
||||
|
||||
Headers and a static library will be installed to the target dir.
|
||||
|
||||
A [conan](https://conan.io/) file is available at [conan-IXWebSocket](https://github.com/Zinnion/conan-IXWebSocket).
|
||||
|
||||
There is a unittest which can be executed by typing `make test`.
|
||||
|
||||
There is a Dockerfile for running some code on Linux. To use docker-compose you must make a docker container first.
|
||||
|
@ -35,7 +35,6 @@ namespace ix
|
||||
_activeJobs.erase(_id);
|
||||
}
|
||||
|
||||
// we want hostname to be copied, not passed as a const reference
|
||||
struct addrinfo* DNSLookup::getAddrInfo(const std::string& hostname,
|
||||
int port,
|
||||
std::string& errMsg)
|
||||
|
@ -196,6 +196,25 @@ namespace ix
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Socket::isWaitNeeded()
|
||||
{
|
||||
int err = getErrno();
|
||||
|
||||
if (err == EWOULDBLOCK || err == EAGAIN)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Socket::closeSocket(int fd)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
@ -228,8 +247,7 @@ namespace ix
|
||||
return ret == len;
|
||||
}
|
||||
// There is possibly something to be writen, try again
|
||||
else if (ret < 0 && (getErrno() == EWOULDBLOCK ||
|
||||
getErrno() == EAGAIN))
|
||||
else if (ret < 0 && Socket::isWaitNeeded())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@ -257,8 +275,7 @@ namespace ix
|
||||
return true;
|
||||
}
|
||||
// There is possibly something to be read, try again
|
||||
else if (ret < 0 && (getErrno() == EWOULDBLOCK ||
|
||||
getErrno() == EAGAIN))
|
||||
else if (ret < 0 && Socket::isWaitNeeded())
|
||||
{
|
||||
// Wait with a 1ms timeout until the socket is ready to read.
|
||||
// This way we are not busy looping
|
||||
@ -317,13 +334,12 @@ namespace ix
|
||||
size_t size = std::min(kChunkSize, length - output.size());
|
||||
ssize_t ret = recv((char*)&_readBuffer[0], size);
|
||||
|
||||
if (ret <= 0 && (getErrno() != EWOULDBLOCK &&
|
||||
getErrno() != EAGAIN))
|
||||
if (ret <= 0 && !Socket::isWaitNeeded())
|
||||
{
|
||||
// Error
|
||||
return std::make_pair(false, std::string());
|
||||
}
|
||||
else if (ret > 0)
|
||||
else
|
||||
{
|
||||
output.insert(output.end(),
|
||||
_readBuffer.begin(),
|
||||
|
@ -41,8 +41,6 @@ namespace ix
|
||||
virtual ~Socket();
|
||||
bool init(std::string& errorMsg);
|
||||
|
||||
void configure();
|
||||
|
||||
// Functions to check whether there is activity on the socket
|
||||
PollResultType poll(int timeoutSecs = kDefaultPollTimeout);
|
||||
bool wakeUpFromPoll(uint8_t wakeUpCode);
|
||||
@ -76,6 +74,7 @@ namespace ix
|
||||
const CancellationRequest& isCancellationRequested);
|
||||
|
||||
static int getErrno();
|
||||
static bool isWaitNeeded();
|
||||
|
||||
// Used as special codes for pipe communication
|
||||
static const uint64_t kSendRequest;
|
||||
|
@ -247,7 +247,7 @@ namespace ix
|
||||
|
||||
if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) < 0)
|
||||
{
|
||||
if (Socket::getErrno() != EWOULDBLOCK)
|
||||
if (!Socket::isWaitNeeded())
|
||||
{
|
||||
// FIXME: that error should be propagated
|
||||
std::stringstream ss;
|
||||
|
@ -38,7 +38,6 @@ namespace ix
|
||||
WebSocket::WebSocket() :
|
||||
_onMessageCallback(OnMessageCallback()),
|
||||
_stop(false),
|
||||
_backgroundThreadRunning(false),
|
||||
_automaticReconnection(true),
|
||||
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
|
||||
_enablePong(kDefaultEnablePong),
|
||||
@ -136,7 +135,6 @@ namespace ix
|
||||
{
|
||||
if (_thread.joinable()) return; // we've already been started
|
||||
|
||||
_backgroundThreadRunning = true;
|
||||
_thread = std::thread(&WebSocket::run, this);
|
||||
}
|
||||
|
||||
@ -157,7 +155,6 @@ namespace ix
|
||||
|
||||
_stop = true;
|
||||
_thread.join();
|
||||
_backgroundThreadRunning = false;
|
||||
_stop = false;
|
||||
|
||||
_automaticReconnection = automaticReconnection;
|
||||
@ -232,16 +229,12 @@ namespace ix
|
||||
using millis = std::chrono::duration<double, std::milli>;
|
||||
millis duration;
|
||||
|
||||
while (true)
|
||||
// Try to connect only once when we don't have automaticReconnection setup
|
||||
if (!isConnected() && !isClosing() && !_stop && !_automaticReconnection)
|
||||
{
|
||||
if (isConnected() || isClosing() || _stop || !_automaticReconnection)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
status = connect(_handshakeTimeoutSecs);
|
||||
|
||||
if (!status.success && !_stop)
|
||||
if (!status.success)
|
||||
{
|
||||
duration = millis(calculateRetryWaitMilliseconds(retries++));
|
||||
|
||||
@ -252,8 +245,38 @@ namespace ix
|
||||
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
||||
connectErr, WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise try to reconnect perpertually
|
||||
while (true)
|
||||
{
|
||||
if (isConnected() || isClosing() || _stop || !_automaticReconnection)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(duration);
|
||||
status = connect(_handshakeTimeoutSecs);
|
||||
|
||||
if (!status.success)
|
||||
{
|
||||
duration = millis(calculateRetryWaitMilliseconds(retries++));
|
||||
|
||||
connectErr.retries = retries;
|
||||
connectErr.wait_time = duration.count();
|
||||
connectErr.reason = status.errorStr;
|
||||
connectErr.http_status = status.http_status;
|
||||
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
||||
connectErr, WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo());
|
||||
|
||||
// Only sleep if we aren't in the middle of stopping
|
||||
if (!_stop)
|
||||
{
|
||||
std::this_thread::sleep_for(duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -317,11 +340,8 @@ namespace ix
|
||||
WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
|
||||
});
|
||||
|
||||
// 4. In blocking mode, getting out of this function is triggered by
|
||||
// an explicit disconnection from the callback, or by the remote end
|
||||
// closing the connection, ie isConnected() == false.
|
||||
// closing the connection, ie isConnectedOrClosing() == false.
|
||||
if (!_backgroundThreadRunning && !isConnected() && !_automaticReconnection) return;
|
||||
// If we aren't trying to reconnect automatically, exit if we aren't connected
|
||||
if (!isConnected() && !_automaticReconnection) return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,7 +154,6 @@ namespace ix
|
||||
static OnTrafficTrackerCallback _onTrafficTrackerCallback;
|
||||
|
||||
std::atomic<bool> _stop;
|
||||
std::atomic<bool> _backgroundThreadRunning;
|
||||
std::atomic<bool> _automaticReconnection;
|
||||
std::thread _thread;
|
||||
std::mutex _writeMutex;
|
||||
|
@ -296,8 +296,7 @@ namespace ix
|
||||
{
|
||||
ssize_t ret = _socket->recv((char*)&_readbuf[0], _readbuf.size());
|
||||
|
||||
if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK ||
|
||||
_socket->getErrno() == EAGAIN))
|
||||
if (ret < 0 && Socket::isWaitNeeded())
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -553,7 +552,7 @@ namespace ix
|
||||
// Get the reason.
|
||||
std::string reason(_rxbuf.begin()+ws.header_size + 2,
|
||||
_rxbuf.begin()+ws.header_size + (size_t) ws.N);
|
||||
|
||||
|
||||
bool remote = true;
|
||||
|
||||
close(code, reason, _rxbuf.size(), remote);
|
||||
@ -844,8 +843,7 @@ namespace ix
|
||||
{
|
||||
ssize_t ret = _socket->send((char*)&_txbuf[0], _txbuf.size());
|
||||
|
||||
if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK ||
|
||||
_socket->getErrno() == EAGAIN))
|
||||
if (ret < 0 && Socket::isWaitNeeded())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -8,12 +8,11 @@ project (ixwebsocket_unittest)
|
||||
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH})
|
||||
find_package(Sanitizers)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
|
||||
set (CMAKE_CXX_STANDARD 14)
|
||||
|
||||
if (NOT WIN32)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
|
||||
option(USE_TLS "Add TLS support" ON)
|
||||
endif()
|
||||
|
||||
|
@ -39,6 +39,7 @@ namespace ix
|
||||
std::cout << "Disabling automatic reconnection with "
|
||||
"_webSocket.disableAutomaticReconnection()"
|
||||
" not supported yet" << std::endl;
|
||||
_webSocket.disableAutomaticReconnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ namespace ix
|
||||
{
|
||||
if (!redisClient.publish(channel, message, errMsg))
|
||||
{
|
||||
std::cerr << "Error publishing to channel " << channel
|
||||
std::cerr << "Error publishing to channel " << channel
|
||||
<< "error: " << errMsg
|
||||
<< std::endl;
|
||||
return 1;
|
||||
|
Reference in New Issue
Block a user