pandemonium_engine/modules/http_server_simple/web_server_simple.cpp

290 lines
9.1 KiB
C++
Raw Normal View History

/*************************************************************************/
/* export.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "web_server_simple.h"
#include "core/os/os.h"
#include "http_server_simple.h"
2022-07-24 13:53:55 +02:00
int WebServerSimple::get_bind_port() {
return _bind_port;
}
void WebServerSimple::set_bind_port(const int val) {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
_bind_port = val;
}
String WebServerSimple::get_bind_host() {
return _bind_host;
}
void WebServerSimple::set_bind_host(const String &val) {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
_bind_host = val;
}
bool WebServerSimple::get_use_ssl() {
return _use_ssl;
}
void WebServerSimple::set_use_ssl(const bool val) {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
_use_ssl = val;
}
String WebServerSimple::get_ssl_key() {
return _ssl_key;
}
void WebServerSimple::set_ssl_key(const String &val) {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
_ssl_key = val;
}
String WebServerSimple::get_ssl_cert() {
return _ssl_cert;
}
void WebServerSimple::set_ssl_cert(const String &val) {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
_ssl_cert = val;
}
bool WebServerSimple::get_use_poll_thread() {
return _use_poll_thread;
}
void WebServerSimple::set_use_poll_thread(const bool val) {
ERR_FAIL_COND(_running);
_use_poll_thread = val;
}
bool WebServerSimple::get_use_worker_threads() {
return _use_worker_threads;
}
void WebServerSimple::set_use_worker_threads(const bool val) {
ERR_FAIL_COND(_running);
_use_worker_threads = val;
}
int WebServerSimple::get_worker_thread_count() {
return _worker_thread_count;
}
void WebServerSimple::set_worker_thread_count(const int val) {
ERR_FAIL_COND(_running);
_worker_thread_count = val;
}
void WebServerSimple::add_mime_type(const String &file_extension, const String &mime_type) {
_server->mimes[file_extension] = mime_type;
}
void WebServerSimple::remove_mime_type(const String &file_extension) {
_server->mimes.erase(file_extension);
}
bool WebServerSimple::is_running() const {
return _is_running;
}
void WebServerSimple::_start() {
ERR_FAIL_COND(_running);
2022-07-24 13:53:55 +02:00
WebServer::_start();
if (!OS::get_singleton()->can_use_threads()) {
2022-07-24 15:33:32 +02:00
_server->_use_worker_threads = false;
} else {
2022-07-24 15:33:32 +02:00
_server->_use_worker_threads = _use_worker_threads;
}
if (_worker_thread_count <= 0) {
2022-07-24 15:33:32 +02:00
_server->_thread_count = OS::get_singleton()->get_processor_count();
} else {
2022-07-24 15:33:32 +02:00
_server->_thread_count = _worker_thread_count;
}
2022-07-24 13:53:55 +02:00
const uint16_t bind_port = _bind_port;
// Resolve host if needed.
2022-07-24 13:53:55 +02:00
const String bind_host = _bind_host;
IP_Address bind_ip;
if (bind_host.is_valid_ip_address()) {
bind_ip = bind_host;
} else {
bind_ip = IP::get_singleton()->resolve_hostname(bind_host);
}
2022-07-24 13:53:55 +02:00
ERR_FAIL_COND(!bind_ip.is_valid());
2022-07-24 13:53:55 +02:00
const bool use_ssl = _use_ssl;
const String ssl_key = _ssl_key;
const String ssl_cert = _ssl_cert;
Error err;
// Restart server.
2022-07-24 15:33:32 +02:00
_server_lock.lock();
2022-07-24 15:33:32 +02:00
_server->stop();
err = _server->listen(bind_port, bind_ip, use_ssl, ssl_key, ssl_cert);
2022-07-24 15:33:32 +02:00
_server_lock.unlock();
2022-06-30 17:29:02 +02:00
ERR_FAIL_COND_MSG(err != OK, "Error starting HTTP server:\n" + itos(err));
_running = true;
_server_quit = false;
if (!_use_poll_thread || !OS::get_singleton()->can_use_threads()) {
set_process_internal(true);
_single_threaded_poll = true;
} else {
_poll_thread = memnew(Thread);
_poll_thread->start(_server_thread_poll, this);
_single_threaded_poll = false;
}
}
void WebServerSimple::_stop() {
ERR_FAIL_COND(!_running);
WebServer::_stop();
2022-07-24 15:33:32 +02:00
_server_lock.lock();
_server_quit = true;
2022-07-24 15:33:32 +02:00
_server->stop();
if (_poll_thread) {
_poll_thread->wait_to_finish();
memdelete(_poll_thread);
_poll_thread = nullptr;
}
set_process_internal(false);
_running = false;
2022-07-24 15:33:32 +02:00
_server_lock.unlock();
}
WebServerSimple::WebServerSimple() {
2022-07-24 13:53:55 +02:00
_bind_port = 8080;
_bind_host = "127.0.0.1";
_use_ssl = false;
_use_worker_threads = true;
_use_poll_thread = true;
_poll_thread = nullptr;
_worker_thread_count = 4;
_running = false;
2022-07-24 15:33:32 +02:00
_server_quit = false;
2022-07-24 13:53:55 +02:00
2022-07-24 15:33:32 +02:00
_server.instance();
_server->_web_server = this;
}
WebServerSimple::~WebServerSimple() {
2022-07-24 15:33:32 +02:00
_server->stop();
_server_quit = true;
if (_poll_thread) {
_poll_thread->wait_to_finish();
memdelete(_poll_thread);
}
}
void WebServerSimple::_notification(int p_what) {
if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
if (_single_threaded_poll) {
2022-07-24 15:33:32 +02:00
_server_lock.lock();
_server->poll();
_server_lock.unlock();
}
}
}
void WebServerSimple::_bind_methods() {
2022-07-24 13:53:55 +02:00
ClassDB::bind_method(D_METHOD("get_bind_port"), &WebServerSimple::get_bind_port);
ClassDB::bind_method(D_METHOD("set_bind_port", "val"), &WebServerSimple::set_bind_port);
ADD_PROPERTY(PropertyInfo(Variant::INT, "bind_port"), "set_bind_port", "get_bind_port");
ClassDB::bind_method(D_METHOD("get_bind_host"), &WebServerSimple::get_bind_host);
ClassDB::bind_method(D_METHOD("set_bind_host", "val"), &WebServerSimple::set_bind_host);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "bind_host"), "set_bind_host", "get_bind_host");
ClassDB::bind_method(D_METHOD("get_use_ssl"), &WebServerSimple::get_use_ssl);
ClassDB::bind_method(D_METHOD("set_use_ssl", "val"), &WebServerSimple::set_use_ssl);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_ssl"), "set_use_ssl", "get_use_ssl");
ClassDB::bind_method(D_METHOD("get_ssl_key"), &WebServerSimple::get_ssl_key);
ClassDB::bind_method(D_METHOD("set_ssl_key", "val"), &WebServerSimple::set_ssl_key);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "ssl_key"), "set_ssl_key", "get_ssl_key");
ClassDB::bind_method(D_METHOD("get_ssl_cert"), &WebServerSimple::get_ssl_cert);
ClassDB::bind_method(D_METHOD("set_ssl_cert", "val"), &WebServerSimple::set_ssl_cert);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "ssl_cert"), "set_ssl_cert", "get_ssl_cert");
ClassDB::bind_method(D_METHOD("get_use_poll_thread"), &WebServerSimple::get_use_poll_thread);
ClassDB::bind_method(D_METHOD("set_use_poll_thread", "val"), &WebServerSimple::set_use_poll_thread);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_poll_thread"), "set_use_poll_thread", "get_use_poll_thread");
ClassDB::bind_method(D_METHOD("get_use_worker_threads"), &WebServerSimple::get_use_worker_threads);
ClassDB::bind_method(D_METHOD("set_use_worker_threads", "val"), &WebServerSimple::set_use_worker_threads);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_worker_threads"), "set_use_worker_threads", "get_use_worker_threads");
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("add_mime_type", "file_extension", "mime_type"), &WebServerSimple::add_mime_type);
ClassDB::bind_method(D_METHOD("remove_mime_type", "file_extension"), &WebServerSimple::remove_mime_type);
ClassDB::bind_method(D_METHOD("is_running"), &WebServerSimple::is_running);
}
void WebServerSimple::_server_thread_poll(void *data) {
WebServerSimple *ej = (WebServerSimple *)data;
2022-07-24 15:33:32 +02:00
while (!ej->_server_quit) {
OS::get_singleton()->delay_usec(1000);
2022-07-24 15:33:32 +02:00
ej->_server_lock.lock();
ej->_server->poll();
ej->_server_lock.unlock();
}
}