MCPcopy Create free account
hub / github.com/BernardoGiordano/Checkpoint / networkLoop

Function networkLoop

switch/source/server.cpp:330–370  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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
373void Server::registerHandler(const std::string& path, Server::HttpHandler handler)

Callers

nothing calls this directly

Calls 5

acceptFunction · 0.85
storeMethod · 0.80
handleHttpRequestFunction · 0.70
acceptWouldBlockFunction · 0.70
errorFunction · 0.50

Tested by

no test coverage detected