From 56a50eb65e3d4a6f739d53c52236eb7e20b90934 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 4 Jun 2018 16:46:29 +0200 Subject: Moved files --- include/sibs/DirectConnection.hpp | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/sibs/DirectConnection.hpp (limited to 'include/sibs/DirectConnection.hpp') diff --git a/include/sibs/DirectConnection.hpp b/include/sibs/DirectConnection.hpp new file mode 100644 index 0000000..8e92137 --- /dev/null +++ b/include/sibs/DirectConnection.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include "../types.hpp" +#include "../utils.hpp" + +struct addrinfo; + +namespace sibs +{ + class InvalidAddressException : public std::runtime_error + { + public: + InvalidAddressException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class ConnectionException : public std::runtime_error + { + public: + ConnectionException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class SendException : public std::runtime_error + { + public: + SendException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class Ipv4 + { + DISABLE_COPY(Ipv4) + public: + // Throws InvalidAddressException on error + Ipv4(const char *ip, u16 port); + ~Ipv4(); + + struct addrinfo *address; + }; + + enum class PubSubConnectResult + { + OK, + ERROR + }; + + using PubSubConnectCallback = std::function; + using PubSubReceiveDataCallback = std::function; + + struct DirectConnectionPeer + { + int socket; + PubSubReceiveDataCallback receiveDataCallbackFunc; + }; + + class DirectConnections + { + DISABLE_COPY(DirectConnections) + public: + DirectConnections(u16 port = 27137); + ~DirectConnections(); + + // Throws ConnectionException on error + std::shared_ptr connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + // Throws SendException on error + void send(const std::shared_ptr &peer, const void *data, const usize size); + private: + void receiveData(); + bool receiveDataFromPeer(const int socket, char *output); + private: + u16 port; + int eid; + std::unordered_map> peers; + std::thread receiveDataThread; + std::mutex peersMutex; + bool alive; + }; +} -- cgit v1.2.3