Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and optimize semi-broadcast behavior #921

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SDK/include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ struct INetwork : public IExtensible

/// Update server parameters
virtual void update() = 0;

/// Attempt to broadcast a packet to a specific list of players on this network
/// @param data The data span with the length in BITS
/// @param players list of players to send packet to
/// @param exceptPeer send RPC to everyone except this peer
/// @param dispatchEvents dispatch packet related events
virtual bool broadcastPacketToSome(Span<uint8_t> data, int channel, const FlatPtrHashSet<IPlayer>& players, const IPlayer* exceptPeer, bool dispatchEvents = true) = 0;

/// Attempt to broadcast an RPC to a specific list of players on this network
/// @param id The RPC ID for the current network
/// @param data The data span with the length in BITS
/// @param players list of players to send RPC to
/// @param exceptPeer send RPC to everyone except this peer
/// @param dispatchEvents dispatch RPC related events
virtual bool broadcastRPCToSome(int id, Span<uint8_t> data, int channel, const FlatPtrHashSet<IPlayer>& players, const IPlayer* exceptPeer, bool dispatchEvents = true) = 0;
};

/// A component interface which allows for writing a network component
Expand Down
14 changes: 8 additions & 6 deletions Server/Components/Actors/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct PlayerActorData final : IExtension
class Actor final : public IActor, public PoolIDProvider, public NoCopy
{
private:
ICore& core_;
int virtualWorld_;
int16_t skin_;
bool invulnerable_;
Expand Down Expand Up @@ -103,8 +104,9 @@ class Actor final : public IActor, public PoolIDProvider, public NoCopy
}
}

Actor(int skin, Vector3 pos, float angle, bool* allAnimationLibraries, bool* validateAnimations, ICustomModelsComponent*& modelsComponent, IFixesComponent* fixesComponent)
: virtualWorld_(0)
Actor(int skin, Vector3 pos, float angle, bool* allAnimationLibraries, bool* validateAnimations, ICore& core, ICustomModelsComponent*& modelsComponent, IFixesComponent* fixesComponent)
: core_(core)
, virtualWorld_(0)
, skin_(skin)
, invulnerable_(true)
, animationLoop_(false)
Expand All @@ -125,7 +127,7 @@ class Actor final : public IActor, public PoolIDProvider, public NoCopy
NetCode::RPC::SetActorHealthForPlayer RPC;
RPC.ActorID = poolID;
RPC.Health = health_;
PacketHelper::broadcastToSome(RPC, streamedFor_.entries());
PacketHelper::broadcastToSome(RPC, core_.getNetworks(), streamedFor_.entries());
}

float getHealth() const override
Expand Down Expand Up @@ -196,7 +198,7 @@ class Actor final : public IActor, public PoolIDProvider, public NoCopy

NetCode::RPC::ClearActorAnimationsForPlayer RPC;
RPC.ActorID = poolID;
PacketHelper::broadcastToSome(RPC, streamedFor_.entries());
PacketHelper::broadcastToSome(RPC, core_.getNetworks(), streamedFor_.entries());
}

bool isStreamedInForPlayer(const IPlayer& player) const override
Expand Down Expand Up @@ -264,7 +266,7 @@ class Actor final : public IActor, public PoolIDProvider, public NoCopy
NetCode::RPC::SetActorPosForPlayer RPC;
RPC.ActorID = poolID;
RPC.Pos = position;
PacketHelper::broadcastToSome(RPC, streamedFor_.entries());
PacketHelper::broadcastToSome(RPC, core_.getNetworks(), streamedFor_.entries());
}

GTAQuat getRotation() const override
Expand All @@ -279,7 +281,7 @@ class Actor final : public IActor, public PoolIDProvider, public NoCopy
NetCode::RPC::SetActorFacingAngleForPlayer RPC;
RPC.ActorID = poolID;
RPC.Angle = angle_;
PacketHelper::broadcastToSome(RPC, streamedFor_.entries());
PacketHelper::broadcastToSome(RPC, core_.getNetworks(), streamedFor_.entries());
}

void setSkin(int id) override
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Actors/actors_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ActorsComponent final : public IActorsComponent, public PlayerConnectEvent

