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 --- src/BootstrapConnection.cpp | 4 ++-- src/DirectConnection.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src') 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