diff options
Diffstat (limited to 'src/WebSocketSession.cc')
| -rw-r--r-- | src/WebSocketSession.cc | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/WebSocketSession.cc b/src/WebSocketSession.cc index aced5622..b035eb18 100644 --- a/src/WebSocketSession.cc +++ b/src/WebSocketSession.cc @@ -43,6 +43,8 @@ #include "RecoverableException.h" #include "message.h" #include "DownloadEngine.h" +#include "DelayedCommand.h" +#include "WebSocketInteractionCommand.h" #include "rpc_helper.h" #include "RpcResponse.h" #include "json.h" @@ -111,8 +113,9 @@ ssize_t recvCallback(wslay_event_context_ptr wsctx, namespace { void addResponse(WebSocketSession* wsSession, const RpcResponse& res) { + bool notauthorized = rpc::not_authorized(res); std::string response = toJson(res, "", false); - wsSession->addTextMessage(response); + wsSession->addTextMessage(response, notauthorized); } } // namespace @@ -120,8 +123,9 @@ namespace { void addResponse(WebSocketSession* wsSession, const std::vector<RpcResponse>& results) { + bool notauthorized = rpc::any_not_authorized(results.begin(), results.end()); std::string response = toJsonBatch(results, "", false); - wsSession->addTextMessage(response); + wsSession->addTextMessage(response, notauthorized); } } // namespace @@ -264,8 +268,35 @@ int WebSocketSession::onWriteEvent() } } -void WebSocketSession::addTextMessage(const std::string& msg) +namespace { +class TextMessageCommand : public Command { +private: + std::shared_ptr<WebSocketSession> session_; + const std::string msg_; +public: + TextMessageCommand(cuid_t cuid, std::shared_ptr<WebSocketSession> session, + const std::string& msg) + : Command(cuid), session_{std::move(session)}, msg_{msg} + {} + virtual bool execute() CXX11_OVERRIDE + { + session_->addTextMessage(msg_, false); + return true; + } +}; +} // namespace + +void WebSocketSession::addTextMessage(const std::string& msg, bool delayed) +{ + if (delayed) { + auto e = getDownloadEngine(); + auto cuid = command_->getCuid(); + auto c = make_unique<TextMessageCommand>(cuid, command_->getSession(), msg); + e->addCommand(make_unique<DelayedCommand>(cuid, e, 1, std::move(c), false)); + return; + } + // TODO Don't add text message if the size of outbound queue in // wsctx_ exceeds certain limit. wslay_event_msg arg = { |