IActor* create(int skin, Vector3 pos, float angle) override
{
return storage.emplace(skin, pos, angle, core->getConfig().getBool("game.use_all_animations"), core->getConfig().getBool("game.validate_animations"), modelsComponent, fixesComponent_);
return storage.emplace(skin, pos, angle, core->getConfig().getBool("game.use_all_animations"), core->getConfig().getBool("game.validate_animations"), *core, modelsComponent, fixesComponent_);
}

void free() override
Expand Down
136 changes: 136 additions & 0 deletions Server/Components/LegacyNetwork/legacy_network_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,74 @@ class RakNetLegacyNetwork final : public Network, public CoreEventHandler, publi
return rakNetServer.Send((const char*)bs.GetData(), bs.GetNumberOfUnreadBits(), RakNet::HIGH_PRIORITY, reliability, channel, RakNet::UNASSIGNED_PLAYER_ID, true);
}

bool broadcastPacketToSome(Span<uint8_t> data, int channel, const FlatPtrHashSet<IPlayer>& players, const IPlayer* exceptPeer, bool dispatchEvents) override
{
// Don't use constructor because it takes bytes; we want bits
NetworkBitStream bs;
bs.SetData(data.data());
bs.SetWriteOffset(data.size());
bs.SetReadOffset(0);

// Store packet type here, we're gonna use it later
uint8_t type;
bs.readUINT8(type);
bs.SetReadOffset(0);

// Now go through all players to see if we need to send packet to, based on various checks and event results.
RakNet::PlayerIndex* broadcastList = new RakNet::PlayerIndex[players.size()];
int broadcastListSize = 0;
for (auto player : players)
{
if (player && player != exceptPeer && player->getNetworkData().network == this)
{
auto remoteSystem = playerRemoteSystem[player->getID()];
if (remoteSystem && remoteSystem->isActive)
{
int index = rakNetServer.GetIndexFromPlayerID(remoteSystem->playerId);
if (index != -1)
{
bool mustSend = true;
if (dispatchEvents)
{
mustSend = outEventDispatcher.stopAtFalse([type, &bs, player](NetworkOutEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onSendPacket(player, type, bs);
});

if (mustSend)
{
mustSend = packetOutEventDispatcher.stopAtFalse(type, [&bs, player](SingleNetworkOutEventHandler* handler)
{
bs.SetReadOffset(8); // Ignore packet ID
return handler->onSend(player, bs);
});
}
}

if (mustSend)
{
broadcastList[broadcastListSize] = index;
broadcastListSize++;
}
}
}
}
}

if (broadcastListSize)
{
const RakNet::PacketReliability reliability = (channel == OrderingChannel_Unordered) ? RakNet::RELIABLE : RakNet::RELIABLE_ORDERED;
return rakNetServer.Send((const char*)bs.GetData(), bs.GetNumberOfUnreadBits(), RakNet::HIGH_PRIORITY, reliability, channel, broadcastList, broadcastListSize);
}
else
{
delete[] broadcastList;
}

return true;
}

bool sendPacket(IPlayer& peer, Span<uint8_t> data, int channel, bool dispatchEvents) override
{
const PeerNetworkData& netData = peer.getNetworkData();
Expand Down Expand Up @@ -233,6 +301,74 @@ class RakNetLegacyNetwork final : public Network, public CoreEventHandler, publi
return rakNetServer.RPC(id, (const char*)bs.GetData(), bs.GetNumberOfUnreadBits(), RakNet::HIGH_PRIORITY, reliability, channel, RakNet::UNASSIGNED_PLAYER_ID, true, false, RakNet::UNASSIGNED_NETWORK_ID, nullptr);
}

