aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-10-14 04:17:54 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-10-14 04:18:00 +0200
commit55405854c27417ec33797c9e2b438ce17ac70bf2 (patch)
tree2bd3941f879089b32a913203131cd0acae3d968d
parent2a0fa173a0007e549589ed9c1a34a2db3be34361 (diff)
Add peer to listen callback
-rw-r--r--include/sibs/BootstrapConnection.hpp3
-rw-r--r--src/BootstrapConnection.cpp4
-rw-r--r--src/DirectConnection.cpp14
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<void(const void *data, const usize size)>;
+ // @peer is nullptr is data was sent by local user
+ using BoostrapConnectionListenCallbackFunc = std::function<void(const DirectConnectionPeer *peer, const void *data, const usize size)>;
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<DirectConnectionPeer> 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<std::mutex> 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<std::mutex> 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
{