Skip to content

Commit

Permalink
addendum 318c003
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed Feb 1, 2018
1 parent 79a95e5 commit 5048e2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
42 changes: 17 additions & 25 deletions rts/Net/AutohostInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,68 +116,60 @@ std::string AutohostInterface::TryBindSocket(
const std::string& remoteIP, int remotePort,
const std::string& localIP, int localPort)
{
std::string errorMsg = "";
std::string errorMsg;

ip::address localAddr;
ip::address remoteAddr;
asio::error_code err;

try {
socket.open(ip::udp::v6(), err); // test IP v6 support

const bool supportsIPv6 = !err;

remoteAddr = netcode::WrapIP(remoteIP, &err);
if (err) {

if (err)
throw std::runtime_error("Failed to parse address " + remoteIP + ": " + err.message());
}

if (!supportsIPv6 && remoteAddr.is_v6()) {
if (!supportsIPv6 && remoteAddr.is_v6())
throw std::runtime_error("IP v6 not supported, can not use address " + remoteAddr.to_string());
}

if (localIP.empty()) {
// use the "any" address as local "from"
if (remoteAddr.is_v6()) {
localAddr = ip::address_v6::any();
} else {
if (supportsIPv6) {
socket.close();
}
socket.close();
socket.open(ip::udp::v4());

localAddr = ip::address_v4::any();
}
} else {
localAddr = netcode::WrapIP(localIP, &err);
if (err) {

if (err)
throw std::runtime_error("Failed to parse local IP " + localIP + ": " + err.message());
}
if (localAddr.is_v6() != remoteAddr.is_v6()) {

if (localAddr.is_v6() != remoteAddr.is_v6())
throw std::runtime_error("Local IP " + localAddr.to_string() + " and remote IP " + remoteAddr.to_string() + " are IP v4/v6 mixed");
}
}

socket.bind(ip::udp::endpoint(localAddr, localPort));

asio::socket_base::non_blocking_io command(true);
socket.io_control(command);

// A similar, slighly less verbose message is already in GameServer
//LOG("Connecting (UDP) to IP (v%i) %s Port %i",
// (remoteAddr.is_v6() ? 6 : 4), remoteAddr.c_str(), remotePort);
socket.non_blocking(true);
socket.connect(ip::udp::endpoint(remoteAddr, remotePort));
} catch (const std::runtime_error& ex) { // includes also asio::system_error, as it inherits from runtime_error
} catch (const std::runtime_error& ex) {
// also includes asio::system_error, inherits from runtime_error
socket.close();
errorMsg = ex.what();
if (errorMsg.empty()) {

if (errorMsg.empty())
errorMsg = "Unknown problem";
}
}

return errorMsg;
}

AutohostInterface::~AutohostInterface()
{
}

void AutohostInterface::SendStart()
{
Expand Down
2 changes: 1 addition & 1 deletion rts/Net/AutohostInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AutohostInterface
*/
AutohostInterface(const std::string& remoteIP, int remotePort,
const std::string& localIP = "", int localPort = 0);
virtual ~AutohostInterface();
virtual ~AutohostInterface() {}

bool IsInitialized() const { return initialized; }

Expand Down

0 comments on commit 5048e2a

Please sign in to comment.