put websocket server into separate thread

pull/105/head
Falk Werner 2 years ago
parent a5f1cdc7e7
commit d9350e9725

@ -2,44 +2,18 @@
#include "webfuse/fuse.hpp" #include "webfuse/fuse.hpp"
#include "webfuse/filesystem/empty_filesystem.hpp" #include "webfuse/filesystem/empty_filesystem.hpp"
#include "webfuse/ws/server.hpp" #include "webfuse/ws/server.hpp"
#include "webfuse/ws/config.hpp"
#include <csignal>
namespace
{
bool shutdown_requested = false;
void on_shutdown_requested(int)
{
shutdown_requested = true;
}
}
namespace webfuse namespace webfuse
{ {
int app::run(int argc, char * argv[]) int app::run(int argc, char * argv[])
{ {
/*
empty_filesystem filesystem; empty_filesystem filesystem;
fuse fuse_fs(filesystem); fuse fuse_fs(filesystem);
return fuse_fs.run(argc, argv);
*/
signal(SIGINT, &on_shutdown_requested);
ws_config config; ws_config config;
ws_server server(config); ws_server server(config);
while (!shutdown_requested) return fuse_fs.run(argc, argv);
{
server.service();
}
return 0;
} }
} }

@ -3,6 +3,8 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <thread>
#include <atomic>
extern "C" extern "C"
{ {
@ -48,6 +50,7 @@ class ws_server::detail
detail& operator=(detail &&) = delete; detail& operator=(detail &&) = delete;
public: public:
detail(ws_config const & config) detail(ws_config const & config)
: shutdown_requested(false)
{ {
memset(reinterpret_cast<void*>(protocols), 0, sizeof(protocols)); memset(reinterpret_cast<void*>(protocols), 0, sizeof(protocols));
protocols[0].name = "webfuse2"; protocols[0].name = "webfuse2";
@ -65,13 +68,27 @@ public:
lws_vhost * const vhost = lws_create_vhost(context, &info); lws_vhost * const vhost = lws_create_vhost(context, &info);
// port = lws_get_vhost_port(vhost); // port = lws_get_vhost_port(vhost);
thread = std::thread([this]() {
while (!shutdown_requested)
{
lws_service(context, 0);
}
});
} }
~detail() ~detail()
{ {
shutdown_requested = true;
lws_cancel_service(context);
thread.join();
lws_context_destroy(context); lws_context_destroy(context);
} }
std::thread thread;
std::atomic<bool> shutdown_requested;
lws_protocols protocols[2]; lws_protocols protocols[2];
lws_context_creation_info info; lws_context_creation_info info;
lws_context * context; lws_context * context;
@ -106,15 +123,4 @@ ws_server& ws_server::operator=(ws_server && other)
return *this; return *this;
} }
void ws_server::service()
{
lws_service(d->context, 0);
}
void ws_server::interrupt()
{
lws_cancel_service(d->context);
}
} }

@ -15,8 +15,6 @@ public:
~ws_server(); ~ws_server();
ws_server(ws_server && other); ws_server(ws_server && other);
ws_server& operator=(ws_server && other); ws_server& operator=(ws_server && other);
void service();
void interrupt();
private: private:
class detail; class detail;
detail * d; detail * d;

Loading…
Cancel
Save