Skip to content

Commit

Permalink
Fixed|Cleanup|Network|libshell: Server information port number
Browse files Browse the repository at this point in the history
When sending out information, always use the default port number
instead of zero. Also, replace port zero with the default in incoming
data.
  • Loading branch information
skyjake committed Nov 13, 2016
1 parent d716f3e commit e3b7bc2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 51 deletions.
7 changes: 1 addition & 6 deletions doomsday/apps/client/src/network/base/masterserver.cpp
Expand Up @@ -224,12 +224,7 @@ bool MasterWorker::parseResponse(QByteArray const &response)
LOG_NET_WARNING("Server information was in unexpected format");
continue;
}
shell::ServerInfo svInfo = *entryValue->as<RecordValue>().record();
// Include the port number in the host address.
Address addr = svInfo.address();
addr.setPort(svInfo.port());
svInfo.setAddress(addr);
d->servers.append(svInfo);
d->servers.append(*entryValue->as<RecordValue>().record());
}
catch (Error const &er)
{
Expand Down
20 changes: 3 additions & 17 deletions doomsday/apps/client/src/network/serverlink.cpp
Expand Up @@ -432,12 +432,7 @@ QList<Address> ServerLink::foundServers(FoundMask mask) const

bool ServerLink::isFound(Address const &host, FoundMask mask) const
{
Address addr = host;
if (!addr.port())
{
// Zero means default port.
addr.setPort(shell::DEFAULT_PORT);
}
Address const addr = shell::checkPort(host);
return d->allFound(mask).contains(addr);
}

Expand All @@ -452,22 +447,13 @@ bool ServerLink::foundServerInfo(int index, shell::ServerInfo &info, FoundMask m

bool ServerLink::isServerOnLocalNetwork(Address const &host) const
{
Address addr = host;
if (!addr.port())
{
addr.setPort(shell::DEFAULT_PORT);
}
Address const addr = shell::checkPort(host);
return d->finder.foundServers().contains(addr);
}

bool ServerLink::foundServerInfo(de::Address const &host, shell::ServerInfo &info, FoundMask mask) const
{
Address addr = host;
if (!addr.port())
{
addr.setPort(shell::DEFAULT_PORT);
}

Address const addr = shell::checkPort(host);
Impl::Servers const all = d->allFound(mask);
if (!all.contains(addr)) return false;
info = all[addr];
Expand Down
14 changes: 0 additions & 14 deletions doomsday/apps/server/src/serverapp.cpp
Expand Up @@ -248,20 +248,14 @@ shell::ServerInfo ServerApp::currentServerInfo()
reinterpret_cast<char const *>(gx.GetVariable(DD_PLUGIN_NAME)),
reinterpret_cast<char const *>(gx.GetVariable(DD_PLUGIN_VERSION_SHORT))));

//dd_snprintf(info->plugin, sizeof(info->plugin) - 1, "%s %s", ;
info.setGameId(game().id());
info.setGameConfig(reinterpret_cast<char const *>(gx.GetVariable(DD_GAME_CONFIG)));
info.setName(serverName);
info.setDescription(serverInfo);
//info->numPlayers = Sv_GetNumPlayers();

// The server player is there, it's just hidden.
info.setMaxPlayers(de::min(svMaxPlayers, DDMAXPLAYERS - (isDedicated ? 1 : 0)));

// Don't go over the limit.
//if (info->maxPlayers > ::svMaxPlayers)
// info->maxPlayers = ::svMaxPlayers;

//info->canJoin = ;
shell::ServerInfo::Flags flags(0);
if (isServer != 0 && Sv_GetNumPlayers() < svMaxPlayers)
Expand All @@ -275,29 +269,21 @@ shell::ServerInfo ServerApp::currentServerInfo()
{
auto &map = world().map();
String const mapPath = (map.hasManifest() ? map.manifest().composeUri().path() : "(unknown map)");
//qstrncpy(info->map, mapPath.toUtf8().constData(), sizeof(info->map) - 1);
info.setMap(mapPath);
}

// These are largely unused at the moment... Mainly intended for the game's custom values.
//std::memcpy(info->data, ::serverData, sizeof(info->data));

QHostInfo const host = QHostInfo::fromName(QHostInfo::localHostName());
if (!host.addresses().isEmpty())
{
/// @todo Maybe check that it's not a loopback address?
info.setAddress(Address(host.addresses().at(0), duint16(nptIPPort)));
}

// Also include the port we're using.
//info->port = ::nptIPPort;

// Let's compile a list of client names.
for (dint i = 0; i < DDMAXPLAYERS; ++i)
{
if (DD_Player(i)->isConnected())
{
//M_LimitedStrCat(info->clientNames, DD_Player(i)->name, 15, ';', sizeof(info->clientNames));
info.addPlayer(DD_Player(i)->name);
}
}
Expand Down
9 changes: 8 additions & 1 deletion doomsday/sdk/libshell/include/de/shell/libshell.h
Expand Up @@ -19,8 +19,9 @@
#ifndef LIBSHELL_MAIN_H
#define LIBSHELL_MAIN_H

#include <de/String>
#include <de/Address>
#include <de/Range>
#include <de/String>

/** @defgroup shell Shell Access */

Expand Down Expand Up @@ -55,6 +56,12 @@ namespace shell {
// Default TCP/UDP port for servers to listen on.
static duint16 const DEFAULT_PORT = 13209;

inline Address checkPort(Address const &address)
{
if (address.port() == 0) return Address(address.host(), DEFAULT_PORT);
return address;
}

/**
* Line of word-wrapped text.
*/
Expand Down
30 changes: 17 additions & 13 deletions doomsday/sdk/libshell/src/serverfinder.cpp
Expand Up @@ -37,7 +37,7 @@ DENG2_PIMPL_NOREF(ServerFinder)
Beacon beacon;
struct Found
{
Record *message;
shell::ServerInfo *message;
Time at;

Found() : message(0), at(Time()) {}
Expand Down Expand Up @@ -130,15 +130,14 @@ int ServerFinder::maxPlayers(Address const &server) const

ServerInfo ServerFinder::messageFromServer(Address const &address) const
{
if (!d->servers.contains(address))
Address addr = shell::checkPort(address);
if (!d->servers.contains(addr))
{
/// @throws NotFoundError @a address not found in the registry of server responses.
throw NotFoundError("ServerFinder::messageFromServer",
"No message from server " + address.asText());
"No message from server " + addr.asText());
}
ServerInfo info(*d->servers[address].message);
info.setAddress(Address(address.host(), info.port()));
return info;
return *d->servers[addr].message;
}

void ServerFinder::found(Address host, Block block)
Expand All @@ -151,21 +150,26 @@ void ServerFinder::found(Address host, Block block)
LOG_TRACE("Received a server message from %s with %i bytes")
<< host << block.size();

shell::ServerInfo receivedInfo;
Reader(block).withHeader() >> receivedInfo;
receivedInfo.setAddress(host);

Address const from = receivedInfo.address(); // port validated

// Replace or insert the information for this host.
Impl::Found found;
if (d->servers.contains(host))
if (d->servers.contains(from))
{
found.message = d->servers[host].message;
d->servers[host].at = Time();
*d->servers[from].message = receivedInfo;
d->servers[from].at = Time();
}
else
{
found.message = new Record;
d->servers.insert(host, found);
found.message = new shell::ServerInfo(receivedInfo);
d->servers.insert(from, found);
}
Reader(block).withHeader() >> *found.message;

//qDebug() << "Server found:\n" << found.message->asText().toLatin1().constData();
//qDebug() << "Server found:\n" << receivedInfo.asText().toLatin1().constData();

emit updated();
}
Expand Down

0 comments on commit e3b7bc2

Please sign in to comment.