diff options
-rw-r--r-- | include/sibs/DirectConnection.hpp | 9 | ||||
-rw-r--r-- | src/DirectConnection.cpp | 12 |
2 files changed, 15 insertions, 6 deletions
diff --git a/include/sibs/DirectConnection.hpp b/include/sibs/DirectConnection.hpp index d96890b..eb44153 100644 --- a/include/sibs/DirectConnection.hpp +++ b/include/sibs/DirectConnection.hpp @@ -42,6 +42,12 @@ namespace sibs using PubSubReceiveDataCallback = std::function<void(std::shared_ptr<DirectConnectionPeer> peer, MessageType messageType, const void *data, const usize size)>; using PubSubSendDataCallback = std::function<void(PubSubResult result, const std::string &resultStr)>; using PubSubOnRemoveDisconnectedPeerCallback = std::function<void(std::shared_ptr<DirectConnectionPeer> peer)>; + + enum class PeerType + { + SERVER, + CLIENT + }; struct DirectConnectionPeer { @@ -49,6 +55,7 @@ namespace sibs Ipv4 address; PubSubReceiveDataCallback receiveDataCallbackFunc; int sharedKeys = 0; + PeerType type = PeerType::CLIENT; bool operator == (const DirectConnectionPeer &other) const; bool operator != (const DirectConnectionPeer &other) const; @@ -76,7 +83,7 @@ namespace sibs protected: std::unique_ptr<Socket> createSocket(const Ipv4 &addressToBind, bool rendezvous, bool reuseAddr, bool bind = true); private: - void connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind); + void connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind, bool server); void removeDisconnectedPeers(); void receiveData(); int receiveDataFromPeer(const int socket, char *output, usize *receivedTotalSize); diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp index b548a02..a00eeea 100644 --- a/src/DirectConnection.cpp +++ b/src/DirectConnection.cpp @@ -102,12 +102,12 @@ namespace sibs void DirectConnections::connectServer(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc) { - connect(address, false, true, connectCallbackFunc, receiveDataCallbackFunc, true); + connect(address, false, true, connectCallbackFunc, receiveDataCallbackFunc, true, true); } void DirectConnections::connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc) { - connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true); + connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true, false); } static bool isReady(const std::shared_future<PubSubConnectResult> &connectionResultFuture) @@ -115,7 +115,7 @@ namespace sibs return connectionResultFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready; } - void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind) + void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind, bool server) { connectionResultsMutex.lock(); auto it = connectionResults.find(address); @@ -141,7 +141,7 @@ namespace sibs } else { - connectionResults[address] = std::async(std::launch::async, [this, address, rendezvous, reuseAddr, connectCallbackFunc, receiveDataCallbackFunc, bind]() + connectionResults[address] = std::async(std::launch::async, [this, address, rendezvous, reuseAddr, connectCallbackFunc, receiveDataCallbackFunc, bind, server]() { std::shared_ptr<DirectConnectionPeer> peer = std::make_shared<DirectConnectionPeer>(); std::unique_ptr<Socket> socket; @@ -172,6 +172,7 @@ namespace sibs peer->socket = std::move(socket); peer->address = address; peer->receiveDataCallbackFunc = receiveDataCallbackFunc; + peer->type = (server ? PeerType::SERVER : PeerType::CLIENT); peersMutex.unlock(); if(connectCallbackFunc) @@ -240,7 +241,8 @@ namespace sibs std::lock_guard<std::mutex> lock(peersMutex); for(auto &it : peers) { - result.push_back(it.second); + if(it.second->type == PeerType::CLIENT) + result.push_back(it.second); } return result; } |