Main server loop; accepts and manages clients.
196{
197 while (!stop_token.stop_requested() &&
m_running) {
198 fd_set readfds;
199 FD_ZERO(&readfds);
201
202 timeval timeout { .tv_sec = 1, .tv_usec = 0 };
203
204#ifdef MAYAFLUX_PLATFORM_WINDOWS
205 int activity = select(0, &readfds, nullptr, nullptr, &timeout);
206#else
207 int activity = select(
m_server_fd + 1, &readfds,
nullptr,
nullptr, &timeout);
208#endif
209
210 if (activity < 0) {
212#ifndef MAYAFLUX_PLATFORM_WINDOWS
213 if (code == EINTR)
214 continue;
215#endif
218 }
219 break;
220 }
221
222 if (activity == 0)
223 continue;
224
225 sockaddr_in client_addr {};
226 socklen_t client_len = sizeof(client_addr);
227
228#ifdef MAYAFLUX_PLATFORM_WINDOWS
229 SOCKET client_socket = accept(
static_cast<SOCKET
>(
m_server_fd),
reinterpret_cast<sockaddr*
>(&client_addr), &client_len);
230 int client_fd = static_cast<int>(client_socket);
231 if (client_socket == INVALID_SOCKET) {
233 if (code == WSAEWOULDBLOCK)
234 continue;
237 }
238 continue;
239 }
240#else
241 int client_fd = accept(
m_server_fd,
reinterpret_cast<sockaddr*
>(&client_addr), &client_len);
242 if (client_fd < 0) {
243 if (errno == EAGAIN || errno == EWOULDBLOCK)
244 continue;
246 LILA_ERROR(Emitter::SERVER,
"Accept failed: " + std::string(strerror(errno)));
247 }
248 continue;
249 }
250#endif
251
253 LILA_WARN(Emitter::SERVER,
"Failed to set non-blocking on client fd " + std::to_string(client_fd));
254 }
255
257 }
258}
std::atomic< bool > m_running
Server running state.
void handle_client(int client_fd)
Handles communication with a single client.
int m_server_fd
Server socket file descriptor.
static int set_nonblocking(int fd)
static std::string socket_error_string(int code)
static int socket_errno()