From aac821b1d3f1148a084e097e94f383cff3dad467 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 14 Oct 2018 03:55:36 +0200 Subject: add back shit --- src/DirectConnection.cpp | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp index ec597de..540f7cb 100644 --- a/src/DirectConnection.cpp +++ b/src/DirectConnection.cpp @@ -72,11 +72,29 @@ namespace sibs if(rendezvous || bind) { - if(UDT::bind(socket, (sockaddr*)&addressToBind.address, sizeof(addressToBind.address)) == UDT::ERROR) + if(reuseAddr) { - std::string errMsg = "UDT: Failed to bind, error: "; - errMsg += UDT::getlasterror_desc(); - throw SocketCreateException(errMsg); + 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); + } + } + else + { + 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"); } } @@ -90,23 +108,7 @@ namespace sibs void DirectConnections::connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc) { - Ipv4 myIp(nullptr, port); - - UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, 0); - bool rendezvous = true; - UDT::setsockopt(client, 0, UDT_RENDEZVOUS, &rendezvous, sizeof(bool)); - - if (UDT::ERROR == UDT::bind(client, (sockaddr*)&myIp.address, sizeof(myIp.address))) { - printf("bind error: %s\n", UDT::getlasterror_desc()); - exit(2); - } - - if (UDT::ERROR == UDT::connect(client, (sockaddr*)&address.address, sizeof(address.address))) { - printf("connect error: %s\n", UDT::getlasterror_desc()); - exit(3); - } - printf("connected to peer!\n"); - //connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true); + connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true); } void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind) -- cgit v1.2.3