Fixed compilation for windows and hide task executor impl details

This commit is contained in:
WolverinDEV 2021-04-19 19:29:46 +02:00
parent 444dea13c5
commit f4aaec6481
9 changed files with 87 additions and 80 deletions

View File

@ -1,5 +1,4 @@
#include <cstring>
#include <misc/base64.h>
#include "misc/endianness.h"
#include <misc/digest.h>
#define FIXEDINT_H_INCLUDED
@ -68,7 +67,7 @@ LicensePublicKey license::teamspeak::Anonymous::root_key = {
};
size_t license::teamspeak::Anonymous::root_index = 1;
shared_ptr<LicenseChain> license::teamspeak::Anonymous::chain = []() -> shared_ptr<LicenseChain> {
std::shared_ptr<LicenseChain> default_anonymous_chain() {
string error;
auto str = istringstream(string((const char*) default_chain, sizeof(default_chain)));
auto chain = LicenseChain::parse(str, error);
@ -78,7 +77,9 @@ shared_ptr<LicenseChain> license::teamspeak::Anonymous::chain = []() -> shared_p
}
return chain;
}();
}
shared_ptr<LicenseChain> license::teamspeak::Anonymous::chain{default_anonymous_chain()};
#define IOERROR(message) \
do {\
@ -326,9 +327,9 @@ inline string importHash(const std::string& hash) {
memset(buffer, 0, 64);
memcpy(buffer, (void*) hash.data(), 32);
buffer[0] &= 0xF8;
buffer[31] &= 0x3F;
buffer[31] |= 0x40;
buffer[0] &= (uint8_t) 0xF8;
buffer[31] &= (uint8_t) 0x3F;
buffer[31] |= (uint8_t) 0x40;
sc_reduce(buffer);
return string((char*) buffer, 32);
}

View File

@ -1005,12 +1005,12 @@ namespace ts {
}
template<class F>
inline std::result_of_t<F(PropertyManager &)> operator->*(F &&f) {
inline std::invoke_result<F(PropertyManager &)> operator->*(F &&f) {
return std::forward<F>(f)(*handle);
}
template<class F>
inline std::result_of_t<F(PropertyManager const &)> operator->*(F &&f) const {
inline std::invoke_result<F(PropertyManager const &)> operator->*(F &&f) const {
return std::forward<F>(f)(*handle);
}

View File

@ -13,7 +13,7 @@ std::string base64::decode(const char* input, size_t size) {
return ret;
}
std::string base64::encode(const char* input, const unsigned long inputSize) {
std::string base64::encode(const char* input, size_t inputSize) {
auto outlen = static_cast<unsigned long>(inputSize + (inputSize / 3.0) + 16);
auto outbuf = new unsigned char[outlen]; //Reserve output memory
if(base64_encode((unsigned char*) input, inputSize, outbuf, &outlen) != CRYPT_OK){

View File

@ -10,14 +10,14 @@ namespace base64 {
* @param inputSize The size of the input to decode
* @return A Base64-encoded version of the encoded string
*/
extern std::string encode(const char* input, const unsigned long inputSize);
extern std::string encode(const char* input, size_t inputSize);
/**
* Encodes a given string in Base64
* @param input The input string to Base64-encode
* @return A Base64-encoded version of the encoded string
*/
inline std::string encode(const std::string& input) { return encode(input.data(), (unsigned long) input.size()); }
inline std::string encode(const std::string& input) { return encode(input.data(), input.size()); }
inline std::string encode(const std::string_view& input) { return encode(input.data(), input.size()); }

View File

@ -4,14 +4,75 @@
#include "./task_executor.h"
#include "./threads.h"
#include <thread>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <future>
#include <optional>
#include <condition_variable>
using std::chrono::system_clock;
using namespace ts;
struct task_executor::task {
task_id id{0};
std::string name{};
std::function<void()> callback{};
/* will be set to true if the task has been canceled but is executing right now */
bool canceled{false};
std::optional<std::promise<void>> finish_callback{};
task* next{nullptr};
};
struct task_executor::task_recurring {
task_id id{0};
std::string name{};
bool shutdown{false};
std::optional<std::promise<void>> finish_callback{};
std::chrono::nanoseconds interval{};
std::chrono::system_clock::time_point last_invoked{};
std::chrono::system_clock::time_point scheduled_invoke{};
std::function<void(const std::chrono::system_clock::time_point& /* last executed */)> callback{};
task_recurring* next{nullptr};
};
struct task_executor::task_context {
std::mutex mutex{};
std::condition_variable notify{};
bool shutdown{false};
task_id id_index{1};
size_t task_count{};
task* task_head{nullptr};
task** task_tail{&this->task_head};
size_t task_recurring_count{};
task_recurring* task_recurring_head{nullptr};
task_exception_handler exception_handler{task_executor::abort_exception_handler};
};
struct task_executor::executor_context {
task_executor* handle{nullptr};
std::thread thread_handle{};
std::shared_ptr<struct task_context> task_context{};
/**
* Must be accessed while holding the task_context.mutex and shall never be changed except for the executor.
* Lifetime will be granted while holding the lock.
*/
task* executing_task{nullptr};
task_recurring* executing_recurring_task{nullptr};
};
task_executor::task_executor(size_t num_threads, const std::string &thread_prefix) {
this->task_context = std::make_shared<struct task_context>();

View File

@ -1,8 +1,6 @@
#pragma once
#include <thread>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <atomic>
@ -54,64 +52,10 @@ namespace ts {
std::chrono::nanoseconds /* interval */,
std::function<void(const std::chrono::system_clock::time_point& /* last scheduled */)> /* callback */);
private:
struct task {
task_id id{0};
std::string name{};
std::function<void()> callback{};
/* will be set to true if the task has been canceled but is executing right now */
bool canceled{false};
std::optional<std::promise<void>> finish_callback{};
task* next{nullptr};
};
struct task_recurring {
task_id id{0};
std::string name{};
bool shutdown{false};
std::optional<std::promise<void>> finish_callback{};
std::chrono::nanoseconds interval{};
std::chrono::system_clock::time_point last_invoked{};
std::chrono::system_clock::time_point scheduled_invoke{};
std::function<void(const std::chrono::system_clock::time_point& /* last executed */)> callback{};
task_recurring* next{nullptr};
};
struct task_context {
std::mutex mutex{};
std::condition_variable notify{};
bool shutdown{false};
task_id id_index{1};
size_t task_count{};
task* task_head{nullptr};
task** task_tail{&this->task_head};
size_t task_recurring_count{};
task_recurring* task_recurring_head{nullptr};
task_exception_handler exception_handler{task_executor::abort_exception_handler};
};
struct executor_context {
task_executor* handle;
std::thread thread_handle{};
std::shared_ptr<struct task_context> task_context{};
/**
* Must be accessed while holding the task_context.mutex and shall never be changed except for the executor.
* Lifetime will be granted while holding the lock.
*/
task* executing_task{nullptr};
task_recurring* executing_recurring_task{nullptr};
};
struct task;
struct task_recurring;
struct task_context;
struct executor_context;
enum struct task_cancel_result {
not_found,

View File

@ -198,13 +198,13 @@ bool CryptHandler::generate_key_nonce(
le2be32(generation, buffer, 2);
memcpy(&buffer[6], this->iv_struct, this->iv_struct_length);
digest::sha256(buffer, buffer_length, key_cache.key_nonce);
digest::sha256(buffer, buffer_length, key_cache.key_nonce.value);
key_cache.generation = generation;
}
memcpy(key.data(), key_cache.key, 16);
memcpy(nonce.data(), key_cache.nonce, 16);
memcpy(key.data(), key_cache.key_nonce.key, 16);
memcpy(nonce.data(), key_cache.key_nonce.nonce, 16);
}
//Xor the key

View File

@ -53,13 +53,13 @@ namespace ts::connection {
struct KeyCache {
uint16_t generation = 0xFFEF;
union {
union _key_nonce {
struct {
uint8_t key[16];
uint8_t nonce[16];
};
uint8_t key_nonce[32];
};
uint8_t value[32];
} key_nonce;
};
bool encryption_initialized_{false};
@ -72,8 +72,8 @@ namespace ts::connection {
uint8_t current_mac[8]{};
std::mutex cache_key_lock{};
std::array<KeyCache, protocol::PACKET_MAX> cache_key_client{};
std::array<KeyCache, protocol::PACKET_MAX> cache_key_server{};
std::array<KeyCache, /* protocol::PACKET_MAX */ 0x08> cache_key_client{};
std::array<KeyCache, /* protocol::PACKET_MAX */ 0x08> cache_key_server{};
static_assert(sizeof(current_mac) == sizeof(default_mac), "invalid mac");
static_assert(sizeof(iv_struct) == 64, "invalid iv struct");

View File

@ -13,6 +13,7 @@
#include <list>
#include <deque>
#include <memory>
#include <optional>
#include <pipes/buffer.h>
#include "../Variable.h"