bool broadcastRPCToSome(int id, Span<uint8_t> data, int channel, const FlatPtrHashSet<IPlayer>& players, const IPlayer* exceptPeer, bool dispatchEvents) override
{
if (id == INVALID_PACKET_ID)
{
return false;
}

// Don't use constructor because it takes bytes; we want bits
NetworkBitStream bs;
bs.SetData(data.data());
bs.SetWriteOffset(data.size());
bs.SetReadOffset(0);

// Now go through all players to see if we need to send RPC to, based on various checks and event results.
RakNet::PlayerIndex* broadcastList = new RakNet::PlayerIndex[players.size()];
int broadcastListSize = 0;
for (auto player : players)
{
if (player && player != exceptPeer && player->getNetworkData().network == this)
{
auto remoteSystem = playerRemoteSystem[player->getID()];
if (remoteSystem && remoteSystem->isActive)
{
int index = rakNetServer.GetIndexFromPlayerID(remoteSystem->playerId);
if (index != -1)
{
bool mustSend = true;
if (dispatchEvents)
{
mustSend = outEventDispatcher.stopAtFalse([id, &bs, player](NetworkOutEventHandler* handler)
{
bs.resetReadPointer();
return handler->onSendRPC(player, id, bs);
});

if (mustSend)
{
mustSend = rpcOutEventDispatcher.stopAtFalse(id, [&bs, player](SingleNetworkOutEventHandler* handler)
{
bs.resetReadPointer();
return handler->onSend(player, bs);
});
}
}

if (mustSend)
{
broadcastList[broadcastListSize] = index;
broadcastListSize++;
}
}
}
}
}

if (broadcastListSize)
{
const RakNet::PacketReliability reliability = (channel == OrderingChannel_Unordered) ? RakNet::RELIABLE : RakNet::RELIABLE_ORDERED;
return rakNetServer.RPC(id, (const char*)bs.GetData(), bs.GetNumberOfUnreadBits(), RakNet::HIGH_PRIORITY, reliability, channel, broadcastList, broadcastListSize);
}
else
{
delete[] broadcastList;
}

return true;
}

bool sendRPC(IPlayer& peer, int id, Span<uint8_t> data, int channel, bool dispatchEvents) override
{
if (id == INVALID_PACKET_ID)
Expand Down
26 changes: 13 additions & 13 deletions Server/Components/Vehicles/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void Vehicle::setPlate(StringView plate)
NetCode::RPC::SetVehiclePlate plateRPC;
plateRPC.VehicleID = poolID;
plateRPC.plate = numberPlate;
PacketHelper::broadcastToSome(plateRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(plateRPC, pool->getNetworks(), streamedFor_.entries());
}

const StringView Vehicle::getPlate()
Expand All @@ -434,7 +434,7 @@ void Vehicle::setColour(int col1, int col2)
colourRPC.EventType = VehicleSCMEvent_SetColour;
colourRPC.Arg1 = col1;
colourRPC.Arg2 = col2;
PacketHelper::broadcastToSome(colourRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(colourRPC, pool->getNetworks(), streamedFor_.entries());
}

Pair<int, int> Vehicle::getColour() const
Expand Down Expand Up @@ -462,7 +462,7 @@ void Vehicle::setDamageStatus(int PanelStatus, int DoorStatus, uint8_t LightStat
static_cast<DefaultEventDispatcher<VehicleEventHandler>&>(pool->getEventDispatcher()).dispatch(&VehicleEventHandler::onVehicleDamageStatusUpdate, *lock.entry, *vehicleUpdater);
}

PacketHelper::broadcastToSome(damageStatus, streamedFor_.entries(), vehicleUpdater);
PacketHelper::broadcastToSome(damageStatus, pool->getNetworks(), streamedFor_.entries(), vehicleUpdater);
}

void Vehicle::getDamageStatus(int& PanelStatus, int& DoorStatus, int& LightStatus, int& TyreStatus)
Expand All @@ -481,7 +481,7 @@ void Vehicle::setPaintJob(int paintjob)
paintRPC.EventType = VehicleSCMEvent_SetPaintjob;
paintRPC.VehicleID = poolID;
paintRPC.Arg1 = paintjob;
PacketHelper::broadcastToSome(paintRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(paintRPC, pool->getNetworks(), streamedFor_.entries());
}

int Vehicle::getPaintJob()
Expand Down Expand Up @@ -510,7 +510,7 @@ void Vehicle::addComponent(int component)
modRPC.EventType = VehicleSCMEvent_AddComponent;
modRPC.VehicleID = poolID;
modRPC.Arg1 = component;
PacketHelper::broadcastToSome(modRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(modRPC, pool->getNetworks(), streamedFor_.entries());
}

int Vehicle::getComponentInSlot(int slot)
Expand Down Expand Up @@ -538,7 +538,7 @@ void Vehicle::removeComponent(int component)
NetCode::RPC::RemoveVehicleComponent modRPC;
modRPC.VehicleID = poolID;
modRPC.Component = component;
PacketHelper::broadcastToSome(modRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(modRPC, pool->getNetworks(), streamedFor_.entries());
}

