Skip to content

Instantly share code, notes, and snippets.

@khuttun
Created February 3, 2017 19:41
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save khuttun/ec546b2b23cfc37124f2395c9a2a8014 to your computer and use it in GitHub Desktop.
Save khuttun/ec546b2b23cfc37124f2395c9a2a8014 to your computer and use it in GitHub Desktop.
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