Initial Code
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "datapath.hpp"
|
||||
#if defined(_WIN32)
|
||||
#include "windows/server.hpp"
|
||||
#include "windows/socket.hpp"
|
||||
#endif
|
||||
|
||||
datapath::error datapath::connect(std::shared_ptr<datapath::isocket>& socket, std::string path)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return datapath::windows::socket::connect(socket, path);
|
||||
#else
|
||||
return datapath::error::Unknown;
|
||||
#endif
|
||||
}
|
||||
|
||||
datapath::error datapath::host(std::shared_ptr<datapath::iserver>& server, std::string path,
|
||||
datapath::permissions permissions, size_t max_clients)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return datapath::windows::server::host(server, path, permissions, max_clients);
|
||||
#else
|
||||
return datapath::error::Unknown;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "overlapped-queue.hpp"
|
||||
|
||||
datapath::windows::overlapped_queue::overlapped_queue(size_t backlog)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->objs_lock);
|
||||
for (size_t idx = 0; idx < backlog; idx++) {
|
||||
free_objs.push(std::make_shared<datapath::windows::overlapped>());
|
||||
}
|
||||
}
|
||||
|
||||
datapath::windows::overlapped_queue::~overlapped_queue()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->objs_lock);
|
||||
while (free_objs.size() > 0) {
|
||||
free_objs.pop();
|
||||
}
|
||||
used_objs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<datapath::windows::overlapped> datapath::windows::overlapped_queue::alloc()
|
||||
{
|
||||
std::shared_ptr<datapath::windows::overlapped> obj;
|
||||
std::unique_lock<std::mutex> ul(this->objs_lock);
|
||||
|
||||
if (free_objs.size() > 0) {
|
||||
obj = free_objs.front();
|
||||
free_objs.pop();
|
||||
} else {
|
||||
obj = std::make_shared<datapath::windows::overlapped>();
|
||||
}
|
||||
|
||||
used_objs.push_back(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped_queue::free(std::shared_ptr<datapath::windows::overlapped> overlapped)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->objs_lock);
|
||||
for (auto itr = used_objs.begin(); itr != used_objs.end(); itr++) {
|
||||
if (*itr == overlapped) {
|
||||
used_objs.erase(itr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
free_objs.push(overlapped);
|
||||
}
|
||||
|
||||
/*
|
||||
// Security Descriptor Stuff
|
||||
SECURITY_ATTRIBUTES security_attributes;
|
||||
PSECURITY_DESCRIPTOR security_descriptor_ptr = NULL;
|
||||
PSID sid_everyone_ptr = NULL;
|
||||
PSID sid_admin_ptr = NULL;
|
||||
PACL acl_ptr = NULL;
|
||||
EXPLICIT_ACCESS explicit_access[2];
|
||||
SID_IDENTIFIER_AUTHORITY sid_auth_world = SECURITY_WORLD_SID_AUTHORITY;
|
||||
SID_IDENTIFIER_AUTHORITY sid_auth_nt = SECURITY_NT_AUTHORITY;
|
||||
|
||||
bool datapath::windows::overlapped_queue::create_security_attributes()
|
||||
{
|
||||
DWORD dwRes;
|
||||
|
||||
// Create a well-known SID for the Everyone group.
|
||||
if (!AllocateAndInitializeSid(&sid_auth_world, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &sid_everyone_ptr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize an EXPLICIT_ACCESS structure for an ACE.
|
||||
// The ACE will allow Everyone read access to the key.
|
||||
ZeroMemory(&explicit_access, 2 * sizeof(EXPLICIT_ACCESS));
|
||||
explicit_access[0].grfAccessPermissions = KEY_READ;
|
||||
explicit_access[0].grfAccessMode = SET_ACCESS;
|
||||
explicit_access[0].grfInheritance = NO_INHERITANCE;
|
||||
explicit_access[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
||||
explicit_access[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
|
||||
explicit_access[0].Trustee.ptstrName = (LPTSTR)sid_everyone_ptr;
|
||||
|
||||
// Create a SID for the BUILTIN\Administrators group.
|
||||
if (!AllocateAndInitializeSid(&sid_auth_nt, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0,
|
||||
0, 0, &sid_admin_ptr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize an EXPLICIT_ACCESS structure for an ACE.
|
||||
// The ACE will allow the Administrators group full access to
|
||||
// the key.
|
||||
explicit_access[1].grfAccessPermissions = KEY_ALL_ACCESS;
|
||||
explicit_access[1].grfAccessMode = SET_ACCESS;
|
||||
explicit_access[1].grfInheritance = NO_INHERITANCE;
|
||||
explicit_access[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
||||
explicit_access[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
|
||||
explicit_access[1].Trustee.ptstrName = (LPTSTR)sid_admin_ptr;
|
||||
|
||||
// Create a new ACL that contains the new ACEs.
|
||||
dwRes = SetEntriesInAcl(2, explicit_access, NULL, &acl_ptr);
|
||||
if (ERROR_SUCCESS != dwRes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize a security descriptor.
|
||||
security_descriptor_ptr = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
|
||||
if (NULL == security_descriptor_ptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!InitializeSecurityDescriptor(security_descriptor_ptr, SECURITY_DESCRIPTOR_REVISION)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add the ACL to the security descriptor.
|
||||
if (!SetSecurityDescriptorDacl(security_descriptor_ptr,
|
||||
TRUE, // bDaclPresent flag
|
||||
acl_ptr,
|
||||
FALSE)) // not a default DACL
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize a security attributes structure.
|
||||
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
security_attributes.lpSecurityDescriptor = security_descriptor_ptr;
|
||||
security_attributes.bInheritHandle = FALSE;
|
||||
return true;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped_queue::destroy_security_attributes()
|
||||
{
|
||||
if (sid_everyone_ptr)
|
||||
FreeSid(sid_everyone_ptr);
|
||||
if (sid_admin_ptr)
|
||||
FreeSid(sid_admin_ptr);
|
||||
if (acl_ptr)
|
||||
LocalFree(acl_ptr);
|
||||
if (security_descriptor_ptr)
|
||||
LocalFree(security_descriptor_ptr);
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cinttypes>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include "overlapped.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <AccCtrl.h>
|
||||
#include <AclAPI.h>
|
||||
#include <windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
class overlapped_queue {
|
||||
std::queue<std::shared_ptr<datapath::windows::overlapped>> free_objs;
|
||||
std::list<std::shared_ptr<datapath::windows::overlapped>> used_objs;
|
||||
std::mutex objs_lock;
|
||||
|
||||
public:
|
||||
overlapped_queue(size_t backlog = 8);
|
||||
|
||||
virtual ~overlapped_queue();
|
||||
|
||||
std::shared_ptr<datapath::windows::overlapped> alloc();
|
||||
|
||||
void free(std::shared_ptr<datapath::windows::overlapped> overlapped);
|
||||
};
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "overlapped.hpp"
|
||||
|
||||
datapath::windows::overlapped::overlapped() : overlapped_ptr(nullptr), data(nullptr)
|
||||
{
|
||||
size_t memory_size = sizeof(OVERLAPPED) + sizeof(void*);
|
||||
buffer.resize(memory_size);
|
||||
|
||||
reinterpret_cast<void*&>(buffer[sizeof(OVERLAPPED)]) = this;
|
||||
handle = INVALID_HANDLE_VALUE;
|
||||
|
||||
// Initialize OVERLAPPED
|
||||
overlapped_ptr = &reinterpret_cast<OVERLAPPED&>(buffer[0]);
|
||||
memset(overlapped_ptr, 0, sizeof(OVERLAPPED));
|
||||
overlapped_ptr->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
|
||||
}
|
||||
|
||||
datapath::windows::overlapped::~overlapped()
|
||||
{
|
||||
cancel();
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
OVERLAPPED* datapath::windows::overlapped::get_overlapped()
|
||||
{
|
||||
return this->overlapped_ptr;
|
||||
}
|
||||
|
||||
HANDLE datapath::windows::overlapped::get_handle()
|
||||
{
|
||||
return this->handle;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped::set_handle(HANDLE handle)
|
||||
{
|
||||
this->handle = handle;
|
||||
}
|
||||
|
||||
void* datapath::windows::overlapped::get_data()
|
||||
{
|
||||
return this->data;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped::set_data(void* data)
|
||||
{
|
||||
this->data = data;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped::cancel()
|
||||
{
|
||||
if (overlapped_ptr) {
|
||||
CancelIoEx(handle, overlapped_ptr);
|
||||
ResetEvent(overlapped_ptr->hEvent);
|
||||
CloseHandle(overlapped_ptr->hEvent);
|
||||
}
|
||||
}
|
||||
|
||||
bool datapath::windows::overlapped::is_completed()
|
||||
{
|
||||
if (overlapped_ptr) {
|
||||
return HasOverlappedIoCompleted(overlapped_ptr);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void datapath::windows::overlapped::reset()
|
||||
{
|
||||
cancel();
|
||||
if (overlapped_ptr) {
|
||||
overlapped_ptr->Internal = 0;
|
||||
overlapped_ptr->InternalHigh = 0;
|
||||
overlapped_ptr->Offset = 0;
|
||||
overlapped_ptr->OffsetHigh = 0;
|
||||
overlapped_ptr->Pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void* datapath::windows::overlapped::get_waitable()
|
||||
{
|
||||
return reinterpret_cast<void*>(overlapped_ptr->hEvent);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cinttypes>
|
||||
#include <vector>
|
||||
#include "waitable.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
class overlapped : public datapath::waitable {
|
||||
std::vector<char> buffer;
|
||||
OVERLAPPED* overlapped_ptr;
|
||||
HANDLE handle;
|
||||
void* data;
|
||||
|
||||
public:
|
||||
overlapped();
|
||||
~overlapped();
|
||||
|
||||
OVERLAPPED* get_overlapped();
|
||||
|
||||
HANDLE get_handle();
|
||||
void set_handle(HANDLE handle);
|
||||
|
||||
void* get_data();
|
||||
void set_data(void* data);
|
||||
|
||||
void cancel();
|
||||
|
||||
bool is_completed();
|
||||
|
||||
void reset();
|
||||
|
||||
public /*virtual override*/:
|
||||
virtual void* get_waitable() override;
|
||||
};
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "server.hpp"
|
||||
#include "socket.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
// Buffer Size that Windows just ignores, for the most part.
|
||||
#define WIN_BUFFER_SIZE 64 * 1024 * 1024
|
||||
#define WIN_WAIT_TIME 100
|
||||
#define WIN_BACKLOG_NUM 8
|
||||
|
||||
datapath::error datapath::windows::server::create(std::string path, datapath::permissions permissions,
|
||||
size_t max_clients)
|
||||
{
|
||||
// If old sockets are available, close them.
|
||||
this->close();
|
||||
|
||||
// Apply options
|
||||
this->max_clients = max_clients;
|
||||
this->path = path;
|
||||
|
||||
// Generate Security Attributes.
|
||||
std::memset(&this->security_attributes, 0, sizeof(SECURITY_ATTRIBUTES));
|
||||
this->security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
this->security_attributes.lpSecurityDescriptor = nullptr;
|
||||
this->security_attributes.bInheritHandle = true;
|
||||
// TODO: Respect permissions.
|
||||
|
||||
// Spawn x backlog connections
|
||||
// TODO: Add parameter for this.
|
||||
for (size_t n = 0; n < WIN_BACKLOG_NUM; n++) {
|
||||
HANDLE handle = _create_socket(path, n == 0);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
// Clean up again.
|
||||
this->close();
|
||||
return datapath::error::CriticalFailure;
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
sockets.push_back(handle);
|
||||
waiting_sockets.push_back(handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Watcher Thread
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->watcher.lock);
|
||||
this->watcher.shutdown = false;
|
||||
this->watcher.task = std::thread(std::bind(&datapath::windows::server::_watcher, this));
|
||||
}
|
||||
|
||||
is_created = true;
|
||||
return datapath::error::Success;
|
||||
}
|
||||
|
||||
HANDLE datapath::windows::server::_create_socket(std::string path, bool initial)
|
||||
{
|
||||
if (!datapath::windows::utility::make_pipe_path(path)) {
|
||||
return INVALID_HANDLE_VALUE;
|
||||
}
|
||||
std::wstring wpath = datapath::windows::utility::make_wide_string(path);
|
||||
|
||||
DWORD file_flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED;
|
||||
if (initial) {
|
||||
file_flags |= FILE_FLAG_FIRST_PIPE_INSTANCE;
|
||||
}
|
||||
|
||||
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);
|
||||
return handle;
|
||||
}
|
||||
|
||||
void datapath::windows::server::_watcher()
|
||||
{
|
||||
std::map<HANDLE, std::shared_ptr<datapath::windows::overlapped>> ovmap;
|
||||
|
||||
while (!this->watcher.shutdown) {
|
||||
// Verify existing connections.
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
for (auto itr = this->active_sockets.begin(); itr != this->active_sockets.end(); itr++) {
|
||||
if (itr->second.expired()) {
|
||||
this->active_sockets.erase(itr);
|
||||
this->waiting_sockets.push_back(itr->first);
|
||||
continue;
|
||||
}
|
||||
auto obj = itr->second.lock();
|
||||
if (!obj->good()) {
|
||||
this->active_sockets.erase(itr);
|
||||
this->waiting_sockets.push_back(itr->first);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update list of overlappeds to track.
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
for (auto itr = this->waiting_sockets.begin(); itr != this->waiting_sockets.end(); itr++) {
|
||||
if (ovmap.count(*itr) == 0) {
|
||||
auto ov = std::make_shared<datapath::windows::overlapped>();
|
||||
ov->set_handle(*itr);
|
||||
ov->set_data(this);
|
||||
ov->on_wait_success.add([this, &ovmap, &itr](datapath::error ec) {
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
this->waiting_sockets.remove(*itr);
|
||||
this->pending_sockets.push_back(*itr);
|
||||
ovmap.erase(*itr);
|
||||
});
|
||||
BOOL suc = ConnectNamedPipe(*itr, ov->get_overlapped());
|
||||
if (suc) {
|
||||
ovmap.insert({*itr, ov});
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for any overlapped objects.
|
||||
if (ovmap.size() > 0) {
|
||||
// No lock as we aren't touching any list or map yet.
|
||||
std::vector<datapath::waitable*> waits;
|
||||
waits.reserve(ovmap.size());
|
||||
|
||||
for (auto kv : ovmap) {
|
||||
waits.push_back(&(*kv.second));
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
datapath::error ec = datapath::waitable::wait_any(waits, index, std::chrono::milliseconds(1));
|
||||
} else {
|
||||
// Just sleep 1ms to not use too much CPU.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::server::accept(std::shared_ptr<datapath::isocket>& socket)
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
if (this->pending_sockets.size() == 0) {
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
|
||||
HANDLE handle = this->pending_sockets.front();
|
||||
this->pending_sockets.pop_front();
|
||||
|
||||
if (!socket) {
|
||||
socket = std::dynamic_pointer_cast<datapath::isocket>(std::make_shared<datapath::windows::socket>());
|
||||
}
|
||||
std::shared_ptr<datapath::windows::socket> obj = std::dynamic_pointer_cast<datapath::windows::socket>(socket);
|
||||
|
||||
obj->_connect(handle);
|
||||
|
||||
// Stock up on backlog and total sockets.
|
||||
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);
|
||||
this->waiting_sockets.push_back(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->active_sockets.insert({handle, socket});
|
||||
return datapath::error::Success;
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::server::close()
|
||||
{
|
||||
// Watcher Thread
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->watcher.lock);
|
||||
this->watcher.shutdown = true;
|
||||
if (this->watcher.task.joinable()) {
|
||||
this->watcher.task.join();
|
||||
}
|
||||
}
|
||||
|
||||
// Kill all sockets.
|
||||
std::unique_lock<std::mutex> ul(this->lock);
|
||||
for (HANDLE socket : sockets) {
|
||||
DisconnectNamedPipe(socket);
|
||||
CloseHandle(socket);
|
||||
}
|
||||
|
||||
// Notify Sockets of being dead.
|
||||
|
||||
// Clear all lists.
|
||||
sockets.clear();
|
||||
waiting_sockets.clear();
|
||||
pending_sockets.clear();
|
||||
active_sockets.clear();
|
||||
|
||||
return datapath::error::Success;
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::server::host(std::shared_ptr<datapath::iserver>& server, std::string path,
|
||||
datapath::permissions permissions, size_t max_clients = -1)
|
||||
{
|
||||
if (!server) {
|
||||
server = std::dynamic_pointer_cast<datapath::iserver>(std::make_shared<datapath::windows::server>());
|
||||
}
|
||||
std::shared_ptr<datapath::windows::server> obj = std::dynamic_pointer_cast<datapath::windows::server>(server);
|
||||
|
||||
return obj->create(path, permissions, max_clients);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include "iserver.hpp"
|
||||
#include "permissions.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
class server : public iserver {
|
||||
bool is_created = false;
|
||||
size_t max_clients = -1;
|
||||
std::string path;
|
||||
|
||||
private /*critical data*/:
|
||||
// Lock for critical data.
|
||||
std::mutex lock;
|
||||
|
||||
SECURITY_ATTRIBUTES security_attributes;
|
||||
|
||||
std::list<HANDLE> sockets;
|
||||
std::list<HANDLE> waiting_sockets;
|
||||
std::list<HANDLE> pending_sockets;
|
||||
std::map<HANDLE, std::weak_ptr<datapath::isocket>> active_sockets;
|
||||
|
||||
struct {
|
||||
std::thread task;
|
||||
std::mutex lock;
|
||||
bool shutdown = false;
|
||||
} watcher;
|
||||
|
||||
protected:
|
||||
datapath::error create(std::string path, datapath::permissions permissions, size_t max_clients);
|
||||
|
||||
HANDLE _create_socket(std::string path, bool initial = false);
|
||||
|
||||
void _watcher();
|
||||
|
||||
public /*virtual override*/:
|
||||
virtual datapath::error accept(std::shared_ptr<datapath::isocket>& socket) override;
|
||||
|
||||
virtual datapath::error close() override;
|
||||
|
||||
public:
|
||||
static datapath::error host(std::shared_ptr<datapath::iserver>& server, std::string path,
|
||||
datapath::permissions permissions, size_t max_clients);
|
||||
};
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "socket.hpp"
|
||||
#include <cinttypes>
|
||||
#include "task.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#define SIZE_ELEMENT uint32_t
|
||||
|
||||
void datapath::windows::socket::_connect(HANDLE handle)
|
||||
{
|
||||
this->socket_handle = handle;
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
this->is_connected = true;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->watcher.lock);
|
||||
this->watcher.shutdown = false;
|
||||
this->watcher.task = std::thread(std::bind(&datapath::windows::socket::_watcher, this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void datapath::windows::socket::_disconnect()
|
||||
{
|
||||
if (this->on_close) {
|
||||
this->on_close(datapath::error::Closed);
|
||||
}
|
||||
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(this->watcher.lock);
|
||||
this->watcher.shutdown = true;
|
||||
}
|
||||
if (this->watcher.task.joinable()) {
|
||||
this->watcher.task.join();
|
||||
}
|
||||
}
|
||||
|
||||
this->is_connected = false;
|
||||
}
|
||||
|
||||
void datapath::windows::socket::_watcher()
|
||||
{
|
||||
enum class readstate { Unknown, Header, Content } state = readstate::Unknown;
|
||||
|
||||
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> waitable;
|
||||
|
||||
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);
|
||||
|
||||
// ToDo: Add optional message size limit, messages above this size kill the connection for attempting DoS.
|
||||
size_t msg_size = reinterpret_cast<SIZE_ELEMENT&>(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(), NULL)) {
|
||||
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) {
|
||||
// We have content!
|
||||
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.
|
||||
}
|
||||
waitable.reset();
|
||||
});
|
||||
|
||||
while (!this->watcher.shutdown) {
|
||||
if (this->socket_handle == INVALID_HANDLE_VALUE) {
|
||||
break;
|
||||
}
|
||||
if (!this->is_connected) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (state == readstate::Unknown) {
|
||||
// Read the header of the next message.
|
||||
// The header simply contains the length of the message.
|
||||
// ToDo: Figure out if Message transfer/read mode and WaitCommEvent work together.
|
||||
|
||||
read_header_ov->set_handle(this->socket_handle);
|
||||
read_header_ov->set_data(this);
|
||||
read_buffer.resize(sizeof(SIZE_ELEMENT));
|
||||
|
||||
// Read content.
|
||||
if (ReadFileEx(this->socket_handle, read_buffer.data(), DWORD(read_buffer.size()),
|
||||
read_header_ov->get_overlapped(), NULL)) {
|
||||
state = readstate::Header;
|
||||
waitable = read_header_ov;
|
||||
|
||||
} else {
|
||||
state = readstate::Unknown;
|
||||
waitable.reset();
|
||||
}
|
||||
} else if (state == readstate::Header) {
|
||||
// This logic is in the on_wait_success handler.
|
||||
} else if (state == readstate::Content) {
|
||||
// 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);
|
||||
state = readstate::Unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!waitable) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
} else {
|
||||
datapath::error err = waitable->wait(std::chrono::milliseconds(1));
|
||||
if (err == datapath::error::Closed) {
|
||||
_disconnect();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
datapath::windows::socket::socket() : is_connected(false), socket_handle(INVALID_HANDLE_VALUE) {}
|
||||
|
||||
datapath::windows::socket::~socket()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
bool datapath::windows::socket::good()
|
||||
{
|
||||
return this->is_connected;
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::socket::close()
|
||||
{
|
||||
if (this->is_connected) {
|
||||
DisconnectNamedPipe(this->socket_handle);
|
||||
_disconnect();
|
||||
return datapath::error::Success;
|
||||
}
|
||||
return datapath::error::Closed;
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::socket::write(std::shared_ptr<datapath::itask>& task, const std::vector<char>& data)
|
||||
{
|
||||
if (!task) {
|
||||
task = std::dynamic_pointer_cast<datapath::itask>(std::make_shared<datapath::windows::task>());
|
||||
}
|
||||
std::shared_ptr<datapath::windows::task> obj = std::dynamic_pointer_cast<datapath::windows::task>(task);
|
||||
std::shared_ptr<datapath::windows::overlapped> ov = std::make_shared<datapath::windows::overlapped>();
|
||||
|
||||
obj->_assign(data, ov);
|
||||
|
||||
BOOL suc =
|
||||
WriteFileEx(socket_handle, obj->data().data(), DWORD(obj->data().size()), ov->get_overlapped(), NULL);
|
||||
if (suc) {
|
||||
return datapath::error::Success;
|
||||
} else {
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::socket::connect(std::shared_ptr<datapath::isocket>& socket, std::string path)
|
||||
{
|
||||
if (!datapath::windows::utility::make_pipe_path(path)) {
|
||||
return datapath::error::InvalidPath;
|
||||
}
|
||||
std::wstring wpath = datapath::windows::utility::make_wide_string(path);
|
||||
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
HANDLE handle = CreateFileW(wpath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
|
||||
if ((handle == INVALID_HANDLE_VALUE) || (GetLastError() != ERROR_SUCCESS)) {
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
|
||||
DWORD pipe_read_mode = PIPE_WAIT | PIPE_READMODE_BYTE;
|
||||
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
if (!SetNamedPipeHandleState(handle, &pipe_read_mode, NULL, NULL)) {
|
||||
// ToDo. This doesn't actually affect us as the default mode is the one we're setting
|
||||
}
|
||||
|
||||
if (!socket) {
|
||||
socket = std::dynamic_pointer_cast<datapath::isocket>(std::make_shared<datapath::windows::socket>());
|
||||
}
|
||||
std::shared_ptr<datapath::windows::socket> obj = std::dynamic_pointer_cast<datapath::windows::socket>(socket);
|
||||
|
||||
obj->_connect(handle);
|
||||
|
||||
return datapath::error::Success;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include "event.hpp"
|
||||
#include "isocket.hpp"
|
||||
#include "overlapped-queue.hpp"
|
||||
#include "overlapped.hpp"
|
||||
#include "server.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
class socket : public isocket {
|
||||
bool is_connected;
|
||||
HANDLE socket_handle;
|
||||
|
||||
struct {
|
||||
std::thread task;
|
||||
std::mutex lock;
|
||||
bool shutdown = false;
|
||||
} watcher;
|
||||
|
||||
protected:
|
||||
void _connect(HANDLE handle);
|
||||
|
||||
void _disconnect();
|
||||
|
||||
void _watcher();
|
||||
|
||||
public:
|
||||
socket();
|
||||
|
||||
virtual ~socket();
|
||||
|
||||
public /*virtual override*/:
|
||||
virtual bool good() override;
|
||||
|
||||
virtual datapath::error close() override;
|
||||
|
||||
virtual datapath::error write(std::shared_ptr<datapath::itask>& task,
|
||||
const std::vector<char>& data) override;
|
||||
|
||||
public:
|
||||
static datapath::error connect(std::shared_ptr<datapath::isocket>& socket, std::string path);
|
||||
|
||||
friend class datapath::windows::server;
|
||||
};
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "task.hpp"
|
||||
|
||||
void datapath::windows::task::_assign(const std::vector<char>& data, std::shared_ptr<datapath::windows::overlapped> ov){
|
||||
this->buffer.resize(data.size());
|
||||
std::memcpy(buffer.data(), data.data(), data.size());
|
||||
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()); });
|
||||
}
|
||||
|
||||
datapath::windows::task::~task()
|
||||
{
|
||||
cancel();
|
||||
}
|
||||
|
||||
datapath::error datapath::windows::task::cancel()
|
||||
{
|
||||
if (!overlapped) {
|
||||
return datapath::error::Unknown;
|
||||
}
|
||||
if (!overlapped->is_completed()) {
|
||||
overlapped->cancel();
|
||||
return datapath::error::Success;
|
||||
}
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
|
||||
bool datapath::windows::task::is_completed()
|
||||
{
|
||||
if (!overlapped) {
|
||||
return false;
|
||||
}
|
||||
return overlapped->is_completed();
|
||||
}
|
||||
|
||||
size_t datapath::windows::task::length()
|
||||
{
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
const std::vector<char>& datapath::windows::task::data()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void* datapath::windows::task::get_waitable()
|
||||
{
|
||||
if (!overlapped) {
|
||||
return nullptr;
|
||||
}
|
||||
return overlapped->get_waitable();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include "itask.hpp"
|
||||
#include "overlapped.hpp"
|
||||
#include "socket.hpp"
|
||||
#include "server.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
class task : public itask {
|
||||
std::shared_ptr<datapath::windows::overlapped> overlapped;
|
||||
std::vector<char> buffer;
|
||||
|
||||
protected:
|
||||
void _assign(const std::vector<char>& data, std::shared_ptr<datapath::windows::overlapped> ov);
|
||||
|
||||
public:
|
||||
task();
|
||||
~task();
|
||||
|
||||
public /*virtual override*/ /*itask*/:
|
||||
virtual datapath::error cancel() override;
|
||||
|
||||
virtual bool is_completed() override;
|
||||
|
||||
virtual size_t length() override;
|
||||
|
||||
virtual const std::vector<char>& data() override;
|
||||
|
||||
public /*virtual override*/ /*waitable*/:
|
||||
virtual void* get_waitable() override;
|
||||
|
||||
friend class datapath::windows::socket;
|
||||
friend class datapath::windows::server;
|
||||
};
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
}
|
||||
|
||||
namespace datapath {
|
||||
namespace windows {
|
||||
namespace utility {
|
||||
static inline bool make_pipe_path(std::string& string)
|
||||
{
|
||||
// Convert path to WinAPI compatible string.
|
||||
if (string.length() >= (MAX_PATH - 10ull)) {
|
||||
// \\.\pipe\ is 9 characters, but we count 10 here.
|
||||
return false;
|
||||
}
|
||||
for (char& v : string) {
|
||||
if (v == '\\') { // Backslash is not allowed.
|
||||
v = '/';
|
||||
}
|
||||
}
|
||||
string = {"\\\\.\\pipe\\" + string};
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline std::wstring make_wide_string(std::string string)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
return converter.from_bytes(string);
|
||||
}
|
||||
|
||||
} // namespace utility
|
||||
} // namespace windows
|
||||
} // namespace datapath
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
Low Latency IPC Library for high-speed traffic
|
||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "waitable.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
extern "C" {
|
||||
#include <Windows.h>
|
||||
}
|
||||
|
||||
datapath::error datapath::waitable::wait(datapath::waitable* obj, std::chrono::nanoseconds duration)
|
||||
{
|
||||
assert(obj == nullptr);
|
||||
|
||||
HANDLE handle = (HANDLE)obj->get_waitable();
|
||||
int64_t timeout = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
|
||||
if (timeout < 0) {
|
||||
timeout = 0;
|
||||
} else if (timeout > std::numeric_limits<int32_t>::max()) {
|
||||
timeout = std::numeric_limits<int32_t>::max();
|
||||
}
|
||||
|
||||
do {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
DWORD result = WaitForSingleObjectEx(handle, DWORD(timeout), TRUE);
|
||||
switch (result) {
|
||||
case WAIT_OBJECT_0:
|
||||
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);
|
||||
return datapath::error::Closed;
|
||||
case WAIT_IO_COMPLETION:
|
||||
duration = (std::chrono::high_resolution_clock::now() - start);
|
||||
timeout = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
continue;
|
||||
}
|
||||
} while (timeout > 0);
|
||||
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
|
||||
datapath::error datapath::waitable::wait(datapath::waitable** objs, size_t count, std::chrono::nanoseconds duration)
|
||||
{
|
||||
assert(objs == nullptr);
|
||||
assert(count == 0);
|
||||
assert(count > MAXIMUM_WAIT_OBJECTS);
|
||||
|
||||
int64_t timeout = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
|
||||
if (timeout < 0) {
|
||||
timeout = 0;
|
||||
} else if (timeout > std::numeric_limits<int32_t>::max()) {
|
||||
timeout = std::numeric_limits<int32_t>::max();
|
||||
}
|
||||
|
||||
// Rebuild a valid obj+index translation list.
|
||||
std::vector<HANDLE> handles(count);
|
||||
std::vector<size_t> indexes(count);
|
||||
size_t valid_handles = 0;
|
||||
for (size_t idx = 0; idx < count; idx++) {
|
||||
datapath::waitable* obj = objs[idx];
|
||||
if (obj) {
|
||||
handles[valid_handles] = reinterpret_cast<HANDLE>(obj->get_waitable());
|
||||
indexes[valid_handles] = idx;
|
||||
valid_handles++;
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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<std::chrono::milliseconds>(duration).count();
|
||||
continue;
|
||||
}
|
||||
} while (timeout > 0);
|
||||
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
|
||||
datapath::error datapath::waitable::wait_any(datapath::waitable** objs, size_t count, size_t& index,
|
||||
std::chrono::nanoseconds duration)
|
||||
{
|
||||
assert(objs == nullptr);
|
||||
assert(count == 0);
|
||||
assert(count > MAXIMUM_WAIT_OBJECTS);
|
||||
|
||||
int64_t timeout = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
|
||||
if (timeout < 0) {
|
||||
timeout = 0;
|
||||
} else if (timeout > std::numeric_limits<int32_t>::max()) {
|
||||
timeout = std::numeric_limits<int32_t>::max();
|
||||
}
|
||||
|
||||
// Rebuild a valid obj+index translation list.
|
||||
std::vector<HANDLE> handles(count);
|
||||
std::vector<size_t> indexes(count);
|
||||
size_t valid_handles = 0;
|
||||
for (size_t idx = 0; idx < count; idx++) {
|
||||
datapath::waitable* obj = objs[idx];
|
||||
if (obj) {
|
||||
handles[valid_handles] = reinterpret_cast<HANDLE>(obj->get_waitable());
|
||||
indexes[valid_handles] = idx;
|
||||
valid_handles++;
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
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);
|
||||
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);
|
||||
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<std::chrono::milliseconds>(duration).count();
|
||||
continue;
|
||||
}
|
||||
} while (timeout > 0);
|
||||
|
||||
return datapath::error::Failure;
|
||||
}
|
||||
Reference in New Issue
Block a user