Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
struct Connection {
void disconnected()
{
m_connection = Disconnected();
}
void interrupted()
{
m_connection = std::visit(InterruptedEvent(*this), m_connection);
}
void notifyInterrupted(std::chrono::system_clock::time_point)
{
}
std::string m_serverAddress;
struct Disconnected {};
struct Connecting {};
struct Connected {
ConnectionId m_id;
std::chrono::system_clock::time_point m_connectedTime;
std::optional<std::chrono::milliseconds> m_lastPingTime;
};
struct ConnectionInterrupted {
std::chrono::system_clock::time_point m_disconnectedTime;
Timer m_reconnectTimer;
};
using State = std::variant<
Disconnected, Connecting, Connected, ConnectionInterrupted>;
struct InterruptedEvent {
InterruptedEvent(Connection& c) : m_c(c) {}
State operator()(const Disconnected&){ return Disconnected(); }
State operator()(const Connecting&){ return Disconnected(); }
State operator()(const Connected&)
{
const auto now = std::chrono::system_clock::now();
m_c.notifyInterrupted(now);
return ConnectionInterrupted{now, 5000};
}
State operator()(const ConnectionInterrupted& s){ return s; }
private:
Connection& m_c;
};
State m_connection;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment