diff --git a/CMakeLists.txt b/CMakeLists.txt index 435a4e37..f53ba576 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,8 @@ set(obs-websocket_SOURCES src/WSEvents.cpp src/Config.cpp src/Utils.cpp + src/rpc/RpcRequest.cpp + src/rpc/RpcResponse.h src/forms/settings-dialog.cpp) set(obs-websocket_HEADERS @@ -56,6 +58,8 @@ set(obs-websocket_HEADERS src/WSEvents.h src/Config.h src/Utils.h + src/rpc/RpcRequest.h + src/rpc/RpcResponse.h src/forms/settings-dialog.h) # --- Platform-independent build settings --- diff --git a/src/rpc/RpcRequest.cpp b/src/rpc/RpcRequest.cpp new file mode 100644 index 00000000..277565da --- /dev/null +++ b/src/rpc/RpcRequest.cpp @@ -0,0 +1,27 @@ +/* +obs-websocket +Copyright (C) 2016-2019 Stéphane Lepin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see +*/ + +#include "RpcRequest.h" + +RpcRequest::RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params) : + _messageId(messageId), + _methodName(methodName) +{ + _parameters = obs_data_create(); + obs_data_apply(_parameters, params); +} diff --git a/src/rpc/RpcRequest.h b/src/rpc/RpcRequest.h new file mode 100644 index 00000000..4231be03 --- /dev/null +++ b/src/rpc/RpcRequest.h @@ -0,0 +1,46 @@ +/* +obs-websocket +Copyright (C) 2016-2019 Stéphane Lepin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see +*/ + +#pragma once + +#include +#include +#include "../obs-websocket.h" + +class RpcRequest +{ +public: + explicit RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params); + + const QString& messageId() const { + return _messageId; + } + + const QString& methodName() const { + return _methodName; + } + + const obs_data_t* parameters() const { + return _parameters; + } + +private: + const QString _messageId; + const QString _methodName; + OBSDataAutoRelease _parameters; +}; diff --git a/src/rpc/RpcResponse.cpp b/src/rpc/RpcResponse.cpp new file mode 100644 index 00000000..028c4247 --- /dev/null +++ b/src/rpc/RpcResponse.cpp @@ -0,0 +1,48 @@ +/* +obs-websocket +Copyright (C) 2016-2019 Stéphane Lepin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see +*/ + +#include "RpcResponse.h" +#include "RpcRequest.h" + +RpcResponse::RpcResponse( + ResponseStatus status, const QString& messageId, + const QString& methodName, obs_data_t* additionalFields +) : + _status(status), + _messageId(messageId), + _methodName(methodName), + _additionalFields(nullptr) +{ + if (additionalFields) { + _additionalFields = obs_data_create(); + obs_data_apply(_additionalFields, additionalFields); + } +} + +const RpcResponse::RpcResponse ok(const RpcRequest& request, obs_data_t* additionalFields) +{ + RpcResponse response(ResponseStatus::Ok, request.messageId(), request.methodName(), additionalFields); + return response; +} + +const RpcResponse::RpcResponse fail(const RpcRequest& request, const QString& errorMessage) +{ + RpcResponse response(ResponseStatus::Error, request.messageId(), request.methodName()); + response._errorMessage = errorMessage; + return response; +} diff --git a/src/rpc/RpcResponse.h b/src/rpc/RpcResponse.h new file mode 100644 index 00000000..613b3276 --- /dev/null +++ b/src/rpc/RpcResponse.h @@ -0,0 +1,72 @@ +/* +obs-websocket +Copyright (C) 2016-2019 Stéphane Lepin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see +*/ + +#pragma once + +#include +#include +#include "../obs-websocket.h" + +class RpcRequest; + +enum ResponseStatus +{ + Unknown, + Ok, + Error +}; + +class RpcResponse +{ +public: + static RpcResponse ofRequest(const RpcRequest& request); + static const RpcResponse ok(const RpcRequest& request, obs_data_t* additionalFields = nullptr); + static const RpcResponse fail(const RpcRequest& request, const QString& errorMessage); + + ResponseStatus status() { + return _status; + } + + const QString& messageId() const { + return _messageId; + } + + const QString& methodName() const { + return _methodName; + } + + const QString& errorMessage() const { + return _errorMessage; + } + + const obs_data_t* parameters() const { + return _additionalFields; + } + +private: + explicit RpcResponse( + ResponseStatus status, + const QString& messageId, const QString& methodName, + obs_data_t* additionalFields = nullptr + ); + const ResponseStatus _status; + const QString _messageId; + const QString _methodName; + QString _errorMessage; + OBSDataAutoRelease _additionalFields; +};