FastLED 3.10.8
Loading...
Searching...
No Matches
native_server.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/vector.h"
14#include "fl/stl/noexcept.h"
15
16namespace fl {
17
18// Client connection managed by server
19struct ServerClientConnection {
20 asio::ip::tcp::socket socket;
21 HttpConnection connection;
22 u32 clientId; // Unique identifier for this client
23
24 // Default constructor (required for fl::vector operations)
25 ServerClientConnection() FL_NO_EXCEPT
26 : connection(ConnectionConfig())
27 , clientId(0)
28 {
29 }
30
31 ServerClientConnection(ServerClientConnection&& other) FL_NO_EXCEPT
32 : socket(fl::move(other.socket))
33 , connection(other.connection)
34 , clientId(other.clientId)
35 {
36 }
37
38 ServerClientConnection& operator=(ServerClientConnection&& other) FL_NO_EXCEPT {
39 if (this != &other) {
40 socket = fl::move(other.socket);
41 connection = other.connection;
42 clientId = other.clientId;
43 }
44 return *this;
45 }
46
47 // Not copyable (socket is not copyable)
48 ServerClientConnection(const ServerClientConnection&) FL_NO_EXCEPT = delete;
49 ServerClientConnection& operator=(const ServerClientConnection&) FL_NO_EXCEPT = delete;
50};
51
52// Native HTTP server using POSIX sockets
53// Always non-blocking — blocking I/O is never appropriate on embedded
54class NativeHttpServer {
55public:
56 // Constructor
57 NativeHttpServer(u16 port, const ConnectionConfig& config = ConnectionConfig()) FL_NO_EXCEPT;
58 ~NativeHttpServer() FL_NO_EXCEPT;
59
60 // Disable copy (socket ownership)
61 NativeHttpServer(const NativeHttpServer&) FL_NO_EXCEPT = delete;
62 NativeHttpServer& operator=(const NativeHttpServer&) FL_NO_EXCEPT = delete;
63
64 // Server lifecycle
65 bool start() FL_NO_EXCEPT; // Start listening for connections
66 void stop() FL_NO_EXCEPT; // Stop server and disconnect all clients
67 bool isListening() const FL_NO_EXCEPT;
68 u16 port() const FL_NO_EXCEPT { return mPort; } // Actual port (useful when constructed with port 0)
69
70 // Client management
71 void acceptClients() FL_NO_EXCEPT; // Accept new client connections (non-blocking)
72 size_t getClientCount() const FL_NO_EXCEPT;
73 bool hasClient(u32 clientId) const FL_NO_EXCEPT;
74 void disconnectClient(u32 clientId) FL_NO_EXCEPT;
75 void disconnectAllClients() FL_NO_EXCEPT;
76
77 // Socket I/O (per-client)
78 int send(u32 clientId, fl::span<const u8> data) FL_NO_EXCEPT;
79 int recv(u32 clientId, fl::span<u8> buffer) FL_NO_EXCEPT;
80
81 // Broadcast to all clients
82 void broadcast(fl::span<const u8> data) FL_NO_EXCEPT;
83
84 // Update loop (handles disconnections, heartbeat)
85 void update(u32 currentTimeMs) FL_NO_EXCEPT;
86
87 // Get list of active client IDs
88 fl::vector<u32> getClientIds() const FL_NO_EXCEPT;
89
90 // Access the underlying acceptor
91 asio::ip::tcp::acceptor& acceptorRef() FL_NO_EXCEPT { return mAcceptor; }
92 const asio::ip::tcp::acceptor& acceptorRef() const FL_NO_EXCEPT { return mAcceptor; }
93
94private:
95 u16 mPort;
96 asio::ip::tcp::acceptor mAcceptor;
97 bool mIsListening;
98 u32 mNextClientId;
99 ConnectionConfig mConfig;
100 fl::vector<ServerClientConnection> mClients;
101
102 // Platform-specific server operations
103 bool platformStartListening() FL_NO_EXCEPT;
104 void platformStopListening() FL_NO_EXCEPT;
105
106 // Client helpers
107 ServerClientConnection* findClient(u32 clientId) FL_NO_EXCEPT;
108 const ServerClientConnection* findClient(u32 clientId) const FL_NO_EXCEPT;
109 void removeClient(u32 clientId) FL_NO_EXCEPT;
110 bool isSocketConnected(const asio::ip::tcp::socket& sock) const FL_NO_EXCEPT;
111};
112
113} // namespace fl
114
115#endif // FASTLED_HAS_NETWORKING
unsigned char u8
Definition stdint.h:131
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT