Code polish/cleanup

This commit is contained in:
nathan 2018-04-04 14:56:37 +02:00
parent a4cf9f4834
commit 292f50a059
5 changed files with 41 additions and 19 deletions

View File

@ -26,7 +26,6 @@ import io.netty.util.AttributeKey;
public public
class ClientDiscoverHostHandler extends SimpleChannelInboundHandler<DatagramPacket> { class ClientDiscoverHostHandler extends SimpleChannelInboundHandler<DatagramPacket> {
// This uses CHANNEL LOCAL DATA to save the data. // This uses CHANNEL LOCAL DATA to save the data.
public static final AttributeKey<BroadcastResponse> STATE = AttributeKey.valueOf(ClientDiscoverHostHandler.class, "Discover.state"); public static final AttributeKey<BroadcastResponse> STATE = AttributeKey.valueOf(ClientDiscoverHostHandler.class, "Discover.state");
ClientDiscoverHostHandler() { ClientDiscoverHostHandler() {

View File

@ -29,7 +29,7 @@ class KryoDecoder extends ByteToMessageDecoder {
private final CryptoSerializationManager serializationManager; private final CryptoSerializationManager serializationManager;
public public
KryoDecoder(CryptoSerializationManager serializationManager) { KryoDecoder(final CryptoSerializationManager serializationManager) {
super(); super();
this.serializationManager = serializationManager; this.serializationManager = serializationManager;
} }
@ -44,7 +44,6 @@ class KryoDecoder extends ByteToMessageDecoder {
@Override @Override
protected protected
void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception { void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception {
// Make sure if the length field was received, // Make sure if the length field was received,
// and read the length of the next object from the socket. // and read the length of the next object from the socket.
int lengthLength = OptimizeUtilsByteBuf.canReadInt(in); int lengthLength = OptimizeUtilsByteBuf.canReadInt(in);

View File

@ -33,7 +33,6 @@ import io.netty.handler.timeout.IdleStateEvent;
@Sharable @Sharable
public public
class KryoDecoderUdp extends MessageToMessageDecoder<Object> { class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
private final CryptoSerializationManager serializationManager; private final CryptoSerializationManager serializationManager;
public public
@ -76,17 +75,15 @@ class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
void decode(ChannelHandlerContext context, Object message, List<Object> out) throws Exception { void decode(ChannelHandlerContext context, Object message, List<Object> out) throws Exception {
ByteBuf data = (ByteBuf) ((AddressedEnvelope) message).content(); ByteBuf data = (ByteBuf) ((AddressedEnvelope) message).content();
if (data != null) { try {
try { // no connection here because we haven't created one yet. When we do, we replace this handler with a new one.
// no connection here because we haven't created one yet. When we do, we replace this handler with a new one. Object object = serializationManager.read(data, data.writerIndex());
Object read = serializationManager.read(data, data.writerIndex()); out.add(object);
out.add(read); } catch (IOException e) {
} catch (IOException e) { String msg = "Unable to deserialize object";
String msg = "Unable to deserialize object"; LoggerFactory.getLogger(this.getClass())
LoggerFactory.getLogger(this.getClass()) .error(msg, e);
.error(msg, e); throw new IOException(msg, e);
throw new IOException(msg, e);
}
} }
} }
} }

View File

@ -23,10 +23,13 @@ import org.slf4j.LoggerFactory;
import dorkbox.network.connection.CryptoConnection; import dorkbox.network.connection.CryptoConnection;
import dorkbox.network.serialization.CryptoSerializationManager; import dorkbox.network.serialization.CryptoSerializationManager;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
@Sharable @Sharable
public public
@ -39,13 +42,36 @@ class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
this.serializationManager = serializationManager; this.serializationManager = serializationManager;
} }
/**
* Invoked when a {@link Channel} has been idle for a while.
*/
@Override
public
void userEventTriggered(ChannelHandlerContext context, Object event) throws Exception {
// if (e.getState() == IdleState.READER_IDLE) {
// e.getChannel().close();
// } else if (e.getState() == IdleState.WRITER_IDLE) {
// e.getChannel().write(new Object());
// } else
if (event instanceof IdleStateEvent) {
if (((IdleStateEvent) event).state() == IdleState.ALL_IDLE) {
// will auto-flush if necessary
// TODO: if we have been idle TOO LONG, then we close this channel!
// if we are idle for a much smaller amount of time, then we pass the idle message up to the connection?
// this.sessionManager.onIdle(this);
}
}
super.userEventTriggered(context, event);
}
@Override @Override
public public
void decode(ChannelHandlerContext context, DatagramPacket in, List<Object> out) throws Exception { void decode(ChannelHandlerContext context, DatagramPacket in, List<Object> out) throws Exception {
CryptoConnection last = (CryptoConnection) context.pipeline()
.last();
try { try {
CryptoConnection last = (CryptoConnection) context.pipeline()
.last();
ByteBuf data = in.content(); ByteBuf data = in.content();
Object object = serializationManager.readWithCrypto(last, data, data.readableBytes()); Object object = serializationManager.readWithCrypto(last, data, data.readableBytes());
out.add(object); out.add(object);

View File

@ -34,11 +34,12 @@ public
class KryoEncoderUdp extends MessageToMessageEncoder<Object> { class KryoEncoderUdp extends MessageToMessageEncoder<Object> {
private static final int maxSize = EndPoint.udpMaxSize; private static final int maxSize = EndPoint.udpMaxSize;
private final CryptoSerializationManager serializationManager; private final CryptoSerializationManager serializationManager;
public public
KryoEncoderUdp(CryptoSerializationManager serializationManager) { KryoEncoderUdp(final CryptoSerializationManager serializationManager) {
super(); super();
this.serializationManager = serializationManager; this.serializationManager = serializationManager;
} }