cmake: Further refactoring and formatting
This commit is contained in:
+4
-4
@@ -1,6 +1,6 @@
|
||||
# Basic Formatting
|
||||
TabWidth: 8
|
||||
UseTab: ForIndentation
|
||||
TabWidth: 4
|
||||
UseTab: ForContinuationAndIndentation
|
||||
ColumnLimit: 120
|
||||
|
||||
# Language
|
||||
@@ -13,7 +13,7 @@ ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
IndentCaseLabels: false
|
||||
#IndentPPDirectives: true
|
||||
IndentWidth: 8
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: true
|
||||
NamespaceIndentation: All
|
||||
|
||||
@@ -30,7 +30,7 @@ SortIncludes: true
|
||||
AlignAfterOpenBracket: true
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignConsecutiveDeclarations: true
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: true
|
||||
DerivePointerAlignment: false
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
# top-most EditorConfig file
|
||||
root = true
|
||||
|
||||
# Unix-style newlines with a newline ending every file.
|
||||
[*]
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
charset = utf-8
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
+70
-54
@@ -91,25 +91,26 @@ endif()
|
||||
# Options
|
||||
#================================================================================#
|
||||
# Static or Dynamic?
|
||||
option(${PropertyPrefix}MAKE_STATIC "Make Static Library" ON)
|
||||
option(${PropertyPrefix}MAKE_DYNAMIC "Make Dynamic Library" OFF)
|
||||
option(${PropertyPrefix}MAKE_MODULE "Make Module Library" OFF)
|
||||
option(${PropertyPrefix}MAKE_DYNAMIC "Create dynamically linked library instead of static library." OFF)
|
||||
option(${PropertyPrefix}MAKE_MODULE "Create dynamically linked module instead of dynamically linked library." OFF)
|
||||
option(${PropertyPrefix}BUILD_SAMPLES "Build Samples" ON)
|
||||
|
||||
#================================================================================#
|
||||
# Sources
|
||||
option(${PropertyPrefix}BUILD_SAMPLES "Build Samples" ON)
|
||||
#================================================================================#
|
||||
|
||||
# Configure Version Header
|
||||
configure_file(
|
||||
"${PROJECT_SOURCE_DIR}/cmake/version.hpp.in"
|
||||
"${PROJECT_BINARY_DIR}/include/version.hpp"
|
||||
"${PROJECT_BINARY_DIR}/generated/version.hpp"
|
||||
)
|
||||
|
||||
# Public (exported with module)
|
||||
set(PROJECT_PUBLIC
|
||||
"include/bitmask.hpp"
|
||||
# Source Files
|
||||
set(PROJECT_PUBLIC "")
|
||||
list(APPEND PROJECT_PUBLIC
|
||||
"include/datapath.hpp"
|
||||
"include/error.hpp"
|
||||
"include/bitmask.hpp"
|
||||
"include/event.hpp"
|
||||
"include/isocket.hpp"
|
||||
"include/iserver.hpp"
|
||||
@@ -119,27 +120,30 @@ set(PROJECT_PUBLIC
|
||||
"include/threadpool.hpp"
|
||||
)
|
||||
|
||||
set(PROJECT_PUBLIC_GENERATED
|
||||
"${PROJECT_BINARY_DIR}/include/version.hpp"
|
||||
set(PROJECT_PRIVATE "")
|
||||
list(APPEND PROJECT_PRIVATE
|
||||
"source/threadpool.cpp"
|
||||
)
|
||||
|
||||
set(PROJECT_DATA
|
||||
set(PROJECT_TEMPLATES "")
|
||||
list(APPEND PROJECT_TEMPLATES
|
||||
"${PROJECT_SOURCE_DIR}/cmake/version.hpp.in"
|
||||
)
|
||||
|
||||
set(PROJECT_GENERATED "")
|
||||
list(APPEND PROJECT_GENERATED
|
||||
"${PROJECT_BINARY_DIR}/generated/version.hpp"
|
||||
)
|
||||
|
||||
set(PROJECT_DATA "")
|
||||
list(APPEND PROJECT_DATA
|
||||
"README.md"
|
||||
"LICENSE"
|
||||
)
|
||||
|
||||
# Private (only compiled/used locally)
|
||||
set(PROJECT_PRIVATE
|
||||
"source/threadpool.cpp"
|
||||
)
|
||||
set(PROJECT_LIBRARIES "")
|
||||
|
||||
# Libraries
|
||||
set(PROJECT_LIBRARIES
|
||||
)
|
||||
|
||||
# Defines
|
||||
set(PROJECT_DEFINES
|
||||
)
|
||||
set(PROJECT_DEFINES "")
|
||||
|
||||
# Platforms
|
||||
if(WIN32)
|
||||
@@ -225,45 +229,58 @@ elseif("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")
|
||||
endif()
|
||||
|
||||
# Grouping
|
||||
source_group("Data Files" FILES $PROJECT_DATA)
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/source" PREFIX "Source" FILES ${PROJECT_PRIVATE})
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/include" PREFIX "Include" FILES ${PROJECT_PUBLIC})
|
||||
source_group(TREE "${PROJECT_BINARY_DIR}" PREFIX "Generated" FILES ${PROJECT_PUBLIC_GENERATED})
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}" PREFIX "Data Files" FILES ${PROJECT_DATA})
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/cmake" PREFIX "Template Files" FILES ${PROJECT_TEMPLATES})
|
||||
source_group(TREE "${PROJECT_BINARY_DIR}/generated" PREFIX "Generated Files" FILES ${PROJECT_GENERATED})
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/include" PREFIX "Exported Files" FILES ${PROJECT_PUBLIC})
|
||||
|
||||
# Filter Sources
|
||||
set(_TMP_SOURCE ${PROJECT_PRIVATE})
|
||||
list(FILTER _TMP_SOURCE INCLUDE REGEX "\.(c|cpp)$")
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/source" PREFIX "Source Files" FILES ${_TMP_SOURCE})
|
||||
|
||||
# Filter Headers
|
||||
set(_TMP_HEADER ${PROJECT_PRIVATE})
|
||||
list(FILTER _TMP_HEADER INCLUDE REGEX "\.(h|hpp)$")
|
||||
source_group(TREE "${PROJECT_SOURCE_DIR}/source" PREFIX "Header Files" FILES ${_TMP_HEADER})
|
||||
|
||||
#================================================================================#
|
||||
# Building
|
||||
#================================================================================#
|
||||
|
||||
# Library definition
|
||||
if(${PropertyPrefix}MAKE_STATIC)
|
||||
add_library(${PROJECT_NAME} STATIC
|
||||
${PROJECT_PRIVATE}
|
||||
${PROJECT_PUBLIC}
|
||||
${PROJECT_PUBLIC_GENERATED}
|
||||
${PROJECT_DATA}
|
||||
)
|
||||
elseif(${PropertyPrefix}MAKE_DYNAMIC)
|
||||
add_library(${PROJECT_NAME} SHARED
|
||||
${PROJECT_PRIVATE}
|
||||
${PROJECT_PUBLIC}
|
||||
${PROJECT_PUBLIC_GENERATED}
|
||||
${PROJECT_DATA}
|
||||
)
|
||||
elseif(${PropertyPrefix}MAKE_MODULE)
|
||||
add_library(${PROJECT_NAME} MODULE
|
||||
${PROJECT_PRIVATE}
|
||||
${PROJECT_PUBLIC}
|
||||
${PROJECT_PUBLIC_GENERATED}
|
||||
${PROJECT_DATA}
|
||||
)
|
||||
set(_BUILD_TYPE)
|
||||
if(${PropertyPrefix}MAKE_DYNAMIC)
|
||||
if(${PropertyPrefix}MAKE_MODULE)
|
||||
set(_BUILD_TYPE MODULE)
|
||||
else()
|
||||
message(CRITICAL "Building nothing completed, aborting. Check MAKE_STATIC, MAKE_DYNAMIC and MAKE_DYNAMIC.")
|
||||
return()
|
||||
set(_BUILD_TYPE SHARED)
|
||||
endif()
|
||||
else()
|
||||
set(_BUILD_TYPE STATIC)
|
||||
endif()
|
||||
|
||||
add_library(${PROJECT_NAME} ${_BUILD_TYPE}
|
||||
${PROJECT_PUBLIC}
|
||||
${PROJECT_PRIVATE}
|
||||
${PROJECT_GENERATED}
|
||||
${PROJECT_TEMPLATES}
|
||||
${PROJECT_DATA}
|
||||
)
|
||||
|
||||
# Clang
|
||||
if("${PropertyPrefix}" STREQUAL "")
|
||||
clang_format(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
DEPENDENCY
|
||||
VERSION 9.0.0
|
||||
)
|
||||
endif()
|
||||
|
||||
# Includes
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE source
|
||||
PUBLIC include
|
||||
PRIVATE "source"
|
||||
PUBLIC "include"
|
||||
)
|
||||
|
||||
# Defines
|
||||
@@ -297,10 +314,9 @@ else()
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
################################################################################
|
||||
#================================================================================#
|
||||
# Samples
|
||||
################################################################################
|
||||
#================================================================================#
|
||||
if(${PropertyPrefix}BUILD_SAMPLES)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/samples)
|
||||
endif()
|
||||
|
||||
+17
-17
@@ -24,10 +24,10 @@
|
||||
namespace datapath {
|
||||
template<typename... _args>
|
||||
class event {
|
||||
std::list<std::function<void(_args...)>> listeners;
|
||||
std::list<std::function<void(_args...)>> _listeners;
|
||||
|
||||
std::function<void()> listen_cb;
|
||||
std::function<void()> silence_cb;
|
||||
std::function<void()> _listen_cb;
|
||||
std::function<void()> _silence_cb;
|
||||
|
||||
public /* functions */:
|
||||
|
||||
@@ -40,20 +40,20 @@ namespace datapath {
|
||||
// Add new listener.
|
||||
inline void add(std::function<void(_args...)> listener)
|
||||
{
|
||||
if (listeners.size() == 0) {
|
||||
if (listen_cb) {
|
||||
listen_cb();
|
||||
if (_listeners.size() == 0) {
|
||||
if (_listen_cb) {
|
||||
_listen_cb();
|
||||
}
|
||||
}
|
||||
listeners.push_back(listener);
|
||||
_listeners.push_back(listener);
|
||||
}
|
||||
|
||||
// Remove existing listener.
|
||||
inline void remove(std::function<void(_args...)> listener)
|
||||
{
|
||||
listeners.remove(listener);
|
||||
if (listeners.size() == 0) {
|
||||
if (silence_cb) {
|
||||
_listeners.remove(listener);
|
||||
if (_listeners.size() == 0) {
|
||||
if (_silence_cb) {
|
||||
silence_cb();
|
||||
}
|
||||
}
|
||||
@@ -62,15 +62,15 @@ namespace datapath {
|
||||
// Check if empty / no listeners.
|
||||
inline bool empty()
|
||||
{
|
||||
return listeners.empty();
|
||||
return _listeners.empty();
|
||||
}
|
||||
|
||||
// Remove all listeners.
|
||||
inline void clear()
|
||||
{
|
||||
listeners.clear();
|
||||
if (silence_cb) {
|
||||
silence_cb();
|
||||
_listeners.clear();
|
||||
if (_silence_cb) {
|
||||
_silence_cb();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace datapath {
|
||||
template<typename... _largs>
|
||||
inline void operator()(_args... args)
|
||||
{
|
||||
for (auto& l : listeners) {
|
||||
for (auto& l : _listeners) {
|
||||
l(args...);
|
||||
}
|
||||
}
|
||||
@@ -108,12 +108,12 @@ namespace datapath {
|
||||
public /* events */:
|
||||
void set_listen_callback(std::function<void()> cb)
|
||||
{
|
||||
this->listen_cb = cb;
|
||||
this->_listen_cb = cb;
|
||||
}
|
||||
|
||||
void set_silence_callback(std::function<void()> cb)
|
||||
{
|
||||
this->silence_cb = cb;
|
||||
this->_silence_cb = cb;
|
||||
}
|
||||
};
|
||||
}; // namespace datapath
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ namespace datapath {
|
||||
* @param std::shared_ptr<datapath::isocket> Socket.
|
||||
* @return void
|
||||
*/
|
||||
datapath::event<bool&, std::shared_ptr<datapath::isocket>> on_accept;
|
||||
datapath::event<bool&, std::shared_ptr<datapath::isocket>> _on_accept;
|
||||
|
||||
public:
|
||||
virtual datapath::error close() = 0;
|
||||
|
||||
+2
-2
@@ -24,9 +24,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
namespace datapath {
|
||||
class isocket {
|
||||
public /*events*/:
|
||||
datapath::event<const std::vector<char>&> on_message;
|
||||
datapath::event<const std::vector<char>&> _on_message;
|
||||
|
||||
datapath::event<> on_close;
|
||||
datapath::event<> _on_close;
|
||||
|
||||
public:
|
||||
virtual bool good() = 0;
|
||||
|
||||
+2
-2
@@ -26,9 +26,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
namespace datapath {
|
||||
class itask : public waitable {
|
||||
public /*event*/:
|
||||
datapath::event<datapath::error> on_failure;
|
||||
datapath::event<datapath::error> _on_failure;
|
||||
|
||||
datapath::event<datapath::error, const std::vector<char>&> on_success;
|
||||
datapath::event<datapath::error, const std::vector<char>&> _on_success;
|
||||
|
||||
public:
|
||||
virtual datapath::error cancel() = 0;
|
||||
|
||||
@@ -21,12 +21,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#include "bitmask.hpp"
|
||||
|
||||
namespace datapath {
|
||||
enum class permissions : int8_t {
|
||||
None,
|
||||
User,
|
||||
Group,
|
||||
World,
|
||||
Reserved
|
||||
};
|
||||
enum class permissions : int8_t { None, User, Group, World, Reserved };
|
||||
ENABLE_BITMASK_OPERATORS(datapath::permissions);
|
||||
} // namespace datapath
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
@@ -58,7 +59,7 @@ namespace datapath {
|
||||
void push(std::shared_ptr<task> task);
|
||||
};
|
||||
|
||||
std::map<affinity_t, std::shared_ptr<worker>> workers;
|
||||
std::map<affinity_t, std::shared_ptr<worker>> _workers;
|
||||
|
||||
public:
|
||||
pool();
|
||||
|
||||
@@ -25,9 +25,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
namespace datapath {
|
||||
class waitable {
|
||||
public /*events*/:
|
||||
datapath::event<datapath::error> on_wait_error;
|
||||
datapath::event<datapath::error> _on_wait_error;
|
||||
|
||||
datapath::event<datapath::error> on_wait_success;
|
||||
datapath::event<datapath::error> _on_wait_success;
|
||||
|
||||
public:
|
||||
virtual void* get_waitable() = 0;
|
||||
|
||||
@@ -65,8 +65,7 @@ void datapath::threadpool::pool::worker::runner()
|
||||
my_task.reset();
|
||||
}
|
||||
if (this->queue.size() == 0) {
|
||||
this->signal.wait(slock,
|
||||
[this]() { return (this->should_stop) || (this->queue.size() > 0); });
|
||||
this->signal.wait(slock, [this]() { return (this->should_stop) || (this->queue.size() > 0); });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,13 +93,13 @@ datapath::threadpool::pool::pool()
|
||||
uint64_t num_hw_concurrency = std::thread::hardware_concurrency();
|
||||
for (uint64_t idx = 0; idx < num_hw_concurrency; idx++) {
|
||||
auto worker = std::make_shared<datapath::threadpool::pool::worker>(1 << idx);
|
||||
this->workers.insert({idx, worker});
|
||||
this->_workers.insert({idx, worker});
|
||||
}
|
||||
}
|
||||
|
||||
datapath::threadpool::pool::~pool()
|
||||
{
|
||||
this->workers.clear();
|
||||
this->_workers.clear();
|
||||
}
|
||||
|
||||
bool datapath::threadpool::pool::push(std::shared_ptr<task> task)
|
||||
@@ -114,13 +113,13 @@ bool datapath::threadpool::pool::push(std::shared_ptr<task> task)
|
||||
throw std::invalid_argument("task->function must not be nullptr");
|
||||
}
|
||||
/// Check for invalid affinity masks.
|
||||
if ((task->mask & (this->workers.size() - 1)) == 0) {
|
||||
if ((task->mask & (this->_workers.size() - 1)) == 0) {
|
||||
throw std::invalid_argument("mask does not fit any thread");
|
||||
}
|
||||
|
||||
affinity_t lowest_id;
|
||||
size_t lowest_count = std::numeric_limits<size_t>::max();
|
||||
for (auto kv : workers) {
|
||||
for (auto kv : _workers) {
|
||||
if ((kv.second->affinity & task->mask) == 0) {
|
||||
continue;
|
||||
}
|
||||
@@ -135,18 +134,18 @@ bool datapath::threadpool::pool::push(std::shared_ptr<task> task)
|
||||
return false;
|
||||
}
|
||||
|
||||
this->workers[lowest_id]->push(task);
|
||||
this->_workers[lowest_id]->push(task);
|
||||
return true;
|
||||
}
|
||||
|
||||
void datapath::threadpool::pool::clear(affinity_t mask)
|
||||
{
|
||||
// Early-Exit tests.
|
||||
if ((mask & (this->workers.size() - 1)) == 0) {
|
||||
if ((mask & (this->_workers.size() - 1)) == 0) {
|
||||
throw std::invalid_argument("mask does not fit any thread");
|
||||
}
|
||||
|
||||
for (auto kv : workers) {
|
||||
for (auto kv : _workers) {
|
||||
if ((kv.second->affinity & mask) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
+10
-15
@@ -84,8 +84,8 @@ HANDLE datapath::windows::server::_create_socket(std::string path, bool initial)
|
||||
|
||||
DWORD pipe_flags = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT;
|
||||
|
||||
HANDLE handle = CreateNamedPipeW(wpath.c_str(), file_flags, pipe_flags, PIPE_UNLIMITED_INSTANCES,
|
||||
WIN_BUFFER_SIZE, WIN_BUFFER_SIZE, WIN_WAIT_TIME, &this->security_attributes);
|
||||
HANDLE handle = CreateNamedPipeW(wpath.c_str(), file_flags, pipe_flags, PIPE_UNLIMITED_INSTANCES, WIN_BUFFER_SIZE,
|
||||
WIN_BUFFER_SIZE, WIN_WAIT_TIME, &this->security_attributes);
|
||||
return handle;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ void datapath::windows::server::_watcher()
|
||||
auto ov = std::make_shared<datapath::windows::overlapped>();
|
||||
ov->set_handle(handle);
|
||||
ov->set_data(this);
|
||||
ov->on_wait_success.add([this, &ovmap, &itr, &handle](datapath::error ec) {
|
||||
ov->_on_wait_success.add([this, &ovmap, &itr, &handle](datapath::error ec) {
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
this->waiting_sockets.erase(itr);
|
||||
this->pending_sockets.push_back(handle);
|
||||
@@ -152,9 +152,8 @@ void datapath::windows::server::_watcher()
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
std::list<HANDLE> to_kill;
|
||||
|
||||
if (this->on_accept) {
|
||||
for (auto itr = this->pending_sockets.begin(); itr != this->pending_sockets.end();
|
||||
itr++) {
|
||||
if (this->_on_accept) {
|
||||
for (auto itr = this->pending_sockets.begin(); itr != this->pending_sockets.end(); itr++) {
|
||||
HANDLE handle = *itr;
|
||||
bool accept = true;
|
||||
|
||||
@@ -162,16 +161,14 @@ void datapath::windows::server::_watcher()
|
||||
sock->_connect(handle);
|
||||
|
||||
auto isock = std::dynamic_pointer_cast<datapath::isocket>(sock);
|
||||
this->on_accept(accept, isock);
|
||||
this->_on_accept(accept, isock);
|
||||
|
||||
if (accept) {
|
||||
to_kill.push_back(handle);
|
||||
this->active_sockets.insert({handle, sock});
|
||||
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size())
|
||||
< WIN_BACKLOG_NUM) {
|
||||
if ((this->sockets.size() <= this->max_clients)
|
||||
&& (this->max_clients > 0)) {
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size()) < WIN_BACKLOG_NUM) {
|
||||
if ((this->sockets.size() <= this->max_clients) && (this->max_clients > 0)) {
|
||||
HANDLE handle = _create_socket(this->path, false);
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
this->sockets.push_back(handle);
|
||||
@@ -199,8 +196,7 @@ void datapath::windows::server::_watcher()
|
||||
for (auto itr = this->active_sockets.begin(); itr != this->active_sockets.end(); itr++) {
|
||||
if (itr->second.expired()) {
|
||||
// Enforce backlog size
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size())
|
||||
< WIN_BACKLOG_NUM) {
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size()) < WIN_BACKLOG_NUM) {
|
||||
this->waiting_sockets.push_back(itr->first);
|
||||
} else {
|
||||
DisconnectNamedPipe(itr->first);
|
||||
@@ -213,8 +209,7 @@ void datapath::windows::server::_watcher()
|
||||
auto obj = itr->second.lock();
|
||||
if (!obj->good()) {
|
||||
// Enforce backlog size
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size())
|
||||
< WIN_BACKLOG_NUM) {
|
||||
if ((this->waiting_sockets.size() + this->pending_sockets.size()) < WIN_BACKLOG_NUM) {
|
||||
this->waiting_sockets.push_back(itr->first);
|
||||
} else {
|
||||
DisconnectNamedPipe(itr->first);
|
||||
|
||||
+13
-17
@@ -39,8 +39,8 @@ void datapath::windows::socket::_connect(HANDLE handle)
|
||||
|
||||
void datapath::windows::socket::_disconnect()
|
||||
{
|
||||
if (this->on_close) {
|
||||
this->on_close();
|
||||
if (this->_on_close) {
|
||||
this->_on_close();
|
||||
}
|
||||
|
||||
{
|
||||
@@ -62,19 +62,16 @@ void datapath::windows::socket::_watcher()
|
||||
|
||||
std::vector<char> read_buffer;
|
||||
|
||||
std::shared_ptr<datapath::windows::overlapped> read_header_ov =
|
||||
std::make_shared<datapath::windows::overlapped>();
|
||||
std::shared_ptr<datapath::windows::overlapped> read_content_ov =
|
||||
std::make_shared<datapath::windows::overlapped>();
|
||||
std::shared_ptr<datapath::windows::overlapped> read_header_ov = std::make_shared<datapath::windows::overlapped>();
|
||||
std::shared_ptr<datapath::windows::overlapped> read_content_ov = std::make_shared<datapath::windows::overlapped>();
|
||||
std::shared_ptr<datapath::windows::overlapped> waitable;
|
||||
|
||||
read_header_ov->on_wait_error.add([&state, &waitable](datapath::error ec) {
|
||||
read_header_ov->_on_wait_error.add([&state, &waitable](datapath::error ec) {
|
||||
// There was an error waiting on the header.
|
||||
state = readstate::Unknown;
|
||||
waitable.reset();
|
||||
});
|
||||
read_header_ov->on_wait_success.add(
|
||||
[this, &read_buffer, &read_content_ov, &state, &waitable](datapath::error ec) {
|
||||
read_header_ov->_on_wait_success.add([this, &read_buffer, &read_content_ov, &state, &waitable](datapath::error ec) {
|
||||
read_content_ov->set_handle(this->socket_handle);
|
||||
read_content_ov->set_data(this);
|
||||
|
||||
@@ -92,15 +89,15 @@ void datapath::windows::socket::_watcher()
|
||||
waitable.reset();
|
||||
}
|
||||
});
|
||||
read_content_ov->on_wait_error.add([&state, &waitable](datapath::error ec) {
|
||||
read_content_ov->_on_wait_error.add([&state, &waitable](datapath::error ec) {
|
||||
// There was an error waiting on the content.
|
||||
state = readstate::Unknown;
|
||||
waitable.reset();
|
||||
});
|
||||
read_content_ov->on_wait_success.add([this, &read_buffer, &state, &waitable](datapath::error ec) {
|
||||
read_content_ov->_on_wait_success.add([this, &read_buffer, &state, &waitable](datapath::error ec) {
|
||||
// We have content!
|
||||
if (this->on_message) {
|
||||
this->on_message(read_buffer);
|
||||
if (this->_on_message) {
|
||||
this->_on_message(read_buffer);
|
||||
state = readstate::Unknown;
|
||||
} else {
|
||||
// We're buffering the message in read_buffer until there is a hook to on_message.
|
||||
@@ -127,8 +124,7 @@ void datapath::windows::socket::_watcher()
|
||||
|
||||
// Read content.
|
||||
if (ReadFileEx(this->socket_handle, read_buffer.data(), DWORD(read_buffer.size()),
|
||||
read_header_ov->get_overlapped(),
|
||||
&datapath::windows::utility::def_io_completion_routine)) {
|
||||
read_header_ov->get_overlapped(), &datapath::windows::utility::def_io_completion_routine)) {
|
||||
state = readstate::Header;
|
||||
waitable = read_header_ov;
|
||||
|
||||
@@ -142,8 +138,8 @@ void datapath::windows::socket::_watcher()
|
||||
// This logic is in the on_wait_success handler, and continued here.
|
||||
if (!waitable) {
|
||||
// We currently have a message buffered, but there was no handler last time we checked.
|
||||
if (this->on_message) {
|
||||
this->on_message(read_buffer);
|
||||
if (this->_on_message) {
|
||||
this->_on_message(read_buffer);
|
||||
state = readstate::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#define SIZE_ELEMENT uint32_t
|
||||
|
||||
void datapath::windows::task::_assign(const std::vector<char>& data, std::shared_ptr<datapath::windows::overlapped> ov){
|
||||
void datapath::windows::task::_assign(const std::vector<char>& data, std::shared_ptr<datapath::windows::overlapped> ov)
|
||||
{
|
||||
this->buffer.resize(data.size() + sizeof(SIZE_ELEMENT));
|
||||
std::memcpy(buffer.data() + sizeof(SIZE_ELEMENT), data.data(), data.size());
|
||||
reinterpret_cast<SIZE_ELEMENT&>(buffer[0]) = SIZE_ELEMENT(data.size());
|
||||
@@ -29,8 +30,8 @@ void datapath::windows::task::_assign(const std::vector<char>& data, std::shared
|
||||
|
||||
datapath::windows::task::task()
|
||||
{
|
||||
this->on_wait_error.add([this](datapath::error ec) { this->on_failure(ec); });
|
||||
this->on_wait_success.add([this](datapath::error ec) { this->on_success(ec, this->data()); });
|
||||
this->_on_wait_error.add([this](datapath::error ec) { this->_on_failure(ec); });
|
||||
this->_on_wait_success.add([this](datapath::error ec) { this->_on_success(ec, this->data()); });
|
||||
}
|
||||
|
||||
datapath::windows::task::~task()
|
||||
|
||||
@@ -20,8 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#include <memory>
|
||||
#include "itask.hpp"
|
||||
#include "overlapped.hpp"
|
||||
#include "socket.hpp"
|
||||
#include "server.hpp"
|
||||
#include "socket.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
|
||||
@@ -50,8 +50,7 @@ namespace datapath {
|
||||
return converter.from_bytes(string);
|
||||
}
|
||||
|
||||
static VOID CALLBACK def_io_completion_routine(_In_ DWORD dwErrorCode,
|
||||
_In_ DWORD dwNumberOfBytesTransfered,
|
||||
static VOID CALLBACK def_io_completion_routine(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered,
|
||||
_Inout_ LPOVERLAPPED lpOverlapped)
|
||||
{
|
||||
SetEvent(lpOverlapped->hEvent);
|
||||
|
||||
@@ -42,12 +42,12 @@ datapath::error datapath::waitable::wait(datapath::waitable* obj, std::chrono::n
|
||||
DWORD result = WaitForSingleObjectEx(handle, DWORD(timeout), TRUE);
|
||||
switch (result) {
|
||||
case WAIT_OBJECT_0:
|
||||
obj->on_wait_success(datapath::error::Success);
|
||||
obj->_on_wait_success(datapath::error::Success);
|
||||
return datapath::error::Success;
|
||||
case WAIT_TIMEOUT:
|
||||
return datapath::error::TimedOut;
|
||||
case WAIT_ABANDONED:
|
||||
obj->on_wait_error(datapath::error::Closed);
|
||||
obj->_on_wait_error(datapath::error::Closed);
|
||||
return datapath::error::Closed;
|
||||
case WAIT_IO_COMPLETION:
|
||||
duration = (std::chrono::high_resolution_clock::now() - start);
|
||||
@@ -96,12 +96,12 @@ datapath::error datapath::waitable::wait(datapath::waitable** objs, size_t count
|
||||
DWORD result = WaitForMultipleObjectsEx(handles.size(), handles.data(), TRUE, DWORD(timeout), TRUE);
|
||||
if ((result >= WAIT_OBJECT_0) && (result < (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS))) {
|
||||
for (auto idx : indexes) {
|
||||
objs[idx]->on_wait_success(datapath::error::Success);
|
||||
objs[idx]->_on_wait_success(datapath::error::Success);
|
||||
}
|
||||
return datapath::error::Success;
|
||||
} else if ((result >= WAIT_ABANDONED_0) && (result < (WAIT_ABANDONED_0 + MAXIMUM_WAIT_OBJECTS))) {
|
||||
for (auto idx : indexes) {
|
||||
objs[idx]->on_wait_error(datapath::error::Closed);
|
||||
objs[idx]->_on_wait_error(datapath::error::Closed);
|
||||
}
|
||||
return datapath::error::Closed;
|
||||
} else if (result == WAIT_TIMEOUT) {
|
||||
@@ -154,11 +154,11 @@ datapath::error datapath::waitable::wait_any(datapath::waitable** objs, size_t c
|
||||
DWORD result = WaitForMultipleObjectsEx(handles.size(), handles.data(), FALSE, DWORD(timeout), TRUE);
|
||||
if ((result >= WAIT_OBJECT_0) && (result < (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS))) {
|
||||
index = indexes[result - WAIT_OBJECT_0];
|
||||
objs[index]->on_wait_success(datapath::error::Success);
|
||||
objs[index]->_on_wait_success(datapath::error::Success);
|
||||
return datapath::error::Success;
|
||||
} else if ((result >= WAIT_ABANDONED_0) && (result < (WAIT_ABANDONED_0 + MAXIMUM_WAIT_OBJECTS))) {
|
||||
index = indexes[result - WAIT_OBJECT_0];
|
||||
objs[index]->on_wait_error(datapath::error::Closed);
|
||||
objs[index]->_on_wait_error(datapath::error::Closed);
|
||||
return datapath::error::Closed;
|
||||
} else if (result == WAIT_TIMEOUT) {
|
||||
return datapath::error::TimedOut;
|
||||
|
||||
Reference in New Issue
Block a user