| 328 | } |
| 329 | |
| 330 | void networkLoop(void*) |
| 331 | { |
| 332 | fcntl(serverSocket, F_SETFL, fcntl(serverSocket, F_GETFL, 0) | O_NONBLOCK); |
| 333 | |
| 334 | int lastErrno = 0; |
| 335 | u32 suppressed = 0; |
| 336 | auto flushSuppressed = [&]() { |
| 337 | if (suppressed > 0) { |
| 338 | Logging::error("The previous accept() error repeated {} more times.", suppressed); |
| 339 | suppressed = 0; |
| 340 | } |
| 341 | lastErrno = 0; |
| 342 | }; |
| 343 | |
| 344 | serverIsRunning.store(true); |
| 345 | while (serverRunning.test_and_set()) { |
| 346 | struct sockaddr_in clientAddr; |
| 347 | u32 clientLen = sizeof(clientAddr); |
| 348 | s32 clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientLen); |
| 349 | if (clientSocket >= 0) { |
| 350 | flushSuppressed(); |
| 351 | fcntl(clientSocket, F_SETFL, fcntl(clientSocket, F_GETFL, 0) & ~O_NONBLOCK); |
| 352 | handleHttpRequest(clientSocket); |
| 353 | close(clientSocket); |
| 354 | } |
| 355 | else if (!acceptWouldBlock(errno)) { |
| 356 | if (errno == lastErrno) { |
| 357 | suppressed++; |
| 358 | } |
| 359 | else { |
| 360 | flushSuppressed(); |
| 361 | Logging::error("accept() failed on the log server socket with errno {}.", errno); |
| 362 | lastErrno = errno; |
| 363 | } |
| 364 | } |
| 365 | svcSleepThread(100'000'000ULL); // 100ms; don't peg a core |
| 366 | } |
| 367 | |
| 368 | flushSuppressed(); |
| 369 | serverIsRunning.store(false); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void Server::registerHandler(const std::string& path, Server::HttpHandler handler) |
nothing calls this directly
no test coverage detected