Add constants for closing code and messages
This commit is contained in:
parent
5c4840f129
commit
0e59927384
@ -42,6 +42,7 @@ set( IXWEBSOCKET_SOURCES
|
|||||||
ixwebsocket/IXSelectInterrupt.cpp
|
ixwebsocket/IXSelectInterrupt.cpp
|
||||||
ixwebsocket/IXSelectInterruptFactory.cpp
|
ixwebsocket/IXSelectInterruptFactory.cpp
|
||||||
ixwebsocket/IXConnectionState.cpp
|
ixwebsocket/IXConnectionState.cpp
|
||||||
|
ixwebsocket/IXWebSocketCloseConstants.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set( IXWEBSOCKET_HEADERS
|
set( IXWEBSOCKET_HEADERS
|
||||||
@ -72,6 +73,7 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXSelectInterrupt.h
|
ixwebsocket/IXSelectInterrupt.h
|
||||||
ixwebsocket/IXSelectInterruptFactory.h
|
ixwebsocket/IXSelectInterruptFactory.h
|
||||||
ixwebsocket/IXConnectionState.h
|
ixwebsocket/IXConnectionState.h
|
||||||
|
ixwebsocket/IXWebSocketCloseConstants.h
|
||||||
)
|
)
|
||||||
|
|
||||||
if (UNIX)
|
if (UNIX)
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include "IXWebSocketSendInfo.h"
|
#include "IXWebSocketSendInfo.h"
|
||||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||||
#include "IXWebSocketHttpHeaders.h"
|
#include "IXWebSocketHttpHeaders.h"
|
||||||
|
#include "IXWebSocketCloseConstants.h"
|
||||||
#include "IXProgressCallback.h"
|
#include "IXProgressCallback.h"
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -101,8 +102,8 @@ namespace ix
|
|||||||
void start();
|
void start();
|
||||||
|
|
||||||
// stop is synchronous
|
// stop is synchronous
|
||||||
void stop(uint16_t code = 1000,
|
void stop(uint16_t code = WebSocketCloseConstants::kNormalClosureCode,
|
||||||
const std::string& reason = "Normal closure");
|
const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage);
|
||||||
|
|
||||||
// Run in blocking mode, by connecting first manually, and then calling run.
|
// Run in blocking mode, by connecting first manually, and then calling run.
|
||||||
WebSocketInitResult connect(int timeoutSecs);
|
WebSocketInitResult connect(int timeoutSecs);
|
||||||
|
23
ixwebsocket/IXWebSocketCloseConstants.cpp
Normal file
23
ixwebsocket/IXWebSocketCloseConstants.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* IXWebSocketCloseConstants.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "IXWebSocketCloseConstants.h"
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
const uint16_t WebSocketCloseConstants::kNormalClosureCode(1000);
|
||||||
|
const uint16_t WebSocketCloseConstants::kInternalErrorCode(1011);
|
||||||
|
const uint16_t WebSocketCloseConstants::kAbnormalCloseCode(1006);
|
||||||
|
const uint16_t WebSocketCloseConstants::kProtocolErrorCode(1002);
|
||||||
|
const uint16_t WebSocketCloseConstants::kNoStatusCodeErrorCode(1005);
|
||||||
|
|
||||||
|
const std::string WebSocketCloseConstants::kNormalClosureMessage("Normal closure");
|
||||||
|
const std::string WebSocketCloseConstants::kInternalErrorMessage("Internal error");
|
||||||
|
const std::string WebSocketCloseConstants::kAbnormalCloseMessage("Abnormal closure");
|
||||||
|
const std::string WebSocketCloseConstants::kPingTimeoutMessage("Ping timeout");
|
||||||
|
const std::string WebSocketCloseConstants::kProtocolErrorMessage("Protocol error");
|
||||||
|
const std::string WebSocketCloseConstants::kNoStatusCodeErrorMessage("No status code");
|
||||||
|
}
|
29
ixwebsocket/IXWebSocketCloseConstants.h
Normal file
29
ixwebsocket/IXWebSocketCloseConstants.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* IXWebSocketCloseConstants.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
struct WebSocketCloseConstants
|
||||||
|
{
|
||||||
|
static const uint16_t kNormalClosureCode;
|
||||||
|
static const uint16_t kInternalErrorCode;
|
||||||
|
static const uint16_t kAbnormalCloseCode;
|
||||||
|
static const uint16_t kProtocolErrorCode;
|
||||||
|
static const uint16_t kNoStatusCodeErrorCode;
|
||||||
|
|
||||||
|
static const std::string kNormalClosureMessage;
|
||||||
|
static const std::string kInternalErrorMessage;
|
||||||
|
static const std::string kAbnormalCloseMessage;
|
||||||
|
static const std::string kPingTimeoutMessage;
|
||||||
|
static const std::string kProtocolErrorMessage;
|
||||||
|
static const std::string kNoStatusCodeErrorMessage;
|
||||||
|
};
|
||||||
|
}
|
@ -74,21 +74,11 @@ namespace ix
|
|||||||
const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(200);
|
const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(200);
|
||||||
constexpr size_t WebSocketTransport::kChunkSize;
|
constexpr size_t WebSocketTransport::kChunkSize;
|
||||||
|
|
||||||
const uint16_t WebSocketTransport::kInternalErrorCode(1011);
|
|
||||||
const uint16_t WebSocketTransport::kAbnormalCloseCode(1006);
|
|
||||||
const uint16_t WebSocketTransport::kProtocolErrorCode(1002);
|
|
||||||
const uint16_t WebSocketTransport::kNoStatusCodeErrorCode(1005);
|
|
||||||
const std::string WebSocketTransport::kInternalErrorMessage("Internal error");
|
|
||||||
const std::string WebSocketTransport::kAbnormalCloseMessage("Abnormal closure");
|
|
||||||
const std::string WebSocketTransport::kPingTimeoutMessage("Ping timeout");
|
|
||||||
const std::string WebSocketTransport::kProtocolErrorMessage("Protocol error");
|
|
||||||
const std::string WebSocketTransport::kNoStatusCodeErrorMessage("No status code");
|
|
||||||
|
|
||||||
WebSocketTransport::WebSocketTransport() :
|
WebSocketTransport::WebSocketTransport() :
|
||||||
_useMask(true),
|
_useMask(true),
|
||||||
_readyState(ReadyState::CLOSED),
|
_readyState(ReadyState::CLOSED),
|
||||||
_closeCode(kInternalErrorCode),
|
_closeCode(WebSocketCloseConstants::kInternalErrorCode),
|
||||||
_closeReason(kInternalErrorMessage),
|
_closeReason(WebSocketCloseConstants::kInternalErrorMessage),
|
||||||
_closeWireSize(0),
|
_closeWireSize(0),
|
||||||
_closeRemote(false),
|
_closeRemote(false),
|
||||||
_enablePerMessageDeflate(false),
|
_enablePerMessageDeflate(false),
|
||||||
@ -221,8 +211,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_closeDataMutex);
|
std::lock_guard<std::mutex> lock(_closeDataMutex);
|
||||||
_onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
|
_onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
|
||||||
_closeCode = kInternalErrorCode;
|
_closeCode = WebSocketCloseConstants::kInternalErrorCode;
|
||||||
_closeReason = kInternalErrorMessage;
|
_closeReason = WebSocketCloseConstants::kInternalErrorMessage;
|
||||||
_closeWireSize = 0;
|
_closeWireSize = 0;
|
||||||
_closeRemote = false;
|
_closeRemote = false;
|
||||||
}
|
}
|
||||||
@ -292,7 +282,8 @@ namespace ix
|
|||||||
// ping response (PONG) exceeds the maximum delay, then close the connection
|
// ping response (PONG) exceeds the maximum delay, then close the connection
|
||||||
if (pingTimeoutExceeded())
|
if (pingTimeoutExceeded())
|
||||||
{
|
{
|
||||||
close(kInternalErrorCode, kPingTimeoutMessage);
|
close(WebSocketCloseConstants::kInternalErrorCode,
|
||||||
|
WebSocketCloseConstants::kPingTimeoutMessage);
|
||||||
}
|
}
|
||||||
// If ping is enabled and no ping has been sent for a duration
|
// If ping is enabled and no ping has been sent for a duration
|
||||||
// exceeding our ping interval, send a ping to the server.
|
// exceeding our ping interval, send a ping to the server.
|
||||||
@ -637,8 +628,8 @@ namespace ix
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// no close code received
|
// no close code received
|
||||||
code = kNoStatusCodeErrorCode;
|
code = WebSocketCloseConstants::kNoStatusCodeErrorCode;
|
||||||
reason = kNoStatusCodeErrorMessage;
|
reason = WebSocketCloseConstants::kNoStatusCodeErrorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -672,7 +663,9 @@ namespace ix
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Unexpected frame type
|
// Unexpected frame type
|
||||||
close(kProtocolErrorCode, kProtocolErrorMessage, _rxbuf.size());
|
close(WebSocketCloseConstants::kProtocolErrorCode,
|
||||||
|
WebSocketCloseConstants::kProtocolErrorMessage,
|
||||||
|
_rxbuf.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erase the message that has been processed from the input/read buffer
|
// Erase the message that has been processed from the input/read buffer
|
||||||
@ -695,7 +688,9 @@ namespace ix
|
|||||||
// 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 != ReadyState::CLOSED)
|
else if (_readyState != ReadyState::CLOSED)
|
||||||
{
|
{
|
||||||
closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, false);
|
closeSocketAndSwitchToClosedState(WebSocketCloseConstants::kAbnormalCloseCode,
|
||||||
|
WebSocketCloseConstants::kAbnormalCloseMessage,
|
||||||
|
0, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1001,7 +996,7 @@ namespace ix
|
|||||||
bool compress = false;
|
bool compress = false;
|
||||||
|
|
||||||
// if a status is set/was read
|
// if a status is set/was read
|
||||||
if (code != kNoStatusCodeErrorCode)
|
if (code != WebSocketCloseConstants::kNoStatusCodeErrorCode)
|
||||||
{
|
{
|
||||||
// See list of close events here:
|
// See list of close events here:
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
#include "IXWebSocketHandshake.h"
|
#include "IXWebSocketHandshake.h"
|
||||||
#include "IXProgressCallback.h"
|
#include "IXProgressCallback.h"
|
||||||
|
#include "IXWebSocketCloseConstants.h"
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -91,8 +92,8 @@ namespace ix
|
|||||||
const OnProgressCallback& onProgressCallback);
|
const OnProgressCallback& onProgressCallback);
|
||||||
WebSocketSendInfo sendPing(const std::string& message);
|
WebSocketSendInfo sendPing(const std::string& message);
|
||||||
|
|
||||||
void close(uint16_t code = 1000,
|
void close(uint16_t code = WebSocketCloseConstants::kNormalClosureCode,
|
||||||
const std::string& reason = "Normal closure",
|
const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage,
|
||||||
size_t closeWireSize = 0,
|
size_t closeWireSize = 0,
|
||||||
bool remote = false);
|
bool remote = false);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user