void Vehicle::putPlayer(IPlayer& player, int SeatID)
Expand Down Expand Up @@ -588,7 +588,7 @@ void Vehicle::setHealth(float Health)
NetCode::RPC::SetVehicleHealth setVehicleHealthRPC;
setVehicleHealthRPC.VehicleID = poolID;
setVehicleHealthRPC.health = Health;
PacketHelper::broadcastToSome(setVehicleHealthRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(setVehicleHealthRPC, pool->getNetworks(), streamedFor_.entries());
}

void Vehicle::setInterior(int InteriorID)
Expand All @@ -597,7 +597,7 @@ void Vehicle::setInterior(int InteriorID)
NetCode::RPC::LinkVehicleToInterior linkVehicleToInteriorRPC;
linkVehicleToInteriorRPC.VehicleID = poolID;
linkVehicleToInteriorRPC.InteriorID = InteriorID;
PacketHelper::broadcastToSome(linkVehicleToInteriorRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(linkVehicleToInteriorRPC, pool->getNetworks(), streamedFor_.entries());
}

int Vehicle::getInterior()
Expand All @@ -612,7 +612,7 @@ void Vehicle::setZAngle(float angle)
NetCode::RPC::SetVehicleZAngle setVehicleZAngleRPC;
setVehicleZAngleRPC.VehicleID = poolID;
setVehicleZAngleRPC.angle = angle;
PacketHelper::broadcastToSome(setVehicleZAngleRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(setVehicleZAngleRPC, pool->getNetworks(), streamedFor_.entries());
}

float Vehicle::getZAngle()
Expand All @@ -627,7 +627,7 @@ void Vehicle::setParams(const VehicleParams& params)
NetCode::RPC::SetVehicleParams vehicleRPC;
vehicleRPC.VehicleID = poolID;
vehicleRPC.params = params;
PacketHelper::broadcastToSome(vehicleRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(vehicleRPC, pool->getNetworks(), streamedFor_.entries());
}

// Set the vehicle's parameters for a specific player.
Expand Down Expand Up @@ -655,7 +655,7 @@ void Vehicle::setPosition(Vector3 position)
NetCode::RPC::SetVehiclePosition setVehiclePosition;
setVehiclePosition.VehicleID = poolID;
setVehiclePosition.position = position;
PacketHelper::broadcastToSome(setVehiclePosition, streamedFor_.entries());
PacketHelper::broadcastToSome(setVehiclePosition, pool->getNetworks(), streamedFor_.entries());
}

Vector3 Vehicle::getPosition() const
Expand Down Expand Up @@ -741,7 +741,7 @@ void Vehicle::attachTrailer(IVehicle& trailer)
NetCode::RPC::AttachTrailer trailerRPC;
trailerRPC.TrailerID = this->trailer->poolID;
trailerRPC.VehicleID = poolID;
PacketHelper::broadcastToSome(trailerRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(trailerRPC, pool->getNetworks(), streamedFor_.entries());
}

void Vehicle::detachTrailer()
Expand All @@ -750,7 +750,7 @@ void Vehicle::detachTrailer()
{
NetCode::RPC::DetachTrailer trailerRPC;
trailerRPC.VehicleID = poolID;
PacketHelper::broadcastToSome(trailerRPC, streamedFor_.entries());
PacketHelper::broadcastToSome(trailerRPC, pool->getNetworks(), streamedFor_.entries());
trailer->setCab(nullptr);
trailer = nullptr;
detaching = true;
Expand Down
12 changes: 11 additions & 1 deletion Server/Components/Vehicles/vehicles_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class VehiclesComponent final : public IVehiclesComponent, public CoreEventHandl
enterExitRPC.EventType = VehicleSCMEvent_EnterExitModShop;
enterExitRPC.Arg1 = scmEvent.Arg1;
enterExitRPC.Arg2 = scmEvent.Arg2;
PacketHelper::broadcastToSome(enterExitRPC, vehicle.streamedForPlayers(), &peer);
PacketHelper::broadcastToSome(enterExitRPC, self.getNetworks(), vehicle.streamedForPlayers(), &peer);
break;
}
}
Expand Down Expand Up @@ -292,6 +292,16 @@ class VehiclesComponent final : public IVehiclesComponent, public CoreEventHandl
} vehicleDeathHandler;

public:
ICore* getCore()
{
return core;
}

const FlatPtrHashSet<INetwork>& getNetworks()
{
return core->getNetworks();
}

IPlayerPool& getPlayers()
{
return core->getPlayers();
Expand Down
Loading
Loading