diff --git a/.clang-format b/.clang-format index 37b9bdf..1384260 100644 --- a/.clang-format +++ b/.clang-format @@ -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 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..794e044 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1675be1..412a5ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -150,48 +154,48 @@ if(WIN32) list(APPEND PROJECT_DEFINES _CRT_SECURE_NO_WARNINGS - WIN32_LEAN_AND_MEAN - NOGPICAPMASKS - NOVIRTUALKEYCODES - NOWINMESSAGES - NOWINSTYLES - NOSYSMETRICS - NOMENUS - NOICONS - NOKEYSTATES - NOSYSCOMMANDS - NORASTEROPS - NOSHOWWINDOW - NOATOM - NOCLIPBOARD - NOCOLOR - NOCTLMGR - NODRAWTEXT - NOGDI - NOKERNEL - #NOUSER - #NONLS - NOMB - NOMEMMGR - NOMETAFILE - NOMINMAX - NOMSG - NOOPENFILE - NOSCROLL - NOSERVICE - NOSOUND - NOTEXTMETRIC - NOWH - NOWINOFFSETS - NOCOMM - NOKANJI - NOHELP - NOPROFILER - NODEFERWINDOWPOS - NOMCX - NOIME - NOMDI - NOINOUT + WIN32_LEAN_AND_MEAN + NOGPICAPMASKS + NOVIRTUALKEYCODES + NOWINMESSAGES + NOWINSTYLES + NOSYSMETRICS + NOMENUS + NOICONS + NOKEYSTATES + NOSYSCOMMANDS + NORASTEROPS + NOSHOWWINDOW + NOATOM + NOCLIPBOARD + NOCOLOR + NOCTLMGR + NODRAWTEXT + NOGDI + NOKERNEL + #NOUSER + #NONLS + NOMB + NOMEMMGR + NOMETAFILE + NOMINMAX + NOMSG + NOOPENFILE + NOSCROLL + NOSERVICE + NOSOUND + NOTEXTMETRIC + NOWH + NOWINOFFSETS + NOCOMM + NOKANJI + NOHELP + NOPROFILER + NODEFERWINDOWPOS + NOMCX + NOIME + NOMDI + NOINOUT ) list(APPEND PROJECT_PRIVATE @@ -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() + set(_BUILD_TYPE SHARED) + endif() else() - message(CRITICAL "Building nothing completed, aborting. Check MAKE_STATIC, MAKE_DYNAMIC and MAKE_DYNAMIC.") - return() + 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() diff --git a/include/bitmask.hpp b/include/bitmask.hpp index 425ec8c..d1b4585 100644 --- a/include/bitmask.hpp +++ b/include/bitmask.hpp @@ -38,8 +38,8 @@ typename std::enable_if::enable, Enum>::type oper return static_cast(static_cast(lhs) & static_cast(rhs)); } -#define ENABLE_BITMASK_OPERATORS(x) \ - template<> \ +#define ENABLE_BITMASK_OPERATORS(x) \ + template<> \ struct enable_bitmask_operators { \ static const bool enable = true; \ }; diff --git a/include/datapath.hpp b/include/datapath.hpp index 28520d4..d35230c 100644 --- a/include/datapath.hpp +++ b/include/datapath.hpp @@ -27,5 +27,5 @@ namespace datapath { datapath::error connect(std::shared_ptr& socket, std::string path); datapath::error host(std::shared_ptr& server, std::string path, - datapath::permissions permissions, size_t max_clients = 0); + datapath::permissions permissions, size_t max_clients = 0); } // namespace datapath diff --git a/include/event.hpp b/include/event.hpp index ddf6d88..cc7deab 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -24,10 +24,10 @@ namespace datapath { template class event { - std::list> listeners; + std::list> _listeners; - std::function listen_cb; - std::function silence_cb; + std::function _listen_cb; + std::function _silence_cb; public /* functions */: @@ -40,20 +40,20 @@ namespace datapath { // Add new listener. inline void add(std::function 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 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 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 cb) { - this->listen_cb = cb; + this->_listen_cb = cb; } void set_silence_callback(std::function cb) { - this->silence_cb = cb; + this->_silence_cb = cb; } }; }; // namespace datapath diff --git a/include/iserver.hpp b/include/iserver.hpp index ae0d5ae..3beca97 100644 --- a/include/iserver.hpp +++ b/include/iserver.hpp @@ -33,7 +33,7 @@ namespace datapath { * @param std::shared_ptr Socket. * @return void */ - datapath::event> on_accept; + datapath::event> _on_accept; public: virtual datapath::error close() = 0; diff --git a/include/isocket.hpp b/include/isocket.hpp index ad385ae..ef504c9 100644 --- a/include/isocket.hpp +++ b/include/isocket.hpp @@ -24,9 +24,9 @@ along with this program. If not, see . namespace datapath { class isocket { public /*events*/: - datapath::event&> on_message; + datapath::event&> _on_message; - datapath::event<> on_close; + datapath::event<> _on_close; public: virtual bool good() = 0; diff --git a/include/itask.hpp b/include/itask.hpp index 3f828a2..8643201 100644 --- a/include/itask.hpp +++ b/include/itask.hpp @@ -26,9 +26,9 @@ along with this program. If not, see . namespace datapath { class itask : public waitable { public /*event*/: - datapath::event on_failure; + datapath::event _on_failure; - datapath::event&> on_success; + datapath::event&> _on_success; public: virtual datapath::error cancel() = 0; diff --git a/include/permissions.hpp b/include/permissions.hpp index f2cfbe5..7b23adf 100644 --- a/include/permissions.hpp +++ b/include/permissions.hpp @@ -21,12 +21,6 @@ along with this program. If not, see . #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 diff --git a/include/threadpool.hpp b/include/threadpool.hpp index 489a17d..89f2be2 100644 --- a/include/threadpool.hpp +++ b/include/threadpool.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include #include #include #include @@ -28,7 +29,7 @@ namespace datapath { namespace threadpool { - typedef uint64_t affinity_t; + typedef uint64_t affinity_t; constexpr affinity_t default_mask = std::numeric_limits::max(); @@ -58,7 +59,7 @@ namespace datapath { void push(std::shared_ptr task); }; - std::map> workers; + std::map> _workers; public: pool(); diff --git a/include/waitable.hpp b/include/waitable.hpp index 5f1d746..1911b33 100644 --- a/include/waitable.hpp +++ b/include/waitable.hpp @@ -25,9 +25,9 @@ along with this program. If not, see . namespace datapath { class waitable { public /*events*/: - datapath::event on_wait_error; + datapath::event _on_wait_error; - datapath::event on_wait_success; + datapath::event _on_wait_success; public: virtual void* get_waitable() = 0; @@ -40,22 +40,22 @@ namespace datapath { public /*static*/: static datapath::error wait(datapath::waitable* obj, - std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); + std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); static datapath::error wait(datapath::waitable** objs, size_t count, - std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); + std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); static inline datapath::error wait(std::vector objs, - std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)) + std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)) { return datapath::waitable::wait(objs.data(), objs.size(), duration); } static datapath::error wait_any(datapath::waitable** objs, size_t count, size_t& index, - std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); + std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)); static inline datapath::error wait_any(std::vector objs, size_t& index, - std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)) + std::chrono::nanoseconds duration = std::chrono::nanoseconds(0)) { return datapath::waitable::wait_any(objs.data(), objs.size(), index, duration); } diff --git a/source/threadpool.cpp b/source/threadpool.cpp index 17c156f..acd5861 100644 --- a/source/threadpool.cpp +++ b/source/threadpool.cpp @@ -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(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) @@ -114,13 +113,13 @@ bool datapath::threadpool::pool::push(std::shared_ptr 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::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) 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; } diff --git a/source/windows/datapath.cpp b/source/windows/datapath.cpp index d8dcca5..61062f9 100644 --- a/source/windows/datapath.cpp +++ b/source/windows/datapath.cpp @@ -26,7 +26,7 @@ datapath::error datapath::connect(std::shared_ptr& socket, st } datapath::error datapath::host(std::shared_ptr& server, std::string path, - datapath::permissions permissions, size_t max_clients) + datapath::permissions permissions, size_t max_clients) { return datapath::windows::server::host(server, path, permissions, max_clients); } diff --git a/source/windows/server.cpp b/source/windows/server.cpp index 29869c5..897b03d 100644 --- a/source/windows/server.cpp +++ b/source/windows/server.cpp @@ -26,7 +26,7 @@ along with this program. If not, see . #define WIN_BACKLOG_NUM 8 datapath::error datapath::windows::server::create(std::string path, datapath::permissions permissions, - size_t max_clients) + size_t max_clients) { // If old sockets are available, close them. this->close(); @@ -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; } @@ -105,7 +105,7 @@ void datapath::windows::server::_watcher() } size_t index = 0; - datapath::error ec = datapath::waitable::wait_any(waits, index, std::chrono::milliseconds(0)); + datapath::error ec = datapath::waitable::wait_any(waits, index, std::chrono::milliseconds(0)); if (ec != datapath::error::Success) { datapath::waitable::wait_any(waits, index, std::chrono::milliseconds(1)); } @@ -122,7 +122,7 @@ void datapath::windows::server::_watcher() auto ov = std::make_shared(); 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 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 ul(this->lock); std::list 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(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); @@ -299,7 +294,7 @@ datapath::error datapath::windows::server::close() } datapath::error datapath::windows::server::host(std::shared_ptr& server, std::string path, - datapath::permissions permissions, size_t max_clients) + datapath::permissions permissions, size_t max_clients) { if (!server) { server = std::dynamic_pointer_cast(std::make_shared()); diff --git a/source/windows/server.hpp b/source/windows/server.hpp index 18a3532..d4c60af 100644 --- a/source/windows/server.hpp +++ b/source/windows/server.hpp @@ -65,7 +65,7 @@ namespace datapath { public: static datapath::error host(std::shared_ptr& server, std::string path, - datapath::permissions permissions, size_t max_clients); + datapath::permissions permissions, size_t max_clients); }; } // namespace windows } // namespace datapath \ No newline at end of file diff --git a/source/windows/socket.cpp b/source/windows/socket.cpp index 7cfec0c..60791ef 100644 --- a/source/windows/socket.cpp +++ b/source/windows/socket.cpp @@ -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,45 +62,42 @@ void datapath::windows::socket::_watcher() std::vector read_buffer; - std::shared_ptr read_header_ov = - std::make_shared(); - std::shared_ptr read_content_ov = - std::make_shared(); + std::shared_ptr read_header_ov = std::make_shared(); + std::shared_ptr read_content_ov = std::make_shared(); std::shared_ptr 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_content_ov->set_handle(this->socket_handle); - read_content_ov->set_data(this); + 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); - // ToDo: Add optional message size limit, messages above this size kill the connection for attempting DoS. - size_t msg_size = reinterpret_cast(read_buffer[0]); - read_buffer.resize(msg_size); + // ToDo: Add optional message size limit, messages above this size kill the connection for attempting DoS. + size_t msg_size = reinterpret_cast(read_buffer[0]); + read_buffer.resize(msg_size); - // Read content. - if (ReadFileEx(this->socket_handle, read_buffer.data(), DWORD(read_buffer.size()), - read_content_ov->get_overlapped(), &datapath::windows::utility::def_io_completion_routine)) { - state = readstate::Content; - waitable = read_content_ov; - } else { - state = readstate::Unknown; - waitable.reset(); - } - }); - read_content_ov->on_wait_error.add([&state, &waitable](datapath::error ec) { + // Read content. + if (ReadFileEx(this->socket_handle, read_buffer.data(), DWORD(read_buffer.size()), + read_content_ov->get_overlapped(), &datapath::windows::utility::def_io_completion_routine)) { + state = readstate::Content; + waitable = read_content_ov; + } else { + state = readstate::Unknown; + waitable.reset(); + } + }); + 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; } } @@ -197,7 +193,7 @@ datapath::error datapath::windows::socket::write(std::shared_ptr_assign(data, ov); BOOL suc = WriteFileEx(socket_handle, obj->data().data(), DWORD(obj->data().size()), ov->get_overlapped(), - &datapath::windows::utility::def_io_completion_routine); + &datapath::windows::utility::def_io_completion_routine); if (suc) { return datapath::error::Success; } else { @@ -214,7 +210,7 @@ datapath::error datapath::windows::socket::connect(std::shared_ptr& task, - const std::vector& data) override; + const std::vector& data) override; public: static datapath::error connect(std::shared_ptr& socket, std::string path); diff --git a/source/windows/task.cpp b/source/windows/task.cpp index 14d8882..4c0a880 100644 --- a/source/windows/task.cpp +++ b/source/windows/task.cpp @@ -20,17 +20,18 @@ along with this program. If not, see . #define SIZE_ELEMENT uint32_t -void datapath::windows::task::_assign(const std::vector& data, std::shared_ptr ov){ +void datapath::windows::task::_assign(const std::vector& data, std::shared_ptr ov) +{ this->buffer.resize(data.size() + sizeof(SIZE_ELEMENT)); std::memcpy(buffer.data() + sizeof(SIZE_ELEMENT), data.data(), data.size()); reinterpret_cast(buffer[0]) = SIZE_ELEMENT(data.size()); - this->overlapped = ov; + this->overlapped = ov; } 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() diff --git a/source/windows/task.hpp b/source/windows/task.hpp index 87903bb..668844f 100644 --- a/source/windows/task.hpp +++ b/source/windows/task.hpp @@ -20,8 +20,8 @@ along with this program. If not, see . #include #include "itask.hpp" #include "overlapped.hpp" -#include "socket.hpp" #include "server.hpp" +#include "socket.hpp" extern "C" { #include diff --git a/source/windows/utility.hpp b/source/windows/utility.hpp index ca9be2a..938fa22 100644 --- a/source/windows/utility.hpp +++ b/source/windows/utility.hpp @@ -50,13 +50,12 @@ namespace datapath { return converter.from_bytes(string); } - static VOID CALLBACK def_io_completion_routine(_In_ DWORD dwErrorCode, - _In_ DWORD dwNumberOfBytesTransfered, - _Inout_ LPOVERLAPPED lpOverlapped) + static VOID CALLBACK def_io_completion_routine(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered, + _Inout_ LPOVERLAPPED lpOverlapped) { SetEvent(lpOverlapped->hEvent); } } // namespace utility - } // namespace windows + } // namespace windows } // namespace datapath diff --git a/source/windows/waitable.cpp b/source/windows/waitable.cpp index 8754911..ae43050 100644 --- a/source/windows/waitable.cpp +++ b/source/windows/waitable.cpp @@ -42,16 +42,16 @@ 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); - timeout -= std::chrono::duration_cast(duration).count(); + timeout -= std::chrono::duration_cast(duration).count(); if (timeout <= 0) { timeout = 0; } @@ -96,19 +96,19 @@ 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) { return datapath::error::TimedOut; } else if (result == WAIT_IO_COMPLETION) { duration = (std::chrono::high_resolution_clock::now() - start); - timeout -= std::chrono::duration_cast(duration).count(); + timeout -= std::chrono::duration_cast(duration).count(); if (timeout <= 0) { timeout = 0; } @@ -122,7 +122,7 @@ datapath::error datapath::waitable::wait(datapath::waitable** objs, size_t count } datapath::error datapath::waitable::wait_any(datapath::waitable** objs, size_t count, size_t& index, - std::chrono::nanoseconds duration) + std::chrono::nanoseconds duration) { assert(objs != nullptr); assert((count > 0) && (count <= MAXIMUM_WAIT_OBJECTS)); @@ -154,17 +154,17 @@ 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; } else if (result == WAIT_IO_COMPLETION) { duration = (std::chrono::high_resolution_clock::now() - start); - timeout -= std::chrono::duration_cast(duration).count(); + timeout -= std::chrono::duration_cast(duration).count(); if (timeout <= 0) { timeout = 0; }