server/logger: Support trying additional ports

This commit is contained in:
Nick Renieris 2022-06-26 06:30:38 +03:00
parent 62cc2a5102
commit 740f562906

View file

@ -2,6 +2,10 @@
#include "helpers.hpp" #include "helpers.hpp"
#include "nn/result.h" #include "nn/result.h"
// If connection fails, try X ports above the specified one
// Useful for debugging multple clients on the same machine
constexpr u32 ADDITIONAL_LOG_PORT_COUNT = 2;
Logger* Logger::sInstance = nullptr; Logger* Logger::sInstance = nullptr;
void Logger::createInstance() { void Logger::createInstance() {
@ -51,17 +55,25 @@ nn::Result Logger::init(const char* ip, u16 port) {
serverAddress.family = 2; serverAddress.family = 2;
nn::Result result; nn::Result result;
bool connected = false;
for (u32 i = 0; i < ADDITIONAL_LOG_PORT_COUNT + 1; ++i) {
result = nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress));
if (result.isSuccess()) {
connected = true;
break;
}
this->port++;
serverAddress.port = nn::socket::InetHtons(this->port);
}
if ((result = nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress))).isFailure()) { if (connected) {
this->socket_log_state = SOCKET_LOG_CONNECTED;
this->isDisableName = false;
return 0;
} else {
this->socket_log_state = SOCKET_LOG_UNAVAILABLE; this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
return result; return result;
} }
this->socket_log_state = SOCKET_LOG_CONNECTED;
this->isDisableName = false;
return 0;
} }
void Logger::log(const char *fmt, va_list args) { // impl for replacing seads system::print void Logger::log(const char *fmt, va_list args) { // impl for replacing seads system::print