use C++11 enums (#67)
* use C++11 enums * small rename * update tests * update tests * update ws * update ws * update README.md
This commit is contained in:
committed by
Benjamin Sergeant
parent
80226cb7d3
commit
99a3bbc4f9
@ -52,7 +52,7 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Cannot parse url: " << url;
|
||||
return std::make_tuple(code, HttpErrorCode_UrlMalformed,
|
||||
return std::make_tuple(code, HttpErrorCode::UrlMalformed,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -63,7 +63,7 @@ namespace ix
|
||||
|
||||
if (!_socket)
|
||||
{
|
||||
return std::make_tuple(code, HttpErrorCode_CannotCreateSocket,
|
||||
return std::make_tuple(code, HttpErrorCode::CannotCreateSocket,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -116,7 +116,7 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Cannot connect to url: " << url;
|
||||
return std::make_tuple(code, HttpErrorCode_CannotConnect,
|
||||
return std::make_tuple(code, HttpErrorCode::CannotConnect,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -142,7 +142,7 @@ namespace ix
|
||||
if (!_socket->writeBytes(req, isCancellationRequested))
|
||||
{
|
||||
std::string errorMsg("Cannot send request");
|
||||
return std::make_tuple(code, HttpErrorCode_SendError,
|
||||
return std::make_tuple(code, HttpErrorCode::SendError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -156,7 +156,7 @@ namespace ix
|
||||
if (!lineValid)
|
||||
{
|
||||
std::string errorMsg("Cannot retrieve status line");
|
||||
return std::make_tuple(code, HttpErrorCode_CannotReadStatusLine,
|
||||
return std::make_tuple(code, HttpErrorCode::CannotReadStatusLine,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -171,7 +171,7 @@ namespace ix
|
||||
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
|
||||
{
|
||||
std::string errorMsg("Cannot parse response code from status line");
|
||||
return std::make_tuple(code, HttpErrorCode_MissingStatus,
|
||||
return std::make_tuple(code, HttpErrorCode::MissingStatus,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -183,7 +183,7 @@ namespace ix
|
||||
if (!headersValid)
|
||||
{
|
||||
std::string errorMsg("Cannot parse http headers");
|
||||
return std::make_tuple(code, HttpErrorCode_HeaderParsingError,
|
||||
return std::make_tuple(code, HttpErrorCode::HeaderParsingError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -194,7 +194,7 @@ namespace ix
|
||||
if (headers.find("Location") == headers.end())
|
||||
{
|
||||
std::string errorMsg("Missing location header for redirect");
|
||||
return std::make_tuple(code, HttpErrorCode_MissingLocation,
|
||||
return std::make_tuple(code, HttpErrorCode::MissingLocation,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -203,7 +203,7 @@ namespace ix
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Too many redirects: " << redirects;
|
||||
return std::make_tuple(code, HttpErrorCode_TooManyRedirects,
|
||||
return std::make_tuple(code, HttpErrorCode::TooManyRedirects,
|
||||
headers, payload, ss.str(),
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -215,7 +215,7 @@ namespace ix
|
||||
|
||||
if (verb == "HEAD")
|
||||
{
|
||||
return std::make_tuple(code, HttpErrorCode_Ok,
|
||||
return std::make_tuple(code, HttpErrorCode::Ok,
|
||||
headers, payload, std::string(),
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -236,7 +236,7 @@ namespace ix
|
||||
if (!chunkResult.first)
|
||||
{
|
||||
errorMsg = "Cannot read chunk";
|
||||
return std::make_tuple(code, HttpErrorCode_ChunkReadError,
|
||||
return std::make_tuple(code, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -254,7 +254,7 @@ namespace ix
|
||||
|
||||
if (!lineResult.first)
|
||||
{
|
||||
return std::make_tuple(code, HttpErrorCode_ChunkReadError,
|
||||
return std::make_tuple(code, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -281,7 +281,7 @@ namespace ix
|
||||
if (!chunkResult.first)
|
||||
{
|
||||
errorMsg = "Cannot read chunk";
|
||||
return std::make_tuple(code, HttpErrorCode_ChunkReadError,
|
||||
return std::make_tuple(code, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -292,7 +292,7 @@ namespace ix
|
||||
|
||||
if (!lineResult.first)
|
||||
{
|
||||
return std::make_tuple(code, HttpErrorCode_ChunkReadError,
|
||||
return std::make_tuple(code, HttpErrorCode::ChunkReadError,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -307,7 +307,7 @@ namespace ix
|
||||
else
|
||||
{
|
||||
std::string errorMsg("Cannot read http body");
|
||||
return std::make_tuple(code, HttpErrorCode_CannotReadBody,
|
||||
return std::make_tuple(code, HttpErrorCode::CannotReadBody,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
@ -321,14 +321,14 @@ namespace ix
|
||||
if (!gzipInflate(payload, decompressedPayload))
|
||||
{
|
||||
std::string errorMsg("Error decompressing payload");
|
||||
return std::make_tuple(code, HttpErrorCode_Gzip,
|
||||
return std::make_tuple(code, HttpErrorCode::Gzip,
|
||||
headers, payload, errorMsg,
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
payload = decompressedPayload;
|
||||
}
|
||||
|
||||
return std::make_tuple(code, HttpErrorCode_Ok,
|
||||
return std::make_tuple(code, HttpErrorCode::Ok,
|
||||
headers, payload, std::string(),
|
||||
uploadSize, downloadSize);
|
||||
}
|
||||
|
@ -19,23 +19,23 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
enum HttpErrorCode
|
||||
enum class HttpErrorCode
|
||||
{
|
||||
HttpErrorCode_Ok = 0,
|
||||
HttpErrorCode_CannotConnect = 1,
|
||||
HttpErrorCode_Timeout = 2,
|
||||
HttpErrorCode_Gzip = 3,
|
||||
HttpErrorCode_UrlMalformed = 4,
|
||||
HttpErrorCode_CannotCreateSocket = 5,
|
||||
HttpErrorCode_SendError = 6,
|
||||
HttpErrorCode_ReadError = 7,
|
||||
HttpErrorCode_CannotReadStatusLine = 8,
|
||||
HttpErrorCode_MissingStatus = 9,
|
||||
HttpErrorCode_HeaderParsingError = 10,
|
||||
HttpErrorCode_MissingLocation = 11,
|
||||
HttpErrorCode_TooManyRedirects = 12,
|
||||
HttpErrorCode_ChunkReadError = 13,
|
||||
HttpErrorCode_CannotReadBody = 14
|
||||
Ok = 0,
|
||||
CannotConnect = 1,
|
||||
Timeout = 2,
|
||||
Gzip = 3,
|
||||
UrlMalformed = 4,
|
||||
CannotCreateSocket = 5,
|
||||
SendError = 6,
|
||||
ReadError = 7,
|
||||
CannotReadStatusLine = 8,
|
||||
MissingStatus = 9,
|
||||
HeaderParsingError = 10,
|
||||
MissingLocation = 11,
|
||||
TooManyRedirects = 12,
|
||||
ChunkReadError = 13,
|
||||
CannotReadBody = 14
|
||||
};
|
||||
|
||||
using HttpResponse = std::tuple<int, // status
|
||||
|
@ -41,12 +41,12 @@ namespace ix
|
||||
|
||||
enum class PollResultType
|
||||
{
|
||||
ReadyForRead = 0,
|
||||
ReadyForWrite = 1,
|
||||
Timeout = 2,
|
||||
Error = 3,
|
||||
SendRequest = 4,
|
||||
CloseRequest = 5
|
||||
ReadyForRead = 0,
|
||||
ReadyForWrite = 1,
|
||||
Timeout = 2,
|
||||
Error = 3,
|
||||
SendRequest = 4,
|
||||
CloseRequest = 5
|
||||
};
|
||||
|
||||
class Socket {
|
||||
|
@ -51,7 +51,7 @@ namespace ix
|
||||
_ws.setOnCloseCallback(
|
||||
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote)
|
||||
{
|
||||
_onMessageCallback(WebSocket_MessageType_Close, "", wireSize,
|
||||
_onMessageCallback(WebSocketMessageType::Close, "", wireSize,
|
||||
WebSocketErrorInfo(), WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo(code, reason, remote));
|
||||
}
|
||||
@ -172,7 +172,7 @@ namespace ix
|
||||
return status;
|
||||
}
|
||||
|
||||
_onMessageCallback(WebSocket_MessageType_Open, "", 0,
|
||||
_onMessageCallback(WebSocketMessageType::Open, "", 0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo());
|
||||
@ -195,7 +195,7 @@ namespace ix
|
||||
return status;
|
||||
}
|
||||
|
||||
_onMessageCallback(WebSocket_MessageType_Open, "", 0,
|
||||
_onMessageCallback(WebSocketMessageType::Open, "", 0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo());
|
||||
@ -204,12 +204,12 @@ namespace ix
|
||||
|
||||
bool WebSocket::isConnected() const
|
||||
{
|
||||
return getReadyState() == WebSocket_ReadyState_Open;
|
||||
return getReadyState() == ReadyState::Open;
|
||||
}
|
||||
|
||||
bool WebSocket::isClosing() const
|
||||
{
|
||||
return getReadyState() == WebSocket_ReadyState_Closing;
|
||||
return getReadyState() == ReadyState::Closing;
|
||||
}
|
||||
|
||||
void WebSocket::close()
|
||||
@ -266,7 +266,7 @@ namespace ix
|
||||
connectErr.reason = status.errorStr;
|
||||
connectErr.http_status = status.http_status;
|
||||
|
||||
_onMessageCallback(WebSocket_MessageType_Error, "", 0,
|
||||
_onMessageCallback(WebSocketMessageType::Error, "", 0,
|
||||
connectErr, WebSocketOpenInfo(),
|
||||
WebSocketCloseInfo());
|
||||
}
|
||||
@ -287,17 +287,17 @@ namespace ix
|
||||
firstConnectionAttempt = false;
|
||||
|
||||
// if here we are closed then checkConnection was not able to connect
|
||||
if (getReadyState() == WebSocket_ReadyState_Closed)
|
||||
if (getReadyState() == ReadyState::Closed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// 2. Poll to see if there's any new data available
|
||||
WebSocketTransport::PollPostTreatment pollPostTreatment = _ws.poll();
|
||||
WebSocketTransport::PollResult pollResult = _ws.poll();
|
||||
|
||||
// 3. Dispatch the incoming messages
|
||||
_ws.dispatch(
|
||||
pollPostTreatment,
|
||||
pollResult,
|
||||
[this](const std::string& msg,
|
||||
size_t wireSize,
|
||||
bool decompressionError,
|
||||
@ -307,24 +307,24 @@ namespace ix
|
||||
switch (messageKind)
|
||||
{
|
||||
default:
|
||||
case WebSocketTransport::MSG:
|
||||
case WebSocketTransport::MessageKind::MSG:
|
||||
{
|
||||
webSocketMessageType = WebSocket_MessageType_Message;
|
||||
webSocketMessageType = WebSocketMessageType::Message;
|
||||
} break;
|
||||
|
||||
case WebSocketTransport::PING:
|
||||
case WebSocketTransport::MessageKind::PING:
|
||||
{
|
||||
webSocketMessageType = WebSocket_MessageType_Ping;
|
||||
webSocketMessageType = WebSocketMessageType::Ping;
|
||||
} break;
|
||||
|
||||
case WebSocketTransport::PONG:
|
||||
case WebSocketTransport::MessageKind::PONG:
|
||||
{
|
||||
webSocketMessageType = WebSocket_MessageType_Pong;
|
||||
webSocketMessageType = WebSocketMessageType::Pong;
|
||||
} break;
|
||||
|
||||
case WebSocketTransport::FRAGMENT:
|
||||
case WebSocketTransport::MessageKind::FRAGMENT:
|
||||
{
|
||||
webSocketMessageType = WebSocket_MessageType_Fragment;
|
||||
webSocketMessageType = WebSocketMessageType::Fragment;
|
||||
} break;
|
||||
}
|
||||
|
||||
@ -429,11 +429,11 @@ namespace ix
|
||||
{
|
||||
switch (_ws.getReadyState())
|
||||
{
|
||||
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;
|
||||
default: return WebSocket_ReadyState_Closed;
|
||||
case ix::WebSocketTransport::ReadyState::OPEN : return ReadyState::Open;
|
||||
case ix::WebSocketTransport::ReadyState::CONNECTING: return ReadyState::Connecting;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSING : return ReadyState::Closing;
|
||||
case ix::WebSocketTransport::ReadyState::CLOSED : return ReadyState::Closed;
|
||||
default: return ReadyState::Closed;
|
||||
}
|
||||
}
|
||||
|
||||
@ -441,11 +441,11 @@ namespace ix
|
||||
{
|
||||
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";
|
||||
default: return "CLOSED";
|
||||
case ReadyState::Open : return "OPEN";
|
||||
case ReadyState::Connecting: return "CONNECTING";
|
||||
case ReadyState::Closing : return "CLOSING";
|
||||
case ReadyState::Closed : return "CLOSED";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,23 +24,23 @@
|
||||
namespace ix
|
||||
{
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants
|
||||
enum ReadyState
|
||||
enum class ReadyState
|
||||
{
|
||||
WebSocket_ReadyState_Connecting = 0,
|
||||
WebSocket_ReadyState_Open = 1,
|
||||
WebSocket_ReadyState_Closing = 2,
|
||||
WebSocket_ReadyState_Closed = 3
|
||||
Connecting = 0,
|
||||
Open = 1,
|
||||
Closing = 2,
|
||||
Closed = 3
|
||||
};
|
||||
|
||||
enum WebSocketMessageType
|
||||
enum class WebSocketMessageType
|
||||
{
|
||||
WebSocket_MessageType_Message = 0,
|
||||
WebSocket_MessageType_Open = 1,
|
||||
WebSocket_MessageType_Close = 2,
|
||||
WebSocket_MessageType_Error = 3,
|
||||
WebSocket_MessageType_Ping = 4,
|
||||
WebSocket_MessageType_Pong = 5,
|
||||
WebSocket_MessageType_Fragment = 6
|
||||
Message = 0,
|
||||
Open = 1,
|
||||
Close = 2,
|
||||
Error = 3,
|
||||
Ping = 4,
|
||||
Pong = 5,
|
||||
Fragment = 6
|
||||
};
|
||||
|
||||
struct WebSocketOpenInfo
|
||||
@ -119,6 +119,8 @@ namespace ix
|
||||
static void resetTrafficTrackerCallback();
|
||||
|
||||
ReadyState getReadyState() const;
|
||||
static std::string readyStateToString(ReadyState readyState);
|
||||
|
||||
const std::string& getUrl() const;
|
||||
const WebSocketPerMessageDeflateOptions& getPerMessageDeflateOptions() const;
|
||||
int getHeartBeatPeriod() const;
|
||||
@ -138,7 +140,6 @@ namespace ix
|
||||
bool isConnected() const;
|
||||
bool isClosing() const;
|
||||
void checkConnection(bool firstConnectionAttempt);
|
||||
std::string readyStateToString(ReadyState readyState);
|
||||
static void invokeTrafficTrackerCallback(size_t size, bool incoming);
|
||||
|
||||
// Server
|
||||
|
@ -23,7 +23,6 @@ namespace ix
|
||||
WebSocketPerMessageDeflateOptions(std::string extension);
|
||||
|
||||
std::string generateHeader();
|
||||
std::string parseHeader();
|
||||
bool enabled() const;
|
||||
bool getClientNoContextTakeover() const;
|
||||
bool getServerNoContextTakeover() const;
|
||||
|
@ -86,7 +86,7 @@ namespace ix
|
||||
|
||||
WebSocketTransport::WebSocketTransport() :
|
||||
_useMask(true),
|
||||
_readyState(CLOSED),
|
||||
_readyState(ReadyState::CLOSED),
|
||||
_closeCode(kInternalErrorCode),
|
||||
_closeReason(kInternalErrorMessage),
|
||||
_closeWireSize(0),
|
||||
@ -173,7 +173,7 @@ namespace ix
|
||||
timeoutSecs);
|
||||
if (result.success)
|
||||
{
|
||||
setReadyState(OPEN);
|
||||
setReadyState(ReadyState::OPEN);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -201,22 +201,22 @@ namespace ix
|
||||
auto result = webSocketHandshake.serverHandshake(fd, timeoutSecs);
|
||||
if (result.success)
|
||||
{
|
||||
setReadyState(OPEN);
|
||||
setReadyState(ReadyState::OPEN);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
WebSocketTransport::ReadyStateValues WebSocketTransport::getReadyState() const
|
||||
WebSocketTransport::ReadyState WebSocketTransport::getReadyState() const
|
||||
{
|
||||
return _readyState;
|
||||
}
|
||||
|
||||
void WebSocketTransport::setReadyState(ReadyStateValues readyStateValue)
|
||||
void WebSocketTransport::setReadyState(ReadyState readyState)
|
||||
{
|
||||
// No state change, return
|
||||
if (_readyState == readyStateValue) return;
|
||||
if (_readyState == readyState) return;
|
||||
|
||||
if (readyStateValue == CLOSED)
|
||||
if (readyState == ReadyState::CLOSED)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_closeDataMutex);
|
||||
_onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
|
||||
@ -226,7 +226,7 @@ namespace ix
|
||||
_closeRemote = false;
|
||||
}
|
||||
|
||||
_readyState = readyStateValue;
|
||||
_readyState = readyState;
|
||||
}
|
||||
|
||||
void WebSocketTransport::setOnCloseCallback(const OnCloseCallback& onCloseCallback)
|
||||
@ -262,9 +262,9 @@ namespace ix
|
||||
return now - _closingTimePoint > std::chrono::milliseconds(kClosingMaximumWaitingDelayInMs);
|
||||
}
|
||||
|
||||
WebSocketTransport::PollPostTreatment WebSocketTransport::poll()
|
||||
WebSocketTransport::PollResult WebSocketTransport::poll()
|
||||
{
|
||||
if (_readyState == OPEN)
|
||||
if (_readyState == ReadyState::OPEN)
|
||||
{
|
||||
// if (1) ping timeout is enabled and (2) duration since last received
|
||||
// ping response (PONG) exceeds the maximum delay, then close the connection
|
||||
@ -284,7 +284,7 @@ namespace ix
|
||||
|
||||
// No timeout if state is not OPEN, otherwise computed
|
||||
// pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set)
|
||||
int lastingTimeoutDelayInMs = (_readyState != OPEN) ? 0 : _pingIntervalOrTimeoutGCDSecs;
|
||||
int lastingTimeoutDelayInMs = (_readyState != ReadyState::OPEN) ? 0 : _pingIntervalOrTimeoutGCDSecs;
|
||||
|
||||
if (_pingIntervalOrTimeoutGCDSecs > 0)
|
||||
{
|
||||
@ -319,7 +319,7 @@ namespace ix
|
||||
if (result == PollResultType::Error)
|
||||
{
|
||||
_socket->close();
|
||||
setReadyState(CLOSED);
|
||||
setReadyState(ReadyState::CLOSED);
|
||||
break;
|
||||
}
|
||||
else if (result == PollResultType::ReadyForWrite)
|
||||
@ -345,7 +345,7 @@ namespace ix
|
||||
|
||||
_socket->close();
|
||||
|
||||
return CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH;
|
||||
return PollResult::AbnormalClose;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -364,15 +364,15 @@ namespace ix
|
||||
_socket->close();
|
||||
}
|
||||
|
||||
if (_readyState == CLOSING && closingDelayExceeded())
|
||||
if (_readyState == ReadyState::CLOSING && closingDelayExceeded())
|
||||
{
|
||||
_rxbuf.clear();
|
||||
// close code and reason were set when calling close()
|
||||
_socket->close();
|
||||
setReadyState(CLOSED);
|
||||
setReadyState(ReadyState::CLOSED);
|
||||
}
|
||||
|
||||
return NONE;
|
||||
return PollResult::Succeeded;
|
||||
}
|
||||
|
||||
bool WebSocketTransport::isSendBufferEmpty() const
|
||||
@ -434,7 +434,7 @@ namespace ix
|
||||
// | Payload Data continued ... |
|
||||
// +---------------------------------------------------------------+
|
||||
//
|
||||
void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment,
|
||||
void WebSocketTransport::dispatch(WebSocketTransport::PollResult pollResult,
|
||||
const OnMessageCallback& onMessageCallback)
|
||||
{
|
||||
while (true)
|
||||
@ -521,7 +521,7 @@ namespace ix
|
||||
//
|
||||
if (ws.fin && _chunks.empty())
|
||||
{
|
||||
emitMessage(MSG,
|
||||
emitMessage(MessageKind::MSG,
|
||||
std::string(_rxbuf.begin()+ws.header_size,
|
||||
_rxbuf.begin()+ws.header_size+(size_t) ws.N),
|
||||
ws,
|
||||
@ -541,12 +541,12 @@ namespace ix
|
||||
_rxbuf.begin()+ws.header_size+(size_t)ws.N));
|
||||
if (ws.fin)
|
||||
{
|
||||
emitMessage(MSG, getMergedChunks(), ws, onMessageCallback);
|
||||
emitMessage(MessageKind::MSG, getMergedChunks(), ws, onMessageCallback);
|
||||
_chunks.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
emitMessage(FRAGMENT, std::string(), ws, onMessageCallback);
|
||||
emitMessage(MessageKind::FRAGMENT, std::string(), ws, onMessageCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -564,7 +564,7 @@ namespace ix
|
||||
sendData(wsheader_type::PONG, pingData, compress);
|
||||
}
|
||||
|
||||
emitMessage(PING, pingData, ws, onMessageCallback);
|
||||
emitMessage(MessageKind::PING, pingData, ws, onMessageCallback);
|
||||
}
|
||||
else if (ws.opcode == wsheader_type::PONG)
|
||||
{
|
||||
@ -575,7 +575,7 @@ namespace ix
|
||||
std::lock_guard<std::mutex> lck(_lastReceivePongTimePointMutex);
|
||||
_lastReceivePongTimePoint = std::chrono::steady_clock::now();
|
||||
|
||||
emitMessage(PONG, pongData, ws, onMessageCallback);
|
||||
emitMessage(MessageKind::PONG, pongData, ws, onMessageCallback);
|
||||
}
|
||||
else if (ws.opcode == wsheader_type::CLOSE)
|
||||
{
|
||||
@ -605,7 +605,7 @@ namespace ix
|
||||
}
|
||||
|
||||
// We receive a CLOSE frame from remote and are NOT the ones who triggered the close
|
||||
if (_readyState != CLOSING)
|
||||
if (_readyState != ReadyState::CLOSING)
|
||||
{
|
||||
// send back the CLOSE frame
|
||||
sendCloseFrame(code, reason);
|
||||
@ -646,18 +646,18 @@ namespace ix
|
||||
|
||||
// if an abnormal closure was raised in poll, and nothing else triggered a CLOSED state in
|
||||
// the received and processed data then close the connection
|
||||
if (pollPostTreatment == CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH)
|
||||
if (pollResult == PollResult::AbnormalClose)
|
||||
{
|
||||
_rxbuf.clear();
|
||||
|
||||
// if we previously closed the connection (CLOSING state), then set state to CLOSED (code/reason were set before)
|
||||
if (_readyState == CLOSING)
|
||||
if (_readyState == ReadyState::CLOSING)
|
||||
{
|
||||
_socket->close();
|
||||
setReadyState(CLOSED);
|
||||
setReadyState(ReadyState::CLOSED);
|
||||
}
|
||||
// if we weren't closing, then close using abnormal close code and message
|
||||
else if (_readyState != CLOSED)
|
||||
else if (_readyState != ReadyState::CLOSED)
|
||||
{
|
||||
closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, false);
|
||||
}
|
||||
@ -692,7 +692,7 @@ namespace ix
|
||||
size_t wireSize = message.size();
|
||||
|
||||
// When the RSV1 bit is 1 it means the message is compressed
|
||||
if (_enablePerMessageDeflate && ws.rsv1 && messageKind != FRAGMENT)
|
||||
if (_enablePerMessageDeflate && ws.rsv1 && messageKind != MessageKind::FRAGMENT)
|
||||
{
|
||||
std::string decompressedMessage;
|
||||
bool success = _perMessageDeflate.decompress(message, decompressedMessage);
|
||||
@ -719,7 +719,7 @@ namespace ix
|
||||
bool compress,
|
||||
const OnProgressCallback& onProgressCallback)
|
||||
{
|
||||
if (_readyState != OPEN)
|
||||
if (_readyState != ReadyState::OPEN)
|
||||
{
|
||||
return WebSocketSendInfo();
|
||||
}
|
||||
@ -945,7 +945,7 @@ namespace ix
|
||||
{
|
||||
_socket->close();
|
||||
|
||||
setReadyState(CLOSED);
|
||||
setReadyState(ReadyState::CLOSED);
|
||||
break;
|
||||
}
|
||||
else
|
||||
@ -988,14 +988,14 @@ namespace ix
|
||||
_closeWireSize = closeWireSize;
|
||||
_closeRemote = remote;
|
||||
}
|
||||
setReadyState(CLOSED);
|
||||
setReadyState(ReadyState::CLOSED);
|
||||
}
|
||||
|
||||
void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
|
||||
{
|
||||
_requestInitCancellation = true;
|
||||
|
||||
if (_readyState == CLOSING || _readyState == CLOSED) return;
|
||||
if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED) return;
|
||||
|
||||
sendCloseFrame(code, reason);
|
||||
{
|
||||
@ -1009,7 +1009,7 @@ namespace ix
|
||||
std::lock_guard<std::mutex> lock(_closingTimePointMutex);
|
||||
_closingTimePoint = std::chrono::steady_clock::now();
|
||||
}
|
||||
setReadyState(CLOSING);
|
||||
setReadyState(ReadyState::CLOSING);
|
||||
|
||||
// wake up the poll, but do not close yet
|
||||
_socket->wakeUpFromPoll(Socket::kSendRequest);
|
||||
|
@ -40,7 +40,7 @@ namespace ix
|
||||
class WebSocketTransport
|
||||
{
|
||||
public:
|
||||
enum ReadyStateValues
|
||||
enum class ReadyState
|
||||
{
|
||||
CLOSING,
|
||||
CLOSED,
|
||||
@ -48,7 +48,7 @@ namespace ix
|
||||
OPEN
|
||||
};
|
||||
|
||||
enum MessageKind
|
||||
enum class MessageKind
|
||||
{
|
||||
MSG,
|
||||
PING,
|
||||
@ -56,10 +56,10 @@ namespace ix
|
||||
FRAGMENT
|
||||
};
|
||||
|
||||
enum PollPostTreatment
|
||||
enum class PollResult
|
||||
{
|
||||
NONE,
|
||||
CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH
|
||||
Succeeded,
|
||||
AbnormalClose
|
||||
};
|
||||
|
||||
using OnMessageCallback = std::function<void(const std::string&,
|
||||
@ -84,7 +84,7 @@ namespace ix
|
||||
WebSocketInitResult connectToSocket(int fd, // Server
|
||||
int timeoutSecs);
|
||||
|
||||
PollPostTreatment poll();
|
||||
PollResult poll();
|
||||
WebSocketSendInfo sendBinary(const std::string& message,
|
||||
const OnProgressCallback& onProgressCallback);
|
||||
WebSocketSendInfo sendText(const std::string& message,
|
||||
@ -96,10 +96,10 @@ namespace ix
|
||||
size_t closeWireSize = 0,
|
||||
bool remote = false);
|
||||
|
||||
ReadyStateValues getReadyState() const;
|
||||
void setReadyState(ReadyStateValues readyStateValue);
|
||||
ReadyState getReadyState() const;
|
||||
void setReadyState(ReadyState readyState);
|
||||
void setOnCloseCallback(const OnCloseCallback& onCloseCallback);
|
||||
void dispatch(PollPostTreatment pollPostTreatment,
|
||||
void dispatch(PollResult pollResult,
|
||||
const OnMessageCallback& onMessageCallback);
|
||||
size_t bufferedAmount() const;
|
||||
|
||||
@ -113,11 +113,11 @@ namespace ix
|
||||
bool mask;
|
||||
enum opcode_type {
|
||||
CONTINUATION = 0x0,
|
||||
TEXT_FRAME = 0x1,
|
||||
TEXT_FRAME = 0x1,
|
||||
BINARY_FRAME = 0x2,
|
||||
CLOSE = 8,
|
||||
PING = 9,
|
||||
PONG = 0xa,
|
||||
CLOSE = 8,
|
||||
PING = 9,
|
||||
PONG = 0xa,
|
||||
} opcode;
|
||||
int N0;
|
||||
uint64_t N;
|
||||
@ -153,7 +153,7 @@ namespace ix
|
||||
std::shared_ptr<Socket> _socket;
|
||||
|
||||
// Hold the state of the connection (OPEN, CLOSED, etc...)
|
||||
std::atomic<ReadyStateValues> _readyState;
|
||||
std::atomic<ReadyState> _readyState;
|
||||
|
||||
OnCloseCallback _onCloseCallback;
|
||||
uint16_t _closeCode;
|
||||
|
Reference in New Issue
Block a user