]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
server : bind to any port when specified (#10590)
authoralek3y <redacted>
Sun, 1 Dec 2024 11:33:12 +0000 (12:33 +0100)
committerGitHub <redacted>
Sun, 1 Dec 2024 11:33:12 +0000 (13:33 +0200)
examples/server/server.cpp

index 9c86407c28ebacfc8183e6e6a9f0bbbdf90ed977..1c765f0ea22ecce854a1a5af1ca8e767c91ccd09 100644 (file)
@@ -3347,8 +3347,18 @@ int main(int argc, char ** argv) {
         llama_backend_free();
     };
 
-    // bind HTTP listen port, run the HTTP server in a thread
-    if (!svr->bind_to_port(params.hostname, params.port)) {
+    // bind HTTP listen port
+    bool was_bound = false;
+    if (params.port == 0) {
+        int bound_port = svr->bind_to_any_port(params.hostname);
+        if ((was_bound = (bound_port >= 0))) {
+            params.port = bound_port;
+        }
+    } else {
+        was_bound = svr->bind_to_port(params.hostname, params.port);
+    }
+
+    if (!was_bound) {
         //LOG_ERROR("couldn't bind HTTP server socket", {
         //    {"hostname", params.hostname},
         //    {"port", params.port},
@@ -3357,6 +3367,8 @@ int main(int argc, char ** argv) {
         clean_up();
         return 1;
     }
+
+    // run the HTTP server in a thread
     std::thread t([&]() { svr->listen_after_bind(); });
     svr->wait_until_ready();