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
class ClientDiscoverHostHandler extends SimpleChannelInboundHandler<DatagramPacket> {
// This uses CHANNEL LOCAL DATA to save the data.
public static final AttributeKey<BroadcastResponse> STATE = AttributeKey.valueOf(ClientDiscoverHostHandler.class, "Discover.state");
ClientDiscoverHostHandler() {

View File

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

View File

@ -33,7 +33,6 @@ import io.netty.handler.timeout.IdleStateEvent;
@Sharable
public
class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
private final CryptoSerializationManager serializationManager;
public
@ -76,17 +75,15 @@ class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
void decode(ChannelHandlerContext context, Object message, List<Object> out) throws Exception {
ByteBuf data = (ByteBuf) ((AddressedEnvelope) message).content();
if (data != null) {
try {
// no connection here because we haven't created one yet. When we do, we replace this handler with a new one.
Object read = serializationManager.read(data, data.writerIndex());
out.add(read);
} catch (IOException e) {
String msg = "Unable to deserialize object";
LoggerFactory.getLogger(this.getClass())
.error(msg, e);
throw new IOException(msg, e);
}
try {
// 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());
out.add(object);
} catch (IOException e) {
String msg = "Unable to deserialize object";
LoggerFactory.getLogger(this.getClass())
.error(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.serialization.CryptoSerializationManager;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
@Sharable
public
@ -39,13 +42,36 @@ class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
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
public
void decode(ChannelHandlerContext context, DatagramPacket in, List<Object> out) throws Exception {
CryptoConnection last = (CryptoConnection) context.pipeline()
.last();
try {
CryptoConnection last = (CryptoConnection) context.pipeline()
.last();
ByteBuf data = in.content();
Object object = serializationManager.readWithCrypto(last, data, data.readableBytes());
out.add(object);

View File

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