From d55b66b7fffd49c4ee84c8e94eec029cb8734eec Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Jul 2022 14:15:00 +0200 Subject: [PATCH] Only start the server thread when it's actually needed. --- .../http_server_simple/web_server_simple.cpp | 36 +++++++++++++++++-- .../http_server_simple/web_server_simple.h | 5 ++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/modules/web/http_server_simple/web_server_simple.cpp b/modules/web/http_server_simple/web_server_simple.cpp index cef4d67df..0025c1507 100644 --- a/modules/web/http_server_simple/web_server_simple.cpp +++ b/modules/web/http_server_simple/web_server_simple.cpp @@ -96,7 +96,13 @@ void WebServerSimple::set_worker_thread_count(const int val) { _worker_thread_count = val; } +bool WebServerSimple::is_running() const { + return _is_running; +} + void WebServerSimple::_start() { + ERR_FAIL_COND(_running); + WebServer::_start(); if (!OS::get_singleton()->can_use_threads()) { @@ -137,15 +143,31 @@ void WebServerSimple::_start() { server->stop(); err = server->listen(bind_port, bind_ip, use_ssl, ssl_key, ssl_cert); } - ERR_FAIL_COND_MSG(err != OK, "Error starting HTTP server:\n" + itos(err)); + + _running = true; + + if (!server_thread) { + server_thread = memnew(Thread); + server_thread->start(_server_thread_poll, this); + } } void WebServerSimple::_stop() { + ERR_FAIL_COND(!_running); + WebServer::_stop(); MutexLock lock(server_lock); server->stop(); + + if (server_thread) { + server_thread->wait_to_finish(); + memdelete(server_thread); + server_thread = nullptr; + } + + _running = false; } WebServerSimple::WebServerSimple() { @@ -159,17 +181,23 @@ WebServerSimple::WebServerSimple() { _poll_thread_count = 1; _worker_thread_count = 4; + _running = false; + server_quit = false; server.instance(); server->_web_server = this; - server_thread.start(_server_thread_poll, this); + server_thread = nullptr; } WebServerSimple::~WebServerSimple() { server->stop(); server_quit = true; - server_thread.wait_to_finish(); + + if (server_thread) { + server_thread->wait_to_finish(); + memdelete(server_thread); + } } void WebServerSimple::_bind_methods() { @@ -208,6 +236,8 @@ void WebServerSimple::_bind_methods() { ClassDB::bind_method(D_METHOD("get_worker_thread_count"), &WebServerSimple::get_worker_thread_count); ClassDB::bind_method(D_METHOD("set_worker_thread_count", "val"), &WebServerSimple::set_worker_thread_count); ADD_PROPERTY(PropertyInfo(Variant::INT, "worker_thread_count"), "set_worker_thread_count", "get_worker_thread_count"); + + ClassDB::bind_method(D_METHOD("is_running"), &WebServerSimple::is_running); } void WebServerSimple::_server_thread_poll(void *data) { diff --git a/modules/web/http_server_simple/web_server_simple.h b/modules/web/http_server_simple/web_server_simple.h index 4b3d5527c..b6ee0bf3f 100644 --- a/modules/web/http_server_simple/web_server_simple.h +++ b/modules/web/http_server_simple/web_server_simple.h @@ -73,6 +73,8 @@ public: int get_worker_thread_count(); void set_worker_thread_count(const int val); + bool is_running() const; + void _start(); void _stop(); @@ -98,7 +100,8 @@ protected: Ref server; bool server_quit; Mutex server_lock; - Thread server_thread; + Thread *server_thread; + bool _running; static void _server_thread_poll(void *data); };