Minor refactoring of lan handling, added client's ip address and port

to the answer from the client (which can later be used when the
client requests a connection from a server).
This commit is contained in:
hiker 2015-11-10 17:00:47 +11:00
parent 833c20ceff
commit 51411a796a
3 changed files with 43 additions and 24 deletions

View File

@ -186,6 +186,7 @@ STKHost::STKHost(const irr::core::stringw &server_name)
void STKHost::init()
{
m_network = NULL;
m_lan_network = NULL;
m_listening_thread = NULL;
m_game_setup = NULL;
m_is_registered = false;
@ -411,39 +412,18 @@ void* STKHost::mainLoop(void* self)
STKHost* myself = (STKHost*)(self);
ENetHost* host = myself->m_network->getENetHost();
// Separate network/socket connection in case of LAN server
// listening to broadcast.
Network *discovery_host = NULL;
if(myself->isServer() && STKHost::isLAN())
{
TransportAddress address(0, 2757);
ENetAddress eaddr = address.toEnetAddress();
discovery_host = new Network(1, 1, 0, 0, &eaddr);
myself->m_lan_network = new Network(1, 1, 0, 0, &eaddr);
}
const int LEN=2048;
char buffer[LEN];
while (!myself->mustStopListening())
{
if(discovery_host)
if(myself->m_lan_network)
{
TransportAddress sender;
int len= discovery_host->receiveRawPacket(buffer, LEN, &sender, 1);
if(len>0 && std::string(buffer, len)=="stk-server")
{
Log::verbose("STKHost", "Received LAN server query");
std::string name = StringUtils::wide_to_utf8(
myself->get()->getServerName().c_str());
if(name.size()>2)
name = name.substr(0, 255);
NetworkString s;
s.addUInt8((uint8_t)name.size());
s.addString(name.c_str());
s.addUInt8(getMaxPlayers());
s.addUInt8(1); // FIXME: current number of connected players
discovery_host->sendRawPacket(s.getBytes(), s.size(), sender);
} // if message is server-requested
myself->handleLANRequests();
} // if discovery host
while (enet_host_service(host, &event, 20) != 0)
@ -490,6 +470,39 @@ void* STKHost::mainLoop(void* self)
return NULL;
} // mainLoop
// ----------------------------------------------------------------------------
void STKHost::handleLANRequests()
{
const int LEN=2048;
char buffer[LEN];
TransportAddress sender;
int len = m_lan_network->receiveRawPacket(buffer, LEN, &sender, 1);
if(len<=0) return;
if (std::string(buffer, len) == "stk-server")
{
Log::verbose("STKHost", "Received LAN server query");
std::string name = StringUtils::wide_to_utf8(getServerName().c_str());
// Avoid buffer overflows
if (name.size() > 255)
name = name.substr(0, 255);
// Send the answer, consisting of server name, max players,
// current players, and the client's ip address and port
// number (which solves the problem which network interface
// might be the right one if there is more than one).
NetworkString s;
s.addUInt8((uint8_t)name.size());
s.addString(name.c_str());
s.addUInt8(getMaxPlayers());
s.addUInt8(0); // FIXME: current number of connected players
s.addUInt32(sender.getIP());
s.addUInt16(sender.getPort());
m_lan_network->sendRawPacket(s.getBytes(), s.size(), sender);
} // if message is server-requested
} // handleLANRequests
// ----------------------------------------------------------------------------
/** \brief Tells if a peer is known.
* \return True if the peer is known, false elseway.

View File

@ -67,6 +67,9 @@ private:
/** ENet host interfacing sockets. */
Network* m_network;
/** A separate network connection (socket) to handle LAN requests. */
Network *m_lan_network;
/** Network console */
NetworkConsole *m_network_console;
@ -110,6 +113,7 @@ private:
STKHost(const irr::core::stringw &server_name);
virtual ~STKHost();
void init();
void handleLANRequests();
public:

View File

@ -177,6 +177,8 @@ XMLRequest* ServersManager::getLANRefreshRequest() const
StringUtils::utf8_to_wide(name.c_str());
uint8_t max_players = s.getUInt8(1+name_len );
uint8_t players = s.getUInt8(1+name.size()+1);
uint32_t my_ip = s.getUInt32(1+name.size()+2);
uint32_t my_port = s.getUInt16(1+name.size()+6);
ServersManager::get()
->addServer(new Server(name_w, /*lan*/true,
max_players, players,