Various NetworkConnection improvements.

- Keep connection state.
- Store remote id
- Getter for proto_rev
- Cleanup on Stop()
- Cleaner termination of write thread.
This commit is contained in:
Peter Johnson
2015-07-29 20:31:59 -07:00
parent c01f2977ac
commit 9a621e9272
2 changed files with 39 additions and 3 deletions

View File

@@ -21,6 +21,8 @@ namespace nt {
class NetworkConnection {
public:
enum State { kCreated, kInit, kHandshake, kActive, kDead };
typedef std::shared_ptr<Message> Incoming;
typedef ConcurrentQueue<Incoming> IncomingQueue;
typedef std::vector<std::shared_ptr<Message>> Outgoing;
@@ -37,8 +39,16 @@ class NetworkConnection {
TCPStream& stream() { return *m_stream; }
OutgoingQueue& outgoing() { return m_outgoing; }
IncomingQueue& incoming() { return m_incoming; }
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); }
std::string remote_id() const;
void set_remote_id(StringRef remote_id);
NetworkConnection(const NetworkConnection&) = delete;
NetworkConnection& operator=(const NetworkConnection&) = delete;
@@ -54,6 +64,9 @@ 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_remote_id_mutex;
std::string m_remote_id;
};
} // namespace nt