FastLED 3.9.15
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());
23 // Legacy constructor (backward compatible)
24 NativeHttpClient(const string& host, u16 port, const ConnectionConfig& config = ConnectionConfig());
25 ~NativeHttpClient() FL_NOEXCEPT;
26
27 // Disable copy (socket ownership)
28 NativeHttpClient(const NativeHttpClient&) FL_NOEXCEPT = delete;
29 NativeHttpClient& operator=(const NativeHttpClient&) FL_NOEXCEPT = delete;
30
31 // Connection management
32 bool connect(); // Initiate connection
33 void disconnect(); // Close connection
34 void close(); // Permanent close (no reconnect)
35 bool isConnected() const;
36 ConnectionState getState() const;
37
38 // Socket I/O
39 int send(fl::span<const u8> data);
40 int recv(fl::span<u8> buffer);
41
42 // Update loop (handles reconnection, heartbeat)
43 void update(u32 currentTimeMs);
44
45 // Heartbeat
46 bool shouldSendHeartbeat(u32 currentTimeMs) const;
47 void onHeartbeatSent();
48 void onHeartbeatReceived();
49
50 // Reconnection state
51 u32 getReconnectDelayMs() const;
52 u32 getReconnectAttempts() const;
53
54 // Access the endpoint
55 const asio::ip::tcp::endpoint& endpoint() const { return mEndpoint; }
56
57 // Access the underlying tcp::socket
58 asio::ip::tcp::socket& socket() { return mSocket; }
59 const asio::ip::tcp::socket& socket() const { 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();
68 void platformDisconnect();
69 bool isSocketConnected() const;
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_NOEXCEPT