Created
February 3, 2017 19:41
-
-
Save khuttun/ec546b2b23cfc37124f2395c9a2a8014 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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