5#ifdef FASTLED_HAS_NETWORKING
9 #include "platforms/win/socket_win.h"
11 #include "platforms/posix/socket_posix.h"
37 if (platformStartListening()) {
47 disconnectAllClients();
50 platformStopListening();
65 ServerClientConnection conn;
66 conn.clientId = mNextClientId;
74 conn.connection.onConnected(0);
80size_t NativeHttpServer::getClientCount() const
FL_NO_EXCEPT {
81 return mClients.size();
84bool NativeHttpServer::hasClient(u32 clientId)
const FL_NO_EXCEPT {
85 return findClient(clientId) !=
nullptr;
88void NativeHttpServer::disconnectClient(u32 clientId)
FL_NO_EXCEPT {
89 removeClient(clientId);
92void NativeHttpServer::disconnectAllClients()
FL_NO_EXCEPT {
97int NativeHttpServer::send(u32 clientId, fl::span<const u8> data)
FL_NO_EXCEPT {
98 ServerClientConnection* client = findClient(clientId);
99 if (!client || !client->socket.is_open()) {
104 size_t n = client->socket.write_some(data, ec);
111 client->connection.onDisconnected();
115 return static_cast<int>(n);
118int NativeHttpServer::recv(u32 clientId, fl::span<u8> buffer)
FL_NO_EXCEPT {
119 ServerClientConnection* client = findClient(clientId);
120 if (!client || !client->socket.is_open()) {
125 size_t n = client->socket.read_some(buffer, ec);
132 client->connection.onDisconnected();
137 client->connection.onHeartbeatReceived();
139 return static_cast<int>(n);
142void NativeHttpServer::broadcast(fl::span<const u8> data)
FL_NO_EXCEPT {
144 for (
auto& client : mClients) {
145 send(client.clientId, data);
149void NativeHttpServer::update(u32 currentTimeMs)
FL_NO_EXCEPT {
151 for (
size_t i = 0; i < mClients.size(); ) {
152 auto& client = mClients[i];
155 client.connection.update(currentTimeMs);
158 if (!client.connection.isConnected() || !isSocketConnected(client.socket)) {
160 mClients.erase(mClients.begin() + i);
168fl::vector<u32> NativeHttpServer::getClientIds() const
FL_NO_EXCEPT {
171 for (
const auto& client : mClients) {
177bool NativeHttpServer::platformStartListening()
FL_NO_EXCEPT {
184 mPort = mAcceptor.port();
186 ec = mAcceptor.listen(10);
195void NativeHttpServer::platformStopListening()
FL_NO_EXCEPT {
199ServerClientConnection* NativeHttpServer::findClient(u32 clientId)
FL_NO_EXCEPT {
200 for (
auto& client : mClients) {
201 if (client.clientId == clientId) {
208const ServerClientConnection* NativeHttpServer::findClient(u32 clientId)
const FL_NO_EXCEPT {
209 for (
const auto& client : mClients) {
210 if (client.clientId == clientId) {
217void NativeHttpServer::removeClient(u32 clientId)
FL_NO_EXCEPT {
218 for (
size_t i = 0; i < mClients.size(); ++i) {
219 if (mClients[i].clientId == clientId) {
221 mClients.erase(mClients.begin() + i);
227bool NativeHttpServer::isSocketConnected(
const asio::ip::tcp::socket& sock)
const FL_NO_EXCEPT {
228 if (!sock.is_open()) {
234 socklen_t len =
sizeof(error);
235 int ret = getsockopt(sock.native_handle(), SOL_SOCKET, SO_ERROR, (
char*)&error, &len);
237 if (ret != 0 || error != 0) {
void reserve(fl::size n) FL_NO_EXCEPT
void push_back(const T &value) FL_NO_EXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
void stop() FL_NO_EXCEPT
Stop WiFi entirely (STA and AP).
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Base definition for an LED controller.
Asio-compatible error code: numeric code + optional human-readable message.