Now the simple server is using a List in a thread safe way.

This commit is contained in:
Relintai 2022-07-24 11:53:16 +02:00
parent 492f29163c
commit 7d3486e5ee
4 changed files with 69 additions and 9 deletions

View File

@ -305,21 +305,63 @@ void HTTPServerSimple::poll() {
connection->peer = connection->tcp; connection->peer = connection->tcp;
connection->time = OS::get_singleton()->get_ticks_usec(); connection->time = OS::get_singleton()->get_ticks_usec();
_connections_lock.write_lock();
_connections.push_back(connection); _connections.push_back(connection);
_connections_lock.write_unlock();
} }
//TODO This should be done by worker threads (with a proper lock free queue) /*
for (int i = 0; i < _connections.size(); ++i) { //THis will only work well in a worker thread
Ref<HTTPServerConnection> c = _connections[i]; while (!_connections.empty()) {
_connections_lock.write_lock();
Ref<HTTPServerConnection> c = _connections.front()->get();
_connections.pop_front();
_connections_lock.write_unlock();
if (c->closed()) { if (c->closed()) {
_connections.remove(i);
--i;
continue; continue;
} }
c->update(); c->update();
if (c->closed()) {
continue;
} }
_connections_lock.write_lock();
_connections.push_back(c);
_connections_lock.write_unlock();
}
*/
//Single threaded ver
_connections_lock.write_lock();
List<Ref<HTTPServerConnection>>::Element *e = _connections.front();
while (e) {
Ref<HTTPServerConnection> c = e->get();
if (c->closed()) {
List<Ref<HTTPServerConnection>>::Element *etmp = e->next();
_connections.erase(e);
e = etmp;
continue;
}
c->update();
if (c->closed()) {
List<Ref<HTTPServerConnection>>::Element *etmp = e->next();
_connections.erase(e);
e = etmp;
continue;
}
e = e->next();
}
_connections_lock.write_unlock();
} }
HTTPServerSimple::HTTPServerSimple() { HTTPServerSimple::HTTPServerSimple() {
@ -341,11 +383,17 @@ HTTPServerSimple::~HTTPServerSimple() {
} }
void HTTPServerSimple::_clear_clients() { void HTTPServerSimple::_clear_clients() {
for (int i = 0; i < _connections.size(); ++i) { //stop worker threads first!
_connections.write[i]->close();
_connections_lock.write_lock();
for (List<Ref<HTTPServerConnection>>::Element *e = _connections.front(); e; e = e->next()) {
e->get()->close();
} }
_connections.clear(); _connections.clear();
_connections_lock.write_unlock();
} }
void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) { void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) {
@ -355,6 +403,7 @@ void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) {
const String key_path = cache_path.plus_file("html5_server.key"); const String key_path = cache_path.plus_file("html5_server.key");
const String crt_path = cache_path.plus_file("html5_server.crt"); const String crt_path = cache_path.plus_file("html5_server.crt");
bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path); bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
if (!regen) { if (!regen) {
key = Ref<CryptoKey>(CryptoKey::create()); key = Ref<CryptoKey>(CryptoKey::create());
cert = Ref<X509Certificate>(X509Certificate::create()); cert = Ref<X509Certificate>(X509Certificate::create());
@ -362,6 +411,7 @@ void HTTPServerSimple::_set_internal_certs(Ref<Crypto> p_crypto) {
regen = true; regen = true;
} }
} }
if (regen) { if (regen) {
key = p_crypto->generate_rsa(2048); key = p_crypto->generate_rsa(2048);
key->save(key_path); key->save(key_path);

View File

@ -34,6 +34,8 @@
#include "core/io/stream_peer_ssl.h" #include "core/io/stream_peer_ssl.h"
#include "core/io/tcp_server.h" #include "core/io/tcp_server.h"
#include "core/io/zip_io.h" #include "core/io/zip_io.h"
#include "core/list.h"
#include "core/os/rw_lock.h"
#include "core/vector.h" #include "core/vector.h"
#include "core/project_settings.h" #include "core/project_settings.h"
@ -103,8 +105,8 @@ private:
Ref<CryptoKey> key; Ref<CryptoKey> key;
bool use_ssl = false; bool use_ssl = false;
//TODO add a lock free queue List<Ref<HTTPServerConnection>> _connections;
Vector<Ref<HTTPServerConnection>> _connections; RWLock _connections_lock;
void _clear_clients(); void _clear_clients();
void _set_internal_certs(Ref<Crypto> p_crypto); void _set_internal_certs(Ref<Crypto> p_crypto);

View File

@ -73,6 +73,10 @@ void WebServerSimple::_stop() {
} }
WebServerSimple::WebServerSimple() { WebServerSimple::WebServerSimple() {
_use_worker_threads = true;
_use_poll_thread = true;
_thread_count = 1;
server.instance(); server.instance();
server->_web_server = this; server->_web_server = this;
server_thread.start(_server_thread_poll, this); server_thread.start(_server_thread_poll, this);

View File

@ -60,6 +60,10 @@ protected:
Mutex server_lock; Mutex server_lock;
Thread server_thread; Thread server_thread;
bool _use_poll_thread;
bool _use_worker_threads;
int _thread_count;
static void _server_thread_poll(void *data); static void _server_thread_poll(void *data);
}; };