Replace "tell version to client" part with simple ack because YAGNI
This commit is contained in:
@@ -59,7 +59,7 @@ public class NetworkClient extends NetworkEventSource implements AutoCloseable
|
||||
|
||||
private void registerHandlers()
|
||||
{
|
||||
this.registerHandler(HelloMessage.class, (helloMessage, channelContext) ->
|
||||
this.registerAckHandler(HelloMessage.class, channelContext ->
|
||||
{
|
||||
LOGGER.info("Connected to server: "+channelContext.channel().remoteAddress());
|
||||
});
|
||||
@@ -136,11 +136,6 @@ public class NetworkClient extends NetworkEventSource implements AutoCloseable
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
if (this.closeReason != null)
|
||||
{
|
||||
LOGGER.error(this.closeReason);
|
||||
}
|
||||
|
||||
if (this.connectionState == EConnectionState.CLOSED)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -2,10 +2,8 @@ package com.seibel.distanthorizons.core.network;
|
||||
|
||||
import com.seibel.distanthorizons.core.logging.DhLoggerBuilder;
|
||||
import com.seibel.distanthorizons.core.network.messages.AckMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.HelloMessage;
|
||||
import com.seibel.distanthorizons.core.network.protocol.INetworkMessage;
|
||||
import com.seibel.distanthorizons.core.network.protocol.MessageHandler;
|
||||
import com.seibel.distanthorizons.coreapi.ModInfo;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -17,29 +15,8 @@ public abstract class NetworkEventSource implements AutoCloseable
|
||||
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
|
||||
|
||||
protected final MessageHandler messageHandler = new MessageHandler();
|
||||
protected String closeReason = null;
|
||||
|
||||
|
||||
|
||||
public NetworkEventSource()
|
||||
{
|
||||
this.registerHandler(HelloMessage.class, (helloMessage, channelContext) ->
|
||||
{
|
||||
if (helloMessage.version != ModInfo.PROTOCOL_VERSION)
|
||||
{
|
||||
try
|
||||
{
|
||||
String closeReason = "Ignoring message from channel ["+channelContext.name()+"], due to version mismatch. Expected version: ["+ModInfo.PROTOCOL_VERSION+"], received version: ["+helloMessage.version+"].";
|
||||
LOGGER.info(closeReason);
|
||||
this.close(closeReason);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public <T extends INetworkMessage> void registerHandler(Class<T> clazz, BiConsumer<T, ChannelHandlerContext> handler) { this.messageHandler.registerHandler(clazz, handler); }
|
||||
|
||||
@@ -54,10 +31,4 @@ public abstract class NetworkEventSource implements AutoCloseable
|
||||
});
|
||||
}
|
||||
|
||||
public void close(String reason) throws Exception
|
||||
{
|
||||
this.closeReason = reason;
|
||||
this.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.seibel.distanthorizons.core.network;
|
||||
|
||||
import com.seibel.distanthorizons.core.logging.DhLoggerBuilder;
|
||||
import com.seibel.distanthorizons.core.network.messages.AckMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.CloseReasonMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.CloseMessage;
|
||||
import com.seibel.distanthorizons.core.network.messages.HelloMessage;
|
||||
import com.seibel.distanthorizons.core.network.protocol.NetworkChannelInitializer;
|
||||
import com.seibel.distanthorizons.coreapi.ModInfo;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
@@ -41,7 +43,23 @@ public class NetworkServer extends NetworkEventSource implements AutoCloseable
|
||||
this.registerHandler(HelloMessage.class, (helloMessage, channelContext) ->
|
||||
{
|
||||
LOGGER.info("Client connected: "+channelContext.channel().remoteAddress());
|
||||
channelContext.channel().writeAndFlush(new HelloMessage());
|
||||
|
||||
if (helloMessage.version != ModInfo.PROTOCOL_VERSION)
|
||||
{
|
||||
try
|
||||
{
|
||||
String disconnectReason = "Version mismatch. Server version: ["+ModInfo.PROTOCOL_VERSION+"], client version: ["+helloMessage.version+"].";
|
||||
LOGGER.info("Disconnecting the client ["+channelContext.name()+"]: "+disconnectReason);
|
||||
this.disconnectClient(channelContext, disconnectReason);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
channelContext.writeAndFlush(new AckMessage(HelloMessage.class));
|
||||
});
|
||||
|
||||
this.registerHandler(CloseMessage.class, (closeMessage, channelContext) ->
|
||||
@@ -83,11 +101,6 @@ public class NetworkServer extends NetworkEventSource implements AutoCloseable
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
if (this.closeReason != null)
|
||||
{
|
||||
LOGGER.error(this.closeReason);
|
||||
}
|
||||
|
||||
if (this.isClosed)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -50,7 +50,7 @@ public class DhClientWorld extends AbstractDhWorld implements IDhClientWorld
|
||||
}
|
||||
|
||||
private void registerNetworkHandlers() {
|
||||
networkClient.registerHandler(HelloMessage.class, (msg, ctx) -> {
|
||||
networkClient.registerAckHandler(HelloMessage.class, ctx -> {
|
||||
ctx.writeAndFlush(new PlayerUUIDMessage(MC_CLIENT.getPlayerUUID()));
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user