HttpConnection: Manages connection lifecycle and state transitions
| 33 | |
| 34 | // HttpConnection: Manages connection lifecycle and state transitions |
| 35 | class HttpConnection { |
| 36 | public: |
| 37 | explicit HttpConnection(const ConnectionConfig& config = ConnectionConfig()); |
| 38 | |
| 39 | // State management |
| 40 | ConnectionState getState() const; |
| 41 | bool isConnected() const; |
| 42 | bool isDisconnected() const; |
| 43 | bool shouldReconnect() const; |
| 44 | |
| 45 | // Connection control |
| 46 | void connect(); // Initiate connection |
| 47 | void disconnect(); // Graceful disconnect |
| 48 | void close(); // Permanent close (no reconnect) |
| 49 | |
| 50 | // Connection events (call these from transport layer) |
| 51 | void onConnected(u32 currentTimeMs = 0); // Connection established |
| 52 | void onDisconnected(); // Connection lost |
| 53 | void onError(); // Connection error |
| 54 | |
| 55 | // Asio-compatible: handle connection event from error_code |
| 56 | void onEvent(const asio::error_code& ec, u32 currentTimeMs = 0); |
| 57 | |
| 58 | // Heartbeat management |
| 59 | void onHeartbeatSent(); // Called when heartbeat sent |
| 60 | void onHeartbeatReceived(); // Called when heartbeat/data received |
| 61 | bool shouldSendHeartbeat(u32 currentTimeMs) const; |
| 62 | |
| 63 | // Update loop (call regularly) |
| 64 | void update(u32 currentTimeMs); |
| 65 | |
| 66 | // Reconnection state |
| 67 | u32 getReconnectDelayMs() const; |
| 68 | u32 getReconnectAttempts() const; |
| 69 | |
| 70 | // Timeout detection |
| 71 | bool isTimedOut(u32 currentTimeMs) const; |
| 72 | |
| 73 | private: |
| 74 | ConnectionConfig mConfig; |
| 75 | ConnectionState mState; |
| 76 | |
| 77 | // Reconnection state |
| 78 | u32 mReconnectAttempts; |
| 79 | u32 mReconnectDelayMs; |
| 80 | u32 mNextReconnectTimeMs; |
| 81 | bool mWasReconnecting; // Track if we were in RECONNECTING before CONNECTING |
| 82 | |
| 83 | // Heartbeat state |
| 84 | u32 mLastHeartbeatSentMs; |
| 85 | u32 mLastDataReceivedMs; |
| 86 | |
| 87 | // State transition helpers |
| 88 | void transitionTo(ConnectionState newState, u32 currentTimeMs); |
| 89 | void resetReconnectState(); |
| 90 | void resetReconnectAttempts(); |
| 91 | u32 calculateBackoffDelay() const; |
| 92 | }; |
no test coverage detected