Some smaller updates

This commit is contained in:
WolverinDEV 2020-04-08 13:50:03 +02:00
parent eb61daab43
commit 74fa735004
6 changed files with 17 additions and 6 deletions

@ -1 +1 @@
Subproject commit 136fac9ad2a22bf39327f77cd59f2f83a02b2eec
Subproject commit ed01d0360c5f980486e3bf83abdd3610d129aa7b

View File

@ -8,7 +8,7 @@
#include "license.h"
namespace license::client {
class LicenseServerClient {
class LicenseServerClient : public std::enable_shared_from_this<LicenseServerClient> {
public:
enum ConnectionState {
CONNECTING,

View File

@ -43,11 +43,13 @@ LicenseServerClient::LicenseServerClient(const sockaddr_in &address, int pversio
}
LicenseServerClient::~LicenseServerClient() {
const auto is_event_loop = this->network.event_dispatch.get_id() == std::this_thread::get_id();
{
std::unique_lock slock{this->connection_lock};
this->connection_state = ConnectionState::UNCONNECTED;
}
this->cleanup_network_resources(); /* force cleanup ignoring the previous state */
if(is_event_loop) this->network.event_dispatch.detach();
if(this->buffers.read)
Buffer::free(this->buffers.read);
@ -92,11 +94,15 @@ bool LicenseServerClient::start_connection(std::string &error) {
this->network.event_base = event_base_new();
this->network.event_read = event_new(this->network.event_base, this->network.file_descriptor, EV_READ | EV_PERSIST, [](int, short e, void* _this) {
auto client = reinterpret_cast<LicenseServerClient*>(_this);
auto client_ref = client->shared_from_this(); /* We're not allowed to delete outself while hading data. This will lead to dangling pointers */
client->callback_read(e);
client_ref.reset();
}, this);
this->network.event_write = event_new(this->network.event_base, this->network.file_descriptor, EV_WRITE, [](int, short e, void* _this) {
auto client = reinterpret_cast<LicenseServerClient*>(_this);
auto client_ref = client->shared_from_this(); /* We're not allowed to delete outself while hading data. This will lead to dangling pointers */
client->callback_write(e);
client_ref.reset();
}, this);
this->network.event_dispatch = std::thread([&] {
@ -242,8 +248,11 @@ void LicenseServerClient::callback_write(short events) {
}
if(events & EV_WRITE) {
if(this->connection_state == ConnectionState::CONNECTING)
if(this->connection_state == ConnectionState::CONNECTING) {
this->callback_socket_connected();
if(this->connection_state == ConnectionState::UNCONNECTED) /* state may change in the callback */
return;
}
ssize_t written_bytes{0};
@ -364,6 +373,8 @@ void LicenseServerClient::handle_data(void *recv_buffer, size_t length) {
if(buffer_length < header->length + sizeof(protocol::packet_header)) return;
this->handle_raw_packet(header->packetId, buffer_ptr + buffer_offset + sizeof(protocol::packet_header), header->length);
if(this->connection_state == ConnectionState::UNCONNECTED) return; /* state may change while we're handing the packet */
buffer_offset += header->length + sizeof(protocol::packet_header);
buffer_length -= header->length + sizeof(protocol::packet_header);
}

2
music

@ -1 +1 @@
Subproject commit 39fa2c60737152f8c60710c541698d83d8fdcd7f
Subproject commit 7ef7ea785aebc26d3f9c6e396270e7b03eccf587

View File

@ -229,7 +229,7 @@ void LicenseService::handle_dns_lookup_result(bool success, const std::variant<s
this->request_state_ = request_state::connecting;
assert(!this->current_client);
this->current_client = std::make_unique<::license::client::LicenseServerClient>(std::get<sockaddr_in>(result), 3);
this->current_client = std::make_shared<::license::client::LicenseServerClient>(std::get<sockaddr_in>(result), 3);
this->current_client->callback_connected = [&]{ this->handle_client_connected(); };
this->current_client->callback_disconnected = [&](bool expected, const std::string& error) {
this->handle_client_disconnected(error);

View File

@ -75,7 +75,7 @@ namespace ts::server::license {
std::recursive_timed_mutex request_lock{};
request_state request_state_{request_state::empty};
std::unique_ptr<::license::client::LicenseServerClient> current_client{nullptr};
std::shared_ptr<::license::client::LicenseServerClient> current_client{nullptr};
std::shared_ptr<InstanceLicenseInfo> license_request_data{nullptr};
std::condition_variable sync_request_cv;