Fix connection notification races. (#130)

Use a mutex on the connection state and one-shot all change notifications.

Fixes #127.
This commit is contained in:
Peter Johnson
2016-10-21 19:40:56 -07:00
committed by GitHub
parent 5c1b7ecd17
commit 86c43df8d1
3 changed files with 38 additions and 19 deletions

View File

@@ -60,14 +60,15 @@ class NetworkConnection {
void QueueOutgoing(std::shared_ptr<Message> msg);
void PostOutgoing(bool keep_alive);
void NotifyIfActive(ConnectionListenerCallback callback) const;
unsigned int uid() const { return m_uid; }
unsigned int proto_rev() const { return m_proto_rev; }
void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
State state() const { return static_cast<State>(m_state.load()); }
void set_state(State state) { m_state = static_cast<int>(state); }
State state() const;
void set_state(State state);
std::string remote_id() const;
void set_remote_id(StringRef remote_id);
@@ -94,7 +95,8 @@ class NetworkConnection {
std::thread m_write_thread;
std::atomic_bool m_active;
std::atomic_uint m_proto_rev;
std::atomic_int m_state;
mutable std::mutex m_state_mutex;
State m_state;
mutable std::mutex m_remote_id_mutex;
std::string m_remote_id;
std::atomic_ullong m_last_update;