diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-10-14 03:55:36 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 22:56:48 +0200 |
commit | aac821b1d3f1148a084e097e94f383cff3dad467 (patch) | |
tree | 02b06cf950b4ebc4e5d3e7626fa6ab39b3d876a1 | |
parent | fdbcee4063db89a8ba49cc5c2367376884f4b9d4 (diff) |
add back shit
-rw-r--r-- | src/DirectConnection.cpp | 44 |
1 files changed, 23 insertions, 21 deletions
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) |