From 5d42dd6a18e3b8b6eb46739b8a1d15997e229de2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 8 Jun 2018 04:16:48 +0200 Subject: Make connect asynchronous --- include/sibs/BootstrapConnection.hpp | 8 ++++++++ include/sibs/BootstrapNode.hpp | 2 +- include/sibs/DirectConnection.hpp | 14 +++++++------- 3 files changed, 16 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/sibs/BootstrapConnection.hpp b/include/sibs/BootstrapConnection.hpp index 094ee32..e77222d 100644 --- a/include/sibs/BootstrapConnection.hpp +++ b/include/sibs/BootstrapConnection.hpp @@ -7,6 +7,12 @@ namespace sibs { + class BootstrapConnectionException : public std::runtime_error + { + public: + BootstrapConnectionException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + class PubsubKeyAlreadyListeningException : public std::runtime_error { public: @@ -19,6 +25,7 @@ namespace sibs { DISABLE_COPY(BootstrapConnection) public: + // Throws BootstrapConnectionException on error BootstrapConnection(const Ipv4 &bootstrapAddress); // Throws PubsubKeyAlreadyListeningException if we are already listening on the key @pubsubKey @@ -33,5 +40,6 @@ namespace sibs PubsubKeyMap listenCallbackFuncs; PubsubKeyMap>> subscribedPeers; std::mutex listenerCallbackFuncMutex; + std::mutex subscribedPeersMutex; }; } diff --git a/include/sibs/BootstrapNode.hpp b/include/sibs/BootstrapNode.hpp index 13a62e1..48e527c 100644 --- a/include/sibs/BootstrapNode.hpp +++ b/include/sibs/BootstrapNode.hpp @@ -17,7 +17,7 @@ namespace sibs { DISABLE_COPY(BootstrapNode) public: - // Throws BootstrapException on error + // Throws SocketCreateException or BootstrapException on error BootstrapNode(const Ipv4 &address); ~BootstrapNode(); private: diff --git a/include/sibs/DirectConnection.hpp b/include/sibs/DirectConnection.hpp index 45ba5d9..b7c467e 100644 --- a/include/sibs/DirectConnection.hpp +++ b/include/sibs/DirectConnection.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -8,16 +7,17 @@ #include #include #include +#include #include "IpAddress.hpp" #include "../types.hpp" #include "../utils.hpp" namespace sibs { - class ConnectionException : public std::runtime_error + class SocketCreateException : public std::runtime_error { public: - ConnectionException(const std::string &errMsg) : std::runtime_error(errMsg) {} + SocketCreateException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; enum class PubSubResult @@ -28,7 +28,7 @@ namespace sibs struct DirectConnectionPeer; - using PubSubConnectCallback = std::function; + using PubSubConnectCallback = std::function peer, PubSubResult result, const std::string &resultStr)>; using PubSubReceiveDataCallback = std::function peer, const void *data, const usize size)>; using PubSubSendDataCallback = std::function; @@ -48,14 +48,14 @@ namespace sibs ~DirectConnections(); // Throws ConnectionException on error - std::shared_ptr connectServer(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + void connectServer(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc); // Throws ConnectionException on error - std::shared_ptr connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + void connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc); void send(const std::shared_ptr &peer, std::shared_ptr> data, PubSubSendDataCallback sendDataCallbackFunc = nullptr); protected: int createSocket(const Ipv4 &addressToBind, bool rendezvous, bool reuseAddr); private: - std::shared_ptr connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubReceiveDataCallback receiveDataCallbackFunc); + void connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc); void receiveData(); int receiveDataFromPeer(const int socket, char *output); bool removePeer(int peerSocket); -- cgit v1.2.3