FastLED 3.9.15
Loading...
Searching...
No Matches
connection.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/stdint.h"
5
6namespace fl {
7
8// Connection states for HTTP streaming
9enum class ConnectionState {
10 DISCONNECTED, // Not connected, idle
11 CONNECTING, // Connection attempt in progress
12 CONNECTED, // Connected and active
13 RECONNECTING, // Reconnection attempt after failure
14 CLOSED // Connection permanently closed (user requested)
15};
16
17// Connection configuration
19 // Reconnection settings
20 u32 reconnectInitialDelayMs = 1000; // Initial delay: 1s
21 u32 reconnectMaxDelayMs = 30000; // Max delay: 30s
22 u32 reconnectBackoffMultiplier = 2; // Exponential backoff multiplier
23
24 // Heartbeat settings
25 u32 heartbeatIntervalMs = 30000; // Send heartbeat every 30s
26
27 // Timeout settings
28 u32 connectionTimeoutMs = 60000; // Detect dead connection after 60s
29
30 // Max reconnection attempts (0 = infinite)
32};
33
34// HttpConnection: Manages connection lifecycle and state transitions
36public:
37 explicit HttpConnection(const ConnectionConfig& config = ConnectionConfig());
38
39 // State management
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
73private:
76
77 // Reconnection state
81 bool mWasReconnecting; // Track if we were in RECONNECTING before CONNECTING
82
83 // Heartbeat state
86
87 // State transition helpers
88 void transitionTo(ConnectionState newState, u32 currentTimeMs);
91 u32 calculateBackoffDelay() const;
92};
93
94} // namespace fl
bool isConnected() const
void update(u32 currentTimeMs)
ConnectionState getState() const
u32 calculateBackoffDelay() const
bool shouldReconnect() const
bool isTimedOut(u32 currentTimeMs) const
HttpConnection(const ConnectionConfig &config=ConnectionConfig())
u32 getReconnectAttempts() const
ConnectionConfig mConfig
Definition connection.h:74
void onEvent(const asio::error_code &ec, u32 currentTimeMs=0)
void onConnected(u32 currentTimeMs=0)
bool shouldSendHeartbeat(u32 currentTimeMs) const
u32 getReconnectDelayMs() const
void transitionTo(ConnectionState newState, u32 currentTimeMs)
bool isDisconnected() const
ConnectionState mState
Definition connection.h:75
ConnectionState
Definition connection.h:9
Base definition for an LED controller.
Definition crgb.hpp:179
Asio-compatible error code: numeric code + optional human-readable message.
Definition error_code.h:31