diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:12:52 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:20:27 +0900 |
| commit | d64089632e82a2548752c7a6baaa68365a795a74 (patch) | |
| tree | 57fc7b85a8c1290975d58fa4761b04a7695bf7e3 /src/WebSocketSession.cc | |
| parent | c81b1321122e8509b55c94c0cbca0d8d6d319f0c (diff) | |
| parent | a7cd943a06b1a5ff4515b57060b84037f8d01f9f (diff) | |
Merge branch 'master' into random-webseeding
Diffstat (limited to 'src/WebSocketSession.cc')
| -rw-r--r-- | src/WebSocketSession.cc | 154 |
1 files changed, 75 insertions, 79 deletions
diff --git a/src/WebSocketSession.cc b/src/WebSocketSession.cc index d51e789c..b32feb24 100644 --- a/src/WebSocketSession.cc +++ b/src/WebSocketSession.cc @@ -56,24 +56,25 @@ namespace aria2 { namespace rpc { namespace { -ssize_t sendCallback(wslay_event_context_ptr wsctx, - const uint8_t* data, size_t len, int flags, - void* userData) +ssize_t sendCallback(wslay_event_context_ptr wsctx, const uint8_t* data, + size_t len, int flags, void* userData) { WebSocketSession* session = reinterpret_cast<WebSocketSession*>(userData); const std::shared_ptr<SocketCore>& socket = session->getSocket(); try { ssize_t r = socket->writeData(data, len); - if(r == 0) { - if(socket->wantRead() || socket->wantWrite()) { + if (r == 0) { + if (socket->wantRead() || socket->wantWrite()) { wslay_event_set_error(wsctx, WSLAY_ERR_WOULDBLOCK); - } else { + } + else { wslay_event_set_error(wsctx, WSLAY_ERR_CALLBACK_FAILURE); } r = -1; } return r; - } catch(RecoverableException& e) { + } + catch (RecoverableException& e) { A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, e); wslay_event_set_error(wsctx, WSLAY_ERR_CALLBACK_FAILURE); return -1; @@ -82,27 +83,29 @@ ssize_t sendCallback(wslay_event_context_ptr wsctx, } // namespace namespace { -ssize_t recvCallback(wslay_event_context_ptr wsctx, - uint8_t* buf, size_t len, int flags, - void* userData) +ssize_t recvCallback(wslay_event_context_ptr wsctx, uint8_t* buf, size_t len, + int flags, void* userData) { WebSocketSession* session = reinterpret_cast<WebSocketSession*>(userData); const std::shared_ptr<SocketCore>& socket = session->getSocket(); try { ssize_t r; socket->readData(buf, len); - if(len == 0) { - if(socket->wantRead() || socket->wantWrite()) { + if (len == 0) { + if (socket->wantRead() || socket->wantWrite()) { wslay_event_set_error(wsctx, WSLAY_ERR_WOULDBLOCK); - } else { + } + else { wslay_event_set_error(wsctx, WSLAY_ERR_CALLBACK_FAILURE); } r = -1; - } else { + } + else { r = len; } return r; - } catch(RecoverableException& e) { + } + catch (RecoverableException& e) { A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, e); wslay_event_set_error(wsctx, WSLAY_ERR_CALLBACK_FAILURE); return -1; @@ -130,10 +133,9 @@ void addResponse(WebSocketSession* wsSession, } // namespace namespace { -void onFrameRecvStartCallback -(wslay_event_context_ptr wsctx, - const struct wslay_event_on_frame_recv_start_arg* arg, - void* userData) +void onFrameRecvStartCallback( + wslay_event_context_ptr wsctx, + const struct wslay_event_on_frame_recv_start_arg* arg, void* userData) { WebSocketSession* wsSession = reinterpret_cast<WebSocketSession*>(userData); wsSession->setIgnorePayload(wslay_is_ctrl_frame(arg->opcode)); @@ -141,13 +143,12 @@ void onFrameRecvStartCallback } // namespace namespace { -void onFrameRecvChunkCallback -(wslay_event_context_ptr wsctx, - const struct wslay_event_on_frame_recv_chunk_arg* arg, - void* userData) +void onFrameRecvChunkCallback( + wslay_event_context_ptr wsctx, + const struct wslay_event_on_frame_recv_chunk_arg* arg, void* userData) { WebSocketSession* wsSession = reinterpret_cast<WebSocketSession*>(userData); - if(!wsSession->getIgnorePayload()) { + if (!wsSession->getIgnorePayload()) { // The return value is ignored here. It will be evaluated in // onMsgRecvCallback. wsSession->parseUpdate(arg->data, arg->data_length); @@ -161,30 +162,31 @@ void onMsgRecvCallback(wslay_event_context_ptr wsctx, void* userData) { WebSocketSession* wsSession = reinterpret_cast<WebSocketSession*>(userData); - if(!wslay_is_ctrl_frame(arg->opcode)) { + if (!wslay_is_ctrl_frame(arg->opcode)) { // TODO Only process text frame ssize_t error = 0; auto json = wsSession->parseFinal(nullptr, 0, error); - if(error < 0) { + if (error < 0) { A2_LOG_INFO("Failed to parse JSON-RPC request"); - RpcResponse res - (createJsonRpcErrorResponse(-32700, "Parse error.", Null::g())); + RpcResponse res( + createJsonRpcErrorResponse(-32700, "Parse error.", Null::g())); addResponse(wsSession, res); return; } Dict* jsondict = downcast<Dict>(json); auto e = wsSession->getDownloadEngine(); - if(jsondict) { - RpcResponse res = - processJsonRpcRequest(jsondict, e); + if (jsondict) { + RpcResponse res = processJsonRpcRequest(jsondict, e); addResponse(wsSession, res); - } else { + } + else { List* jsonlist = downcast<List>(json); - if(jsonlist) { + if (jsonlist) { // This is batch call std::vector<RpcResponse> results; - for(List::ValueType::const_iterator i = jsonlist->begin(), - eoi = jsonlist->end(); i != eoi; ++i) { + for (List::ValueType::const_iterator i = jsonlist->begin(), + eoi = jsonlist->end(); + i != eoi; ++i) { Dict* jsondict = downcast<Dict>(*i); if (jsondict) { auto resp = processJsonRpcRequest(jsondict, e); @@ -192,15 +194,17 @@ void onMsgRecvCallback(wslay_event_context_ptr wsctx, } } addResponse(wsSession, results); - } else { - RpcResponse res(createJsonRpcErrorResponse - (-32600, "Invalid Request.", Null::g())); + } + else { + RpcResponse res( + createJsonRpcErrorResponse(-32600, "Invalid Request.", Null::g())); addResponse(wsSession, res); } } - } else { - RpcResponse res(createJsonRpcErrorResponse - (-32600, "Invalid Request.", Null::g())); + } + else { + RpcResponse res( + createJsonRpcErrorResponse(-32600, "Invalid Request.", Null::g())); addResponse(wsSession, res); } } @@ -208,11 +212,11 @@ void onMsgRecvCallback(wslay_event_context_ptr wsctx, WebSocketSession::WebSocketSession(const std::shared_ptr<SocketCore>& socket, DownloadEngine* e) - : socket_(socket), - e_(e), - ignorePayload_(false), - receivedLength_(0), - command_(nullptr) + : socket_(socket), + e_(e), + ignorePayload_(false), + receivedLength_(0), + command_(nullptr) { wslay_event_callbacks callbacks; memset(&callbacks, 0, sizeof(wslay_event_callbacks)); @@ -227,55 +231,46 @@ WebSocketSession::WebSocketSession(const std::shared_ptr<SocketCore>& socket, wslay_event_config_set_no_buffering(wsctx_, 1); } -WebSocketSession::~WebSocketSession() -{ - wslay_event_context_free(wsctx_); -} +WebSocketSession::~WebSocketSession() { wslay_event_context_free(wsctx_); } -bool WebSocketSession::wantRead() -{ - return wslay_event_want_read(wsctx_); -} +bool WebSocketSession::wantRead() { return wslay_event_want_read(wsctx_); } -bool WebSocketSession::wantWrite() -{ - return wslay_event_want_write(wsctx_); -} +bool WebSocketSession::wantWrite() { return wslay_event_want_write(wsctx_); } -bool WebSocketSession::finish() -{ - return !wantRead() && !wantWrite(); -} +bool WebSocketSession::finish() { return !wantRead() && !wantWrite(); } int WebSocketSession::onReadEvent() { - if(wslay_event_recv(wsctx_) == 0) { + if (wslay_event_recv(wsctx_) == 0) { return 0; - } else { + } + else { return -1; } } int WebSocketSession::onWriteEvent() { - if(wslay_event_send(wsctx_) == 0) { + if (wslay_event_send(wsctx_) == 0) { return 0; - } else { + } + else { return -1; } } namespace { -class TextMessageCommand : public Command -{ +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} - {} + const std::string& msg) + : Command(cuid), session_{std::move(session)}, msg_{msg} + { + } virtual bool execute() CXX11_OVERRIDE { session_->addTextMessage(msg_, false); @@ -297,9 +292,9 @@ void WebSocketSession::addTextMessage(const std::string& msg, bool delayed) // TODO Don't add text message if the size of outbound queue in // wsctx_ exceeds certain limit. - wslay_event_msg arg = { - WSLAY_TEXT_FRAME, reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size() - }; + wslay_event_msg arg = {WSLAY_TEXT_FRAME, + reinterpret_cast<const uint8_t*>(msg.c_str()), + msg.size()}; wslay_event_queue_msg(wsctx_, &arg); } @@ -317,19 +312,20 @@ ssize_t WebSocketSession::parseUpdate(const uint8_t* data, size_t len) { // Cap the number of bytes to feed the parser size_t maxlen = e_->getOption()->getAsInt(PREF_RPC_MAX_REQUEST_SIZE); - if(receivedLength_ + len <= maxlen) { + if (receivedLength_ + len <= maxlen) { receivedLength_ += len; - } else { + } + else { len = 0; } return parser_.parseUpdate(reinterpret_cast<const char*>(data), len); } -std::unique_ptr<ValueBase> WebSocketSession::parseFinal -(const uint8_t* data, size_t len, ssize_t& error) +std::unique_ptr<ValueBase> +WebSocketSession::parseFinal(const uint8_t* data, size_t len, ssize_t& error) { auto res = - parser_.parseFinal(reinterpret_cast<const char*>(data), len, error); + parser_.parseFinal(reinterpret_cast<const char*>(data), len, error); receivedLength_ = 0; return res; } |
