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/IXSelectInterruptFactory.cpp
|
||||
ixwebsocket/IXConnectionState.cpp
|
||||
ixwebsocket/IXWebSocketCloseConstants.cpp
|
||||
)
|
||||
|
||||
set( IXWEBSOCKET_HEADERS
|
||||
@ -72,6 +73,7 @@ set( IXWEBSOCKET_HEADERS
|
||||
ixwebsocket/IXSelectInterrupt.h
|
||||
ixwebsocket/IXSelectInterruptFactory.h
|
||||
ixwebsocket/IXConnectionState.h
|
||||
ixwebsocket/IXWebSocketCloseConstants.h
|
||||
)
|
||||
|
||||
if (UNIX)
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "IXWebSocketSendInfo.h"
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXWebSocketCloseConstants.h"
|
||||
#include "IXProgressCallback.h"
|
||||
|
||||
namespace ix
|
||||
@ -101,8 +102,8 @@ namespace ix
|
||||
void start();
|
||||
|
||||
// stop is synchronous
|
||||
void stop(uint16_t code = 1000,
|
||||
const std::string& reason = "Normal closure");
|
||||
void stop(uint16_t code = WebSocketCloseConstants::kNormalClosureCode,
|
||||
const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage);
|
||||
|
||||
// Run in blocking mode, by connecting first manually, and then calling run.
|
||||
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);
|
||||
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() :
|
||||
_useMask(true),
|
||||
_readyState(ReadyState::CLOSED),
|
||||
_closeCode(kInternalErrorCode),
|
||||
_closeReason(kInternalErrorMessage),
|
||||
_closeCode(WebSocketCloseConstants::kInternalErrorCode),
|
||||
_closeReason(WebSocketCloseConstants::kInternalErrorMessage),
|
||||
_closeWireSize(0),
|
||||
_closeRemote(false),
|
||||
_enablePerMessageDeflate(false),
|
||||
@ -221,8 +211,8 @@ namespace ix
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_closeDataMutex);
|
||||
_onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
|
||||
_closeCode = kInternalErrorCode;
|
||||
_closeReason = kInternalErrorMessage;
|
||||
_closeCode = WebSocketCloseConstants::kInternalErrorCode;
|
||||
_closeReason = WebSocketCloseConstants::kInternalErrorMessage;
|
||||
_closeWireSize = 0;
|
||||
_closeRemote = false;
|
||||
}
|
||||
@ -292,7 +282,8 @@ namespace ix
|
||||
// ping response (PONG) exceeds the maximum delay, then close the connection
|
||||
if (pingTimeoutExceeded())
|
||||
{
|
||||
close(kInternalErrorCode, kPingTimeoutMessage);
|
||||
close(WebSocketCloseConstants::kInternalErrorCode,
|
||||
WebSocketCloseConstants::kPingTimeoutMessage);
|
||||
}
|
||||
// If ping is enabled and no ping has been sent for a duration
|
||||
// exceeding our ping interval, send a ping to the server.
|
||||
@ -637,8 +628,8 @@ namespace ix
|
||||
else
|
||||
{
|
||||
// no close code received
|
||||
code = kNoStatusCodeErrorCode;
|
||||
reason = kNoStatusCodeErrorMessage;
|
||||
code = WebSocketCloseConstants::kNoStatusCodeErrorCode;
|
||||
reason = WebSocketCloseConstants::kNoStatusCodeErrorMessage;
|
||||
}
|
||||
|
||||
// We receive a CLOSE frame from remote and are NOT the ones who triggered the close
|
||||
@ -672,7 +663,9 @@ namespace ix
|
||||
else
|
||||
{
|
||||
// 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
|
||||
@ -695,7 +688,9 @@ namespace ix
|
||||
// if we weren't closing, then close using abnormal close code and message
|
||||
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;
|
||||
|
||||
// if a status is set/was read
|
||||
if (code != kNoStatusCodeErrorCode)
|
||||
if (code != WebSocketCloseConstants::kNoStatusCodeErrorCode)
|
||||
{
|
||||
// See list of close events here:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "IXCancellationRequest.h"
|
||||
#include "IXWebSocketHandshake.h"
|
||||
#include "IXProgressCallback.h"
|
||||
#include "IXWebSocketCloseConstants.h"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
@ -91,8 +92,8 @@ namespace ix
|
||||
const OnProgressCallback& onProgressCallback);
|
||||
WebSocketSendInfo sendPing(const std::string& message);
|
||||
|
||||
void close(uint16_t code = 1000,
|
||||
const std::string& reason = "Normal closure",
|
||||
void close(uint16_t code = WebSocketCloseConstants::kNormalClosureCode,
|
||||
const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage,
|
||||
size_t closeWireSize = 0,
|
||||
bool remote = false);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user