2019-07-17 13:37:18 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ThreadPool/Mutex.h>
|
|
|
|
#include <ThreadPool/Thread.h>
|
|
|
|
#include <event.h>
|
2019-07-23 04:37:56 -04:00
|
|
|
#include <misc/net.h>
|
2019-07-17 13:37:18 -04:00
|
|
|
|
|
|
|
namespace ts {
|
|
|
|
namespace server {
|
2020-01-26 12:04:38 -05:00
|
|
|
class VirtualServer;
|
2019-07-17 13:37:18 -04:00
|
|
|
class WebClient;
|
|
|
|
|
|
|
|
class WebControlServer {
|
|
|
|
friend class WebClient;
|
|
|
|
public:
|
2020-01-23 20:57:58 -05:00
|
|
|
struct Binding {
|
|
|
|
sockaddr_storage address{};
|
|
|
|
int file_descriptor = 0;
|
|
|
|
::event* event_accept = nullptr;
|
2019-07-23 04:37:56 -04:00
|
|
|
|
2020-01-23 20:57:58 -05:00
|
|
|
inline std::string as_string() { return net::to_string(address, true); }
|
|
|
|
};
|
2019-07-23 04:37:56 -04:00
|
|
|
|
2020-01-26 12:04:38 -05:00
|
|
|
explicit WebControlServer(const std::shared_ptr<VirtualServer>&);
|
2019-07-17 13:37:18 -04:00
|
|
|
~WebControlServer();
|
|
|
|
|
2020-01-23 20:57:58 -05:00
|
|
|
bool start(const std::deque<std::shared_ptr<Binding>>& /* bindings */, std::string& /* error */);
|
|
|
|
inline bool running(){ return _running; }
|
2019-07-17 13:37:18 -04:00
|
|
|
void stop();
|
|
|
|
|
2020-01-26 12:04:38 -05:00
|
|
|
std::shared_ptr<VirtualServer> getTS(){ return this->handle; }
|
2019-07-17 13:37:18 -04:00
|
|
|
|
|
|
|
std::deque<std::shared_ptr<WebClient>> connectedClients(){
|
|
|
|
threads::MutexLock l(this->clientLock);
|
|
|
|
return this->clients;
|
|
|
|
}
|
|
|
|
|
2021-04-21 07:19:46 -04:00
|
|
|
[[nodiscard]] std::optional<std::string> external_binding();
|
2019-07-17 13:37:18 -04:00
|
|
|
private:
|
2020-01-26 12:04:38 -05:00
|
|
|
std::shared_ptr<VirtualServer> handle = nullptr;
|
2019-07-17 13:37:18 -04:00
|
|
|
|
|
|
|
threads::Mutex clientLock;
|
|
|
|
std::deque<std::shared_ptr<WebClient>> clients;
|
|
|
|
|
|
|
|
bool _running = false;
|
2020-01-23 20:57:58 -05:00
|
|
|
std::deque<std::shared_ptr<Binding>> bindings;
|
2019-07-23 04:37:56 -04:00
|
|
|
|
2021-04-14 17:12:51 -04:00
|
|
|
std::mutex server_reserve_fd_lock{};
|
|
|
|
int server_reserve_fd{-1}; /* -1 = unset | 0 = in use | > 0 ready to use */
|
2019-07-23 04:37:56 -04:00
|
|
|
|
2020-01-23 20:57:58 -05:00
|
|
|
//IO stuff
|
|
|
|
std::chrono::system_clock::time_point accept_event_deleted;
|
2019-07-17 13:37:18 -04:00
|
|
|
private:
|
2021-04-15 06:54:52 -04:00
|
|
|
static void on_client_receive(int, short, void *);
|
2019-07-17 13:37:18 -04:00
|
|
|
void unregisterConnection(const std::shared_ptr<WebClient>&);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|