Move socket option to bind and listen function

This commit is contained in:
JP Meijers 2026-02-20 18:06:23 +02:00
parent c17be43767
commit bb9fcb96c5
No known key found for this signature in database
GPG Key ID: 57E7B2C7D93F3D34
1 changed files with 7 additions and 6 deletions

View File

@ -72,12 +72,6 @@ bool server_http_context::init(const common_params & params) {
srv.reset(new httplib::Server());
#endif
// Enable address reuse to allow immediate restart of server
srv->set_socket_options([](socket_t sock) {
int val = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
});
srv->set_default_headers({{"Server", "llama.cpp"}});
srv->set_logger(log_server_request);
srv->set_exception_handler([](const httplib::Request &, httplib::Response & res, const std::exception_ptr & ep) {
@ -288,6 +282,13 @@ bool server_http_context::start() {
was_bound = srv->bind_to_port(hostname, 8080);
} else {
LOG_INF("%s: binding port with default address family\n", __func__);
// Enable address reuse to allow immediate restart of server
srv->set_socket_options([](socket_t sock) {
int flag = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
});
// bind HTTP listen port
if (port == 0) {
int bound_port = srv->bind_to_any_port(hostname);