diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-06-08 04:16:48 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 22:56:48 +0200 |
commit | 5d42dd6a18e3b8b6eb46739b8a1d15997e229de2 (patch) | |
tree | fea25599cbaa650011489c2685d3299cdbdbd743 /include | |
parent | 824e669ddb12f1c30074c84eb731fa598f92ccfa (diff) |
Make connect asynchronous
Diffstat (limited to 'include')
-rw-r--r-- | include/sibs/BootstrapConnection.hpp | 8 | ||||
-rw-r--r-- | include/sibs/BootstrapNode.hpp | 2 | ||||
-rw-r--r-- | include/sibs/DirectConnection.hpp | 14 |
3 files changed, 16 insertions, 8 deletions
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<BoostrapConnectionListenCallbackFunc> listenCallbackFuncs; PubsubKeyMap<std::vector<std::shared_ptr<DirectConnectionPeer>>> 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 <stdexcept> #include <unordered_map> #include <functional> #include <string> @@ -8,16 +7,17 @@ #include <thread> #include <mutex> #include <vector> +#include <stdexcept> #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<void(PubSubResult result, const std::string &resultStr)>; + using PubSubConnectCallback = std::function<void(std::shared_ptr<DirectConnectionPeer> peer, PubSubResult result, const std::string &resultStr)>; using PubSubReceiveDataCallback = std::function<void(std::shared_ptr<DirectConnectionPeer> peer, const void *data, const usize size)>; using PubSubSendDataCallback = std::function<void(PubSubResult result, const std::string &resultStr)>; @@ -48,14 +48,14 @@ namespace sibs ~DirectConnections(); // Throws ConnectionException on error - std::shared_ptr<DirectConnectionPeer> connectServer(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + void connectServer(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc); // Throws ConnectionException on error - std::shared_ptr<DirectConnectionPeer> connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); + void connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc); void send(const std::shared_ptr<DirectConnectionPeer> &peer, std::shared_ptr<std::vector<u8>> data, PubSubSendDataCallback sendDataCallbackFunc = nullptr); protected: int createSocket(const Ipv4 &addressToBind, bool rendezvous, bool reuseAddr); private: - std::shared_ptr<DirectConnectionPeer> 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); |