diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-10-14 03:55:36 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-10-14 03:55:39 +0200 |
commit | 2a0fa173a0007e549589ed9c1a34a2db3be34361 (patch) | |
tree | 44ccdf3788cdc3ac0e46b6c3f2383fa55b51fa70 /src | |
parent | 8ce21af2486b2da3273d554ffdf058cb07f49a8f (diff) |
add back shit
Diffstat (limited to 'src')
-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) |