#pragma once
// Spec: shell.md "Sockets" (LiveMessagesSocket). Ticket flow: every connect asks the UrlProvider for a fresh
// websocket URL (ticket), opens a dp::SocketSession, decodes frames with ChatSocketCodec. When the socket ends
// while active: emit Disconnected, wait 200 ms (close code 4013) or the backoff (200 ms doubling to 5000 ms,
// reset to 200 ms when a connection opened), then reconnect. Timers run on the UI thread (dp::Timer).
// DIFF: Swift resets the backoff when the task is resumed, this port resets it when the socket is open.
// SocketSession reports no numeric close code: 4013 is detected as the substring "4013" in the close reason.
#include <functional>
#include <memory>
#include <string>
#include "api/ApiTypes.hpp"
#include "api/MessagesSocket.hpp"
namespace dp {
class LiveMessagesSocket : public MessagesSocket {
public:
using UrlProvider = std::function<CancelPtr(Done<std::string>)>;
LiveMessagesSocket(std::string receiverUid, UrlProvider provider);
~LiveMessagesSocket() override;
void setEventHandler(EventHandler handler) override;
void connect() override;
void disconnect() override;
void send(const std::string& content, const std::vector<std::string>& attachmentUids,
const std::string& clientId) override;
void sendTyping() override;
void sendRead(const std::string& withUid) override;
// Next reconnect delay in ms that a failure would use (test hook).
int nextBackoffMs() const;
struct Core;
private:
std::shared_ptr<Core> core_;
};
} // namespace dp