Skip to content

Commit

Permalink
Network|Refactor: Sending data over the network
Browse files Browse the repository at this point in the history
Continuing to prepare the old network subsystem for moving to libdoomsday.

The low-level buffer sending mechanism is now handled by DoomsdayApp. This allows it to be handled differently depending on whether the client or the server is sending the data.
  • Loading branch information
skyjake committed Jul 17, 2020
1 parent 84e6ce9 commit 7490ad2
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 54 deletions.
1 change: 1 addition & 0 deletions doomsday/apps/client/include/clientapp.h
Expand Up @@ -118,6 +118,7 @@ class ClientApp : public de::BaseGuiApp, public DoomsdayApp
void unloadGame(const GameProfile &upcomingGame) override;
void gameSessionWasSaved(const AbstractSession &session, GameStateFolder &toFolder) override;
void gameSessionWasLoaded(const AbstractSession &session, const GameStateFolder &fromFolder) override;
void sendDataToPlayer(int player, const de::IByteArray &data) override;

private:
DE_PRIVATE(d)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/apps/client/include/network/net_buf.h
Expand Up @@ -102,7 +102,7 @@ void N_ClearMessages(void);
* Handles broadcasts using recursion.
* Clients can only send packets to the server.
*/
void N_SendPacket(int flags);
void N_SendPacket(void);

/**
* An attempt is made to extract a message from the message queue.
Expand Down
6 changes: 6 additions & 0 deletions doomsday/apps/client/src/clientapp.cpp
Expand Up @@ -926,6 +926,12 @@ void ClientApp::gameSessionWasLoaded(const AbstractSession &session,
}
}

void ClientApp::sendDataToPlayer(int, const de::IByteArray &data)
{
// We can only send data to the server.
serverLink() << data;
}

ClientPlayer &ClientApp::player(int console) // static
{
return DoomsdayApp::players().at(console).as<ClientPlayer>();
Expand Down
57 changes: 6 additions & 51 deletions doomsday/apps/client/src/network/base/net_buf.cpp
Expand Up @@ -214,68 +214,23 @@ void N_ClearMessages()
::entryCount = 0;
}

void N_SendPacket(dint flags)
void N_SendPacket(void)
{
#ifdef __SERVER__
duint dest = 0;
#else
DE_UNUSED(flags);
#endif

// Is the network available?
if(!::allowSending)
return;

// Figure out the destination DPNID.
#ifdef __SERVER__
try
{
if(::netBuffer.player >= 0 && ::netBuffer.player < DDMAXPLAYERS)
{
if(!DD_Player(::netBuffer.player)->isConnected())
{
// Do not send anything to disconnected players.
return;
}

dest = DD_Player(::netBuffer.player)->remoteUserId;
}
else
if (allowSending)
{
// Broadcast to all non-local players, using recursive calls.
for(dint i = 0; i < DDMAXPLAYERS; ++i)
{
::netBuffer.player = i;
N_SendPacket(flags);
}

// Reset back to -1 to notify of the broadcast.
::netBuffer.player = NSP_BROADCAST;
return;
DoomsdayApp::app().sendDataToPlayer(
netBuffer.player,
ByteRefArray(&netBuffer.msg, netBuffer.headerLength + netBuffer.length));
}
}
#endif

try
{
#ifdef __CLIENT__
de::Transmitter &out = Net_ServerLink();
#else
de::Transmitter &out = App_ServerSystem().user(dest);
#endif

out << de::ByteRefArray(&::netBuffer.msg, ::netBuffer.headerLength + ::netBuffer.length);
}
catch(const Error &er)
{
LOGDEV_NET_WARNING("N_SendPacket failed: ") << er.asText();
}
}

//void N_AddSentBytes(dsize bytes)
//{
// ::numSentBytes += bytes;
//}

dint N_IdentifyPlayer(nodeid_t id)
{
#ifdef __SERVER__
Expand Down
2 changes: 1 addition & 1 deletion doomsday/apps/client/src/network/base/net_main.cpp
Expand Up @@ -146,7 +146,7 @@ void Net_SendBuffer(int toPlayer, int spFlags)
return;

// Send the packet to the network.
N_SendPacket(spFlags);
N_SendPacket();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion doomsday/apps/client/src/network/base/net_ping.cpp
Expand Up @@ -88,7 +88,7 @@ void Net_SendPing(int player, int count)

// Update the length of the message.
::netBuffer.player = player;
N_SendPacket(10000);
N_SendPacket();
}

// Called when a ping packet comes in.
Expand Down
1 change: 1 addition & 0 deletions doomsday/apps/server/include/serverapp.h
Expand Up @@ -69,6 +69,7 @@ class ServerApp : public de::TextApp, public DoomsdayApp
protected:
void reset() override;
void unloadGame(const GameProfile &upcomingGame) override;
void sendDataToPlayer(int player, const de::IByteArray &data) override;

private:
DE_PRIVATE(d)
Expand Down
32 changes: 32 additions & 0 deletions doomsday/apps/server/src/serverapp.cpp
Expand Up @@ -355,6 +355,38 @@ void ServerApp::unloadGame(const GameProfile &upcomingGame)
world::Map::initDummyElements();
}

void ServerApp::sendDataToPlayer(int player, const IByteArray &data)
{
List<Id::Type> ids;

if (player >= 0 && player < DDMAXPLAYERS)
{
auto &plr = players().at(player).as<ServerPlayer>();
// Do not send anything to disconnected players.
if (plr.isConnected())
{
ids << plr.remoteUserId;
}
}
else
{
// Broadcast to all non-local players, using recursive calls.
for (dint i = 1; i < DDMAXPLAYERS; ++i)
{
auto &plr = players().at(player).as<ServerPlayer>();
if (plr.isConnected())
{
ids << plr.remoteUserId;
}
}
}

for (const auto &id : ids)
{
serverSystem().user(id) << data;
}
}

ServerApp &ServerApp::app()
{
DE_ASSERT(serverAppSingleton != nullptr);
Expand Down
8 changes: 8 additions & 0 deletions doomsday/libs/doomsday/include/doomsday/doomsdayapp.h
Expand Up @@ -29,6 +29,7 @@
#include <de/info.h>
#include <de/nativepath.h>
#include <de/packagedownloader.h>
#include <de/transmitter.h>

#include <string>

Expand Down Expand Up @@ -190,6 +191,13 @@ class LIBDOOMSDAY_PUBLIC DoomsdayApp
virtual void gameSessionWasLoaded(const AbstractSession &session,
const GameStateFolder &fromFolder);

/**
* Sends data to a player. Available during multiplayer games.
*
* @param player Player number (0 is always the server).
*/
virtual void sendDataToPlayer(int player, const de::IByteArray &data);

public:
static DoomsdayApp & app();
static de::PackageDownloader &packageDownloader();
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libs/doomsday/src/doomsdayapp.cpp
Expand Up @@ -970,6 +970,11 @@ void DoomsdayApp::gameSessionWasLoaded(const AbstractSession &, const GameStateF
//qDebug() << "App loading from" << fromFolder.description();
}

void DoomsdayApp::sendDataToPlayer(int, const de::IByteArray &)
{
throw Error("DoomsdayApp::sendDataToPlayer", "Network transmission is not available");
}

void DoomsdayApp::setGame(const Game &game)
{
app().d->currentGame = const_cast<Game *>(&game);
Expand Down

0 comments on commit 7490ad2

Please sign in to comment.