FastLED 3.10.8
Loading...
Searching...
No Matches
native_client.h
Go to the documentation of this file.
1#pragma once
2
3// This file requires native socket APIs (Windows or POSIX).
4// On embedded platforms (STM32, AVR, etc.) this file compiles to nothing.
5#ifdef FASTLED_HAS_NETWORKING
6
10#include "fl/stl/string.h"
11#include "fl/stl/span.h"
12#include "fl/stl/stdint.h"
13#include "fl/stl/noexcept.h"
14
15namespace fl {
16
17// Native HTTP client using POSIX sockets
18// Always non-blocking — blocking I/O is never appropriate on embedded
19class NativeHttpClient {
20public:
21 // Constructor (Asio-compatible: accepts endpoint)
22 NativeHttpClient(const asio::ip::tcp::endpoint& ep, const ConnectionConfig& config = ConnectionConfig()) FL_NO_EXCEPT;
23 // Legacy constructor (backward compatible)
24 NativeHttpClient(const string& host, u16 port, const ConnectionConfig& config = ConnectionConfig()) FL_NO_EXCEPT;
25 ~NativeHttpClient() FL_NO_EXCEPT;
26
27 // Disable copy (socket ownership)
28 NativeHttpClient(const NativeHttpClient&) FL_NO_EXCEPT = delete;
29 NativeHttpClient& operator=(const NativeHttpClient&) FL_NO_EXCEPT = delete;
30
31 // Connection management
32 bool connect() FL_NO_EXCEPT; // Initiate connection
33 void disconnect() FL_NO_EXCEPT; // Close connection
34 void close() FL_NO_EXCEPT; // Permanent close (no reconnect)
35 bool isConnected() const FL_NO_EXCEPT;
36 ConnectionState getState() const FL_NO_EXCEPT;
37
38 // Socket I/O
39 int send(fl::span<const u8> data) FL_NO_EXCEPT;
40 int recv(fl::span<u8> buffer) FL_NO_EXCEPT;
41
42 // Update loop (handles reconnection, heartbeat)
43 void update(u32 currentTimeMs) FL_NO_EXCEPT;
44
45 // Heartbeat
46 bool shouldSendHeartbeat(u32 currentTimeMs) const FL_NO_EXCEPT;
47 void onHeartbeatSent() FL_NO_EXCEPT;
48 void onHeartbeatReceived() FL_NO_EXCEPT;
49
50 // Reconnection state
51 u32 getReconnectDelayMs() const FL_NO_EXCEPT;
52 u32 getReconnectAttempts() const FL_NO_EXCEPT;
53
54 // Access the endpoint
55 const asio::ip::tcp::endpoint& endpoint() const FL_NO_EXCEPT { return mEndpoint; }
56
57 // Access the underlying tcp::socket
58 asio::ip::tcp::socket& socket() FL_NO_EXCEPT { return mSocket; }
59 const asio::ip::tcp::socket& socket() const FL_NO_EXCEPT { return mSocket; }
60
61private:
62 asio::ip::tcp::endpoint mEndpoint;
63 asio::ip::tcp::socket mSocket;
64 HttpConnection mConnection;
65
66 // Platform-specific connection
67 bool platformConnect() FL_NO_EXCEPT;
68 void platformDisconnect() FL_NO_EXCEPT;
69 bool isSocketConnected() const FL_NO_EXCEPT;
70};
71
72} // namespace fl
73
74#endif // FASTLED_HAS_NETWORKING
unsigned char u8
Definition stdint.h:131
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT