fix available port detection
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit
This commit is contained in:
parent
e53acf799a
commit
0e8e56ad90
@ -143,9 +143,15 @@ void server::start() {
|
|||||||
|
|
||||||
initialize(*server_);
|
initialize(*server_);
|
||||||
|
|
||||||
fmt::println("port|{}", config_.get_api_port());
|
|
||||||
server_thread_ = std::make_unique<std::thread>([this]() {
|
server_thread_ = std::make_unique<std::thread>([this]() {
|
||||||
server_->set_socket_options([](auto && /* sock */) {});
|
#ifdef _WIN32
|
||||||
|
server_->set_socket_options([](auto &&sock) {
|
||||||
|
int enable = 1;
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
|
||||||
|
reinterpret_cast<const char *>(&enable), sizeof(enable));
|
||||||
|
});
|
||||||
|
#endif // _WIN32
|
||||||
|
|
||||||
server_->listen("127.0.0.1", config_.get_api_port());
|
server_->listen("127.0.0.1", config_.get_api_port());
|
||||||
});
|
});
|
||||||
event_system::instance().raise<service_start_end>(function_name, "server");
|
event_system::instance().raise<service_start_end>(function_name, "server");
|
||||||
|
@ -112,7 +112,6 @@ mount(std::vector<const char *> args, std::string data_directory,
|
|||||||
std::cerr << "FATAL: Unable to get available port" << std::endl;
|
std::cerr << "FATAL: Unable to get available port" << std::endl;
|
||||||
return exit_code::startup_exception;
|
return exit_code::startup_exception;
|
||||||
}
|
}
|
||||||
fmt::println("port|{}", port);
|
|
||||||
config.set_api_port(port);
|
config.set_api_port(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,13 +146,6 @@ mount(std::vector<const char *> args, std::string data_directory,
|
|||||||
<< " Drive" << std::endl;
|
<< " Drive" << std::endl;
|
||||||
if (prov == provider_type::remote) {
|
if (prov == provider_type::remote) {
|
||||||
try {
|
try {
|
||||||
std::uint16_t port{};
|
|
||||||
if (not utils::get_next_available_port(
|
|
||||||
config.get_remote_config().api_port, port)) {
|
|
||||||
std::cerr << "FATAL: Unable to get available port" << std::endl;
|
|
||||||
return exit_code::startup_exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto remote_cfg = config.get_remote_config();
|
auto remote_cfg = config.get_remote_config();
|
||||||
remote_cfg.host_name_or_ip = remote_host;
|
remote_cfg.host_name_or_ip = remote_host;
|
||||||
remote_cfg.api_port = remote_port;
|
remote_cfg.api_port = remote_port;
|
||||||
|
@ -109,7 +109,13 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
|||||||
server_(server) {
|
server_(server) {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
server->set_socket_options([](auto && /* sock */) {});
|
#ifdef _WIN32
|
||||||
|
server_->set_socket_options([](auto &&sock) {
|
||||||
|
int enable = 1;
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
|
||||||
|
reinterpret_cast<const char *>(&enable), sizeof(enable));
|
||||||
|
});
|
||||||
|
#endif // _WIN32
|
||||||
|
|
||||||
server_->set_pre_routing_handler(
|
server_->set_pre_routing_handler(
|
||||||
[this](const httplib::Request &req,
|
[this](const httplib::Request &req,
|
||||||
|
@ -25,8 +25,8 @@
|
|||||||
#include "utils/string.hpp"
|
#include "utils/string.hpp"
|
||||||
|
|
||||||
namespace repertory::utils {
|
namespace repertory::utils {
|
||||||
auto compare_version_strings(std::string version1,
|
auto compare_version_strings(std::string version1, std::string version2)
|
||||||
std::string version2) -> std::int32_t {
|
-> std::int32_t {
|
||||||
|
|
||||||
if (utils::string::contains(version1, "-")) {
|
if (utils::string::contains(version1, "-")) {
|
||||||
version1 = utils::string::split(version1, '-', true)[0U];
|
version1 = utils::string::split(version1, '-', true)[0U];
|
||||||
@ -131,23 +131,46 @@ auto get_next_available_port(std::uint16_t first_port,
|
|||||||
using ip::tcp;
|
using ip::tcp;
|
||||||
|
|
||||||
boost::system::error_code error_code{};
|
boost::system::error_code error_code{};
|
||||||
while (first_port != 0U) {
|
|
||||||
io_context ctx{};
|
std::uint32_t check_port{first_port};
|
||||||
tcp::acceptor acceptor(ctx);
|
while (check_port <= 65535U) {
|
||||||
acceptor.open(tcp::v4(), error_code) ||
|
{
|
||||||
acceptor.bind({tcp::v4(), first_port}, error_code);
|
io_context ctx{};
|
||||||
if (not error_code) {
|
tcp::socket socket(ctx);
|
||||||
break;
|
socket.connect(
|
||||||
|
{
|
||||||
|
tcp::endpoint(ip::address_v4::loopback(),
|
||||||
|
static_cast<std::uint16_t>(check_port)),
|
||||||
|
},
|
||||||
|
error_code);
|
||||||
|
if (not error_code) {
|
||||||
|
++check_port;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
++first_port;
|
{
|
||||||
|
io_context ctx{};
|
||||||
|
tcp::acceptor acceptor(ctx);
|
||||||
|
acceptor.open(tcp::v4(), error_code);
|
||||||
|
if (error_code) {
|
||||||
|
++check_port;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
acceptor.bind({tcp::v4(), static_cast<std::uint16_t>(check_port)},
|
||||||
|
error_code);
|
||||||
|
if (error_code) {
|
||||||
|
++check_port;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
available_port = static_cast<std::uint16_t>(check_port);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (not error_code) {
|
return false;
|
||||||
available_port = first_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
return not error_code;
|
|
||||||
}
|
}
|
||||||
#endif // defined(PROJECT_ENABLE_BOOST)
|
#endif // defined(PROJECT_ENABLE_BOOST)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user