Initial Code

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2019-01-06 11:11:21 +01:00
parent 009868afe6
commit 92e5a327b2
27 changed files with 2468 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
/*
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 <type_traits>
template<typename Enum>
struct enable_bitmask_operators {
static const bool enable = false;
};
template<typename Enum>
typename std::enable_if<enable_bitmask_operators<Enum>::enable, Enum>::type operator|(Enum lhs, Enum rhs)
{
using underlying = typename std::underlying_type<Enum>::type;
return static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
}
template<typename Enum>
typename std::enable_if<enable_bitmask_operators<Enum>::enable, Enum>::type operator&(Enum lhs, Enum rhs)
{
using underlying = typename std::underlying_type<Enum>::type;
return static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
}
#define ENABLE_BITMASK_OPERATORS(x) \
template<> \
struct enable_bitmask_operators<x> { \
static const bool enable = true; \
};
+31
View File
@@ -0,0 +1,31 @@
/*
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 "error.hpp"
#include "iserver.hpp"
#include "isocket.hpp"
#include "itask.hpp"
#include "permissions.hpp"
#include <string>
namespace datapath {
datapath::error connect(std::shared_ptr<datapath::isocket>& socket, std::string path);
datapath::error host(std::shared_ptr<datapath::iserver>& server, std::string path, datapath::permissions permissions, size_t max_clients);
}
+45
View File
@@ -0,0 +1,45 @@
/*
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>
namespace datapath {
enum class error : int32_t {
// Unknown
Unknown = -1,
// Success
Success,
// Failure (Generic)
Failure,
// Failure (Critical Generic)
CriticalFailure,
// Socket Closed
Closed,
// Timed Out
TimedOut,
// Invalid Path
InvalidPath,
};
}
+64
View File
@@ -0,0 +1,64 @@
/*
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 <functional>
#include <list>
namespace datapath {
template<typename... _args>
class event {
std::list<std::function<void(_args...)>> listeners;
public:
void add(std::function<void(_args...)> listener)
{
listeners.push_back(listener);
}
void remove(std::function<void(_args...)> listener)
{
listeners.remove(listener);
}
// Not valid without the extra template.
template<typename... _largs>
void operator()(_largs... args)
{
for (auto& l : listeners) {
l(args...);
}
}
operator bool()
{
return !listeners.empty();
}
public:
bool empty()
{
return listeners.empty();
}
void clear()
{
listeners.clear();
}
};
}; // namespace datapath
+30
View File
@@ -0,0 +1,30 @@
/*
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 "error.hpp"
#include "isocket.hpp"
namespace datapath {
class iserver {
virtual datapath::error accept(std::shared_ptr<datapath::isocket>& socket) = 0;
virtual datapath::error close() = 0;
};
} // namespace datapath
+38
View File
@@ -0,0 +1,38 @@
/*
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 "error.hpp"
#include "event.hpp"
#include "itask.hpp"
namespace datapath {
class isocket {
public /*events*/:
datapath::event<const std::vector<char>&> on_message;
datapath::event<datapath::error> on_close;
public:
virtual bool good() = 0;
virtual datapath::error close() = 0;
virtual datapath::error write(std::shared_ptr<datapath::itask>& task, const std::vector<char>& data) = 0;
};
} // namespace datapath
+42
View File
@@ -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/>.
*/
#pragma once
#include <chrono>
#include <vector>
#include "error.hpp"
#include "event.hpp"
#include "waitable.hpp"
namespace datapath {
class itask : public waitable {
public /*event*/:
datapath::event<datapath::error> on_failure;
datapath::event<datapath::error, const std::vector<char>&> on_success;
public:
virtual datapath::error cancel() = 0;
virtual bool is_completed() = 0;
virtual size_t length() = 0;
virtual const std::vector<char>& data() = 0;
};
} // namespace datapath
+32
View File
@@ -0,0 +1,32 @@
/*
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 "bitmask.hpp"
namespace datapath {
enum class permissions : int8_t {
None,
User,
Group,
World,
Reserved
};
ENABLE_BITMASK_OPERATORS(datapath::permissions);
} // namespace datapath
+63
View File
@@ -0,0 +1,63 @@
/*
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 <chrono>
#include <vector>
#include "error.hpp"
#include "event.hpp"
namespace datapath {
class waitable {
public /*events*/:
datapath::event<datapath::error> on_wait_error;
datapath::event<datapath::error> on_wait_success;
public:
virtual void* get_waitable() = 0;
inline datapath::error wait(std::chrono::nanoseconds duration = std::chrono::nanoseconds(0))
{
return datapath::waitable::wait(this, duration);
}
public /*static*/:
static datapath::error wait(datapath::waitable* obj,
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));
static inline datapath::error wait(std::vector<datapath::waitable*> objs,
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));
static inline datapath::error wait_any(std::vector<datapath::waitable*> objs, size_t& index,
std::chrono::nanoseconds duration = std::chrono::nanoseconds(0))
{
return datapath::waitable::wait_any(objs.data(), objs.size(), index, duration);
}
};
} // namespace datapath