WebSocketSession: Add encoding

This commit is contained in:
tt2468 2021-04-28 10:40:51 -07:00
parent 948750da6a
commit 1245958031
4 changed files with 77 additions and 55 deletions

View File

@ -122,7 +122,8 @@ void WebSocketServer::Stop()
void WebSocketServer::InvalidateSession(websocketpp::connection_hdl hdl)
{
;
blog(LOG_INFO, "Invalidating a session.");
_server.close(hdl, WebSocketCloseCode::SessionInvalidated, "Your session has been invalidated.");
}
std::vector<WebSocketServer::WebSocketState> WebSocketServer::GetWebSocketSessions()

View File

@ -15,7 +15,7 @@ using json = nlohmann::json;
class WebSocketServer
{
public:
enum WebsocketCloseCode: std::uint16_t {
enum WebSocketCloseCode: uint16_t {
UnknownReason = 4000,
// The server was unable to decode the incoming websocket message
MessageDecodeError = 4001,
@ -37,6 +37,11 @@ class WebSocketServer
UnsupportedProtocolVersion = 4009,
};
enum WebSocketEncoding: uint8_t {
Json,
MsgPack
};
struct WebSocketState {
websocketpp::connection_hdl hdl;
std::string remoteAddress;

View File

@ -3,95 +3,107 @@
#include "plugin-macros.generated.h"
WebSocketSession::WebSocketSession() :
incomingMessages(0),
outgoingMessages(0),
rpcVersion(OBS_WEBSOCKET_RPC_VERSION),
isIdentified(false),
ignoreInvalidMessages(false),
ignoreNonFatalRequestChecks(false),
eventSubscriptions(0)
_incomingMessages(0),
_outgoingMessages(0),
_encoding(0),
_challenge(""),
_rpcVersion(OBS_WEBSOCKET_RPC_VERSION),
_isIdentified(false),
_ignoreInvalidMessages(false),
_ignoreNonFatalRequestChecks(false),
_eventSubscriptions(0)
{
}
uint64_t WebSocketSession::IncomingMessages()
{
return _incomingMessages.load();
}
void WebSocketSession::IncrementIncomingMessages()
{
_incomingMessages++;
}
uint64_t WebSocketSession::OutgoingMessages()
{
return _outgoingMessages.load();
}
void WebSocketSession::IncrementOutgoingMessages()
{
_outgoingMessages++;
}
uint8_t WebSocketSession::GetEncoding()
{
return _encoding.load();
}
void WebSocketSession::SetEncoding(uint8_t encoding)
{
_encoding.store(encoding);
}
std::string WebSocketSession::Challenge()
{
std::lock_guard<std::mutex> lock(challengeMutex);
std::string ret(challenge);
std::lock_guard<std::mutex> lock(_challengeMutex);
std::string ret(_challenge);
return ret;
}
void WebSocketSession::SetChallenge(std::string challengeString)
{
std::lock_guard<std::mutex> lock(challengeMutex);
challenge = challengeString;
}
uint64_t WebSocketSession::IncomingMessages()
{
return incomingMessages.load();
}
void WebSocketSession::IncrementIncomingMessages()
{
incomingMessages++;
}
uint64_t WebSocketSession::OutgoingMessages()
{
return outgoingMessages.load();
}
void WebSocketSession::IncrementOutgoingMessages()
{
outgoingMessages++;
std::lock_guard<std::mutex> lock(_challengeMutex);
_challenge = challengeString;
}
uint8_t WebSocketSession::RpcVersion()
{
return rpcVersion.load();
return _rpcVersion.load();
}
void WebSocketSession::SetRpcVersion(uint8_t version)
{
rpcVersion.store(version);
_rpcVersion.store(version);
}
bool WebSocketSession::IsIdentified()
{
return isIdentified.load();
return _isIdentified.load();
}
void WebSocketSession::SetIsIdentified(bool identified)
{
isIdentified.store(identified);
_isIdentified.store(identified);
}
bool WebSocketSession::IgnoreInvalidMessages()
{
return ignoreInvalidMessages.load();
return _ignoreInvalidMessages.load();
}
void WebSocketSession::SetIgnoreInvalidMessages(bool ignore)
{
ignoreInvalidMessages.store(ignore);
_ignoreInvalidMessages.store(ignore);
}
bool WebSocketSession::IgnoreNonFatalRequestChecks()
{
return ignoreNonFatalRequestChecks.load();
return _ignoreNonFatalRequestChecks.load();
}
void WebSocketSession::SetIgnoreNonFatalRequestChecks(bool ignore)
{
ignoreNonFatalRequestChecks.store(ignore);
_ignoreNonFatalRequestChecks.store(ignore);
}
uint64_t WebSocketSession::EventSubscriptions()
{
return eventSubscriptions.load();
return _eventSubscriptions.load();
}
void WebSocketSession::SetEventSubscriptions(uint64_t subscriptions)
{
eventSubscriptions.store(subscriptions);
_eventSubscriptions.store(subscriptions);
}

View File

@ -9,15 +9,18 @@ class WebSocketSession
public:
WebSocketSession();
std::string Challenge();
void SetChallenge(std::string challenge);
uint64_t IncomingMessages();
void IncrementIncomingMessages();
uint64_t OutgoingMessages();
void IncrementOutgoingMessages();
uint8_t GetEncoding();
void SetEncoding(uint8_t encoding);
std::string Challenge();
void SetChallenge(std::string challenge);
uint8_t RpcVersion();
void SetRpcVersion(uint8_t version);
@ -34,13 +37,14 @@ class WebSocketSession
void SetEventSubscriptions(uint64_t subscriptions);
private:
std::mutex challengeMutex;
std::string challenge;
std::atomic<uint64_t> incomingMessages;
std::atomic<uint64_t> outgoingMessages;
std::atomic<uint8_t> rpcVersion;
std::atomic<bool> isIdentified;
std::atomic<bool> ignoreInvalidMessages;
std::atomic<bool> ignoreNonFatalRequestChecks;
std::atomic<uint64_t> eventSubscriptions;
std::atomic<uint64_t> _incomingMessages;
std::atomic<uint64_t> _outgoingMessages;
std::atomic<uint8_t> _encoding;
std::mutex _challengeMutex;
std::string _challenge;
std::atomic<uint8_t> _rpcVersion;
std::atomic<bool> _isIdentified;
std::atomic<bool> _ignoreInvalidMessages;
std::atomic<bool> _ignoreNonFatalRequestChecks;
std::atomic<uint64_t> _eventSubscriptions;
};