2018-09-27 23:56:48 +02:00
|
|
|
/*
|
|
|
|
* IXWebSocket.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXWebSocket.h"
|
2018-12-23 23:14:38 +01:00
|
|
|
#include "IXSetThreadName.h"
|
2019-01-03 05:07:54 +01:00
|
|
|
#include "IXWebSocketHandshake.h"
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cassert>
|
|
|
|
|
2018-10-26 03:51:19 +02:00
|
|
|
namespace
|
|
|
|
{
|
2019-05-10 21:31:21 +02:00
|
|
|
uint64_t calculateRetryWaitMilliseconds(uint32_t retry_count)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-05-10 21:31:21 +02:00
|
|
|
uint64_t wait_time;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-05-10 21:31:21 +02:00
|
|
|
if (retry_count <= 6)
|
|
|
|
{
|
|
|
|
// max wait_time is 6400 ms (2 ^ 6 = 64)
|
|
|
|
wait_time = ((uint64_t)std::pow(2, retry_count) * 100L);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wait_time = 10 * 1000; // 10 sec
|
|
|
|
}
|
|
|
|
|
|
|
|
return wait_time;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 23:14:38 +01:00
|
|
|
namespace ix
|
|
|
|
{
|
2018-09-27 23:56:48 +02:00
|
|
|
OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr;
|
2019-01-04 03:33:08 +01:00
|
|
|
const int WebSocket::kDefaultHandShakeTimeoutSecs(60);
|
2019-04-18 18:24:16 +02:00
|
|
|
const int WebSocket::kDefaultPingIntervalSecs(-1);
|
|
|
|
const int WebSocket::kDefaultPingTimeoutSecs(-1);
|
|
|
|
const bool WebSocket::kDefaultEnablePong(true);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
WebSocket::WebSocket() :
|
|
|
|
_onMessageCallback(OnMessageCallback()),
|
|
|
|
_stop(false),
|
2019-01-04 03:33:08 +01:00
|
|
|
_automaticReconnection(true),
|
2019-01-24 21:42:49 +01:00
|
|
|
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
|
2019-04-18 18:24:16 +02:00
|
|
|
_enablePong(kDefaultEnablePong),
|
|
|
|
_pingIntervalSecs(kDefaultPingIntervalSecs),
|
|
|
|
_pingTimeoutSecs(kDefaultPingTimeoutSecs)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2018-12-31 07:00:49 +01:00
|
|
|
_ws.setOnCloseCallback(
|
2019-04-23 13:31:55 +02:00
|
|
|
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote)
|
2018-12-31 07:00:49 +01:00
|
|
|
{
|
|
|
|
_onMessageCallback(WebSocket_MessageType_Close, "", wireSize,
|
2019-01-04 02:44:10 +01:00
|
|
|
WebSocketErrorInfo(), WebSocketOpenInfo(),
|
2019-04-23 13:31:55 +02:00
|
|
|
WebSocketCloseInfo(code, reason, remote));
|
2018-12-31 07:00:49 +01:00
|
|
|
}
|
|
|
|
);
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
WebSocket::~WebSocket()
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
void WebSocket::setUrl(const std::string& url)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2018-09-27 23:56:48 +02:00
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
const std::string& WebSocket::getUrl() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
return _url;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::setPerMessageDeflateOptions(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions)
|
|
|
|
{
|
2018-12-06 17:27:28 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2018-11-10 03:23:49 +01:00
|
|
|
_perMessageDeflateOptions = perMessageDeflateOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebSocketPerMessageDeflateOptions& WebSocket::getPerMessageDeflateOptions() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
return _perMessageDeflateOptions;
|
|
|
|
}
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
void WebSocket::setHeartBeatPeriod(int heartBeatPeriodSecs)
|
2019-01-24 21:42:49 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2019-04-18 18:24:16 +02:00
|
|
|
_pingIntervalSecs = heartBeatPeriodSecs;
|
2019-01-24 21:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::getHeartBeatPeriod() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2019-04-18 18:24:16 +02:00
|
|
|
return _pingIntervalSecs;
|
2019-01-24 21:42:49 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
void WebSocket::setPingInterval(int pingIntervalSecs)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
_pingIntervalSecs = pingIntervalSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::getPingInterval() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
return _pingIntervalSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::setPingTimeout(int pingTimeoutSecs)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
_pingTimeoutSecs = pingTimeoutSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::getPingTimeout() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
return _pingTimeoutSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::enablePong()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
_enablePong = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::disablePong()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
|
|
|
_enablePong = false;
|
|
|
|
}
|
2019-04-20 01:57:38 +02:00
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
void WebSocket::start()
|
|
|
|
{
|
|
|
|
if (_thread.joinable()) return; // we've already been started
|
|
|
|
|
|
|
|
_thread = std::thread(&WebSocket::run, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::stop()
|
|
|
|
{
|
2019-01-02 06:25:15 +01:00
|
|
|
bool automaticReconnection = _automaticReconnection;
|
|
|
|
|
|
|
|
// This value needs to be forced when shutting down, it is restored later
|
2018-09-27 23:56:48 +02:00
|
|
|
_automaticReconnection = false;
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
2019-05-11 18:51:26 +02:00
|
|
|
if (_thread.joinable())
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-05-11 18:51:26 +02:00
|
|
|
_stop = true;
|
|
|
|
_thread.join();
|
|
|
|
_stop = false;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
_automaticReconnection = automaticReconnection;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 03:33:08 +01:00
|
|
|
WebSocketInitResult WebSocket::connect(int timeoutSecs)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2019-01-24 21:42:49 +01:00
|
|
|
_ws.configure(_perMessageDeflateOptions,
|
2019-04-18 18:24:16 +02:00
|
|
|
_enablePong,
|
|
|
|
_pingIntervalSecs,
|
|
|
|
_pingTimeoutSecs);
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 03:33:08 +01:00
|
|
|
WebSocketInitResult status = _ws.connectToUrl(_url, timeoutSecs);
|
2018-09-27 23:56:48 +02:00
|
|
|
if (!status.success)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
_onMessageCallback(WebSocket_MessageType_Open, "", 0,
|
2019-02-21 03:59:07 +01:00
|
|
|
WebSocketErrorInfo(),
|
2019-01-04 02:44:10 +01:00
|
|
|
WebSocketOpenInfo(status.uri, status.headers),
|
|
|
|
WebSocketCloseInfo());
|
2018-09-27 23:56:48 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-01-04 03:33:08 +01:00
|
|
|
WebSocketInitResult WebSocket::connectToSocket(int fd, int timeoutSecs)
|
2018-12-31 07:00:49 +01:00
|
|
|
{
|
2018-12-31 21:43:47 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_configMutex);
|
2019-04-18 18:24:16 +02:00
|
|
|
_ws.configure(_perMessageDeflateOptions,
|
2019-04-20 01:57:38 +02:00
|
|
|
_enablePong,
|
2019-04-18 18:24:16 +02:00
|
|
|
_pingIntervalSecs,
|
|
|
|
_pingTimeoutSecs);
|
2018-12-31 21:43:47 +01:00
|
|
|
}
|
|
|
|
|
2019-01-04 03:33:08 +01:00
|
|
|
WebSocketInitResult status = _ws.connectToSocket(fd, timeoutSecs);
|
2018-12-31 21:43:47 +01:00
|
|
|
if (!status.success)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageCallback(WebSocket_MessageType_Open, "", 0,
|
2019-02-21 03:59:07 +01:00
|
|
|
WebSocketErrorInfo(),
|
2019-01-04 02:44:10 +01:00
|
|
|
WebSocketOpenInfo(status.uri, status.headers),
|
|
|
|
WebSocketCloseInfo());
|
2018-12-31 21:43:47 +01:00
|
|
|
return status;
|
2018-12-31 07:00:49 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
bool WebSocket::isConnected() const
|
|
|
|
{
|
|
|
|
return getReadyState() == WebSocket_ReadyState_Open;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocket::isClosing() const
|
|
|
|
{
|
|
|
|
return getReadyState() == WebSocket_ReadyState_Closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::close()
|
|
|
|
{
|
|
|
|
_ws.close();
|
|
|
|
}
|
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
void WebSocket::checkConnection(bool firstConnectionAttempt)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
using millis = std::chrono::duration<double, std::milli>;
|
2019-05-11 18:51:26 +02:00
|
|
|
|
|
|
|
uint32_t retries = 0;
|
2018-09-27 23:56:48 +02:00
|
|
|
millis duration;
|
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
// Try to connect perpertually
|
2019-05-11 18:51:26 +02:00
|
|
|
while (true)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-05-11 18:51:26 +02:00
|
|
|
if (isConnected() || isClosing() || _stop)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-05-11 18:51:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
if (!firstConnectionAttempt && !_automaticReconnection)
|
2019-05-11 18:51:26 +02:00
|
|
|
{
|
2019-05-11 19:24:28 +02:00
|
|
|
// Do not attempt to reconnect
|
2019-05-11 18:51:26 +02:00
|
|
|
break;
|
2019-04-30 06:12:34 +02:00
|
|
|
}
|
2019-05-11 18:51:26 +02:00
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
firstConnectionAttempt = false;
|
2019-05-11 18:51:26 +02:00
|
|
|
|
|
|
|
// Only sleep if we are retrying
|
2019-05-11 19:24:28 +02:00
|
|
|
if (retries != 0)
|
2019-04-30 06:12:34 +02:00
|
|
|
{
|
2019-05-11 19:24:28 +02:00
|
|
|
// to do: make sleeping conditional
|
2019-05-11 18:51:26 +02:00
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
// Try to connect synchronously
|
|
|
|
ix::WebSocketInitResult status = connect(_handshakeTimeoutSecs);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-05-11 18:51:26 +02:00
|
|
|
if (!status.success)
|
|
|
|
{
|
|
|
|
WebSocketErrorInfo connectErr;
|
2019-04-30 06:12:34 +02:00
|
|
|
|
2019-05-11 18:51:26 +02:00
|
|
|
if (_automaticReconnection)
|
2019-04-30 06:12:34 +02:00
|
|
|
{
|
|
|
|
duration = millis(calculateRetryWaitMilliseconds(retries++));
|
|
|
|
|
|
|
|
connectErr.wait_time = duration.count();
|
2019-05-11 18:51:26 +02:00
|
|
|
connectErr.retries = retries;
|
2019-04-30 06:12:34 +02:00
|
|
|
}
|
2019-05-11 18:51:26 +02:00
|
|
|
|
|
|
|
connectErr.reason = status.errorStr;
|
|
|
|
connectErr.http_status = status.http_status;
|
|
|
|
|
|
|
|
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
2019-05-11 19:24:28 +02:00
|
|
|
connectErr, WebSocketOpenInfo(),
|
|
|
|
WebSocketCloseInfo());
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::run()
|
|
|
|
{
|
2019-04-26 01:07:49 +02:00
|
|
|
setThreadName(getUrl());
|
2018-12-23 23:14:38 +01:00
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
bool firstConnectionAttempt = true;
|
2019-05-11 18:51:26 +02:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
while (true)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
// 1. Make sure we are always connected
|
2019-05-11 19:24:28 +02:00
|
|
|
checkConnection(firstConnectionAttempt);
|
2019-05-11 18:51:26 +02:00
|
|
|
|
2019-05-11 19:24:28 +02:00
|
|
|
firstConnectionAttempt = false;
|
2019-05-11 18:51:26 +02:00
|
|
|
|
|
|
|
// if here we are closed then checkConnection was not able to connect
|
|
|
|
if (getReadyState() == WebSocket_ReadyState_Closed)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
// 2. Poll to see if there's any new data available
|
2019-05-09 18:21:05 +02:00
|
|
|
WebSocketTransport::PollPostTreatment pollPostTreatment = _ws.poll();
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
// 3. Dispatch the incoming messages
|
|
|
|
_ws.dispatch(
|
2019-05-09 18:21:05 +02:00
|
|
|
pollPostTreatment,
|
2018-10-25 21:01:47 +02:00
|
|
|
[this](const std::string& msg,
|
2018-11-10 03:23:49 +01:00
|
|
|
size_t wireSize,
|
2018-11-15 00:52:28 +01:00
|
|
|
bool decompressionError,
|
2018-10-25 21:01:47 +02:00
|
|
|
WebSocketTransport::MessageKind messageKind)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2018-10-25 21:01:47 +02:00
|
|
|
WebSocketMessageType webSocketMessageType;
|
|
|
|
switch (messageKind)
|
|
|
|
{
|
2019-05-11 18:51:26 +02:00
|
|
|
default:
|
2018-10-25 21:01:47 +02:00
|
|
|
case WebSocketTransport::MSG:
|
|
|
|
{
|
|
|
|
webSocketMessageType = WebSocket_MessageType_Message;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case WebSocketTransport::PING:
|
|
|
|
{
|
|
|
|
webSocketMessageType = WebSocket_MessageType_Ping;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case WebSocketTransport::PONG:
|
|
|
|
{
|
|
|
|
webSocketMessageType = WebSocket_MessageType_Pong;
|
|
|
|
} break;
|
2019-03-11 19:12:43 +01:00
|
|
|
|
|
|
|
case WebSocketTransport::FRAGMENT:
|
|
|
|
{
|
|
|
|
webSocketMessageType = WebSocket_MessageType_Fragment;
|
|
|
|
} break;
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 00:52:28 +01:00
|
|
|
WebSocketErrorInfo webSocketErrorInfo;
|
|
|
|
webSocketErrorInfo.decompressionError = decompressionError;
|
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
_onMessageCallback(webSocketMessageType, msg, wireSize,
|
2019-01-04 02:44:10 +01:00
|
|
|
webSocketErrorInfo, WebSocketOpenInfo(),
|
|
|
|
WebSocketCloseInfo());
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::setOnMessageCallback(const OnMessageCallback& callback)
|
|
|
|
{
|
2019-02-21 03:59:07 +01:00
|
|
|
_onMessageCallback = callback;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::setTrafficTrackerCallback(const OnTrafficTrackerCallback& callback)
|
|
|
|
{
|
|
|
|
_onTrafficTrackerCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::resetTrafficTrackerCallback()
|
|
|
|
{
|
|
|
|
setTrafficTrackerCallback(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::invokeTrafficTrackerCallback(size_t size, bool incoming)
|
|
|
|
{
|
|
|
|
if (_onTrafficTrackerCallback)
|
|
|
|
{
|
|
|
|
_onTrafficTrackerCallback(size, incoming);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
WebSocketSendInfo WebSocket::send(const std::string& text,
|
|
|
|
const OnProgressCallback& onProgressCallback)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
2019-03-22 22:22:58 +01:00
|
|
|
return sendMessage(text, SendMessageKind::Binary, onProgressCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketSendInfo WebSocket::sendText(const std::string& text,
|
|
|
|
const OnProgressCallback& onProgressCallback)
|
|
|
|
{
|
|
|
|
return sendMessage(text, SendMessageKind::Text, onProgressCallback);
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketSendInfo WebSocket::ping(const std::string& text)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
|
|
|
// Standard limit ping message size
|
|
|
|
constexpr size_t pingMaxPayloadSize = 125;
|
2018-11-10 03:23:49 +01:00
|
|
|
if (text.size() > pingMaxPayloadSize) return WebSocketSendInfo(false);
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-03-22 22:22:58 +01:00
|
|
|
return sendMessage(text, SendMessageKind::Ping);
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
WebSocketSendInfo WebSocket::sendMessage(const std::string& text,
|
2019-03-22 22:22:58 +01:00
|
|
|
SendMessageKind sendMessageKind,
|
2019-02-21 03:59:07 +01:00
|
|
|
const OnProgressCallback& onProgressCallback)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
if (!isConnected()) return WebSocketSendInfo(false);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// It is OK to read and write on the same socket in 2 different threads.
|
|
|
|
// https://stackoverflow.com/questions/1981372/are-parallel-calls-to-send-recv-on-the-same-socket-valid
|
|
|
|
//
|
|
|
|
// This makes it so that messages are sent right away, and we dont need
|
|
|
|
// a timeout while we poll to keep wake ups to a minimum (which helps
|
|
|
|
// with battery life), and use the system select call to notify us when
|
|
|
|
// incoming messages are arriving / there's data to be received.
|
|
|
|
//
|
|
|
|
std::lock_guard<std::mutex> lock(_writeMutex);
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketSendInfo webSocketSendInfo;
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-03-22 22:22:58 +01:00
|
|
|
switch (sendMessageKind)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
2019-03-22 22:22:58 +01:00
|
|
|
case SendMessageKind::Text:
|
|
|
|
{
|
|
|
|
webSocketSendInfo = _ws.sendText(text, onProgressCallback);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SendMessageKind::Binary:
|
|
|
|
{
|
|
|
|
webSocketSendInfo = _ws.sendBinary(text, onProgressCallback);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case SendMessageKind::Ping:
|
|
|
|
{
|
|
|
|
webSocketSendInfo = _ws.sendPing(text);
|
|
|
|
} break;
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
return webSocketSendInfo;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadyState WebSocket::getReadyState() const
|
|
|
|
{
|
2019-02-21 03:59:07 +01:00
|
|
|
switch (_ws.getReadyState())
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
case ix::WebSocketTransport::OPEN: return WebSocket_ReadyState_Open;
|
|
|
|
case ix::WebSocketTransport::CONNECTING: return WebSocket_ReadyState_Connecting;
|
|
|
|
case ix::WebSocketTransport::CLOSING: return WebSocket_ReadyState_Closing;
|
|
|
|
case ix::WebSocketTransport::CLOSED: return WebSocket_ReadyState_Closed;
|
2019-01-06 01:26:11 +01:00
|
|
|
default: return WebSocket_ReadyState_Closed;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WebSocket::readyStateToString(ReadyState readyState)
|
|
|
|
{
|
|
|
|
switch (readyState)
|
|
|
|
{
|
|
|
|
case WebSocket_ReadyState_Open: return "OPEN";
|
|
|
|
case WebSocket_ReadyState_Connecting: return "CONNECTING";
|
|
|
|
case WebSocket_ReadyState_Closing: return "CLOSING";
|
|
|
|
case WebSocket_ReadyState_Closed: return "CLOSED";
|
2019-01-06 01:26:11 +01:00
|
|
|
default: return "CLOSED";
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 06:25:15 +01:00
|
|
|
|
|
|
|
void WebSocket::enableAutomaticReconnection()
|
|
|
|
{
|
|
|
|
_automaticReconnection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::disableAutomaticReconnection()
|
|
|
|
{
|
|
|
|
_automaticReconnection = false;
|
|
|
|
}
|
2019-03-14 07:09:45 +01:00
|
|
|
|
|
|
|
size_t WebSocket::bufferedAmount() const
|
|
|
|
{
|
|
|
|
return _ws.bufferedAmount();
|
|
|
|
}
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|