#include "net/SocketSession.hpp" #include #include #include "aiohttp/client/client_session.hpp" #include "aiohttp/loop/task_handle.hpp" #include "core/Log.hpp" #include "core/UiQueue.hpp" #include "net/NetworkImpl.hpp" #include "spec/NetLimits.hpp" namespace dp { struct SocketSession::Impl { std::string url; std::vector> headers; Handlers handlers; std::atomic state{State::Closed}; std::atomic stop{false}; std::mutex mutex; aiohttp::ClientWebSocketResponse* ws = nullptr; // loop thread only, guarded by `alive` bool alive = false; aiohttp::Task run(std::shared_ptr self); void post(std::function fn) { auto self = this; UiQueue::instance().post([fn = std::move(fn), self] { (void)self; fn(); }); } }; aiohttp::Task SocketSession::Impl::run(std::shared_ptr self) { auto& net = *NetworkService::instance().impl_; std::string reason = "closed"; try { aiohttp::WSConnectOptions opts; for (const auto& h : headers) opts.headers.push_back(h); auto socket = co_await net.session->ws_connect(aiohttp::URL(url), std::move(opts)); ws = socket.get(); alive = true; state = State::Open; UiQueue::instance().post([self] { if (!self->stop && self->handlers.onOpen) self->handlers.onOpen(); }); while (!stop) { aiohttp::WSMessage msg = co_await socket->receive(); if (msg.type == aiohttp::WSMsgType::TEXT) { auto text = std::make_shared(std::get(msg.data)); if (text->size() > spec::NetLimits::kMaxSocketFrameBytes) { Log::warn("socket frame dropped: over cap"); continue; } UiQueue::instance().post([self, text] { if (!self->stop && self->handlers.onText) self->handlers.onText(*text); }); } else if (msg.type == aiohttp::WSMsgType::CLOSE || msg.type == aiohttp::WSMsgType::CLOSING || msg.type == aiohttp::WSMsgType::CLOSED || msg.type == aiohttp::WSMsgType::ERROR) { if (const int* code = std::get_if(&msg.data)) reason = "close code " + std::to_string(*code); break; } } alive = false; ws = nullptr; co_await socket->close(); } catch (const std::exception& e) { reason = e.what(); } catch (...) { reason = "unknown error"; } alive = false; ws = nullptr; state = State::Closed; UiQueue::instance().post([self, reason] { if (!self->stop && self->handlers.onClose) self->handlers.onClose(reason); }); } SocketSession::SocketSession(std::string url, std::vector> headers, Handlers handlers) : impl_(std::make_shared()) { impl_->url = std::move(url); impl_->headers = std::move(headers); impl_->handlers = std::move(handlers); } SocketSession::~SocketSession() { close(); } void SocketSession::open() { auto& net = *NetworkService::instance().impl_; if (!net.loop) return; impl_->state = State::Connecting; auto self = impl_; net.loop->call_soon_threadsafe([self, &net] { auto handle = aiohttp::create_task(*net.loop, self->run(self)); handle.detach(); }); } void SocketSession::sendText(std::string text) { auto& net = *NetworkService::instance().impl_; if (!net.loop) return; auto self = impl_; net.loop->call_soon_threadsafe([self, text = std::move(text), &net]() mutable { if (!self->alive || !self->ws) return; auto task = [](std::shared_ptr s, std::string t) -> aiohttp::Task { try { if (s->alive && s->ws) co_await s->ws->send_str(t); } catch (const std::exception& e) { Log::warn(std::string("socket send failed: ") + e.what()); } }; auto handle = aiohttp::create_task(*net.loop, task(self, std::move(text))); handle.detach(); }); } void SocketSession::close() { if (impl_->stop.exchange(true)) return; auto& net = *NetworkService::instance().impl_; if (!net.loop) return; auto self = impl_; net.loop->call_soon_threadsafe([self, &net] { if (!self->alive || !self->ws) return; auto task = [](std::shared_ptr s) -> aiohttp::Task { try { if (s->alive && s->ws) co_await s->ws->close(); } catch (...) { } }; auto handle = aiohttp::create_task(*net.loop, task(self)); handle.detach(); }); } SocketSession::State SocketSession::state() const { return impl_->state.load(); } } // namespace dp