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

Method init

switch/source/server.cpp:396–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

394}
395
396void Server::init()
397{
398 serverShutdown.store(false);
399 serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
400 if (serverSocket < 0) {
401 Logging::error("Failed to create log server socket with error {}: {}", errno, strerror(errno));
402 return;
403 }
404
405 // Without SO_REUSEADDR a quick relaunch while port 8000 is still in TIME_WAIT
406 // makes bind() fail, silently killing both the /logs endpoints and the
407 // wireless receiver with no user-visible reason.
408 int reuse = 1;
409 setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
410
411 struct sockaddr_in serverAddr;
412 memset(&serverAddr, 0, sizeof(serverAddr));
413 serverAddr.sin_family = AF_INET;
414 serverAddr.sin_port = htons(SERVER_PORT);
415 serverAddr.sin_addr.s_addr = gethostid();
416
417 if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
418 Logging::error("Failed to bind log server to port {} with error {}", SERVER_PORT, errno);
419 close(serverSocket);
420 serverSocket = -1;
421 return;
422 }
423
424 if (listen(serverSocket, 5) != 0) {
425 Logging::error("Failed to listen on log server socket with error {}: {}", errno, strerror(errno));
426 close(serverSocket);
427 serverSocket = -1;
428 return;
429 }
430
431 char ipStr[INET_ADDRSTRLEN];
432 inet_ntop(AF_INET, &(serverAddr.sin_addr), ipStr, INET_ADDRSTRLEN);
433
434 serverRunning.test_and_set();
435 // Same priority/core as the FTP + copy workers; the accept loop is IO-bound.
436 if (R_SUCCEEDED(threadCreate(&serverThread, networkLoop, nullptr, nullptr, 0x8000, 0x2C, -2)) && R_SUCCEEDED(threadStart(&serverThread))) {
437 threadValid = true;
438 serverAddress = "http://" + std::string(ipStr) + ":" + std::to_string(SERVER_PORT);
439 Logging::info("Log server listening on {}", serverAddress);
440 }
441 else {
442 // Could not spawn the worker: nothing will accept connections, so leave
443 // the address empty and free the socket.
444 serverRunning.clear();
445 close(serverSocket);
446 serverSocket = -1;
447 }
448}
449
450void Server::exit()
451{

Callers

nothing calls this directly

Calls 7

errorFunction · 0.85
strerrorFunction · 0.85
stringFunction · 0.85
to_stringFunction · 0.85
infoFunction · 0.85
storeMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected