Teaspeak-Server/license/shared/LicenseRequest.h

129 lines
4.7 KiB
C
Raw Normal View History

#pragma once
#include <protocol/buffers.h>
#include <ThreadPool/Mutex.h>
#include <ThreadPool/Thread.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <event.h>
#include "License.h"
#ifdef DEFINE_HELPER
#define LICENSE_FERR(this, class, message) \
do { \
this->currentException = std::make_shared<exceptions::class>(message); \
if(this->currentFuture && this->currentFuture->state() == threads::FutureState::WORKING) this->currentFuture->executionFailed(); \
this->disconnect("internal error"); \
return; \
} while(0)
#endif
namespace license {
namespace exceptions {
class CouldNotConnectException : public LicenseException {
public:
explicit CouldNotConnectException(const std::string &message) : LicenseException(message) {}
};
class ConnectionException : public LicenseException {
public:
explicit ConnectionException(const std::string &message) : LicenseException(message) {}
};
class UnexcpectedDisconnectException : public LicenseException {
public:
explicit UnexcpectedDisconnectException(const std::string &message) : LicenseException(message) {}
};
class InvalidResponseException : public LicenseException {
public:
explicit InvalidResponseException(const std::string &message) : LicenseException(message) {}
};
}
struct ServerInfo {
std::string unique_identifier;
int64_t timestamp;
std::string uname;
std::string version;
};
struct LicenseRequestResponse {
std::shared_ptr<LicenseInfo> license;
bool license_valid;
int64_t speach_varianz_adjustment;
bool speach_reset;
bool properties_valid;
std::chrono::system_clock::time_point age;
};
struct LicenseRequestData {
std::shared_ptr<License> license = nullptr;
std::shared_ptr<ServerInfo> info = nullptr;
int64_t speach_total = 0;
int64_t speach_dead = 0;
int64_t speach_online = 0;
int64_t speach_varianz = 0;
int64_t client_online = 0;
int64_t web_clients_online = 0;
int64_t bots_online = 0;
int64_t queries_online = 0;
int64_t servers_online = 0;
};
class LicenceRequest {
public:
typedef threads::Future<std::shared_ptr<LicenseRequestResponse>> ResponseFuture;
LicenceRequest(const std::shared_ptr<LicenseRequestData>&, const sockaddr_in&);
~LicenceRequest();
std::shared_ptr<exceptions::LicenseException> exception(){ return this->currentException; }
void clearExceptions(){ this->currentException = nullptr; }
ResponseFuture requestInfo();
void abortRequest(const std::chrono::system_clock::time_point& timeout = std::chrono::system_clock::time_point());
void sendPacket(const protocol::packet&);
bool verbose = true;
private:
std::shared_ptr<LicenseRequestData> data;
threads::Future<std::shared_ptr<LicenseRequestResponse>>* currentFuture = nullptr;
std::shared_ptr<LicenseRequestResponse> response = nullptr;
std::shared_ptr<exceptions::LicenseException> currentException;
std::recursive_mutex lock;
protocol::RequestState state = protocol::UNCONNECTED;
sockaddr_in remote_address;
int file_descriptor = 0;
std::thread event_dispatch;
threads::Thread* closeThread = nullptr;
struct event_base* event_base = nullptr;
event* event_read = nullptr;
event* event_write = nullptr;
TAILQ_HEAD(, ts::buffer::RawBuffer) writeQueue;
std::string cryptKey = "";
void beginRequest();
void handleConnected();
static void handleEventRead(int, short, void*);
static void handleEventWrite(int, short, void*);
void handleMessage(const std::string&);
void disconnect(const std::string&);
void closeConnection();
void handlePacketHandshake(const std::string&);
void handlePacketDisconnect(const std::string&);
void handlePacketLicenseInfo(const std::string&);
void handlePacketInfoAdjustment(const std::string&);
};
}