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:
Dimon4eg 2019-05-12 00:22:06 +03:00 committed by Benjamin Sergeant
parent 80226cb7d3
commit 99a3bbc4f9
26 changed files with 238 additions and 238 deletions

View File

@ -37,7 +37,7 @@ webSocket.setOnMessageCallback(
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Message) if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cout << str << std::endl; std::cout << str << std::endl;
} }
@ -77,7 +77,7 @@ server.setOnConnectionCallback(
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
@ -96,7 +96,7 @@ server.setOnConnectionCallback(
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
// For an echo server, we just send back to the client whatever was received by the server // For an echo server, we just send back to the client whatever was received by the server
// All connected clients are available in an std::set. See the broadcast cpp example. // All connected clients are available in an std::set. See the broadcast cpp example.
@ -301,10 +301,10 @@ If the connection was closed and sending failed, the return value will be set to
`getReadyState()` returns the state of the connection. There are 4 possible states. `getReadyState()` returns the state of the connection. There are 4 possible states.
1. WebSocket_ReadyState_Connecting - The connection is not yet open. 1. ReadyState::Connecting - The connection is not yet open.
2. WebSocket_ReadyState_Open - The connection is open and ready to communicate. 2. ReadyState::Open - The connection is open and ready to communicate.
3. WebSocket_ReadyState_Closing - The connection is in the process of closing. 3. ReadyState::Closing - The connection is in the process of closing.
4. WebSocket_MessageType_Close - The connection is closed or could not be opened. 4. ReadyState::Closed - The connection is closed or could not be opened.
### Open and Close notifications ### Open and Close notifications
@ -319,7 +319,7 @@ webSocket.setOnMessageCallback(
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cout << "send greetings" << std::endl; std::cout << "send greetings" << std::endl;
@ -330,7 +330,7 @@ webSocket.setOnMessageCallback(
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cout << "disconnected" << std::endl; std::cout << "disconnected" << std::endl;
@ -345,7 +345,7 @@ webSocket.setOnMessageCallback(
### Error notification ### Error notification
A message will be fired when there is an error with the connection. The message type will be `ix::WebSocket_MessageType_Error`. Multiple fields will be available on the event to describe the error. A message will be fired when there is an error with the connection. The message type will be `ix::WebSocketMessageType::Error`. Multiple fields will be available on the event to describe the error.
``` ```
webSocket.setOnMessageCallback( webSocket.setOnMessageCallback(
@ -356,7 +356,7 @@ webSocket.setOnMessageCallback(
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Error) if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Error: " << error.reason << std::endl; ss << "Error: " << error.reason << std::endl;
@ -396,8 +396,8 @@ webSocket.setOnMessageCallback(
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Ping || if (messageType == ix::WebSocketMessageType::Ping ||
messageType == ix::WebSocket_MessageType_Pong) messageType == ix::WebSocketMessageType::Pong)
{ {
std::cout << "pong data: " << str << std::endl; std::cout << "pong data: " << str << std::endl;
} }

View File

@ -52,7 +52,7 @@ namespace ix
{ {
std::stringstream ss; std::stringstream ss;
ss << "Cannot parse url: " << url; ss << "Cannot parse url: " << url;
return std::make_tuple(code, HttpErrorCode_UrlMalformed, return std::make_tuple(code, HttpErrorCode::UrlMalformed,
headers, payload, ss.str(), headers, payload, ss.str(),
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -63,7 +63,7 @@ namespace ix
if (!_socket) if (!_socket)
{ {
return std::make_tuple(code, HttpErrorCode_CannotCreateSocket, return std::make_tuple(code, HttpErrorCode::CannotCreateSocket,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -116,7 +116,7 @@ namespace ix
{ {
std::stringstream ss; std::stringstream ss;
ss << "Cannot connect to url: " << url; ss << "Cannot connect to url: " << url;
return std::make_tuple(code, HttpErrorCode_CannotConnect, return std::make_tuple(code, HttpErrorCode::CannotConnect,
headers, payload, ss.str(), headers, payload, ss.str(),
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -142,7 +142,7 @@ namespace ix
if (!_socket->writeBytes(req, isCancellationRequested)) if (!_socket->writeBytes(req, isCancellationRequested))
{ {
std::string errorMsg("Cannot send request"); std::string errorMsg("Cannot send request");
return std::make_tuple(code, HttpErrorCode_SendError, return std::make_tuple(code, HttpErrorCode::SendError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -156,7 +156,7 @@ namespace ix
if (!lineValid) if (!lineValid)
{ {
std::string errorMsg("Cannot retrieve status line"); std::string errorMsg("Cannot retrieve status line");
return std::make_tuple(code, HttpErrorCode_CannotReadStatusLine, return std::make_tuple(code, HttpErrorCode::CannotReadStatusLine,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -171,7 +171,7 @@ namespace ix
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1) if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
{ {
std::string errorMsg("Cannot parse response code from status line"); 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, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -183,7 +183,7 @@ namespace ix
if (!headersValid) if (!headersValid)
{ {
std::string errorMsg("Cannot parse http headers"); std::string errorMsg("Cannot parse http headers");
return std::make_tuple(code, HttpErrorCode_HeaderParsingError, return std::make_tuple(code, HttpErrorCode::HeaderParsingError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -194,7 +194,7 @@ namespace ix
if (headers.find("Location") == headers.end()) if (headers.find("Location") == headers.end())
{ {
std::string errorMsg("Missing location header for redirect"); 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, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -203,7 +203,7 @@ namespace ix
{ {
std::stringstream ss; std::stringstream ss;
ss << "Too many redirects: " << redirects; ss << "Too many redirects: " << redirects;
return std::make_tuple(code, HttpErrorCode_TooManyRedirects, return std::make_tuple(code, HttpErrorCode::TooManyRedirects,
headers, payload, ss.str(), headers, payload, ss.str(),
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -215,7 +215,7 @@ namespace ix
if (verb == "HEAD") if (verb == "HEAD")
{ {
return std::make_tuple(code, HttpErrorCode_Ok, return std::make_tuple(code, HttpErrorCode::Ok,
headers, payload, std::string(), headers, payload, std::string(),
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -236,7 +236,7 @@ namespace ix
if (!chunkResult.first) if (!chunkResult.first)
{ {
errorMsg = "Cannot read chunk"; errorMsg = "Cannot read chunk";
return std::make_tuple(code, HttpErrorCode_ChunkReadError, return std::make_tuple(code, HttpErrorCode::ChunkReadError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -254,7 +254,7 @@ namespace ix
if (!lineResult.first) if (!lineResult.first)
{ {
return std::make_tuple(code, HttpErrorCode_ChunkReadError, return std::make_tuple(code, HttpErrorCode::ChunkReadError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -281,7 +281,7 @@ namespace ix
if (!chunkResult.first) if (!chunkResult.first)
{ {
errorMsg = "Cannot read chunk"; errorMsg = "Cannot read chunk";
return std::make_tuple(code, HttpErrorCode_ChunkReadError, return std::make_tuple(code, HttpErrorCode::ChunkReadError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -292,7 +292,7 @@ namespace ix
if (!lineResult.first) if (!lineResult.first)
{ {
return std::make_tuple(code, HttpErrorCode_ChunkReadError, return std::make_tuple(code, HttpErrorCode::ChunkReadError,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -307,7 +307,7 @@ namespace ix
else else
{ {
std::string errorMsg("Cannot read http body"); std::string errorMsg("Cannot read http body");
return std::make_tuple(code, HttpErrorCode_CannotReadBody, return std::make_tuple(code, HttpErrorCode::CannotReadBody,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
@ -321,14 +321,14 @@ namespace ix
if (!gzipInflate(payload, decompressedPayload)) if (!gzipInflate(payload, decompressedPayload))
{ {
std::string errorMsg("Error decompressing payload"); std::string errorMsg("Error decompressing payload");
return std::make_tuple(code, HttpErrorCode_Gzip, return std::make_tuple(code, HttpErrorCode::Gzip,
headers, payload, errorMsg, headers, payload, errorMsg,
uploadSize, downloadSize); uploadSize, downloadSize);
} }
payload = decompressedPayload; payload = decompressedPayload;
} }
return std::make_tuple(code, HttpErrorCode_Ok, return std::make_tuple(code, HttpErrorCode::Ok,
headers, payload, std::string(), headers, payload, std::string(),
uploadSize, downloadSize); uploadSize, downloadSize);
} }

View File

@ -19,23 +19,23 @@
namespace ix namespace ix
{ {
enum HttpErrorCode enum class HttpErrorCode
{ {
HttpErrorCode_Ok = 0, Ok = 0,
HttpErrorCode_CannotConnect = 1, CannotConnect = 1,
HttpErrorCode_Timeout = 2, Timeout = 2,
HttpErrorCode_Gzip = 3, Gzip = 3,
HttpErrorCode_UrlMalformed = 4, UrlMalformed = 4,
HttpErrorCode_CannotCreateSocket = 5, CannotCreateSocket = 5,
HttpErrorCode_SendError = 6, SendError = 6,
HttpErrorCode_ReadError = 7, ReadError = 7,
HttpErrorCode_CannotReadStatusLine = 8, CannotReadStatusLine = 8,
HttpErrorCode_MissingStatus = 9, MissingStatus = 9,
HttpErrorCode_HeaderParsingError = 10, HeaderParsingError = 10,
HttpErrorCode_MissingLocation = 11, MissingLocation = 11,
HttpErrorCode_TooManyRedirects = 12, TooManyRedirects = 12,
HttpErrorCode_ChunkReadError = 13, ChunkReadError = 13,
HttpErrorCode_CannotReadBody = 14 CannotReadBody = 14
}; };
using HttpResponse = std::tuple<int, // status using HttpResponse = std::tuple<int, // status

View File

@ -51,7 +51,7 @@ namespace ix
_ws.setOnCloseCallback( _ws.setOnCloseCallback(
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) [this](uint16_t code, const std::string& reason, size_t wireSize, bool remote)
{ {
_onMessageCallback(WebSocket_MessageType_Close, "", wireSize, _onMessageCallback(WebSocketMessageType::Close, "", wireSize,
WebSocketErrorInfo(), WebSocketOpenInfo(), WebSocketErrorInfo(), WebSocketOpenInfo(),
WebSocketCloseInfo(code, reason, remote)); WebSocketCloseInfo(code, reason, remote));
} }
@ -172,7 +172,7 @@ namespace ix
return status; return status;
} }
_onMessageCallback(WebSocket_MessageType_Open, "", 0, _onMessageCallback(WebSocketMessageType::Open, "", 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers), WebSocketOpenInfo(status.uri, status.headers),
WebSocketCloseInfo()); WebSocketCloseInfo());
@ -195,7 +195,7 @@ namespace ix
return status; return status;
} }
_onMessageCallback(WebSocket_MessageType_Open, "", 0, _onMessageCallback(WebSocketMessageType::Open, "", 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers), WebSocketOpenInfo(status.uri, status.headers),
WebSocketCloseInfo()); WebSocketCloseInfo());
@ -204,12 +204,12 @@ namespace ix
bool WebSocket::isConnected() const bool WebSocket::isConnected() const
{ {
return getReadyState() == WebSocket_ReadyState_Open; return getReadyState() == ReadyState::Open;
} }
bool WebSocket::isClosing() const bool WebSocket::isClosing() const
{ {
return getReadyState() == WebSocket_ReadyState_Closing; return getReadyState() == ReadyState::Closing;
} }
void WebSocket::close() void WebSocket::close()
@ -266,7 +266,7 @@ namespace ix
connectErr.reason = status.errorStr; connectErr.reason = status.errorStr;
connectErr.http_status = status.http_status; connectErr.http_status = status.http_status;
_onMessageCallback(WebSocket_MessageType_Error, "", 0, _onMessageCallback(WebSocketMessageType::Error, "", 0,
connectErr, WebSocketOpenInfo(), connectErr, WebSocketOpenInfo(),
WebSocketCloseInfo()); WebSocketCloseInfo());
} }
@ -287,17 +287,17 @@ namespace ix
firstConnectionAttempt = false; firstConnectionAttempt = false;
// if here we are closed then checkConnection was not able to connect // if here we are closed then checkConnection was not able to connect
if (getReadyState() == WebSocket_ReadyState_Closed) if (getReadyState() == ReadyState::Closed)
{ {
break; break;
} }
// 2. Poll to see if there's any new data available // 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 // 3. Dispatch the incoming messages
_ws.dispatch( _ws.dispatch(
pollPostTreatment, pollResult,
[this](const std::string& msg, [this](const std::string& msg,
size_t wireSize, size_t wireSize,
bool decompressionError, bool decompressionError,
@ -307,24 +307,24 @@ namespace ix
switch (messageKind) switch (messageKind)
{ {
default: default:
case WebSocketTransport::MSG: case WebSocketTransport::MessageKind::MSG:
{ {
webSocketMessageType = WebSocket_MessageType_Message; webSocketMessageType = WebSocketMessageType::Message;
} break; } break;
case WebSocketTransport::PING: case WebSocketTransport::MessageKind::PING:
{ {
webSocketMessageType = WebSocket_MessageType_Ping; webSocketMessageType = WebSocketMessageType::Ping;
} break; } break;
case WebSocketTransport::PONG: case WebSocketTransport::MessageKind::PONG:
{ {
webSocketMessageType = WebSocket_MessageType_Pong; webSocketMessageType = WebSocketMessageType::Pong;
} break; } break;
case WebSocketTransport::FRAGMENT: case WebSocketTransport::MessageKind::FRAGMENT:
{ {
webSocketMessageType = WebSocket_MessageType_Fragment; webSocketMessageType = WebSocketMessageType::Fragment;
} break; } break;
} }
@ -429,11 +429,11 @@ namespace ix
{ {
switch (_ws.getReadyState()) switch (_ws.getReadyState())
{ {
case ix::WebSocketTransport::OPEN: return WebSocket_ReadyState_Open; case ix::WebSocketTransport::ReadyState::OPEN : return ReadyState::Open;
case ix::WebSocketTransport::CONNECTING: return WebSocket_ReadyState_Connecting; case ix::WebSocketTransport::ReadyState::CONNECTING: return ReadyState::Connecting;
case ix::WebSocketTransport::CLOSING: return WebSocket_ReadyState_Closing; case ix::WebSocketTransport::ReadyState::CLOSING : return ReadyState::Closing;
case ix::WebSocketTransport::CLOSED: return WebSocket_ReadyState_Closed; case ix::WebSocketTransport::ReadyState::CLOSED : return ReadyState::Closed;
default: return WebSocket_ReadyState_Closed; default: return ReadyState::Closed;
} }
} }
@ -441,11 +441,11 @@ namespace ix
{ {
switch (readyState) switch (readyState)
{ {
case WebSocket_ReadyState_Open: return "OPEN"; case ReadyState::Open : return "OPEN";
case WebSocket_ReadyState_Connecting: return "CONNECTING"; case ReadyState::Connecting: return "CONNECTING";
case WebSocket_ReadyState_Closing: return "CLOSING"; case ReadyState::Closing : return "CLOSING";
case WebSocket_ReadyState_Closed: return "CLOSED"; case ReadyState::Closed : return "CLOSED";
default: return "CLOSED"; default: return "UNKNOWN";
} }
} }

View File

@ -24,23 +24,23 @@
namespace ix namespace ix
{ {
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants
enum ReadyState enum class ReadyState
{ {
WebSocket_ReadyState_Connecting = 0, Connecting = 0,
WebSocket_ReadyState_Open = 1, Open = 1,
WebSocket_ReadyState_Closing = 2, Closing = 2,
WebSocket_ReadyState_Closed = 3 Closed = 3
}; };
enum WebSocketMessageType enum class WebSocketMessageType
{ {
WebSocket_MessageType_Message = 0, Message = 0,
WebSocket_MessageType_Open = 1, Open = 1,
WebSocket_MessageType_Close = 2, Close = 2,
WebSocket_MessageType_Error = 3, Error = 3,
WebSocket_MessageType_Ping = 4, Ping = 4,
WebSocket_MessageType_Pong = 5, Pong = 5,
WebSocket_MessageType_Fragment = 6 Fragment = 6
}; };
struct WebSocketOpenInfo struct WebSocketOpenInfo
@ -119,6 +119,8 @@ namespace ix
static void resetTrafficTrackerCallback(); static void resetTrafficTrackerCallback();
ReadyState getReadyState() const; ReadyState getReadyState() const;
static std::string readyStateToString(ReadyState readyState);
const std::string& getUrl() const; const std::string& getUrl() const;
const WebSocketPerMessageDeflateOptions& getPerMessageDeflateOptions() const; const WebSocketPerMessageDeflateOptions& getPerMessageDeflateOptions() const;
int getHeartBeatPeriod() const; int getHeartBeatPeriod() const;
@ -138,7 +140,6 @@ namespace ix
bool isConnected() const; bool isConnected() const;
bool isClosing() const; bool isClosing() const;
void checkConnection(bool firstConnectionAttempt); void checkConnection(bool firstConnectionAttempt);
std::string readyStateToString(ReadyState readyState);
static void invokeTrafficTrackerCallback(size_t size, bool incoming); static void invokeTrafficTrackerCallback(size_t size, bool incoming);
// Server // Server

View File

@ -23,7 +23,6 @@ namespace ix
WebSocketPerMessageDeflateOptions(std::string extension); WebSocketPerMessageDeflateOptions(std::string extension);
std::string generateHeader(); std::string generateHeader();
std::string parseHeader();
bool enabled() const; bool enabled() const;
bool getClientNoContextTakeover() const; bool getClientNoContextTakeover() const;
bool getServerNoContextTakeover() const; bool getServerNoContextTakeover() const;

View File

@ -86,7 +86,7 @@ namespace ix
WebSocketTransport::WebSocketTransport() : WebSocketTransport::WebSocketTransport() :
_useMask(true), _useMask(true),
_readyState(CLOSED), _readyState(ReadyState::CLOSED),
_closeCode(kInternalErrorCode), _closeCode(kInternalErrorCode),
_closeReason(kInternalErrorMessage), _closeReason(kInternalErrorMessage),
_closeWireSize(0), _closeWireSize(0),
@ -173,7 +173,7 @@ namespace ix
timeoutSecs); timeoutSecs);
if (result.success) if (result.success)
{ {
setReadyState(OPEN); setReadyState(ReadyState::OPEN);
} }
return result; return result;
} }
@ -201,22 +201,22 @@ namespace ix
auto result = webSocketHandshake.serverHandshake(fd, timeoutSecs); auto result = webSocketHandshake.serverHandshake(fd, timeoutSecs);
if (result.success) if (result.success)
{ {
setReadyState(OPEN); setReadyState(ReadyState::OPEN);
} }
return result; return result;
} }
WebSocketTransport::ReadyStateValues WebSocketTransport::getReadyState() const WebSocketTransport::ReadyState WebSocketTransport::getReadyState() const
{ {
return _readyState; return _readyState;
} }
void WebSocketTransport::setReadyState(ReadyStateValues readyStateValue) void WebSocketTransport::setReadyState(ReadyState readyState)
{ {
// No state change, return // 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); std::lock_guard<std::mutex> lock(_closeDataMutex);
_onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote); _onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
@ -226,7 +226,7 @@ namespace ix
_closeRemote = false; _closeRemote = false;
} }
_readyState = readyStateValue; _readyState = readyState;
} }
void WebSocketTransport::setOnCloseCallback(const OnCloseCallback& onCloseCallback) void WebSocketTransport::setOnCloseCallback(const OnCloseCallback& onCloseCallback)
@ -262,9 +262,9 @@ namespace ix
return now - _closingTimePoint > std::chrono::milliseconds(kClosingMaximumWaitingDelayInMs); 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 // if (1) ping timeout is enabled and (2) duration since last received
// ping response (PONG) exceeds the maximum delay, then close the connection // 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 // No timeout if state is not OPEN, otherwise computed
// pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set) // 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) if (_pingIntervalOrTimeoutGCDSecs > 0)
{ {
@ -319,7 +319,7 @@ namespace ix
if (result == PollResultType::Error) if (result == PollResultType::Error)
{ {
_socket->close(); _socket->close();
setReadyState(CLOSED); setReadyState(ReadyState::CLOSED);
break; break;
} }
else if (result == PollResultType::ReadyForWrite) else if (result == PollResultType::ReadyForWrite)
@ -345,7 +345,7 @@ namespace ix
_socket->close(); _socket->close();
return CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH; return PollResult::AbnormalClose;
} }
else else
{ {
@ -364,15 +364,15 @@ namespace ix
_socket->close(); _socket->close();
} }
if (_readyState == CLOSING && closingDelayExceeded()) if (_readyState == ReadyState::CLOSING && closingDelayExceeded())
{ {
_rxbuf.clear(); _rxbuf.clear();
// close code and reason were set when calling close() // close code and reason were set when calling close()
_socket->close(); _socket->close();
setReadyState(CLOSED); setReadyState(ReadyState::CLOSED);
} }
return NONE; return PollResult::Succeeded;
} }
bool WebSocketTransport::isSendBufferEmpty() const bool WebSocketTransport::isSendBufferEmpty() const
@ -434,7 +434,7 @@ namespace ix
// | Payload Data continued ... | // | Payload Data continued ... |
// +---------------------------------------------------------------+ // +---------------------------------------------------------------+
// //
void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment, void WebSocketTransport::dispatch(WebSocketTransport::PollResult pollResult,
const OnMessageCallback& onMessageCallback) const OnMessageCallback& onMessageCallback)
{ {
while (true) while (true)
@ -521,7 +521,7 @@ namespace ix
// //
if (ws.fin && _chunks.empty()) if (ws.fin && _chunks.empty())
{ {
emitMessage(MSG, emitMessage(MessageKind::MSG,
std::string(_rxbuf.begin()+ws.header_size, std::string(_rxbuf.begin()+ws.header_size,
_rxbuf.begin()+ws.header_size+(size_t) ws.N), _rxbuf.begin()+ws.header_size+(size_t) ws.N),
ws, ws,
@ -541,12 +541,12 @@ namespace ix
_rxbuf.begin()+ws.header_size+(size_t)ws.N)); _rxbuf.begin()+ws.header_size+(size_t)ws.N));
if (ws.fin) if (ws.fin)
{ {
emitMessage(MSG, getMergedChunks(), ws, onMessageCallback); emitMessage(MessageKind::MSG, getMergedChunks(), ws, onMessageCallback);
_chunks.clear(); _chunks.clear();
} }
else 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); sendData(wsheader_type::PONG, pingData, compress);
} }
emitMessage(PING, pingData, ws, onMessageCallback); emitMessage(MessageKind::PING, pingData, ws, onMessageCallback);
} }
else if (ws.opcode == wsheader_type::PONG) else if (ws.opcode == wsheader_type::PONG)
{ {
@ -575,7 +575,7 @@ namespace ix
std::lock_guard<std::mutex> lck(_lastReceivePongTimePointMutex); std::lock_guard<std::mutex> lck(_lastReceivePongTimePointMutex);
_lastReceivePongTimePoint = std::chrono::steady_clock::now(); _lastReceivePongTimePoint = std::chrono::steady_clock::now();
emitMessage(PONG, pongData, ws, onMessageCallback); emitMessage(MessageKind::PONG, pongData, ws, onMessageCallback);
} }
else if (ws.opcode == wsheader_type::CLOSE) 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 // 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 // send back the CLOSE frame
sendCloseFrame(code, reason); 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 // 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 // the received and processed data then close the connection
if (pollPostTreatment == CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH) if (pollResult == PollResult::AbnormalClose)
{ {
_rxbuf.clear(); _rxbuf.clear();
// if we previously closed the connection (CLOSING state), then set state to CLOSED (code/reason were set before) // 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(); _socket->close();
setReadyState(CLOSED); setReadyState(ReadyState::CLOSED);
} }
// if we weren't closing, then close using abnormal close code and message // 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); closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, false);
} }
@ -692,7 +692,7 @@ namespace ix
size_t wireSize = message.size(); size_t wireSize = message.size();
// When the RSV1 bit is 1 it means the message is compressed // 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; std::string decompressedMessage;
bool success = _perMessageDeflate.decompress(message, decompressedMessage); bool success = _perMessageDeflate.decompress(message, decompressedMessage);
@ -719,7 +719,7 @@ namespace ix
bool compress, bool compress,
const OnProgressCallback& onProgressCallback) const OnProgressCallback& onProgressCallback)
{ {
if (_readyState != OPEN) if (_readyState != ReadyState::OPEN)
{ {
return WebSocketSendInfo(); return WebSocketSendInfo();
} }
@ -945,7 +945,7 @@ namespace ix
{ {
_socket->close(); _socket->close();
setReadyState(CLOSED); setReadyState(ReadyState::CLOSED);
break; break;
} }
else else
@ -988,14 +988,14 @@ namespace ix
_closeWireSize = closeWireSize; _closeWireSize = closeWireSize;
_closeRemote = remote; _closeRemote = remote;
} }
setReadyState(CLOSED); setReadyState(ReadyState::CLOSED);
} }
void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote) void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
{ {
_requestInitCancellation = true; _requestInitCancellation = true;
if (_readyState == CLOSING || _readyState == CLOSED) return; if (_readyState == ReadyState::CLOSING || _readyState == ReadyState::CLOSED) return;
sendCloseFrame(code, reason); sendCloseFrame(code, reason);
{ {
@ -1009,7 +1009,7 @@ namespace ix
std::lock_guard<std::mutex> lock(_closingTimePointMutex); std::lock_guard<std::mutex> lock(_closingTimePointMutex);
_closingTimePoint = std::chrono::steady_clock::now(); _closingTimePoint = std::chrono::steady_clock::now();
} }
setReadyState(CLOSING); setReadyState(ReadyState::CLOSING);
// wake up the poll, but do not close yet // wake up the poll, but do not close yet
_socket->wakeUpFromPoll(Socket::kSendRequest); _socket->wakeUpFromPoll(Socket::kSendRequest);

View File

@ -40,7 +40,7 @@ namespace ix
class WebSocketTransport class WebSocketTransport
{ {
public: public:
enum ReadyStateValues enum class ReadyState
{ {
CLOSING, CLOSING,
CLOSED, CLOSED,
@ -48,7 +48,7 @@ namespace ix
OPEN OPEN
}; };
enum MessageKind enum class MessageKind
{ {
MSG, MSG,
PING, PING,
@ -56,10 +56,10 @@ namespace ix
FRAGMENT FRAGMENT
}; };
enum PollPostTreatment enum class PollResult
{ {
NONE, Succeeded,
CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH AbnormalClose
}; };
using OnMessageCallback = std::function<void(const std::string&, using OnMessageCallback = std::function<void(const std::string&,
@ -84,7 +84,7 @@ namespace ix
WebSocketInitResult connectToSocket(int fd, // Server WebSocketInitResult connectToSocket(int fd, // Server
int timeoutSecs); int timeoutSecs);
PollPostTreatment poll(); PollResult poll();
WebSocketSendInfo sendBinary(const std::string& message, WebSocketSendInfo sendBinary(const std::string& message,
const OnProgressCallback& onProgressCallback); const OnProgressCallback& onProgressCallback);
WebSocketSendInfo sendText(const std::string& message, WebSocketSendInfo sendText(const std::string& message,
@ -96,10 +96,10 @@ namespace ix
size_t closeWireSize = 0, size_t closeWireSize = 0,
bool remote = false); bool remote = false);
ReadyStateValues getReadyState() const; ReadyState getReadyState() const;
void setReadyState(ReadyStateValues readyStateValue); void setReadyState(ReadyState readyState);
void setOnCloseCallback(const OnCloseCallback& onCloseCallback); void setOnCloseCallback(const OnCloseCallback& onCloseCallback);
void dispatch(PollPostTreatment pollPostTreatment, void dispatch(PollResult pollResult,
const OnMessageCallback& onMessageCallback); const OnMessageCallback& onMessageCallback);
size_t bufferedAmount() const; size_t bufferedAmount() const;
@ -153,7 +153,7 @@ namespace ix
std::shared_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
// Hold the state of the connection (OPEN, CLOSED, etc...) // Hold the state of the connection (OPEN, CLOSED, etc...)
std::atomic<ReadyStateValues> _readyState; std::atomic<ReadyState> _readyState;
OnCloseCallback _onCloseCallback; OnCloseCallback _onCloseCallback;
uint16_t _closeCode; uint16_t _closeCode;

View File

@ -43,7 +43,7 @@ namespace
bool WebSocketClient::isReady() const bool WebSocketClient::isReady() const
{ {
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open; return _webSocket.getReadyState() == ix::ReadyState::Open;
} }
void WebSocketClient::stop() void WebSocketClient::stop()
@ -88,30 +88,30 @@ namespace
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("client connected"); log("client connected");
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("client disconnected"); log("client disconnected");
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "Error ! " << error.reason; ss << "Error ! " << error.reason;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
ss << "Received pong message " << str; ss << "Received pong message " << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
ss << "Received ping message " << str; ss << "Received ping message " << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
// too many messages to log // too many messages to log
} }
@ -145,7 +145,7 @@ namespace
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
Logger() << "New server connection"; Logger() << "New server connection";
Logger() << "id: " << connectionState->getId(); Logger() << "id: " << connectionState->getId();
@ -156,16 +156,16 @@ namespace
Logger() << it.first << ": " << it.second; Logger() << it.first << ": " << it.second;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("Server closed connection"); log("Server closed connection");
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
log("Server received a ping"); log("Server received a ping");
receivedPingMessages++; receivedPingMessages++;
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
// to many messages to log // to many messages to log
for(auto client: server.getClients()) for(auto client: server.getClients())

View File

@ -52,12 +52,12 @@ namespace
bool WebSocketClient::isReady() const bool WebSocketClient::isReady() const
{ {
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open; return _webSocket.getReadyState() == ix::ReadyState::Open;
} }
bool WebSocketClient::isClosed() const bool WebSocketClient::isClosed() const
{ {
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Closed; return _webSocket.getReadyState() == ix::ReadyState::Closed;
} }
void WebSocketClient::stop() void WebSocketClient::stop()
@ -97,12 +97,12 @@ namespace
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("client connected"); log("client connected");
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("client disconnected"); log("client disconnected");
@ -112,24 +112,24 @@ namespace
} }
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "Error ! " << error.reason; ss << "Error ! " << error.reason;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
_receivedPongMessages++; _receivedPongMessages++;
ss << "Received pong message " << str; ss << "Received pong message " << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
ss << "Received ping message " << str; ss << "Received ping message " << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
ss << "Received message " << str; ss << "Received message " << str;
log(ss.str()); log(ss.str());
@ -174,7 +174,7 @@ namespace
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
Logger() << "New server connection"; Logger() << "New server connection";
Logger() << "id: " << connectionState->getId(); Logger() << "id: " << connectionState->getId();
@ -185,11 +185,11 @@ namespace
Logger() << it.first << ": " << it.second; Logger() << it.first << ": " << it.second;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("Server closed connection"); log("Server closed connection");
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
log("Server received a ping"); log("Server received a ping");
receivedPingMessages++; receivedPingMessages++;

View File

@ -50,7 +50,7 @@ namespace ix
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
Logger() << "New connection"; Logger() << "New connection";
connectionState->computeId(); connectionState->computeId();
@ -64,11 +64,11 @@ namespace ix
connectionId = connectionState->getId(); connectionId = connectionState->getId();
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
Logger() << "Closed connection"; Logger() << "Closed connection";
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())
{ {

View File

@ -60,33 +60,33 @@ namespace
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("cmd_websocket_satori_chat: connected !"); log("cmd_websocket_satori_chat: connected !");
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("cmd_websocket_satori_chat: disconnected !"); log("cmd_websocket_satori_chat: disconnected !");
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "cmd_websocket_satori_chat: Error! "; ss << "cmd_websocket_satori_chat: Error! ";
ss << error.reason; ss << error.reason;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
log("cmd_websocket_satori_chat: received message.!"); log("cmd_websocket_satori_chat: received message.!");
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
log("cmd_websocket_satori_chat: received ping message.!"); log("cmd_websocket_satori_chat: received ping message.!");
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
log("cmd_websocket_satori_chat: received pong message.!"); log("cmd_websocket_satori_chat: received pong message.!");
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
log("cmd_websocket_satori_chat: received fragment.!"); log("cmd_websocket_satori_chat: received fragment.!");
} }

View File

@ -87,7 +87,7 @@ namespace
bool WebSocketChat::isReady() const bool WebSocketChat::isReady() const
{ {
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open; return _webSocket.getReadyState() == ix::ReadyState::Open;
} }
void WebSocketChat::stop() void WebSocketChat::stop()
@ -122,21 +122,21 @@ namespace
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
ss << "cmd_websocket_chat: user " ss << "cmd_websocket_chat: user "
<< _user << _user
<< " Connected !"; << " Connected !";
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "cmd_websocket_chat: user " ss << "cmd_websocket_chat: user "
<< _user << _user
<< " disconnected !"; << " disconnected !";
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
auto result = decodeMessage(str); auto result = decodeMessage(str);
@ -159,20 +159,20 @@ namespace
<< _user << " > "; << _user << " > ";
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "cmd_websocket_chat: Error ! " << error.reason; ss << "cmd_websocket_chat: Error ! " << error.reason;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
log("cmd_websocket_chat: received ping message"); log("cmd_websocket_chat: received ping message");
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
log("cmd_websocket_chat: received pong message"); log("cmd_websocket_chat: received pong message");
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
log("cmd_websocket_chat: received message fragment"); log("cmd_websocket_chat: received message fragment");
} }
@ -228,7 +228,7 @@ namespace
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
Logger() << "New connection"; Logger() << "New connection";
Logger() << "id: " << connectionState->getId(); Logger() << "id: " << connectionState->getId();
@ -239,11 +239,11 @@ namespace
Logger() << it.first << ": " << it.second; Logger() << it.first << ": " << it.second;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
log("Closed connection"); log("Closed connection");
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())
{ {

View File

@ -162,7 +162,7 @@ namespace ix
std::cerr << "Download size: " << downloadSize << std::endl; std::cerr << "Download size: " << downloadSize << std::endl;
std::cerr << "Status: " << statusCode << std::endl; std::cerr << "Status: " << statusCode << std::endl;
if (errorCode != HttpErrorCode_Ok) if (errorCode != HttpErrorCode::Ok)
{ {
std::cerr << "error message: " << errorMsg << std::endl; std::cerr << "error message: " << errorMsg << std::endl;
} }

View File

@ -100,14 +100,14 @@ namespace ix
CobraConnection::invokeTrafficTrackerCallback(wireSize, true); CobraConnection::invokeTrafficTrackerCallback(wireSize, true);
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
invokeEventCallback(ix::CobraConnection_EventType_Open, invokeEventCallback(ix::CobraConnection_EventType_Open,
std::string(), std::string(),
openInfo.headers); openInfo.headers);
sendHandshakeMessage(); sendHandshakeMessage();
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
_authenticated = false; _authenticated = false;
@ -117,7 +117,7 @@ namespace ix
invokeEventCallback(ix::CobraConnection_EventType_Closed, invokeEventCallback(ix::CobraConnection_EventType_Closed,
ss.str()); ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
Json::Value data; Json::Value data;
Json::Reader reader; Json::Reader reader;
@ -187,7 +187,7 @@ namespace ix
invokeErrorCallback("Un-handled message type", str); invokeErrorCallback("Un-handled message type", str);
} }
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
@ -384,7 +384,7 @@ namespace ix
bool CobraConnection::isConnected() const bool CobraConnection::isConnected() const
{ {
return _webSocket->getReadyState() == ix::WebSocket_ReadyState_Open; return _webSocket->getReadyState() == ix::ReadyState::Open;
} }
bool CobraConnection::isAuthenticated() const bool CobraConnection::isAuthenticated() const

View File

@ -65,7 +65,7 @@ namespace snake
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "id: " << state->getId() << std::endl; std::cerr << "id: " << state->getId() << std::endl;
@ -86,13 +86,13 @@ namespace snake
std::cerr << "Cannot connect to redis host" << std::endl; std::cerr << "Cannot connect to redis host" << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cerr << "Closed connection" std::cerr << "Closed connection"
<< " code " << closeInfo.code << " code " << closeInfo.code
<< " reason " << closeInfo.reason << std::endl; << " reason " << closeInfo.reason << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
@ -101,11 +101,11 @@ namespace snake
ss << "HTTP Status: " << error.http_status << std::endl; ss << "HTTP Status: " << error.http_status << std::endl;
std::cerr << ss.str(); std::cerr << ss.str();
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
std::cerr << "Received message fragment" << std::endl; std::cerr << "Received message fragment" << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << wireSize << " bytes" << std::endl; std::cerr << "Received " << wireSize << " bytes" << std::endl;
processCobraMessage(state, webSocket, _appConfig, str); processCobraMessage(state, webSocket, _appConfig, str);

View File

@ -28,7 +28,7 @@ namespace ix
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "id: " << connectionState->getId() << std::endl; std::cerr << "id: " << connectionState->getId() << std::endl;
@ -39,13 +39,13 @@ namespace ix
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cerr << "Closed connection" std::cerr << "Closed connection"
<< " code " << closeInfo.code << " code " << closeInfo.code
<< " reason " << closeInfo.reason << std::endl; << " reason " << closeInfo.reason << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
@ -54,11 +54,11 @@ namespace ix
ss << "HTTP Status: " << error.http_status << std::endl; ss << "HTTP Status: " << error.http_status << std::endl;
std::cerr << ss.str(); std::cerr << ss.str();
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
std::cerr << "Received message fragment" << std::endl; std::cerr << "Received message fragment" << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << wireSize << " bytes" << std::endl; std::cerr << "Received " << wireSize << " bytes" << std::endl;

View File

@ -68,7 +68,7 @@ namespace ix
bool WebSocketChat::isReady() const bool WebSocketChat::isReady() const
{ {
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open; return _webSocket.getReadyState() == ix::ReadyState::Open;
} }
void WebSocketChat::stop() void WebSocketChat::stop()
@ -92,7 +92,7 @@ namespace ix
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("ws chat: connected"); log("ws chat: connected");
std::cout << "Uri: " << openInfo.uri << std::endl; std::cout << "Uri: " << openInfo.uri << std::endl;
@ -107,7 +107,7 @@ namespace ix
<< " Connected !"; << " Connected !";
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "ws chat: user " ss << "ws chat: user "
<< _user << _user
@ -116,7 +116,7 @@ namespace ix
<< " reason " << closeInfo.reason; << " reason " << closeInfo.reason;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
auto result = decodeMessage(str); auto result = decodeMessage(str);
@ -132,7 +132,7 @@ namespace ix
<< _user << " > "; << _user << " > ";
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
ss << "#retries: " << error.retries << std::endl; ss << "#retries: " << error.retries << std::endl;

View File

@ -70,7 +70,7 @@ namespace ix
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("ws_connect: connected"); log("ws_connect: connected");
std::cout << "Uri: " << openInfo.uri << std::endl; std::cout << "Uri: " << openInfo.uri << std::endl;
@ -80,14 +80,14 @@ namespace ix
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "ws_connect: connection closed:"; ss << "ws_connect: connection closed:";
ss << " code " << closeInfo.code; ss << " code " << closeInfo.code;
ss << " reason " << closeInfo.reason << std::endl; ss << " reason " << closeInfo.reason << std::endl;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << wireSize << " bytes" << std::endl; std::cerr << "Received " << wireSize << " bytes" << std::endl;
@ -95,7 +95,7 @@ namespace ix
<< str; << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
ss << "#retries: " << error.retries << std::endl; ss << "#retries: " << error.retries << std::endl;
@ -103,15 +103,15 @@ namespace ix
ss << "HTTP Status: " << error.http_status << std::endl; ss << "HTTP Status: " << error.http_status << std::endl;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
std::cerr << "Received message fragment" << std::endl; std::cerr << "Received message fragment" << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
std::cerr << "Received ping" << std::endl; std::cerr << "Received ping" << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
std::cerr << "Received pong" << std::endl; std::cerr << "Received pong" << std::endl;
} }

View File

@ -28,7 +28,7 @@ namespace ix
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "id: " << connectionState->getId() << std::endl; std::cerr << "id: " << connectionState->getId() << std::endl;
@ -39,13 +39,13 @@ namespace ix
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cerr << "Closed connection" std::cerr << "Closed connection"
<< " code " << closeInfo.code << " code " << closeInfo.code
<< " reason " << closeInfo.reason << std::endl; << " reason " << closeInfo.reason << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
@ -54,7 +54,7 @@ namespace ix
ss << "HTTP Status: " << error.http_status << std::endl; ss << "HTTP Status: " << error.http_status << std::endl;
std::cerr << ss.str(); std::cerr << ss.str();
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " std::cerr << "Received "
<< wireSize << " bytes" << wireSize << " bytes"

View File

@ -150,12 +150,12 @@ namespace ix
std::cerr << "Download size: " << downloadSize << std::endl; std::cerr << "Download size: " << downloadSize << std::endl;
std::cerr << "Status: " << statusCode << std::endl; std::cerr << "Status: " << statusCode << std::endl;
if (errorCode != HttpErrorCode_Ok) if (errorCode != HttpErrorCode::Ok)
{ {
std::cerr << "error message: " << errorMsg << std::endl; std::cerr << "error message: " << errorMsg << std::endl;
} }
if (!headersOnly && errorCode == HttpErrorCode_Ok) if (!headersOnly && errorCode == HttpErrorCode::Ok)
{ {
if (save || !output.empty()) if (save || !output.empty())
{ {

View File

@ -64,7 +64,7 @@ namespace ix
std::cerr << "Received " << wireSize << " bytes" << std::endl; std::cerr << "Received " << wireSize << " bytes" << std::endl;
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
log("ping_pong: connected"); log("ping_pong: connected");
@ -75,7 +75,7 @@ namespace ix
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "ping_pong: disconnected:" ss << "ping_pong: disconnected:"
<< " code " << closeInfo.code << " code " << closeInfo.code
@ -83,25 +83,25 @@ namespace ix
<< str; << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
ss << "ping_pong: received message: " ss << "ping_pong: received message: "
<< str; << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Ping) else if (messageType == ix::WebSocketMessageType::Ping)
{ {
ss << "ping_pong: received ping message: " ss << "ping_pong: received ping message: "
<< str; << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Pong) else if (messageType == ix::WebSocketMessageType::Pong)
{ {
ss << "ping_pong: received pong message: " ss << "ping_pong: received pong message: "
<< str; << str;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
ss << "#retries: " << error.retries << std::endl; ss << "#retries: " << error.retries << std::endl;

View File

@ -191,7 +191,7 @@ namespace ix
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
_condition.notify_one(); _condition.notify_one();
@ -203,21 +203,21 @@ namespace ix
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "ws_receive: connection closed:"; ss << "ws_receive: connection closed:";
ss << " code " << closeInfo.code; ss << " code " << closeInfo.code;
ss << " reason " << closeInfo.reason << std::endl; ss << " reason " << closeInfo.reason << std::endl;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
ss << "ws_receive: transfered " << wireSize << " bytes"; ss << "ws_receive: transfered " << wireSize << " bytes";
log(ss.str()); log(ss.str());
handleMessage(str); handleMessage(str);
_condition.notify_one(); _condition.notify_one();
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
ss << "ws_receive: received fragment " << _receivedFragmentCounter++; ss << "ws_receive: received fragment " << _receivedFragmentCounter++;
log(ss.str()); log(ss.str());
@ -229,7 +229,7 @@ namespace ix
std::this_thread::sleep_for(duration); std::this_thread::sleep_for(duration);
} }
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "ws_receive "; ss << "ws_receive ";
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;

View File

@ -120,7 +120,7 @@ namespace ix
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
_condition.notify_one(); _condition.notify_one();
@ -132,14 +132,14 @@ namespace ix
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
ss << "ws_send: connection closed:"; ss << "ws_send: connection closed:";
ss << " code " << closeInfo.code; ss << " code " << closeInfo.code;
ss << " reason " << closeInfo.reason << std::endl; ss << " reason " << closeInfo.reason << std::endl;
log(ss.str()); log(ss.str());
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
_condition.notify_one(); _condition.notify_one();
@ -160,7 +160,7 @@ namespace ix
std::cerr << "Invalid id" << std::endl; std::cerr << "Invalid id" << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
ss << "ws_send "; ss << "ws_send ";
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;

View File

@ -28,7 +28,7 @@ namespace ix
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
std::cerr << "id: " << connectionState->getId() << std::endl; std::cerr << "id: " << connectionState->getId() << std::endl;
@ -39,13 +39,13 @@ namespace ix
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
} }
else if (messageType == ix::WebSocket_MessageType_Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cerr << "Closed connection" std::cerr << "Closed connection"
<< " code " << closeInfo.code << " code " << closeInfo.code
<< " reason " << closeInfo.reason << std::endl; << " reason " << closeInfo.reason << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Error) else if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Connection error: " << error.reason << std::endl; ss << "Connection error: " << error.reason << std::endl;
@ -54,12 +54,12 @@ namespace ix
ss << "HTTP Status: " << error.http_status << std::endl; ss << "HTTP Status: " << error.http_status << std::endl;
std::cerr << ss.str(); std::cerr << ss.str();
} }
else if (messageType == ix::WebSocket_MessageType_Fragment) else if (messageType == ix::WebSocketMessageType::Fragment)
{ {
std::cerr << "Received message fragment " std::cerr << "Received message fragment "
<< std::endl; << std::endl;
} }
else if (messageType == ix::WebSocket_MessageType_Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << wireSize << " bytes" << std::endl; std::cerr << "Received " << wireSize << " bytes" << std::endl;
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())