/* */ #include "WebSocketInteractionCommand.h" #include "SocketCore.h" #include "DownloadEngine.h" #include "RequestGroupMan.h" #include "WebSocketSession.h" #include "Logger.h" #include "LogFactory.h" #include "fmt.h" #include "SingletonHolder.h" #include "Notifier.h" namespace aria2 { namespace rpc { WebSocketInteractionCommand::WebSocketInteractionCommand (cuid_t cuid, const SharedHandle& wsSession, DownloadEngine* e, const SharedHandle& socket) : Command(cuid), e_(e), socket_(socket), writeCheck_(false), wsSession_(wsSession) { SingletonHolder::instance()->addWebSocketSession(wsSession_); e_->addSocketForReadCheck(socket_, this); } WebSocketInteractionCommand::~WebSocketInteractionCommand() { e_->deleteSocketForReadCheck(socket_, this); if(writeCheck_) { e_->deleteSocketForWriteCheck(socket_, this); } SingletonHolder::instance()->removeWebSocketSession(wsSession_); } void WebSocketInteractionCommand::updateWriteCheck() { if(wsSession_->wantWrite()) { if(!writeCheck_) { writeCheck_ = true; e_->addSocketForWriteCheck(socket_, this); } } else if(writeCheck_) { writeCheck_ = false; e_->deleteSocketForWriteCheck(socket_, this); } } bool WebSocketInteractionCommand::execute() { if(e_->isHaltRequested()) { return true; } if(wsSession_->onReadEvent() == -1 || wsSession_->onWriteEvent() == -1) { if(wsSession_->closeSent() || wsSession_->closeReceived()) { A2_LOG_INFO(fmt("CUID#%" PRId64 " - WebSocket session terminated.", getCuid())); } else { A2_LOG_INFO(fmt("CUID#%" PRId64 " - WebSocket session terminated" " (Possibly due to EOF).", getCuid())); } return true; } if(wsSession_->finish()) { return true; } updateWriteCheck(); e_->addCommand(this); return false; } } // namespace rpc } // namespace aria2