From 3e2c7e59672b282f8e089679ae7f05cfc03e405b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 14 Oct 2018 04:17:54 +0200 Subject: Add peer to listen callback --- include/sibs/BootstrapConnection.hpp | 3 ++- src/BootstrapConnection.cpp | 4 ++-- src/DirectConnection.cpp | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/sibs/BootstrapConnection.hpp b/include/sibs/BootstrapConnection.hpp index e77222d..dd90b7d 100644 --- a/include/sibs/BootstrapConnection.hpp +++ b/include/sibs/BootstrapConnection.hpp @@ -19,7 +19,8 @@ namespace sibs PubsubKeyAlreadyListeningException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; - using BoostrapConnectionListenCallbackFunc = std::function; + // @peer is nullptr is data was sent by local user + using BoostrapConnectionListenCallbackFunc = std::function; class BootstrapConnection { diff --git a/src/BootstrapConnection.cpp b/src/BootstrapConnection.cpp index 9a19943..421dfe1 100644 --- a/src/BootstrapConnection.cpp +++ b/src/BootstrapConnection.cpp @@ -85,7 +85,7 @@ namespace sibs void BootstrapConnection::receiveDataFromPeer(BoostrapConnectionListenCallbackFunc listenCallbackFunc, std::shared_ptr peer, const void *data, const usize size) { if(listenCallbackFunc) - listenCallbackFunc(data, size); + listenCallbackFunc(peer.get(), data, size); } void BootstrapConnection::listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc) @@ -109,7 +109,7 @@ namespace sibs std::lock_guard lock(listenerCallbackFuncMutex); auto listenCallbackFuncIt = listenCallbackFuncs.find(pubsubKey); if(listenCallbackFuncIt != listenCallbackFuncs.end() && listenCallbackFuncIt->second) - listenCallbackFuncIt->second(data->data(), data->size()); + listenCallbackFuncIt->second(nullptr, data->data(), data->size()); } std::lock_guard lock(subscribedPeersMutex); diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp index 540f7cb..71c7f1f 100644 --- a/src/DirectConnection.cpp +++ b/src/DirectConnection.cpp @@ -74,12 +74,26 @@ namespace sibs { if(reuseAddr) { + /* if(UDT::bind(socket, (sockaddr*)&addressToBind.address, sizeof(addressToBind.address)) == UDT::ERROR) { std::string errMsg = "UDT: Failed to bind, error: "; errMsg += UDT::getlasterror_desc(); throw SocketCreateException(errMsg); } + */ + Ipv4 myAddr = addressToBind; + for(int i = 0; i < 2000; ++i) + { + if(UDT::bind(socket, (sockaddr*)&myAddr.address, sizeof(myAddr.address)) == UDT::ERROR) + { + port = (u16)generateRandomNumber(2000, 32000); + myAddr.address.sin_port = htons(port); + } + else + return socket; + } + throw SocketCreateException("UDT: Failed to bind after 2000 tries"); } else { -- cgit v1.2.3