Skip to content

Commit

Permalink
Server|libshell: Only include crucial information in a server broadcast
Browse files Browse the repository at this point in the history
The Beacon advertisement of a server must fit in a UDP packet, so
only include the most important information about it.
  • Loading branch information
skyjake committed Nov 8, 2016
1 parent 9d3d5ad commit 5632821
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion doomsday/apps/server/src/serversystem.cpp
Expand Up @@ -148,7 +148,7 @@ DENG2_PIMPL(ServerSystem)
if (serverSock && App_World().hasMap())
{
Block msg;
de::Writer(msg).withHeader() << ServerApp::currentServerInfo();
de::Writer(msg).withHeader() << ServerApp::currentServerInfo().strippedForBroadcast();
beacon.setMessage(msg);
}
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/net/listensocket.cpp
Expand Up @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/ListenSocket"
Expand Down
3 changes: 2 additions & 1 deletion doomsday/sdk/libshell/include/de/shell/serverinfo.h
Expand Up @@ -48,10 +48,10 @@ class LIBSHELL_PUBLIC ServerInfo : public Record
ServerInfo &operator = (ServerInfo &&moved);

Version version() const;
ProtocolVersion protocolVersion() const;
int compatibilityVersion() const;

Address address() const;
duint16 port() const;
String name() const;
String description() const;
String pluginDescription() const;
Expand All @@ -66,6 +66,7 @@ class LIBSHELL_PUBLIC ServerInfo : public Record

String asStyledText() const;
Block asJSON() const;
Record strippedForBroadcast() const;

ServerInfo &setCompatibilityVersion(int compatVersion);
ServerInfo &setAddress(Address const &address);
Expand Down
53 changes: 36 additions & 17 deletions doomsday/sdk/libshell/src/serverinfo.cpp
Expand Up @@ -25,39 +25,45 @@
namespace de {
namespace shell {

static String const VAR_VERSION ("version");
static String const VAR_PROTOCOL_VERSION ("proto");
static String const VAR_COMPATIBILITY_VERSION ("compat");
static String const VAR_VERSION ("ver");
static String const VAR_COMPATIBILITY_VERSION ("cver");
static String const VAR_HOST ("host");
static String const VAR_PORT ("port");
static String const VAR_NAME ("name");
static String const VAR_DESCRIPTION ("desc");
static String const VAR_PLUGIN ("plugin");
static String const VAR_PACKAGES ("pkgs");
static String const VAR_GAME_ID ("game");
static String const VAR_GAME_CONFIG ("conf");
static String const VAR_GAME_CONFIG ("cfg");
static String const VAR_MAP ("map");
static String const VAR_PLAYERS ("players");
static String const VAR_MAX_PLAYERS ("plmax");
static String const VAR_PLAYERS ("plrs");
static String const VAR_PLAYER_COUNT ("pnum");
static String const VAR_MAX_PLAYERS ("pmax");
static String const VAR_FLAGS ("flags");

ServerInfo::ServerInfo()
{
set(VAR_VERSION, Version::currentBuild().asText());
set(VAR_PROTOCOL_VERSION, DENG2_PROTOCOL_LATEST);
set(VAR_VERSION, Version::currentBuild().baseNumber());
addArray(VAR_PLAYERS);
}

ServerInfo::ServerInfo(ServerInfo const &other)
: Record(other)
{}
{
if (!has(VAR_PLAYERS)) addArray(VAR_PLAYERS);
}

ServerInfo::ServerInfo(Record const &rec)
: Record(rec)
{}
{
if (!has(VAR_PLAYERS)) addArray(VAR_PLAYERS);
}

ServerInfo::ServerInfo(ServerInfo &&moved)
: Record(std::move(moved))
{}
{
if (!has(VAR_PLAYERS)) addArray(VAR_PLAYERS);
}

ServerInfo &ServerInfo::operator = (ServerInfo const &other)
{
Expand All @@ -76,11 +82,6 @@ Version ServerInfo::version() const
return Version(gets(VAR_VERSION));
}

ProtocolVersion ServerInfo::protocolVersion() const
{
return ProtocolVersion(geti(VAR_PROTOCOL_VERSION));
}

int ServerInfo::compatibilityVersion() const
{
return geti(VAR_COMPATIBILITY_VERSION);
Expand All @@ -100,9 +101,15 @@ Address ServerInfo::address() const
ServerInfo &ServerInfo::setAddress(Address const &address)
{
set(VAR_HOST, address.asText());
set(VAR_PORT, address.port());
return *this;
}

duint16 ServerInfo::port() const
{
return duint16(geti(VAR_PORT, shell::DEFAULT_PORT));
}

String ServerInfo::name() const
{
return gets(VAR_NAME);
Expand Down Expand Up @@ -191,13 +198,14 @@ StringList ServerInfo::players() const

int ServerInfo::playerCount() const
{
return int(geta(VAR_PLAYERS).size());
return geti(VAR_PLAYER_COUNT, 0);
}

ServerInfo &ServerInfo::addPlayer(String const &playerName)
{
ArrayValue &players = member(VAR_PLAYERS).value<ArrayValue>();
players.add(playerName);
set(VAR_PLAYER_COUNT, players.size());
return *this;
}

Expand All @@ -209,6 +217,7 @@ ServerInfo &ServerInfo::removePlayer(String const &playerName)
if (players.at(i).asText() == playerName)
{
players.remove(i);
set(VAR_PLAYER_COUNT, players.size());
break;
}
}
Expand Down Expand Up @@ -266,6 +275,16 @@ Block ServerInfo::asJSON() const
return composeJSON(*this);
}

Record ServerInfo::strippedForBroadcast() const
{
Record stripped(*this);
if (stripped.has(VAR_HOST)) stripped.remove(VAR_HOST); // address in network msg
if (stripped.has(VAR_PLUGIN)) stripped.remove(VAR_PLUGIN); // gameId+version is enough
if (stripped.has(VAR_PLAYERS)) stripped.remove(VAR_PLAYERS); // count is enough
if (stripped.has(VAR_PACKAGES)) stripped.remove(VAR_PACKAGES); // queried before connecting
return stripped;
}

ServerInfo &ServerInfo::setFlags(Flags const &flags)
{
set(VAR_FLAGS, duint32(flags));
Expand Down

0 comments on commit 5632821

Please sign in to comment.