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) 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() std::vector<WebSocketServer::WebSocketState> WebSocketServer::GetWebSocketSessions()

View File

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

View File

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

View File

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