FastLED 3.9.15
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_NOEXCEPT
26 : connection(ConnectionConfig())
27 , clientId(0)
28 {
29 }
30
31 ServerClientConnection(ServerClientConnection&& other) FL_NOEXCEPT
32 : socket(fl::move(other.socket))
33 , connection(other.connection)
34 , clientId(other.clientId)
35 {
36 }
37
38 ServerClientConnection& operator=(ServerClientConnection&& other) FL_NOEXCEPT {
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_NOEXCEPT = delete;
49 ServerClientConnection& operator=(const ServerClientConnection&) FL_NOEXCEPT = 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());
58 ~NativeHttpServer() FL_NOEXCEPT;
59
60 // Disable copy (socket ownership)
61 NativeHttpServer(const NativeHttpServer&) FL_NOEXCEPT = delete;
62 NativeHttpServer& operator=(const NativeHttpServer&) FL_NOEXCEPT = delete;
63
64 // Server lifecycle
65 bool start(); // Start listening for connections
66 void stop(); // Stop server and disconnect all clients
67 bool isListening() const;
68 u16 port() const { return mPort; } // Actual port (useful when constructed with port 0)
69
70 // Client management
71 void acceptClients(); // Accept new client connections (non-blocking)
72 size_t getClientCount() const;
73 bool hasClient(u32 clientId) const;
74 void disconnectClient(u32 clientId);
75 void disconnectAllClients();
76
77 // Socket I/O (per-client)
78 int send(u32 clientId, fl::span<const u8> data);
79 int recv(u32 clientId, fl::span<u8> buffer);
80
81 // Broadcast to all clients
82 void broadcast(fl::span<const u8> data);
83
84 // Update loop (handles disconnections, heartbeat)
85 void update(u32 currentTimeMs);
86
87 // Get list of active client IDs
88 fl::vector<u32> getClientIds() const;
89
90 // Access the underlying acceptor
91 asio::ip::tcp::acceptor& acceptorRef() { return mAcceptor; }
92 const asio::ip::tcp::acceptor& acceptorRef() const { 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();
104 void platformStopListening();
105
106 // Client helpers
107 ServerClientConnection* findClient(u32 clientId);
108 const ServerClientConnection* findClient(u32 clientId) const;
109 void removeClient(u32 clientId);
110 bool isSocketConnected(const asio::ip::tcp::socket& sock) const;
111};
112
113} // namespace fl
114
115#endif // FASTLED_HAS_NETWORKING
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition move.h:28
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT