listen job run in its own thread, non blocking

This commit is contained in:
Benjamin Sergeant
2019-01-01 14:52:14 -08:00
parent b749f3c724
commit ead54d6c37
3 changed files with 25 additions and 4 deletions

View File

@ -23,14 +23,15 @@ namespace ix
WebSocketServer::WebSocketServer(int port, const std::string& host, int backlog) :
_port(port),
_host(host),
_backlog(backlog)
_backlog(backlog),
_stop(false)
{
}
WebSocketServer::~WebSocketServer()
{
stop();
}
void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback)
@ -112,6 +113,21 @@ namespace ix
return std::make_pair(true, "");
}
void WebSocketServer::start()
{
if (_thread.joinable()) return; // we've already been started
_thread = std::thread(&WebSocketServer::run, this);
}
// FIXME: we should cancel all the async per connections tasks
void WebSocketServer::stop()
{
_stop = true;
_thread.join();
_stop = false;
}
void WebSocketServer::run()
{
std::future<void> f;