A lot of query reworking
This commit is contained in:
parent
eb77a7fefb
commit
0cd49a6a2b
119
src/EventLoop.h
119
src/EventLoop.h
@ -7,84 +7,83 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
|
||||||
namespace ts {
|
namespace ts::event {
|
||||||
namespace event {
|
class EventExecutor;
|
||||||
class EventExecutor;
|
|
||||||
|
|
||||||
class EventEntry {
|
class EventEntry {
|
||||||
friend class EventExecutor;
|
friend class EventExecutor;
|
||||||
public:
|
public:
|
||||||
virtual void event_execute(const std::chrono::system_clock::time_point& /* scheduled timestamp */) = 0;
|
virtual void event_execute(const std::chrono::system_clock::time_point& /* scheduled timestamp */) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void* _event_ptr = nullptr;
|
void* _event_ptr = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename class_t>
|
template <typename class_t>
|
||||||
class ProxiedEventEntry : public event::EventEntry {
|
class ProxiedEventEntry : public event::EventEntry {
|
||||||
public:
|
public:
|
||||||
using callback_t = void(class_t::*)(const std::chrono::system_clock::time_point &);
|
using callback_t = void(class_t::*)(const std::chrono::system_clock::time_point &);
|
||||||
using static_callback_t = void(*)(class_t *, const std::chrono::system_clock::time_point &);
|
using static_callback_t = void(*)(class_t *, const std::chrono::system_clock::time_point &);
|
||||||
|
|
||||||
ProxiedEventEntry(const std::shared_ptr<class_t>& _instance, callback_t callback) : instance(_instance), callback(callback) { }
|
ProxiedEventEntry(const std::shared_ptr<class_t>& _instance, callback_t callback) : instance(_instance), callback(callback) { }
|
||||||
|
|
||||||
std::weak_ptr<class_t> instance;
|
std::weak_ptr<class_t> instance;
|
||||||
callback_t callback;
|
callback_t callback;
|
||||||
|
|
||||||
void event_execute(const std::chrono::system_clock::time_point &point) override {
|
void event_execute(const std::chrono::system_clock::time_point &point) override {
|
||||||
auto _instance = this->instance.lock();
|
auto _instance = this->instance.lock();
|
||||||
if(!_instance)
|
if(!_instance) {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto callback_ptr = (void**) &this->callback;
|
|
||||||
(*(static_callback_t*) callback_ptr)(&*_instance, point);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
class EventExecutor {
|
auto callback_ptr = (void**) &this->callback;
|
||||||
public:
|
(*(static_callback_t*) callback_ptr)(&*_instance, point);
|
||||||
explicit EventExecutor(std::string /* thread prefix */);
|
}
|
||||||
virtual ~EventExecutor();
|
};
|
||||||
|
|
||||||
bool initialize(int /* num threads */);
|
class EventExecutor {
|
||||||
bool schedule(const std::shared_ptr<EventEntry>& /* entry */);
|
public:
|
||||||
bool cancel(const std::shared_ptr<EventEntry>& /* entry */); /* Note: Will not cancel already running executes */
|
explicit EventExecutor(std::string /* thread prefix */);
|
||||||
void shutdown();
|
virtual ~EventExecutor();
|
||||||
|
|
||||||
inline const std::string& thread_prefix() const { return this->_thread_prefix; }
|
bool initialize(int /* num threads */);
|
||||||
|
bool schedule(const std::shared_ptr<EventEntry>& /* entry */);
|
||||||
|
bool cancel(const std::shared_ptr<EventEntry>& /* entry */); /* Note: Will not cancel already running executes */
|
||||||
|
void shutdown();
|
||||||
|
|
||||||
void threads(int /* num threads */);
|
inline const std::string& thread_prefix() const { return this->_thread_prefix; }
|
||||||
inline int threads() const { return this->target_threads; }
|
|
||||||
private:
|
|
||||||
struct LinkedEntry {
|
|
||||||
LinkedEntry* previous;
|
|
||||||
LinkedEntry* next;
|
|
||||||
|
|
||||||
std::chrono::system_clock::time_point scheduled;
|
void threads(int /* num threads */);
|
||||||
std::weak_ptr<EventEntry> entry;
|
inline int threads() const { return this->target_threads; }
|
||||||
};
|
private:
|
||||||
|
struct LinkedEntry {
|
||||||
|
LinkedEntry* previous;
|
||||||
|
LinkedEntry* next;
|
||||||
|
|
||||||
static void _executor(EventExecutor*);
|
std::chrono::system_clock::time_point scheduled;
|
||||||
void _spawn_executor(std::unique_lock<std::mutex>&);
|
std::weak_ptr<EventEntry> entry;
|
||||||
void _shutdown(std::unique_lock<std::mutex>&);
|
};
|
||||||
void _reset_events(std::unique_lock<std::mutex>&);
|
|
||||||
|
static void _executor(EventExecutor*);
|
||||||
|
void _spawn_executor(std::unique_lock<std::mutex>&);
|
||||||
|
void _shutdown(std::unique_lock<std::mutex>&);
|
||||||
|
void _reset_events(std::unique_lock<std::mutex>&);
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
void _reassign_thread_names(std::unique_lock<std::mutex>&);
|
void _reassign_thread_names(std::unique_lock<std::mutex>&);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool should_shutdown = true;
|
bool should_shutdown = true;
|
||||||
bool should_adjust = false; /* thread adjustments */
|
bool should_adjust = false; /* thread adjustments */
|
||||||
int target_threads = 0;
|
int target_threads = 0;
|
||||||
|
|
||||||
std::vector<std::thread> _threads;
|
std::vector<std::thread> _threads;
|
||||||
std::mutex lock;
|
std::mutex lock;
|
||||||
std::condition_variable condition;
|
std::condition_variable condition;
|
||||||
|
|
||||||
LinkedEntry* head = nullptr;
|
LinkedEntry* head = nullptr;
|
||||||
LinkedEntry* tail = nullptr;
|
LinkedEntry* tail = nullptr;
|
||||||
|
|
||||||
std::string _thread_prefix;
|
std::string _thread_prefix;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
@ -111,9 +111,6 @@ namespace ts {
|
|||||||
this->memory_state.id_branded = true;
|
this->memory_state.id_branded = true;
|
||||||
this->setPacketId(packetId, generationId);
|
this->setPacketId(packetId, generationId);
|
||||||
}
|
}
|
||||||
Command BasicPacket::asCommand() {
|
|
||||||
return Command::parse(this->data());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param buffer -> [mac][Header [uint16 BE packetId | [uint8](4bit flags | 4bit type)]][Data]
|
* @param buffer -> [mac][Header [uint16 BE packetId | [uint8](4bit flags | 4bit type)]][Data]
|
||||||
|
@ -78,39 +78,37 @@ namespace ts {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Command Command::parse(const pipes::buffer_view &buffer, bool expect_type, bool drop_non_utf8) {
|
Command Command::parse(const std::string_view& command_data, bool expect_type, bool drop_non_utf8) {
|
||||||
string_view data{buffer.data_ptr<const char>(), buffer.length()};
|
|
||||||
|
|
||||||
Command result;
|
Command result;
|
||||||
|
|
||||||
size_t current_index = std::string::npos, end_index;
|
size_t current_index = std::string::npos, end_index;
|
||||||
if(expect_type) {
|
if(expect_type) {
|
||||||
current_index = data.find(' ', 0);
|
current_index = command_data.find(' ', 0);
|
||||||
if(current_index == std::string::npos){
|
if(current_index == std::string::npos){
|
||||||
result._command = std::string(data);
|
result._command = std::string{command_data};
|
||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
result._command = std::string(data.substr(0, current_index));
|
result._command = std::string{command_data.substr(0, current_index)};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t bulk_index = 0;
|
size_t bulk_index = 0;
|
||||||
while(++current_index > 0 || (current_index == 0 && !expect_type && (expect_type = true))) {
|
while(++current_index > 0 || (current_index == 0 && !expect_type && (expect_type = true))) {
|
||||||
end_index = data.find_first_of(" |", current_index);
|
end_index = command_data.find_first_of(" |", current_index);
|
||||||
|
|
||||||
if(end_index != current_index && current_index < data.length()) { /* else we've found another space or a pipe */
|
if(end_index != current_index && current_index < command_data.length()) { /* else we've found another space or a pipe */
|
||||||
if(data[current_index] == '-') {
|
if(command_data[current_index] == '-') {
|
||||||
string trigger(data.substr(current_index + 1, end_index - current_index - 1));
|
string trigger(command_data.substr(current_index + 1, end_index - current_index - 1));
|
||||||
result.paramethers.push_back(trigger);
|
result.paramethers.push_back(trigger);
|
||||||
} else {
|
} else {
|
||||||
auto index_assign = data.find_first_of('=', current_index);
|
auto index_assign = command_data.find_first_of('=', current_index);
|
||||||
string key, value;
|
string key, value;
|
||||||
if(index_assign == string::npos || index_assign > end_index) {
|
if(index_assign == string::npos || index_assign > end_index) {
|
||||||
key = data.substr(current_index, end_index - current_index);
|
key = command_data.substr(current_index, end_index - current_index);
|
||||||
} else {
|
} else {
|
||||||
key = data.substr(current_index, index_assign - current_index);
|
key = command_data.substr(current_index, index_assign - current_index);
|
||||||
try {
|
try {
|
||||||
value = query::unescape(string(data.substr(index_assign + 1, end_index - index_assign - 1)), true);
|
value = query::unescape(string(command_data.substr(index_assign + 1, end_index - index_assign - 1)), true);
|
||||||
} catch(const std::invalid_argument& ex) {
|
} catch(const std::invalid_argument& ex) {
|
||||||
(void) ex;
|
(void) ex;
|
||||||
|
|
||||||
@ -134,7 +132,7 @@ namespace ts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(end_index < data.length() && data[end_index] == '|')
|
if(end_index < command_data.length() && command_data[end_index] == '|')
|
||||||
bulk_index++;
|
bulk_index++;
|
||||||
current_index = end_index;
|
current_index = end_index;
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ operator type(){ \
|
|||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
static Command parse(const pipes::buffer_view& buffer, bool expect_command = true, bool drop_non_utf8 = false);
|
static Command parse(const std::string_view& command_data, bool expect_command = true, bool drop_non_utf8 = false);
|
||||||
|
|
||||||
explicit Command(const std::string& command);
|
explicit Command(const std::string& command);
|
||||||
explicit Command(const std::string& command, std::initializer_list<variable>);
|
explicit Command(const std::string& command, std::initializer_list<variable>);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user