[Web] Move polling thread to EditorHTTPServer

This commit is contained in:
Fabio Alessandrelli 2024-02-07 15:59:11 +01:00
parent 222214c05f
commit fdf6fa82c3
4 changed files with 72 additions and 64 deletions

View File

@ -30,6 +30,17 @@
#include "editor_http_server.h"
void EditorHTTPServer::_server_thread_poll(void *data) {
EditorHTTPServer *web_server = static_cast<EditorHTTPServer *>(data);
while (!web_server->server_quit.get()) {
OS::get_singleton()->delay_usec(6900);
{
MutexLock lock(web_server->server_lock);
web_server->_poll();
}
}
}
void EditorHTTPServer::_clear_client() {
peer = Ref<StreamPeer>();
tls = Ref<StreamPeerTLS>();
@ -117,37 +128,7 @@ void EditorHTTPServer::_send_response() {
}
}
void EditorHTTPServer::stop() {
server->stop();
_clear_client();
}
Error EditorHTTPServer::listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert) {
use_tls = p_use_tls;
if (use_tls) {
Ref<Crypto> crypto = Crypto::create();
if (crypto.is_null()) {
return ERR_UNAVAILABLE;
}
if (!p_tls_key.is_empty() && !p_tls_cert.is_empty()) {
key = Ref<CryptoKey>(CryptoKey::create());
Error err = key->load(p_tls_key);
ERR_FAIL_COND_V(err != OK, err);
cert = Ref<X509Certificate>(X509Certificate::create());
err = cert->load(p_tls_cert);
ERR_FAIL_COND_V(err != OK, err);
} else {
_set_internal_certs(crypto);
}
}
return server->listen(p_port, p_address);
}
bool EditorHTTPServer::is_listening() const {
return server->is_listening();
}
void EditorHTTPServer::poll() {
void EditorHTTPServer::_poll() {
if (!server->is_listening()) {
return;
}
@ -211,6 +192,52 @@ void EditorHTTPServer::poll() {
}
}
void EditorHTTPServer::stop() {
server_quit.set(true);
if (server_thread.is_started()) {
server_thread.wait_to_finish();
}
if (server.is_valid()) {
server->stop();
}
_clear_client();
}
Error EditorHTTPServer::listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert) {
MutexLock lock(server_lock);
if (server->is_listening()) {
return ERR_ALREADY_IN_USE;
}
use_tls = p_use_tls;
if (use_tls) {
Ref<Crypto> crypto = Crypto::create();
if (crypto.is_null()) {
return ERR_UNAVAILABLE;
}
if (!p_tls_key.is_empty() && !p_tls_cert.is_empty()) {
key = Ref<CryptoKey>(CryptoKey::create());
Error err = key->load(p_tls_key);
ERR_FAIL_COND_V(err != OK, err);
cert = Ref<X509Certificate>(X509Certificate::create());
err = cert->load(p_tls_cert);
ERR_FAIL_COND_V(err != OK, err);
} else {
_set_internal_certs(crypto);
}
}
Error err = server->listen(p_port, p_address);
if (err == OK) {
server_quit.set(false);
server_thread.start(_server_thread_poll, this);
}
return err;
}
bool EditorHTTPServer::is_listening() const {
MutexLock lock(server_lock);
return server->is_listening();
}
EditorHTTPServer::EditorHTTPServer() {
mimes["html"] = "text/html";
mimes["js"] = "application/javascript";
@ -222,3 +249,7 @@ EditorHTTPServer::EditorHTTPServer() {
server.instantiate();
stop();
}
EditorHTTPServer::~EditorHTTPServer() {
stop();
}

View File

@ -51,17 +51,24 @@ private:
uint8_t req_buf[4096];
int req_pos = 0;
SafeNumeric<bool> server_quit;
Mutex server_lock;
Thread server_thread;
void _clear_client();
void _set_internal_certs(Ref<Crypto> p_crypto);
void _send_response();
void _poll();
static void _server_thread_poll(void *data);
public:
EditorHTTPServer();
~EditorHTTPServer();
void stop();
Error listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert);
bool is_listening() const;
void poll();
};
#endif // WEB_EDITOR_HTTP_SERVER_H

View File

@ -584,7 +584,6 @@ bool EditorExportPlatformWeb::poll_export() {
menu_options = preset.is_valid();
if (server->is_listening()) {
if (menu_options == 0) {
MutexLock lock(server_lock);
server->stop();
} else {
menu_options += 1;
@ -603,7 +602,6 @@ int EditorExportPlatformWeb::get_options_count() const {
Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int p_option, int p_debug_flags) {
if (p_option == 1) {
MutexLock lock(server_lock);
server->stop();
return OK;
}
@ -653,12 +651,8 @@ Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int
const String tls_cert = EDITOR_GET("export/web/tls_certificate");
// Restart server.
{
MutexLock lock(server_lock);
server->stop();
err = server->listen(bind_port, bind_ip, use_tls, tls_key, tls_cert);
}
server->stop();
err = server->listen(bind_port, bind_ip, use_tls, tls_key, tls_cert);
if (err != OK) {
add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Error starting HTTP server: %d."), err));
return err;
@ -674,21 +668,9 @@ Ref<Texture2D> EditorExportPlatformWeb::get_run_icon() const {
return run_icon;
}
void EditorExportPlatformWeb::_server_thread_poll(void *data) {
EditorExportPlatformWeb *ej = static_cast<EditorExportPlatformWeb *>(data);
while (!ej->server_quit.get()) {
OS::get_singleton()->delay_usec(6900);
{
MutexLock lock(ej->server_lock);
ej->server->poll();
}
}
}
EditorExportPlatformWeb::EditorExportPlatformWeb() {
if (EditorNode::get_singleton()) {
server.instantiate();
server_thread.start(_server_thread_poll, this);
#ifdef MODULE_SVG_ENABLED
Ref<Image> img = memnew(Image);
@ -711,11 +693,4 @@ EditorExportPlatformWeb::EditorExportPlatformWeb() {
}
EditorExportPlatformWeb::~EditorExportPlatformWeb() {
if (server.is_valid()) {
server->stop();
}
server_quit.set(true);
if (server_thread.is_started()) {
server_thread.wait_to_finish();
}
}

View File

@ -52,9 +52,6 @@ class EditorExportPlatformWeb : public EditorExportPlatform {
int menu_options = 0;
Ref<EditorHTTPServer> server;
SafeNumeric<bool> server_quit;
Mutex server_lock;
Thread server_thread;
String _get_template_name(bool p_extension, bool p_thread_support, bool p_debug) const {
String name = "web";
@ -99,8 +96,6 @@ class EditorExportPlatformWeb : public EditorExportPlatform {
Error _build_pwa(const Ref<EditorExportPreset> &p_preset, const String p_path, const Vector<SharedObject> &p_shared_objects);
Error _write_or_error(const uint8_t *p_content, int p_len, String p_path);
static void _server_thread_poll(void *data);
public:
virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;