totally fucked up

This commit is contained in:
WolverinDEV 2020-03-17 12:08:01 +01:00
parent 62292af022
commit eb503d4315
3 changed files with 186 additions and 178 deletions

View File

@ -100,13 +100,11 @@ namespace ts {
UNKNOWN = 0xFF
};
enum ConnectionState {
enum ClientState {
UNKNWON,
INIT_LOW, //Web -> WS Handschake
INIT_HIGH, //Web -> Auth
INITIALIZING,
CONNECTED,
DISCONNECTING,
DISCONNECTING_FLUSHING,
DISCONNECTED
};
}
@ -164,7 +162,7 @@ namespace ts {
DEFINE_CONVERTER_ENUM(a, b); \
DEFINE_VARIABLE_TRANSFORM_ENUM(a, b);
DEFINE_TRANSFORMS(ts::server::ConnectionState, uint8_t);
DEFINE_TRANSFORMS(ts::server::ClientState, uint8_t);
DEFINE_TRANSFORMS(ts::server::ClientType, uint8_t);
DEFINE_TRANSFORMS(ts::LicenseType, uint8_t);
DEFINE_TRANSFORMS(ts::PluginTargetMode, uint8_t);

View File

@ -27,6 +27,8 @@ namespace ts {
constexpr static auto write_preferred{write_preferred_};
};
namespace impl {
struct mutex_action_validator {
public:
inline void try_exclusive_lock_acquire();
@ -189,9 +191,10 @@ namespace ts {
uint32_t read_lock_pending{0};
uint32_t read_lock_count{0};
};
}
typedef rw_mutex_impl<rw_mutex_options<true>, mutex_action_validator> rw_safe_mutex;
typedef rw_mutex_impl<rw_mutex_options<true>, mutex_action_validator> rw_unsafe_mutex;
typedef impl::rw_mutex_impl<rw_mutex_options<true>, impl::mutex_action_validator> rw_safe_mutex;
typedef impl::rw_mutex_impl<rw_mutex_options<true>, impl::mutex_action_validator> rw_unsafe_mutex;
typedef rw_safe_mutex rw_mutex;
struct rw_lock_defered_t {};
@ -205,32 +208,37 @@ namespace ts {
template <typename lock_t>
struct rwshared_lock {
public:
explicit rwshared_lock() {
this->lock_type_ = unlocked;
this->lock_ = nullptr;
}
explicit rwshared_lock(lock_t& lock) : rwshared_lock{lock, rw_lock_shared} {}
explicit rwshared_lock(lock_t& lock, const rw_lock_defered_t&) : lock_{lock} {
explicit rwshared_lock(lock_t& lock, const rw_lock_defered_t&) : lock_{&lock} {
this->lock_type_ = unlocked;
}
explicit rwshared_lock(lock_t& lock, const rw_lock_shared_t&) : lock_{lock} {
this->lock_.lock_shared();
explicit rwshared_lock(lock_t& lock, const rw_lock_shared_t&) : lock_{&lock} {
this->lock_->lock_shared();
this->lock_type_ = locked_shared;
}
explicit rwshared_lock(lock_t& lock, const rw_lock_exclusive_t&) : lock_{lock} {
this->lock_.lock();
explicit rwshared_lock(lock_t& lock, const rw_lock_exclusive_t&) : lock_{&lock} {
this->lock_->lock();
this->lock_type_ = locked_exclusive;
}
~rwshared_lock() {
if(this->lock_type_ == locked_shared) {
this->lock_.unlock_shared();
this->lock_->unlock_shared();
} else if(this->lock_type_ == locked_exclusive) {
this->lock_.unlock();
this->lock_->unlock();
}
}
rwshared_lock(const rwshared_lock&) = delete;
rwshared_lock&operator=(const rwshared_lock&) = delete;
rwshared_lock&operator=(rwshared_lock&&) noexcept = default;
/* state testers */
[[nodiscard]] inline bool exclusive_locked() const {
@ -244,13 +252,13 @@ namespace ts {
/* basic lock functions */
inline void lock_shared() {
rw_mutex_assert(this->lock_type_ == unlocked);
this->lock_.lock_shared();
this->lock_->lock_shared();
this->lock_type_ = locked_shared;
}
[[nodiscard]] inline std::cv_status lock_shared_until(const std::chrono::system_clock::time_point& timeout) {
rw_mutex_assert(this->lock_type_ == unlocked);
if(auto error = this->lock_.lock_shared_until(timeout); error == std::cv_status::timeout)
if(auto error = this->lock_->lock_shared_until(timeout); error == std::cv_status::timeout)
return error;
this->lock_type_ = locked_shared;
return std::cv_status::no_timeout;
@ -258,19 +266,19 @@ namespace ts {
inline void unlock_shared() {
rw_mutex_assert(this->lock_type_ == locked_shared);
this->lock_.unlock_shared();
this->lock_->unlock_shared();
this->lock_type_ = unlocked;
}
inline void lock_exclusive() {
rw_mutex_assert(this->lock_type_ == unlocked);
this->lock_.lock();
this->lock_->lock();
this->lock_type_ = locked_exclusive;
}
[[nodiscard]] inline std::cv_status lock_exclusive_until(const std::chrono::system_clock::time_point& timeout) {
rw_mutex_assert(this->lock_type_ == unlocked);
if(auto error = this->lock_.lock_until(timeout); error == std::cv_status::timeout)
if(auto error = this->lock_->lock_until(timeout); error == std::cv_status::timeout)
return error;
this->lock_type_ = locked_exclusive;
return std::cv_status::no_timeout;
@ -278,14 +286,14 @@ namespace ts {
inline void unlock_exclusive() {
rw_mutex_assert(this->lock_type_ == locked_exclusive);
this->lock_.unlock();
this->lock_->unlock();
this->lock_type_ = unlocked;
}
/* upgrade/downgrade functions */
[[nodiscard]] inline rw_action_result upgrade_lock() {
rw_mutex_assert(this->lock_type_ == locked_shared);
auto err = this->lock_.upgrade_lock();
auto err = this->lock_->upgrade_lock();
if(err != rw_action_result::success) return err;
this->lock_type_ = locked_exclusive;
@ -294,7 +302,7 @@ namespace ts {
[[nodiscard]] inline rw_action_result upgrade_lock_until(const std::chrono::system_clock::time_point& timeout) {
rw_mutex_assert(this->lock_type_ == locked_shared);
auto err = this->lock_.upgrade_lock_until(timeout);
auto err = this->lock_->upgrade_lock_until(timeout);
if(err != rw_action_result::success) return err;
this->lock_type_ = locked_exclusive;
@ -303,7 +311,7 @@ namespace ts {
inline void downgrade_lock() {
rw_mutex_assert(this->lock_type_ == locked_exclusive);
this->lock_.downgrade_lock();
this->lock_->downgrade_lock();
this->lock_type_ = locked_shared;
}
@ -345,6 +353,8 @@ namespace ts {
else if(this->lock_type_ == locked_exclusive)
this->unlock_exclusive();
}
[[nodiscard]] inline auto mutex() { return this->lock_; }
private:
enum {
unlocked,
@ -352,7 +362,7 @@ namespace ts {
locked_exclusive
} lock_type_;
lock_t& lock_;
lock_t* lock_;
};
}
@ -366,9 +376,9 @@ namespace ts {
template <typename options_t, typename action_validator_t>
#ifndef DEBUG_RW_MUTEX
rw_mutex_impl<options_t, action_validator_t>::~rw_mutex_impl() = default;
impl::rw_mutex_impl<options_t, action_validator_t>::~rw_mutex_impl() = default;
#else
rw_mutex_impl<options_t, action_validator_t>::~rw_mutex_impl() {
impl::rw_mutex_impl<options_t, action_validator_t>::~rw_mutex_impl() {
rw_mutex_assert(!this->write_locked);
rw_mutex_assert(!this->upgrade_pending);
rw_mutex_assert(this->write_lock_pending == 0);
@ -378,7 +388,7 @@ namespace ts {
#endif
template <typename options_t, typename action_validator_t>
void rw_mutex_impl<options_t, action_validator_t>::lock() {
void impl::rw_mutex_impl<options_t, action_validator_t>::lock() {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_exclusive_lock_acquire();
@ -404,7 +414,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
std::cv_status rw_mutex_impl<options_t, action_validator_t>::lock_until(const std::chrono::system_clock::time_point& timeout) {
std::cv_status impl::rw_mutex_impl<options_t, action_validator_t>::lock_until(const std::chrono::system_clock::time_point& timeout) {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_exclusive_lock_acquire();
this->write_lock_pending++;
@ -437,7 +447,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
void rw_mutex_impl<options_t, action_validator_t>::lock_shared() {
void impl::rw_mutex_impl<options_t, action_validator_t>::lock_shared() {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_shared_lock_acquire();
this->read_lock_pending++;
@ -450,7 +460,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
std::cv_status rw_mutex_impl<options_t, action_validator_t>::lock_shared_until(const std::chrono::system_clock::time_point& timeout) {
std::cv_status impl::rw_mutex_impl<options_t, action_validator_t>::lock_shared_until(const std::chrono::system_clock::time_point& timeout) {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_shared_lock_acquire();
this->read_lock_pending++;
@ -467,7 +477,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
void rw_mutex_impl<options_t, action_validator_t>::unlock() {
void impl::rw_mutex_impl<options_t, action_validator_t>::unlock() {
std::lock_guard slock{this->status_lock_};
this->action_validator_.try_exclusive_lock_release();
rw_mutex_assert(this->write_locked);
@ -496,7 +506,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
void rw_mutex_impl<options_t, action_validator_t>::unlock_shared() {
void impl::rw_mutex_impl<options_t, action_validator_t>::unlock_shared() {
std::lock_guard slock{this->status_lock_};
this->action_validator_.try_shared_lock_release();
rw_mutex_assert(!this->write_locked);
@ -520,7 +530,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
rw_action_result rw_mutex_impl<options_t, action_validator_t>::upgrade_lock() {
rw_action_result impl::rw_mutex_impl<options_t, action_validator_t>::upgrade_lock() {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_shared_lock_upgrade();
rw_mutex_assert(!this->write_locked);
@ -549,7 +559,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
rw_action_result rw_mutex_impl<options_t, action_validator_t>::upgrade_lock_until(const std::chrono::system_clock::time_point& timeout) {
rw_action_result impl::rw_mutex_impl<options_t, action_validator_t>::upgrade_lock_until(const std::chrono::system_clock::time_point& timeout) {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_shared_lock_upgrade();
rw_mutex_assert(!this->write_locked);
@ -581,7 +591,7 @@ namespace ts {
}
template <typename options_t, typename action_validator_t>
void rw_mutex_impl<options_t, action_validator_t>::downgrade_lock() {
void impl::rw_mutex_impl<options_t, action_validator_t>::downgrade_lock() {
std::unique_lock slock{this->status_lock_};
this->action_validator_.try_exclusive_lock_downgrade();
rw_mutex_assert(this->write_locked);
@ -600,16 +610,16 @@ namespace ts {
/* action validator */
/* shared lock part */
void mutex_action_validator::try_shared_lock_acquire() {
void impl::mutex_action_validator::try_shared_lock_acquire() {
if(this->exclusive_lock == std::this_thread::get_id())
throw std::logic_error{"mutex has been locked in exclusive lock by current thread"};
}
void mutex_action_validator::shared_lock_acquire() {
void impl::mutex_action_validator::shared_lock_acquire() {
this->shared_lockers.push_back(std::this_thread::get_id());
}
void mutex_action_validator::try_shared_lock_upgrade() {
void impl::mutex_action_validator::try_shared_lock_upgrade() {
if(this->exclusive_lock == std::this_thread::get_id())
throw std::logic_error{"mutex has been upgraded by current thread"};
@ -624,12 +634,12 @@ namespace ts {
throw std::logic_error{"upgrade not possible because shared mutex is locked more than once by current thread"};
}
void mutex_action_validator::shared_lock_upgrade() {
void impl::mutex_action_validator::shared_lock_upgrade() {
this->exclusive_lock_upgraded = true;
this->exclusive_lock = std::this_thread::get_id();
}
void mutex_action_validator::try_shared_lock_release() {
void impl::mutex_action_validator::try_shared_lock_release() {
if(this->exclusive_lock == std::this_thread::get_id())
throw std::logic_error{this->exclusive_lock_upgraded ? "mutex has been upgraded" : "mutex has been locked in exclusive mode, not in shared mode"};
@ -638,29 +648,29 @@ namespace ts {
throw std::logic_error{"mutex not locked by current thread"};
}
void mutex_action_validator::shared_lock_release() {
void impl::mutex_action_validator::shared_lock_release() {
auto it = std::find(this->shared_lockers.begin(), this->shared_lockers.end(), std::this_thread::get_id());
rw_mutex_assert(it != this->shared_lockers.end()); /* this should never happen (try_shared_lock_release has been called before) */
this->shared_lockers.erase(it);
}
/* exclusive lock part */
void mutex_action_validator::try_exclusive_lock_acquire() {
void impl::mutex_action_validator::try_exclusive_lock_acquire() {
if(this->exclusive_lock == std::this_thread::get_id())
throw std::logic_error{"mutex has been exclusive locked by current thread"};
}
void mutex_action_validator::exclusive_lock_acquire() {
void impl::mutex_action_validator::exclusive_lock_acquire() {
this->exclusive_lock = std::this_thread::get_id();
this->exclusive_lock_upgraded = false;
}
void mutex_action_validator::try_exclusive_lock_downgrade() {
void impl::mutex_action_validator::try_exclusive_lock_downgrade() {
if(this->exclusive_lock != std::this_thread::get_id())
throw std::logic_error{"mutex hasn't been locked in exclusive mode by this thread"};
}
void mutex_action_validator::exclusive_lock_downgrade() {
void impl::mutex_action_validator::exclusive_lock_downgrade() {
if(!this->exclusive_lock_upgraded) {
this->shared_lockers.push_back(std::this_thread::get_id());
} else {
@ -672,12 +682,12 @@ namespace ts {
this->exclusive_lock = std::thread::id{};
}
void mutex_action_validator::try_exclusive_lock_release() {
void impl::mutex_action_validator::try_exclusive_lock_release() {
if(this->exclusive_lock != std::this_thread::get_id())
throw std::logic_error{"mutex hasn't been locked in exclusive mode by this thread"};
}
void mutex_action_validator::exclusive_lock_release() {
void impl::mutex_action_validator::exclusive_lock_release() {
if(this->exclusive_lock_upgraded) {
auto it = std::find(this->shared_lockers.begin(), this->shared_lockers.end(), std::this_thread::get_id());
assert(it != this->shared_lockers.end());

View File

@ -77,7 +77,7 @@ bool CryptHandler::setupSharedSecret(const std::string& alpha, const std::string
return true;
}
void _fe_neg(fe h, const fe f) {
inline void _fe_neg(fe h, const fe f) {
int32_t f0 = f[0];
int32_t f1 = f[1];
int32_t f2 = f[2];