Make data source encoding lazy and move it off server thread

This commit is contained in:
s809
2024-07-01 00:15:54 +05:00
parent 1b48d61d3f
commit 7e48c49e33
3 changed files with 51 additions and 19 deletions
@@ -241,6 +241,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
// This semaphore is intentionally acquired forever
entry.requestCollectionSemaphore.acquireUninterruptibly(Short.MAX_VALUE);
FullDataSourceResponseMessage response = new FullDataSourceResponseMessage(entry.fullDataSource);
for (FullDataSourceRequestMessage msg : entry.requestMessages.values())
{
this.fullDataRequests.remove(msg.futureId);
@@ -252,7 +253,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
}
serverPlayerState.getRateLimiterSet(this).fullDataRequestConcurrencyLimiter.release();
msg.sendResponse(new FullDataSourceResponseMessage(entry.fullDataSource));
msg.sendResponse(response);
}
}
}
@@ -265,6 +266,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
return this.getFullDataProvider().updateDataSourceAsync(data);
}
FullDataPartialUpdateMessage updateMessage = new FullDataPartialUpdateMessage(this.serverLevelWrapper, data);
for (ServerPlayerState serverPlayerState : this.remotePlayerConnectionHandler.getConnectedPlayers())
{
if (!serverPlayerState.config.isRealTimeUpdatesEnabled())
@@ -277,7 +279,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
if (distanceFromPlayer >= serverPlayerState.serverPlayer().getViewDistance() &&
distanceFromPlayer <= serverPlayerState.config.getRenderDistanceRadius())
{
serverPlayerState.session.sendMessage(new FullDataPartialUpdateMessage(this.serverLevelWrapper, data));
serverPlayerState.session.sendMessage(updateMessage);
}
}
@@ -20,6 +20,7 @@
package com.seibel.distanthorizons.core.network.messages.fullData;
import com.google.common.base.MoreObjects;
import com.google.common.base.Suppliers;
import com.seibel.distanthorizons.api.enums.config.EDhApiDataCompressionMode;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.dataObjects.fullData.sources.FullDataSourceV2;
@@ -29,8 +30,11 @@ import com.seibel.distanthorizons.core.network.INetworkObject;
import com.seibel.distanthorizons.core.sql.dto.FullDataSourceV2DTO;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.ILevelWrapper;
import io.netty.buffer.ByteBuf;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.function.Supplier;
public class FullDataPartialUpdateMessage extends NetworkMessage implements ILevelRelatedMessage
{
@@ -38,18 +42,35 @@ public class FullDataPartialUpdateMessage extends NetworkMessage implements ILev
@Override
public String getLevelName() { return this.levelName; }
// Encode only
@Nullable
private final FullDataSourceV2 fullDataSource;
private final Supplier<FullDataSourceV2DTO> dataSourceDtoSupplier = Suppliers.memoize(this::createDataSourceDto);
// Decode only
@Nullable
public FullDataSourceV2DTO dataSourceDto;
public FullDataPartialUpdateMessage() { }
public FullDataPartialUpdateMessage(ILevelWrapper level, FullDataSourceV2 fullDataSource)
public FullDataPartialUpdateMessage() { this.fullDataSource = null; }
public FullDataPartialUpdateMessage(ILevelWrapper level, @NonNull FullDataSourceV2 fullDataSource)
{
this.levelName = level.getDimensionName();
this.fullDataSource = fullDataSource;
}
@Override
public boolean warnWhenUnhandled() { return false; }
private FullDataSourceV2DTO createDataSourceDto()
{
try
{
assert this.fullDataSource != null;
EDhApiDataCompressionMode compressionMode = Config.Client.Advanced.LodBuilding.dataCompression.get();
this.dataSourceDto = FullDataSourceV2DTO.CreateFromDataSource(fullDataSource, compressionMode);
return FullDataSourceV2DTO.CreateFromDataSource(this.fullDataSource, compressionMode);
}
catch (IOException e)
{
@@ -57,15 +78,11 @@ public class FullDataPartialUpdateMessage extends NetworkMessage implements ILev
}
}
@Override
public boolean warnWhenUnhandled() { return false; }
@Override
public void encode(ByteBuf out)
{
this.writeString(this.levelName, out);
this.dataSourceDto.encode(out);
this.dataSourceDtoSupplier.get().encode(out);
}
@Override
@@ -81,6 +98,7 @@ public class FullDataPartialUpdateMessage extends NetworkMessage implements ILev
{
return super.toStringHelper()
.add("levelName", this.levelName)
.add("fullDataSource", this.fullDataSource)
.add("dataSourceDto", this.dataSourceDto);
}
@@ -20,6 +20,7 @@
package com.seibel.distanthorizons.core.network.messages.fullData;
import com.google.common.base.MoreObjects;
import com.google.common.base.Suppliers;
import com.seibel.distanthorizons.api.enums.config.EDhApiDataCompressionMode;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.dataObjects.fullData.sources.FullDataSourceV2;
@@ -30,6 +31,7 @@ import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.function.Supplier;
/**
* Response message, containing the requested full data source,
@@ -37,19 +39,28 @@ import java.io.IOException;
*/
public class FullDataSourceResponseMessage extends TrackableMessage
{
// Encode only
@Nullable
private final FullDataSourceV2 fullDataSource;
private final Supplier<FullDataSourceV2DTO> dataSourceDtoSupplier = Suppliers.memoize(this::createDataSourceDto);
// Decode only
@Nullable
public FullDataSourceV2DTO dataSourceDto;
public FullDataSourceResponseMessage() { }
public FullDataSourceResponseMessage() { this(null); }
public FullDataSourceResponseMessage(@Nullable FullDataSourceV2 fullDataSource)
{
this.fullDataSource = fullDataSource;
}
private FullDataSourceV2DTO createDataSourceDto()
{
try
{
if (fullDataSource != null)
{
EDhApiDataCompressionMode compressionMode = Config.Client.Advanced.LodBuilding.dataCompression.get();
this.dataSourceDto = FullDataSourceV2DTO.CreateFromDataSource(fullDataSource, compressionMode);
}
assert this.fullDataSource != null;
EDhApiDataCompressionMode compressionMode = Config.Client.Advanced.LodBuilding.dataCompression.get();
return FullDataSourceV2DTO.CreateFromDataSource(this.fullDataSource, compressionMode);
}
catch (IOException e)
{
@@ -60,9 +71,9 @@ public class FullDataSourceResponseMessage extends TrackableMessage
@Override
public void encode0(ByteBuf out)
{
if (this.writeOptional(out, this.dataSourceDto))
if (this.writeOptional(out, this.fullDataSource))
{
this.dataSourceDto.encode(out);
this.dataSourceDtoSupplier.get().encode(out);
}
}
@@ -77,6 +88,7 @@ public class FullDataSourceResponseMessage extends TrackableMessage
public MoreObjects.ToStringHelper toStringHelper()
{
return super.toStringHelper()
.add("fullDataSource", this.fullDataSource)
.add("dataSourceDto", this.dataSourceDto);
}