| 1 | /* |
| 2 | * IXConnectionState.h |
| 3 | * Author: Benjamin Sergeant |
| 4 | * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <atomic> |
| 10 | #include <functional> |
| 11 | #include <memory> |
| 12 | #include <stdint.h> |
| 13 | #include <string> |
| 14 | |
| 15 | namespace ix |
| 16 | { |
| 17 | using OnSetTerminatedCallback = std::function<void()>; |
| 18 | |
| 19 | class ConnectionState |
| 20 | { |
| 21 | public: |
| 22 | ConnectionState(); |
| 23 | virtual ~ConnectionState() = default; |
| 24 | |
| 25 | virtual void computeId(); |
| 26 | virtual const std::string& getId() const; |
| 27 | |
| 28 | void setTerminated(); |
| 29 | bool isTerminated() const; |
| 30 | |
| 31 | const std::string& getRemoteIp(); |
| 32 | int getRemotePort(); |
| 33 | |
| 34 | static std::shared_ptr<ConnectionState> createConnectionState(); |
| 35 | |
| 36 | private: |
| 37 | void setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback); |
| 38 | |
| 39 | void setRemoteIp(const std::string& remoteIp); |
| 40 | void setRemotePort(int remotePort); |
| 41 | |
| 42 | protected: |
| 43 | std::atomic<bool> _terminated; |
| 44 | std::string _id; |
| 45 | OnSetTerminatedCallback _onSetTerminatedCallback; |
| 46 | |
| 47 | static std::atomic<uint64_t> _globalId; |
| 48 | |
| 49 | std::string _remoteIp; |
| 50 | int _remotePort; |
| 51 | |
| 52 | friend class SocketServer; |
| 53 | }; |
| 54 | } // namespace ix |
| 55 | |