From 402a3fce0552abffe2ab2808b9657cd73885771f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 4 Nov 2018 04:51:46 +0100 Subject: Remove server node from getPeers func --- include/sibs/DirectConnection.hpp | 9 ++++++++- 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 peer, MessageType messageType, const void *data, const usize size)>; using PubSubSendDataCallback = std::function; using PubSubOnRemoveDisconnectedPeerCallback = std::function 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 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 &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 peer = std::make_shared(); std::unique_ptr 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 lock(peersMutex); for(auto &it : peers) { - result.push_back(it.second); + if(it.second->type == PeerType::CLIENT) + result.push_back(it.second); } return result; } -- cgit v1.2.3