Teaspeak-Server/license/server/LicenseServer.h

115 lines
4.0 KiB
C++

#pragma once
#include <event.h>
#include <protocol/buffers.h>
#include <deque>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <ThreadPool/Thread.h>
#include <shared/License.h>
#include <arpa/inet.h>
#include "LicenseManager.h"
namespace license {
namespace web {
class WebStatistics;
}
namespace stats {
class StatisticManager;
}
class UserManager;
enum ClientType {
SERVER,
MANAGER
};
struct ConnectedClient {
public:
struct {
sockaddr_in remoteAddr;
int fileDescriptor = 0;
event* readEvent = nullptr;
event* writeEvent = nullptr;
threads::Mutex lock;
TAILQ_HEAD(, ts::buffer::RawBuffer) writeQueue;
} network;
struct {
protocol::RequestState state = protocol::UNCONNECTED;
std::chrono::system_clock::time_point last_read;
std::string cryptKey = "";
} protocol;
ClientType type = ClientType::SERVER;
std::string username;
std::string key;
std::string unique_identifier;
void init();
void uninit();
void sendPacket(const protocol::packet&);
inline std::string address() { return inet_ntoa(network.remoteAddr.sin_addr); }
};
class LicenseServer {
public:
explicit LicenseServer(const sockaddr_in&, const std::shared_ptr<server::LicenseManager>&, const std::shared_ptr<stats::StatisticManager>& /* stats */, const std::shared_ptr<web::WebStatistics>& /* web stats */, const std::shared_ptr<UserManager>& /* user manager */);
~LicenseServer();
bool startServer();
bool isRunning(){ return this->running; }
void stopServer();
std::shared_ptr<ConnectedClient> findClient(int fileDescriptor);
void disconnectClient(const std::shared_ptr<ConnectedClient>&, const std::string& reason);
void closeConnection(const std::shared_ptr<ConnectedClient>&, bool blocking = false);
std::deque<std::shared_ptr<ConnectedClient>> getClients() {
std::lock_guard lock(this->lock);
return currentClients;
}
private:
void unregisterClient(const std::shared_ptr<ConnectedClient>&);
void cleanup_clients();
std::shared_ptr<web::WebStatistics> web_statistics;
std::shared_ptr<stats::StatisticManager> statistics;
std::shared_ptr<server::LicenseManager> manager;
std::shared_ptr<UserManager> user_manager;
bool running = false;
std::mutex lock;
std::deque<std::shared_ptr<ConnectedClient>> currentClients;
sockaddr_in* localAddr = nullptr;
int fileDescriptor = 0;
event_base* evBase = nullptr;
event* acceptEvent = nullptr;
event* client_cleanup = nullptr;
threads::Thread* evBaseDispatch = nullptr;
static void handleEventCleanup(int, short, void*);
static void handleEventAccept(int, short, void*);
static void handleEventRead(int, short, void*);
static void handleEventWrite(int, short, void*);
void handleMessage(std::shared_ptr<ConnectedClient>&, const std::string&);
bool handleDisconnect(std::shared_ptr<ConnectedClient>&, protocol::packet&, std::string& error);
bool handleHandshake(std::shared_ptr<ConnectedClient>&, protocol::packet&, std::string& error);
bool handleServerValidation(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketPropertyUpdate(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketAuth(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseCreate(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseList(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseDelete(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
};
}