Make ping updates more accurate

This commit is contained in:
Kae 2024-04-04 14:35:36 +11:00
parent be676518f4
commit 6c896c2ef7
8 changed files with 66 additions and 10 deletions

View File

@ -924,15 +924,26 @@ void WorldStartAcknowledgePacket::write(DataStream& ds) const {
}
PingPacket::PingPacket() {}
PingPacket::PingPacket(int64_t time) : time(time) {}
void PingPacket::read(DataStream& ds) {
void PingPacket::readLegacy(DataStream& ds) {
// Packets can't be empty, read the trash data
ds.read<bool>();
time = 0;
}
void PingPacket::read(DataStream& ds) {
ds.readVlqI(time);
}
void PingPacket::writeLegacy(DataStream& ds) const {
// Packets can't be empty, write some trash data
ds.write<bool>(false);
}
void PingPacket::write(DataStream& ds) const {
// Packets can't be empty, write some trash data
ds.write(false);
ds.writeVlqI(time);
}
EntityCreatePacket::EntityCreatePacket(EntityType entityType, ByteArray storeData, ByteArray firstNetState, EntityId entityId)
@ -1234,15 +1245,25 @@ void FindUniqueEntityResponsePacket::write(DataStream& ds) const {
}
PongPacket::PongPacket() {}
PongPacket::PongPacket(int64_t time) : time(time) {}
void PongPacket::read(DataStream& ds) {
void PongPacket::readLegacy(DataStream& ds) {
// Packets can't be empty, read the trash data
ds.read<bool>();
time = 0;
}
void PongPacket::read(DataStream& ds) {
ds.readVlqI(time);
}
void PongPacket::writeLegacy(DataStream& ds) const {
// Packets can't be empty, write some trash data
ds.write<bool>(false);
}
void PongPacket::write(DataStream& ds) const {
// Packets can't be empty, write some trash data
ds.write<bool>(false);
ds.writeVlqI(time);
}
StepUpdatePacket::StepUpdatePacket() : remoteTime(0.0) {}

View File

@ -603,9 +603,14 @@ struct FindUniqueEntityResponsePacket : PacketBase<PacketType::FindUniqueEntityR
struct PongPacket : PacketBase<PacketType::Pong> {
PongPacket();
PongPacket(int64_t time);
void readLegacy(DataStream& ds) override;
void read(DataStream& ds) override;
void writeLegacy(DataStream& ds) const override;
void write(DataStream& ds) const override;
int64_t time = 0;
};
struct ModifyTileListPacket : PacketBase<PacketType::ModifyTileList> {
@ -717,9 +722,14 @@ struct WorldStartAcknowledgePacket : PacketBase<PacketType::WorldStartAcknowledg
struct PingPacket : PacketBase<PacketType::Ping> {
PingPacket();
PingPacket(int64_t time);
void readLegacy(DataStream& ds) override;
void read(DataStream& ds) override;
void writeLegacy(DataStream& ds) const override;
void write(DataStream& ds) const override;
int64_t time = 0;
};
struct EntityCreatePacket : PacketBase<PacketType::EntityCreate> {

View File

@ -123,6 +123,7 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA
m_mainPlayer->setClientContext(m_clientContext);
m_mainPlayer->setStatistics(m_statistics);
m_worldClient = make_shared<WorldClient>(m_mainPlayer);
m_worldClient->clientState().setLegacy(m_legacyServer);
m_worldClient->setAsyncLighting(true);
for (auto& pair : m_luaCallbacks)
m_worldClient->setLuaCallbacks(pair.first, pair.second);

View File

@ -403,6 +403,10 @@ RectI WorldClient::clientWindow() const {
return m_clientState.window();
}
WorldClientState& WorldClient::clientState() {
return m_clientState;
}
void WorldClient::render(WorldRenderData& renderData, unsigned bufferTiles) {
if (!m_lightingThread && m_asyncLighting)
m_lightingThread = Thread::invoke("WorldClient::lightingMain", mem_fn(&WorldClient::lightingMain), this);
@ -1038,7 +1042,9 @@ void WorldClient::handleIncomingPackets(List<PacketPtr> const& packets) {
m_worldTemplate->setWorldParameters(netLoadVisitableWorldParameters(worldParametersUpdate->parametersData));
} else if (auto pongPacket = as<PongPacket>(packet)) {
if (m_pingTime)
if (pongPacket->time)
m_latency = Time::monotonicMilliseconds() - pongPacket->time;
else if (m_pingTime)
m_latency = Time::monotonicMilliseconds() - m_pingTime.take();
} else {
@ -1203,10 +1209,11 @@ void WorldClient::update(float dt) {
queueUpdatePackets(m_entityUpdateTimer.wrapTick(dt));
if (m_pingTime.isNothing()) {
if ((!m_clientState.legacy() && m_currentStep % 3 == 0) || m_pingTime.isNothing()) {
m_pingTime = Time::monotonicMilliseconds();
m_outgoingPackets.append(make_shared<PingPacket>());
m_outgoingPackets.append(make_shared<PingPacket>(*m_pingTime));
}
LogMap::set("client_ping", m_latency);
// Remove active sectors that are outside of the current monitoring region

View File

@ -143,6 +143,7 @@ public:
void centerClientWindowOnPlayer(Vec2U const& windowSize);
void centerClientWindowOnPlayer();
RectI clientWindow() const;
WorldClientState& clientState();
void update(float dt);
// borderTiles here should extend the client window for border tile

View File

@ -22,6 +22,8 @@ WorldClientState::WorldClientState() {
m_netGroup.addNetElement(&m_playerId);
m_netGroup.addNetElement(&m_clientPresenceEntities);
m_legacy = false;
}
RectI WorldClientState::window() const {
@ -87,6 +89,14 @@ void WorldClientState::readDelta(ByteArray delta) {
m_netGroup.readNetState(std::move(delta));
}
void WorldClientState::setLegacy(bool legacy) {
m_legacy = legacy;
}
bool WorldClientState::legacy() const {
return m_legacy;
}
void WorldClientState::reset() {
m_netVersion = 0;
}

View File

@ -36,6 +36,10 @@ public:
ByteArray writeDelta();
void readDelta(ByteArray delta);
// Whether the client is connected to a legacy server.
void setLegacy(bool legacy);
bool legacy() const;
void reset();
private:
@ -52,6 +56,8 @@ private:
NetElementInt m_playerId;
NetElementData<List<EntityId>> m_clientPresenceEntities;
bool m_legacy;
};
}

View File

@ -520,7 +520,7 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c
response.get<RpcPromiseKeeper<Json>>().fail(entityMessageResponsePacket->response.left());
}
} else if (auto pingPacket = as<PingPacket>(packet)) {
clientInfo->outgoingPackets.append(make_shared<PongPacket>());
clientInfo->outgoingPackets.append(make_shared<PongPacket>(pingPacket->time));
} else if (auto updateWorldProperties = as<UpdateWorldPropertiesPacket>(packet)) {
// Kae: Properties set to null (nil from Lua) should be erased instead of lingering around