From ad7cc4dbe4dacaa5cccfd1c9adeccc0ece2299f4 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Sat, 2 Jun 2018 01:39:25 +0200 Subject: Receive data with epoll_wait --- include/DirectConnection.hpp | 44 +++++++++++++++++++++++++++++++++++++++----- include/Log.hpp | 12 ++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 include/Log.hpp (limited to 'include') diff --git a/include/DirectConnection.hpp b/include/DirectConnection.hpp index c11871f..4075d12 100644 --- a/include/DirectConnection.hpp +++ b/include/DirectConnection.hpp @@ -1,6 +1,12 @@ #pragma once #include +#include +#include +#include +#include +#include +#include #include "types.hpp" #include "utils.hpp" @@ -20,6 +26,12 @@ namespace sibs 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) @@ -31,19 +43,41 @@ namespace sibs 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(); + DirectConnections(u16 port = 27137); ~DirectConnections(); - void connect(const Ipv4 &address); + // 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 init(); - void cleanup(); + void receiveData(); + bool receiveDataFromPeer(const int socket, char *output); private: + u16 port; int eid; - int mySocket; + std::unordered_map> peers; + std::thread receiveDataThread; + std::mutex peersMutex; + bool alive; }; } diff --git a/include/Log.hpp b/include/Log.hpp new file mode 100644 index 0000000..1301c5b --- /dev/null +++ b/include/Log.hpp @@ -0,0 +1,12 @@ +#pragma once + +namespace sibs +{ + class Log + { + public: + static void debug(const char *fmt, ...); + static void warn(const char *fmt, ...); + static void error(const char *fmt, ...); + }; +} -- cgit v1.2.3