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

Function networkLoop

3ds/source/server.cpp:301–346  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

299 }
300
301 static void networkLoop()
302 {
303 // Set server socket to non-blocking
304 fcntl(serverSocket, F_SETFL, fcntl(serverSocket, F_GETFL, 0) | O_NONBLOCK);
305
306 int lastErrno = 0;
307 u32 suppressed = 0;
308 auto flushSuppressed = [&]() {
309 if (suppressed > 0) {
310 Logging::error("The previous accept() error repeated {} more times.", suppressed);
311 suppressed = 0;
312 }
313 lastErrno = 0;
314 };
315
316 serverIsRunning.store(true);
317 while (serverRunning.test_and_set()) {
318 struct sockaddr_in clientAddr;
319 u32 clientLen = sizeof(clientAddr);
320 s32 clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientLen);
321
322 if (clientSocket >= 0) {
323 flushSuppressed();
324 // Set client socket to blocking for simpler sending
325 fcntl(clientSocket, F_SETFL, fcntl(clientSocket, F_GETFL, 0) & ~O_NONBLOCK);
326 handleHttpRequest(clientSocket);
327 close(clientSocket);
328 }
329 else if (!acceptWouldBlock(errno)) {
330 if (errno == lastErrno) {
331 suppressed++;
332 }
333 else {
334 flushSuppressed();
335 Logging::error("accept() failed on the HTTP server socket with errno {}.", errno);
336 lastErrno = errno;
337 }
338 }
339
340 // Prevent 100% CPU usage
341 svcSleepThread(100000000); // 100ms
342 }
343
344 flushSuppressed();
345 serverIsRunning.store(false);
346 }
347}
348
349void 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