Some more updates

This commit is contained in:
WolverinDEV 2020-03-02 13:34:35 +01:00
parent 504c842499
commit 25c507c699
4 changed files with 1292 additions and 1292 deletions

View File

@ -1,97 +1,97 @@
#pragma once #pragma once
#include <protocol/buffers.h> #include <protocol/buffers.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <functional> #include <functional>
#include <mutex> #include <mutex>
#include "license.h" #include "license.h"
namespace license::client { namespace license::client {
class LicenseServerClient { class LicenseServerClient {
public: public:
enum ConnectionState { enum ConnectionState {
CONNECTING, CONNECTING,
INITIALIZING, INITIALIZING,
CONNECTED, CONNECTED,
DISCONNECTING, DISCONNECTING,
UNCONNECTED UNCONNECTED
}; };
typedef std::function<void()> callback_connected_t; typedef std::function<void()> callback_connected_t;
typedef std::function<void(protocol::PacketType /* type */, const void* /* payload */, size_t /* length */)> callback_message_t; typedef std::function<void(protocol::PacketType /* type */, const void* /* payload */, size_t /* length */)> callback_message_t;
typedef std::function<void(bool /* expected */, const std::string& /* reason */)> callback_disconnect_t; typedef std::function<void(bool /* expected */, const std::string& /* reason */)> callback_disconnect_t;
explicit LicenseServerClient(const sockaddr_in&, int /* protocol version */); explicit LicenseServerClient(const sockaddr_in&, int /* protocol version */);
virtual ~LicenseServerClient(); virtual ~LicenseServerClient();
bool start_connection(std::string& /* error */); bool start_connection(std::string& /* error */);
void send_message(protocol::PacketType /* type */, const void* /* buffer */, size_t /* length */); void send_message(protocol::PacketType /* type */, const void* /* buffer */, size_t /* length */);
void close_connection(); void close_connection();
void disconnect(const std::string& /* reason */, std::chrono::system_clock::time_point /* timeout */); void disconnect(const std::string& /* reason */, std::chrono::system_clock::time_point /* timeout */);
bool await_disconnect(); bool await_disconnect();
/* /*
* Events will be called within the event loop. * Events will be called within the event loop.
* All methods are save to call. * All methods are save to call.
* When close_connection or await_disconnect has been called these methods will not be called anymore. * When close_connection or await_disconnect has been called these methods will not be called anymore.
*/ */
callback_message_t callback_message{nullptr}; callback_message_t callback_message{nullptr};
callback_connected_t callback_connected{nullptr}; callback_connected_t callback_connected{nullptr};
callback_disconnect_t callback_disconnected{nullptr}; callback_disconnect_t callback_disconnected{nullptr};
const int protocol_version; const int protocol_version;
private: private:
std::mutex connection_lock{}; std::mutex connection_lock{};
ConnectionState connection_state{ConnectionState::UNCONNECTED}; ConnectionState connection_state{ConnectionState::UNCONNECTED};
std::chrono::system_clock::time_point disconnect_timeout{}; std::chrono::system_clock::time_point disconnect_timeout{};
struct Buffer { struct Buffer {
static Buffer* allocate(size_t /* capacity */); static Buffer* allocate(size_t /* capacity */);
static void free(Buffer* /* ptr */); static void free(Buffer* /* ptr */);
void* data; void* data;
size_t capacity; size_t capacity;
size_t fill; size_t fill;
size_t offset; size_t offset;
TAILQ_ENTRY(Buffer) tail; TAILQ_ENTRY(Buffer) tail;
}; };
/* modify everything here only within the event base, or when exited when connection_lock is locked */ /* modify everything here only within the event base, or when exited when connection_lock is locked */
struct { struct {
sockaddr_in address{}; sockaddr_in address{};
int file_descriptor{0}; int file_descriptor{0};
std::thread event_dispatch{}; std::thread event_dispatch{};
struct event_base* event_base{nullptr}; /* will be cleaned up by the event loop! */ struct event_base* event_base{nullptr}; /* will be cleaned up by the event loop! */
struct event* event_read{nullptr}; struct event* event_read{nullptr};
struct event* event_write{nullptr}; struct event* event_write{nullptr};
} network; } network;
struct { struct {
std::mutex lock{}; std::mutex lock{};
std::condition_variable notify_empty{}; std::condition_variable notify_empty{};
Buffer* read{nullptr}; /* must noch be accessed via lock because only the event loop uses it */ Buffer* read{nullptr}; /* must noch be accessed via lock because only the event loop uses it */
TAILQ_HEAD(, Buffer) write; TAILQ_HEAD(, Buffer) write;
} buffers; } buffers;
struct { struct {
bool initialized{false}; bool initialized{false};
std::string crypt_key{}; std::string crypt_key{};
} communication; } communication;
void callback_read(short /* events */); void callback_read(short /* events */);
void callback_write(short /* events */); void callback_write(short /* events */);
void callback_socket_connected(); void callback_socket_connected();
void cleanup_network_resources(); void cleanup_network_resources();
void handle_data(void*, size_t); void handle_data(void*, size_t);
void handle_raw_packet(protocol::PacketType /* type */, void* /* payload */, size_t /* length */); void handle_raw_packet(protocol::PacketType /* type */, void* /* payload */, size_t /* length */);
void handle_handshake_packet(void* /* payload */, size_t /* length */); void handle_handshake_packet(void* /* payload */, size_t /* length */);
}; };
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,134 +1,134 @@
#pragma once #pragma once
#include <variant> #include <variant>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <memory> #include <memory>
namespace license::client { namespace license::client {
class LicenseServerClient; class LicenseServerClient;
} }
namespace google::protobuf { namespace google::protobuf {
class Message; class Message;
} }
namespace ts::server::license { namespace ts::server::license {
struct InstanceLicenseInfo { struct InstanceLicenseInfo {
std::shared_ptr<::license::License> license{nullptr}; std::shared_ptr<::license::License> license{nullptr};
std::string web_certificate_revision{}; std::string web_certificate_revision{};
struct metrics_ { struct metrics_ {
size_t servers_online{0}; size_t servers_online{0};
size_t client_online{0}; size_t client_online{0};
size_t web_clients_online{0}; size_t web_clients_online{0};
size_t bots_online{0}; size_t bots_online{0};
size_t queries_online{0}; size_t queries_online{0};
size_t speech_total{0}; size_t speech_total{0};
size_t speech_varianz{0}; size_t speech_varianz{0};
size_t speech_online{0}; size_t speech_online{0};
size_t speech_dead{0}; size_t speech_dead{0};
} metrics; } metrics;
struct info_ { struct info_ {
std::chrono::milliseconds timestamp{}; std::chrono::milliseconds timestamp{};
std::string version{}; std::string version{};
std::string uname{}; std::string uname{};
std::string unique_id{}; std::string unique_id{};
} info; } info;
}; };
class LicenseService { class LicenseService {
public: public:
LicenseService(); LicenseService();
~LicenseService(); ~LicenseService();
[[nodiscard]] bool initialize(std::string& /* error */); [[nodiscard]] bool initialize(std::string& /* error */);
void shutdown(); void shutdown();
/* whatever it failed/succeeded */ /* whatever it failed/succeeded */
bool execute_request_sync(const std::chrono::milliseconds& /* timeout */); bool execute_request_sync(const std::chrono::milliseconds& /* timeout */);
[[nodiscard]] inline bool verbose() const { return this->verbose_; } [[nodiscard]] inline bool verbose() const { return this->verbose_; }
void execute_tick(); /* should not be essential to the core functionality! */ void execute_tick(); /* should not be essential to the core functionality! */
private: private:
std::chrono::steady_clock::time_point startup_timepoint_; std::chrono::steady_clock::time_point startup_timepoint_;
enum struct request_state { enum struct request_state {
empty, empty,
/* initializing */ /* initializing */
dns_lookup, dns_lookup,
connecting, connecting,
/* connected states */ /* connected states */
license_validate, license_validate,
license_upgrade, license_upgrade,
property_update, property_update,
/* disconnecting */ /* disconnecting */
finishing finishing
}; };
bool verbose_{false}; bool verbose_{false};
std::recursive_timed_mutex request_lock{}; std::recursive_timed_mutex request_lock{};
request_state request_state_{request_state::empty}; request_state request_state_{request_state::empty};
std::unique_ptr<::license::client::LicenseServerClient> current_client{nullptr}; std::unique_ptr<::license::client::LicenseServerClient> current_client{nullptr};
std::shared_ptr<InstanceLicenseInfo> license_request_data{nullptr}; std::shared_ptr<InstanceLicenseInfo> license_request_data{nullptr};
std::condition_variable sync_request_cv; std::condition_variable sync_request_cv;
std::mutex sync_request_lock; std::mutex sync_request_lock;
struct _timings { struct _timings {
std::chrono::system_clock::time_point last_request{}; std::chrono::system_clock::time_point last_request{};
std::chrono::system_clock::time_point next_request{}; std::chrono::system_clock::time_point next_request{};
std::chrono::system_clock::time_point last_succeeded{}; std::chrono::system_clock::time_point last_succeeded{};
size_t failed_count{0}; size_t failed_count{0};
} timings; } timings;
struct _dns { struct _dns {
std::shared_ptr<std::recursive_mutex> lock{nullptr}; std::shared_ptr<std::recursive_mutex> lock{nullptr};
struct _lookup { struct _lookup {
std::shared_ptr<std::recursive_mutex> lock{nullptr}; std::shared_ptr<std::recursive_mutex> lock{nullptr};
std::thread thread{}; std::thread thread{};
LicenseService* handle{nullptr}; /* may be null, locked via lock */ LicenseService* handle{nullptr}; /* may be null, locked via lock */
}* current_lookup{nullptr}; }* current_lookup{nullptr};
} dns; } dns;
std::optional<std::string> license_invalid_reason{}; /* set if the last license is invalid */ std::optional<std::string> license_invalid_reason{}; /* set if the last license is invalid */
void schedule_next_request(bool /* last request succeeded */); void schedule_next_request(bool /* last request succeeded */);
void begin_request(); void begin_request();
void client_send_message(::license::protocol::PacketType /* type */, ::google::protobuf::Message& /* message */); void client_send_message(::license::protocol::PacketType /* type */, ::google::protobuf::Message& /* message */);
void handle_check_fail(const std::string& /* error */); /* might be called form the DNS loop */ void handle_check_fail(const std::string& /* error */); /* might be called form the DNS loop */
void handle_check_succeeded(); void handle_check_succeeded();
/* if not disconnect message has been set it will just close the connection */ /* if not disconnect message has been set it will just close the connection */
void abort_request(std::lock_guard<std::recursive_timed_mutex>& /* request lock */, const std::string& /* disconnect message */); void abort_request(std::lock_guard<std::recursive_timed_mutex>& /* request lock */, const std::string& /* disconnect message */);
void abort_dns_request(); void abort_dns_request();
void execute_dns_request(); void execute_dns_request();
/* will be called while dns lock has been locked! */ /* will be called while dns lock has been locked! */
void handle_dns_lookup_result(bool /* success */, const std::variant<std::string, sockaddr_in>& /* data */); void handle_dns_lookup_result(bool /* success */, const std::variant<std::string, sockaddr_in>& /* data */);
/* all callbacks bellow are called from the current_client. It will not be null while being within the callback. */ /* all callbacks bellow are called from the current_client. It will not be null while being within the callback. */
void handle_client_connected(); void handle_client_connected();
void handle_client_disconnected(const std::string& /* error */); void handle_client_disconnected(const std::string& /* error */);
void handle_message(::license::protocol::PacketType /* type */, const void* /* buffer */, size_t /* length */); void handle_message(::license::protocol::PacketType /* type */, const void* /* buffer */, size_t /* length */);
void handle_message_license_info(const void* /* buffer */, size_t /* length */); void handle_message_license_info(const void* /* buffer */, size_t /* length */);
void handle_message_license_update(const void* /* buffer */, size_t /* length */); void handle_message_license_update(const void* /* buffer */, size_t /* length */);
void handle_message_property_adjustment(const void* /* buffer */, size_t /* length */); void handle_message_property_adjustment(const void* /* buffer */, size_t /* length */);
void send_license_validate_request(); void send_license_validate_request();
bool send_license_update_request(); bool send_license_update_request();
void send_property_update_request(); void send_property_update_request();
}; };
} }