diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-06-02 01:39:25 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 22:56:48 +0200 |
commit | d2dde382e6423afd88217128dbc66dde74415dca (patch) | |
tree | fc6259687ccf451171a27566ce0c9f40cbbb9048 /include | |
parent | 441cf81acf9dff087addfa8d01a61e4513b1dd6e (diff) |
Receive data with epoll_wait
Diffstat (limited to 'include')
-rw-r--r-- | include/DirectConnection.hpp | 44 | ||||
-rw-r--r-- | include/Log.hpp | 12 |
2 files changed, 51 insertions, 5 deletions
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 <stdexcept> +#include <unordered_map> +#include <functional> +#include <string> +#include <memory> +#include <thread> +#include <mutex> #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<void(PubSubConnectResult result, const std::string &resultStr)>; + using PubSubReceiveDataCallback = std::function<void(const void *data, const usize size)>; + + 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<DirectConnectionPeer> connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + // Throws SendException on error + void send(const std::shared_ptr<DirectConnectionPeer> &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<int, std::shared_ptr<DirectConnectionPeer>> 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, ...); + }; +} |