Code polish/cleanup
This commit is contained in:
parent
a4cf9f4834
commit
292f50a059
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -33,7 +33,6 @@ import io.netty.handler.timeout.IdleStateEvent;
|
|||
@Sharable
|
||||
public
|
||||
class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
|
||||
|
||||
private final CryptoSerializationManager serializationManager;
|
||||
|
||||
public
|
||||
|
@ -76,11 +75,10 @@ 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);
|
||||
Object object = serializationManager.read(data, data.writerIndex());
|
||||
out.add(object);
|
||||
} catch (IOException e) {
|
||||
String msg = "Unable to deserialize object";
|
||||
LoggerFactory.getLogger(this.getClass())
|
||||
|
@ -88,5 +86,4 @@ class KryoDecoderUdp extends MessageToMessageDecoder<Object> {
|
|||
throw new IOException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
try {
|
||||
CryptoConnection last = (CryptoConnection) context.pipeline()
|
||||
.last();
|
||||
|
||||
try {
|
||||
ByteBuf data = in.content();
|
||||
Object object = serializationManager.readWithCrypto(last, data, data.readableBytes());
|
||||
out.add(object);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user