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

Method init

switch/source/server.cpp:416–469  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected