#include "api/LiveMessagesSocket.hpp" #include #include "core/Log.hpp" #include "core/Timer.hpp" #include "model/ChatSocketCodec.hpp" #include "net/SocketSession.hpp" #include "spec/SocketSpec.hpp" namespace dp { using spec::ChatSocketTiming; struct LiveMessagesSocket::Core : std::enable_shared_from_this { std::string receiverUid; UrlProvider provider; EventHandler handler; bool active = false; int backoffMs = ChatSocketTiming::kBackoffInitialMs; unsigned generation = 0; std::unique_ptr session; CancelPtr pendingUrl; CancelPtr pendingTimer; void emit(const ChatSocketEvent& event) { if (handler) handler(event); } void attempt() { if (!active) return; session.reset(); const unsigned gen = ++generation; auto self = shared_from_this(); pendingUrl = provider([self, gen](Result r) { if (!self->active || gen != self->generation) return; if (!r.ok()) { Log::debug("Chat socket connection error: " + r.error().message); self->ended(false); return; } self->open(r.value(), gen); }); } void open(const std::string& url, unsigned gen) { auto self = shared_from_this(); SocketSession::Handlers handlers; handlers.onOpen = [self, gen] { if (self->active && gen == self->generation) self->backoffMs = ChatSocketTiming::kBackoffInitialMs; }; handlers.onText = [self, gen](const std::string& text) { if (!self->active || gen != self->generation) return; if (auto event = ChatSocketCodec::decode(text)) self->emit(*event); else Log::debug("Chat socket received unrecognized frame"); }; handlers.onClose = [self, gen](const std::string& reason) { if (!self->active || gen != self->generation) return; Log::debug("Chat socket connection error: " + reason); self->ended(reason.find(std::to_string(ChatSocketTiming::kCloseCodeReconnectFast)) != std::string::npos); }; session = std::make_unique(url, std::vector>{}, std::move(handlers)); session->open(); } void ended(bool fastCloseCode) { emit(ChatSocketEvent::disconnected()); if (!active) return; // the handler may have disconnected us int delay; if (fastCloseCode) { delay = ChatSocketTiming::kFastReconnectMs; } else { delay = backoffMs; backoffMs = std::min(backoffMs * 2, ChatSocketTiming::kBackoffMaxMs); } Log::debug("Chat socket reconnecting in " + std::to_string(delay) + "ms"); auto self = shared_from_this(); pendingTimer = Timer::after(static_cast(delay), [self] { self->attempt(); }); } void stop() { active = false; ++generation; if (pendingUrl) pendingUrl->cancel(); if (pendingTimer) pendingTimer->cancel(); session.reset(); } void sendText(std::string text) { if (!session || session->state() != SocketSession::State::Open) return; // silently dropped like Swift session->sendText(std::move(text)); } }; LiveMessagesSocket::LiveMessagesSocket(std::string receiverUid, UrlProvider provider) : core_(std::make_shared()) { core_->receiverUid = std::move(receiverUid); core_->provider = std::move(provider); } LiveMessagesSocket::~LiveMessagesSocket() { core_->stop(); } void LiveMessagesSocket::setEventHandler(EventHandler handler) { core_->handler = std::move(handler); } void LiveMessagesSocket::connect() { if (core_->active) return; core_->active = true; core_->backoffMs = ChatSocketTiming::kBackoffInitialMs; core_->attempt(); } void LiveMessagesSocket::disconnect() { core_->stop(); } void LiveMessagesSocket::send(const std::string& content, const std::vector& attachmentUids, const std::string& clientId) { core_->sendText(ChatSocketCodec::encodeSend(core_->receiverUid, content, attachmentUids, clientId)); } void LiveMessagesSocket::sendTyping() { core_->sendText(ChatSocketCodec::encodeTyping(core_->receiverUid)); } void LiveMessagesSocket::sendRead(const std::string& withUid) { core_->sendText(ChatSocketCodec::encodeRead(withUid)); } int LiveMessagesSocket::nextBackoffMs() const { return core_->backoffMs; } } // namespace dp