Make error handling somewhat work

This commit is contained in:
s809
2024-08-10 19:49:33 +05:00
parent dee13a4ec4
commit ba3677b641
9 changed files with 87 additions and 20 deletions
@@ -359,7 +359,6 @@ public class ClientApi
Session session = this.pluginChannelApi.session;
if (session != null)
{
message.setSession(session);
session.tryHandleMessage(message);
}
}
@@ -175,7 +175,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
rateLimiterSet.loginDataSyncRCLimiter.release();
FullDataPayload payload = new FullDataPayload(fullDataSource);
payload.acceptInChunkMessages(FULL_DATA_CHUNK_SIZE, msg.session::sendMessage);
payload.acceptInChunkMessages(FULL_DATA_CHUNK_SIZE, msg.getSession()::sendMessage);
msg.sendResponse(new FullDataSourceResponseMessage(payload));
}, executor);
}
@@ -227,8 +227,8 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
}
// If player is not in this dimension and handling multiple dimensions at once is not allowed
assert msg.session.serverPlayer != null;
if (msg.session.serverPlayer.getLevel() != this.getLevelWrapper())
assert msg.getSession().serverPlayer != null;
if (msg.getSession().serverPlayer.getLevel() != this.getLevelWrapper())
{
// If the message can be replied to - reply with error, otherwise just ignore
if (msg instanceof TrackableMessage)
@@ -236,7 +236,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
((TrackableMessage) msg).sendResponse(new InvalidLevelException(MessageFormat.format(
"Generation not allowed. Requested dimension: {0}, player dimension: {1}, handler dimension: {2}",
((ILevelRelatedMessage) msg).getLevelName(),
msg.session.serverPlayer.getLevel().getDimensionName(),
msg.getSession().serverPlayer.getLevel().getDimensionName(),
this.getLevelWrapper().getDimensionName()
)));
}
@@ -303,7 +303,7 @@ public class DhServerLevel extends AbstractDhLevel implements IDhServerLevel
}
serverPlayerState.getRateLimiterSet(this).fullDataRequestConcurrencyLimiter.release();
payload.acceptInChunkMessages(FULL_DATA_CHUNK_SIZE, msg.session::sendMessage);
payload.acceptInChunkMessages(FULL_DATA_CHUNK_SIZE, msg.getSession()::sendMessage);
msg.sendResponse(new FullDataSourceResponseMessage(payload.retain()));
}
}
@@ -20,9 +20,7 @@ public class RemotePlayerConnectionHandler
ServerPlayerState playerState = this.connectedPlayers.get(player);
if (playerState != null)
{
Session session = playerState.session;
message.setSession(session);
session.tryHandleMessage(message);
playerState.session.tryHandleMessage(message);
}
else
{
@@ -51,7 +49,6 @@ public class RemotePlayerConnectionHandler
Session session = state.session;
for (NetworkMessage message : queuedMessages)
{
message.setSession(session);
session.tryHandleMessage(message);
}
@@ -104,7 +104,7 @@ public abstract class NetworkEventSource
if (!handled && ModInfo.IS_DEV_BUILD)
{
LOGGER.warn("Unhandled message: " + message);
LOGGER.warn("Unhandled message: {}", message);
}
}
@@ -70,7 +70,7 @@ public class MessageRegistry
protected <T extends NetworkMessage> void registerMessage(Class<T> clazz, Supplier<T> supplier)
public <T extends NetworkMessage> void registerMessage(Class<T> clazz, Supplier<T> supplier)
{
int id = this.idToSupplier.size() + 1;
this.idToSupplier.put(id, supplier);
@@ -7,20 +7,16 @@ import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IServerPlayerWrapp
public abstract class NetworkMessage implements INetworkObject
{
public Session session = null;
private Session session = null;
public IServerPlayerWrapper serverPlayer() { return this.session.serverPlayer; }
public Session getConnection()
public Session getSession()
{
return this.session;
}
public void setSession(Session connection)
{
if (this.session != null)
{
throw new IllegalStateException("Session object cannot be changed after initialization.");
}
this.session = connection;
}
@@ -49,7 +49,7 @@ public abstract class TrackableMessage extends NetworkMessage
public void sendResponse(TrackableMessage responseMessage)
{
responseMessage.futureId = this.futureId;
this.session.sendMessage(responseMessage);
this.getSession().sendMessage(responseMessage);
}
public void sendResponse(Exception e)
@@ -0,0 +1,62 @@
/*
* This file is part of the Distant Horizons mod
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2023 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty 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 <https://www.gnu.org/licenses/>.
*/
package com.seibel.distanthorizons.core.network.messages.base;
import com.google.common.base.MoreObjects;
import com.seibel.distanthorizons.core.network.messages.NetworkMessage;
import io.netty.buffer.ByteBuf;
public class CodecCrashMessage extends NetworkMessage
{
public ECrashPhase crashPhase;
public CodecCrashMessage() { }
public CodecCrashMessage(ECrashPhase crashPhase) { this.crashPhase = crashPhase; }
@Override
public void encode(ByteBuf out)
{
if (this.crashPhase == ECrashPhase.ENCODE)
{
throw new RuntimeException("encode force crash");
}
}
@Override
public void decode(ByteBuf in)
{
throw new RuntimeException("decode force crash");
}
@Override
public MoreObjects.ToStringHelper toStringHelper()
{
return super.toStringHelper()
.add("crashPhase", this.crashPhase);
}
public enum ECrashPhase
{
ENCODE,
DECODE
}
}
@@ -7,6 +7,7 @@ import com.seibel.distanthorizons.core.network.event.NetworkEventSource;
import com.seibel.distanthorizons.core.network.event.CloseEvent;
import com.seibel.distanthorizons.core.network.messages.NetworkMessage;
import com.seibel.distanthorizons.core.network.messages.TrackableMessage;
import com.seibel.distanthorizons.core.network.messages.base.CloseReasonMessage;
import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IPluginPacketSender;
import com.seibel.distanthorizons.core.wrapperInterfaces.misc.IServerPlayerWrapper;
import org.apache.logging.log4j.LogManager;
@@ -42,6 +43,11 @@ public class Session extends NetworkEventSource
public Session(@Nullable IServerPlayerWrapper serverPlayer)
{
this.serverPlayer = serverPlayer;
this.registerHandler(CloseReasonMessage.class, msg ->
{
this.close(new SessionClosedException(msg.reason));
});
}
@@ -52,6 +58,8 @@ public class Session extends NetworkEventSource
return;
}
message.setSession(this);
try
{
LOGGER.debug("Received message: {}", message);
@@ -86,8 +94,13 @@ public class Session extends NetworkEventSource
public void sendMessage(NetworkMessage message)
{
if (this.closeReason.get() != null) return;
if (this.closeReason.get() != null)
{
return;
}
LOGGER.debug("Sending message: {}", message);
message.setSession(this);
try
{