Send to all broadcast addresses for connecting to wan server in lan

This commit is contained in:
Benau 2018-05-11 11:13:42 +08:00
parent 7926ad9ba0
commit 4a4c99baf2
2 changed files with 30 additions and 16 deletions

View File

@ -23,6 +23,7 @@
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/protocol_manager.hpp" #include "network/protocol_manager.hpp"
#include "network/server.hpp" #include "network/server.hpp"
#include "network/servers_manager.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
#include "online/xml_request.hpp" #include "online/xml_request.hpp"
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"
@ -97,19 +98,27 @@ void RequestConnection::asynchronousUpdate()
(STKHost::get()->isClientServer() ? "-localhost" : (STKHost::get()->isClientServer() ? "-localhost" :
StringUtils::toString(m_server->getPrivatePort()))); StringUtils::toString(m_server->getPrivatePort())));
TransportAddress server_addr;
if (!NetworkConfig::m_disable_lan && if (!NetworkConfig::m_disable_lan &&
m_server->getAddress().getIP() == m_server->getAddress().getIP() ==
STKHost::get()->getPublicAddress().getIP() && STKHost::get()->getPublicAddress().getIP() &&
!STKHost::get()->isClientServer()) !STKHost::get()->isClientServer())
{ {
// If use lan connection in wan server, send a broadcast // If use lan connection in wan server, send to all
server_addr.setIP(0xffffffff); // broadcast address
for (auto& addr :
ServersManager::get()->getBroadcastAddresses())
{
for (int i = 0; i < 5; i++)
{
STKHost::get()->sendRawPacket(message, addr);
StkTime::sleep(1);
}
}
} }
else else
{ {
TransportAddress server_addr;
server_addr.setIP(m_server->getAddress().getIP()); server_addr.setIP(m_server->getAddress().getIP());
}
// Direct socket always listens on server discovery port // Direct socket always listens on server discovery port
server_addr.setPort(NetworkConfig::get() server_addr.setPort(NetworkConfig::get()
->getServerDiscoveryPort()); ->getServerDiscoveryPort());
@ -121,6 +130,8 @@ void RequestConnection::asynchronousUpdate()
STKHost::get()->sendRawPacket(message, server_addr); STKHost::get()->sendRawPacket(message, server_addr);
StkTime::sleep(1); StkTime::sleep(1);
} }
}
m_state = DONE; m_state = DONE;
} }
else else

View File

@ -315,6 +315,8 @@ void ServersManager::setDefaultBroadcastAddresses()
m_broadcast_address.emplace_back(std::string("255.255.255.255")); m_broadcast_address.emplace_back(std::string("255.255.255.255"));
m_broadcast_address.emplace_back(std::string("127.0.0.255") ); m_broadcast_address.emplace_back(std::string("127.0.0.255") );
m_broadcast_address.emplace_back(std::string("127.0.0.1") ); m_broadcast_address.emplace_back(std::string("127.0.0.1") );
for (auto& addr : m_broadcast_address)
addr.setPort(NetworkConfig::get()->getServerDiscoveryPort());
} // setDefaultBroadcastAddresses } // setDefaultBroadcastAddresses
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -381,7 +383,7 @@ void ServersManager::updateBroadcastAddresses()
if (return_code == ERROR_BUFFER_OVERFLOW) if (return_code == ERROR_BUFFER_OVERFLOW)
{ {
Log::warn("NetworkConfig", "Can not get broadcast addresses."); Log::warn("ServerManager", "Can not get broadcast addresses.");
setDefaultBroadcastAddresses(); setDefaultBroadcastAddresses();
return; return;
} }
@ -418,7 +420,8 @@ void ServersManager::updateBroadcastAddresses()
u = (u & 0x33333333) + ((u >> 2) & 0x33333333); u = (u & 0x33333333) + ((u >> 2) & 0x33333333);
u = (((u + (u >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; u = (((u + (u >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
printf("Interface: %s\tAddress: %s\tmask: %x\n", p->ifa_name, Log::debug("ServerManager",
"Interface: %s\tAddress: %s\tmask: %x\n", p->ifa_name,
ta.toString().c_str(), u); ta.toString().c_str(), u);
addAllBroadcastAddresses(ta, u); addAllBroadcastAddresses(ta, u);
} }