Code cleanup. Removed unnecessary methods from Connection interface

This commit is contained in:
nathan 2015-07-22 10:35:45 +02:00
parent f3fef6fcea
commit 61545f0235
19 changed files with 77 additions and 128 deletions

View File

@ -15,8 +15,6 @@
*/ */
package dorkbox.network.connection; package dorkbox.network.connection;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.bridge.ConnectionBridge; import dorkbox.network.connection.bridge.ConnectionBridge;
import dorkbox.network.connection.idle.IdleBridge; import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleSender; import dorkbox.network.connection.idle.IdleSender;
@ -27,24 +25,6 @@ import dorkbox.util.exceptions.NetException;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public public
interface Connection { interface Connection {
/**
* Initialize the connection with any extra info that is needed but was unavailable at the channel construction.
* <p/>
* This happens BEFORE prep.
*/
void init(Bridge bridge);
/**
* Prepare the channel wrapper, since it doesn't have access to certain fields during it's initialization.
* <p/>
* This happens AFTER init.
*/
void prep();
/**
* @return the AES key/IV, etc associated with this connection
*/
ParametersWithIV getCryptoParameters();
/** /**
* Has the remote ECC public key changed. This can be useful if specific actions are necessary when the key has changed. * Has the remote ECC public key changed. This can be useful if specific actions are necessary when the key has changed.

View File

@ -103,9 +103,9 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
/** /**
* Initialize the connection with any extra info that is needed but was unavailable at the channel construction. * Initialize the connection with any extra info that is needed but was unavailable at the channel construction.
* <p/>
* This happens BEFORE prep.
*/ */
@Override
public
void init(final Bridge bridge) { void init(final Bridge bridge) {
if (bridge != null) { if (bridge != null) {
this.sessionManager = bridge.sessionManager; this.sessionManager = bridge.sessionManager;
@ -126,10 +126,10 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
} }
/** /**
* Prepare the channel wrapper, since it doesn't have access to certain fields during it's construction. * Prepare the channel wrapper, since it doesn't have access to certain fields during it's initialization.
* <p/>
* This happens AFTER init.
*/ */
@Override
public
void prep() { void prep() {
if (this.channelWrapper != null) { if (this.channelWrapper != null) {
this.channelWrapper.init(); this.channelWrapper.init();
@ -140,8 +140,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
/** /**
* @return the AES key/IV, etc associated with this connection * @return the AES key/IV, etc associated with this connection
*/ */
@Override final
public final
ParametersWithIV getCryptoParameters() { ParametersWithIV getCryptoParameters() {
return this.channelWrapper.cryptoParameters(); return this.channelWrapper.cryptoParameters();
} }

View File

@ -77,7 +77,6 @@ class EndPoint {
// TODO: maybe some sort of STUN-like connection keep-alive?? // TODO: maybe some sort of STUN-like connection keep-alive??
// TODO: do we really need this? Maybe?
public static final String LOCAL_CHANNEL = "local_channel"; public static final String LOCAL_CHANNEL = "local_channel";
protected static final String shutdownHookName = "::SHUTDOWN_HOOK::"; protected static final String shutdownHookName = "::SHUTDOWN_HOOK::";
protected static final String stopTreadName = "::STOP_THREAD::"; protected static final String stopTreadName = "::STOP_THREAD::";
@ -417,14 +416,14 @@ class EndPoint {
/** /**
* This method allows the connections used by the client/server to be subclassed (custom implementations). * This method allows the connections used by the client/server to be subclassed (custom implementations).
* <p/> * <p/>
* As this is for the network stack, the new connection MUST subclass {@link Connection} * As this is for the network stack, the new connection MUST subclass {@link ConnectionImpl}
* <p/> * <p/>
* The parameters are ALL NULL when getting the base class, as this instance is just thrown away. * The parameters are ALL NULL when getting the base class, as this instance is just thrown away.
* *
* @return a new network connection * @return a new network connection
*/ */
public public
Connection newConnection(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) { ConnectionImpl newConnection(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
return new ConnectionImpl(logger, endPoint, rmiBridge); return new ConnectionImpl(logger, endPoint, rmiBridge);
} }
@ -437,7 +436,7 @@ class EndPoint {
*/ */
protected final protected final
Connection connection0(MetaChannel metaChannel) { Connection connection0(MetaChannel metaChannel) {
Connection connection; ConnectionImpl connection;
RmiBridge rmiBridge = null; RmiBridge rmiBridge = null;
if (metaChannel != null && rmiEnabled) { if (metaChannel != null && rmiEnabled) {
@ -490,7 +489,7 @@ class EndPoint {
* <p/> * <p/>
* Only the CLIENT injects in front of this) * Only the CLIENT injects in front of this)
*/ */
void connectionConnected0(Connection connection) { void connectionConnected0(ConnectionImpl connection) {
this.isConnected.set(true); this.isConnected.set(true);
// prep the channel wrapper // prep the channel wrapper

View File

@ -131,7 +131,7 @@ class EndPointClient extends EndPoint implements Runnable {
*/ */
@Override @Override
final final
void connectionConnected0(Connection connection) { void connectionConnected0(ConnectionImpl connection) {
// invokes the listener.connection() method, and initialize the connection channels with whatever extra info they might need. // invokes the listener.connection() method, and initialize the connection channels with whatever extra info they might need.
super.connectionConnected0(connection); super.connectionConnected0(connection);

View File

@ -737,7 +737,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@Override @Override
public final public final
void writeWithCryptoTcp(Connection connection, ByteBuf buffer, Object message) { void writeWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, Object message) {
if (connection == null) { if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!"); throw new NetException("Unable to perform crypto when NO network connection!");
} }
@ -752,7 +752,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@Override @Override
public final public final
void writeWithCryptoUdp(Connection connection, ByteBuf buffer, Object message) { void writeWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, Object message) {
if (connection == null) { if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!"); throw new NetException("Unable to perform crypto when NO network connection!");
} }
@ -770,7 +770,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@Override @Override
public final public final
Object readWithCryptoTcp(Connection connection, ByteBuf buffer, int length) { Object readWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, int length) {
if (connection == null) { if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!"); throw new NetException("Unable to perform crypto when NO network connection!");
} }
@ -788,7 +788,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@Override @Override
public final public final
Object readWithCryptoUdp(Connection connection, ByteBuf buffer, int length) { Object readWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, int length) {
if (connection == null) { if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!"); throw new NetException("Unable to perform crypto when NO network connection!");
} }
@ -801,7 +801,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private private
void write0(final Connection connection, final ByteBuf buffer, final Object message, final boolean doCrypto) { void write0(final ConnectionImpl connection, final ByteBuf buffer, final Object message, final boolean doCrypto) {
final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly(); final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly();
Logger logger2 = logger; Logger logger2 = logger;
@ -911,7 +911,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/ */
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
private private
Object read0(final Connection connection, final ByteBuf buffer, final int length, final boolean doCrypto) { Object read0(final ConnectionImpl connection, final ByteBuf buffer, final int length, final boolean doCrypto) {
final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly(); final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly();
Logger logger2 = logger; Logger logger2 = logger;

View File

@ -18,9 +18,9 @@ package dorkbox.network.connection;
import dorkbox.network.connection.registration.MetaChannel; import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.pipeline.KryoEncoder; import dorkbox.network.pipeline.KryoEncoder;
import dorkbox.network.pipeline.KryoEncoderCrypto; import dorkbox.network.pipeline.KryoEncoderCrypto;
import dorkbox.util.exceptions.SecurityException;
import dorkbox.util.collections.IntMap; import dorkbox.util.collections.IntMap;
import dorkbox.util.crypto.Crypto; import dorkbox.util.crypto.Crypto;
import dorkbox.util.exceptions.SecurityException;
import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -135,7 +135,7 @@ class RegistrationWrapper implements UdpServer {
* to the pipeline are finished. * to the pipeline are finished.
*/ */
public public
void connectionConnected0(Connection networkConnection) { void connectionConnected0(ConnectionImpl networkConnection) {
this.endPoint.connectionConnected0(networkConnection); this.endPoint.connectionConnected0(networkConnection);
} }

View File

@ -15,7 +15,7 @@
*/ */
package dorkbox.network.connection.registration; package dorkbox.network.connection.registration;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters;
@ -43,7 +43,7 @@ class MetaChannel {
public Channel udtChannel = null; public Channel udtChannel = null;
public Connection connection; // only needed until the connection has been notified. public ConnectionImpl connection; // only needed until the connection has been notified.
public ECPublicKeyParameters publicKey; // used for ECC crypto + handshake on NETWORK (remote) connections. This is the remote public key. public ECPublicKeyParameters publicKey; // used for ECC crypto + handshake on NETWORK (remote) connections. This is the remote public key.

View File

@ -15,7 +15,7 @@
*/ */
package dorkbox.network.connection.registration.local; package dorkbox.network.connection.registration.local;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper; import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel; import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration; import dorkbox.network.connection.registration.Registration;
@ -87,7 +87,7 @@ class RegistrationLocalHandlerClient extends RegistrationLocalHandler {
// Event though a local channel is XOR with everything else, we still have to make the client clean up it's state. // Event though a local channel is XOR with everything else, we still have to make the client clean up it's state.
registrationWrapper.registerNextProtocol0(); registrationWrapper.registerNextProtocol0();
Connection connection = metaChannel.connection; ConnectionImpl connection = metaChannel.connection;
registrationWrapper.connectionConnected0(connection); registrationWrapper.connectionConnected0(connection);
} }
else { else {

View File

@ -15,7 +15,7 @@
*/ */
package dorkbox.network.connection.registration.local; package dorkbox.network.connection.registration.local;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper; import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel; import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.util.collections.IntMap; import dorkbox.util.collections.IntMap;
@ -79,7 +79,7 @@ class RegistrationLocalHandlerServer extends RegistrationLocalHandler {
logger2.trace("Sent registration"); logger2.trace("Sent registration");
} }
Connection connection = null; ConnectionImpl connection = null;
try { try {
IntMap<MetaChannel> channelMap = this.registrationWrapper.getAndLockChannelMap(); IntMap<MetaChannel> channelMap = this.registrationWrapper.getAndLockChannelMap();
MetaChannel metaChannel = channelMap.remove(channel.hashCode()); MetaChannel metaChannel = channelMap.remove(channel.hashCode());

View File

@ -16,18 +16,17 @@
package dorkbox.network.connection.registration.remote; package dorkbox.network.connection.registration.remote;
import dorkbox.network.Broadcast; import dorkbox.network.Broadcast;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper; import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel; import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration; import dorkbox.network.connection.registration.Registration;
import dorkbox.network.connection.wrapper.UdpWrapper; import dorkbox.network.connection.wrapper.UdpWrapper;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import dorkbox.util.bytes.OptimizeUtilsByteArray; import dorkbox.util.bytes.OptimizeUtilsByteArray;
import dorkbox.util.collections.IntMap; import dorkbox.util.collections.IntMap;
import dorkbox.util.collections.IntMap.Entries; import dorkbox.util.collections.IntMap.Entries;
import dorkbox.util.crypto.Crypto; import dorkbox.util.crypto.Crypto;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
@ -149,7 +148,7 @@ class RegistrationRemoteHandlerServerUDP extends MessageToMessageCodec<DatagramP
public final public final
void sendUDP(ChannelHandlerContext context, Object object, ByteBuf buffer, InetSocketAddress udpRemoteAddress) { void sendUDP(ChannelHandlerContext context, Object object, ByteBuf buffer, InetSocketAddress udpRemoteAddress) {
Connection networkConnection = this.registrationWrapper.getServerUDP(udpRemoteAddress); ConnectionImpl networkConnection = this.registrationWrapper.getServerUDP(udpRemoteAddress);
if (networkConnection != null) { if (networkConnection != null) {
// try to write data! (IT SHOULD ALWAYS BE ENCRYPTED HERE!) // try to write data! (IT SHOULD ALWAYS BE ENCRYPTED HERE!)
this.serializationManager.writeWithCryptoUdp(networkConnection, buffer, object); this.serializationManager.writeWithCryptoUdp(networkConnection, buffer, object);

View File

@ -26,20 +26,20 @@ import java.util.List;
public public
class KryoDecoder extends ByteToMessageDecoder { class KryoDecoder extends ByteToMessageDecoder {
private final OptimizeUtilsByteBuf optimize; private final OptimizeUtilsByteBuf optimize;
private final CryptoSerializationManager kryoWrapper; private final CryptoSerializationManager serializationManager;
public public
KryoDecoder(CryptoSerializationManager kryoWrapper) { KryoDecoder(CryptoSerializationManager serializationManager) {
super(); super();
this.kryoWrapper = kryoWrapper; this.serializationManager = serializationManager;
this.optimize = OptimizeUtilsByteBuf.get(); this.optimize = OptimizeUtilsByteBuf.get();
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected protected
Object readObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext context, ByteBuf in, int length) { Object readObject(CryptoSerializationManager serializationManager, ChannelHandlerContext context, ByteBuf in, int length) {
// 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.
return kryoWrapper.read(in, length); return serializationManager.read(in, length);
} }
@Override @Override
@ -150,13 +150,13 @@ class KryoDecoder extends ByteToMessageDecoder {
length = optimize.readInt(in, true); // object LENGTH length = optimize.readInt(in, true); // object LENGTH
// however many we need to // however many we need to
out.add(readObject(this.kryoWrapper, ctx, in, length)); out.add(readObject(this.serializationManager, ctx, in, length));
} }
// the buffer reader index will be at the correct location, since the read object method advances it. // the buffer reader index will be at the correct location, since the read object method advances it.
} }
else { else {
// exactly one! // exactly one!
out.add(readObject(this.kryoWrapper, ctx, in, length)); out.add(readObject(this.serializationManager, ctx, in, length));
} }
} }
} }

View File

@ -15,9 +15,8 @@
*/ */
package dorkbox.network.pipeline; package dorkbox.network.pipeline;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -29,21 +28,15 @@ public
class KryoDecoderCrypto extends KryoDecoder { class KryoDecoderCrypto extends KryoDecoder {
public public
KryoDecoderCrypto(CryptoSerializationManager kryoWrapper) { KryoDecoderCrypto(CryptoSerializationManager serializationManager) {
super(kryoWrapper); super(serializationManager);
} }
@Override @Override
protected protected
Object readObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, ByteBuf in, int length) { Object readObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, ByteBuf in, int length) {
ChannelHandler last = ctx.pipeline() ChannelHandler last = ctx.pipeline()
.last(); .last();
if (last instanceof Connection) { return serializationManager.readWithCryptoTcp((ConnectionImpl) last, in, length);
return kryoWrapper.readWithCryptoTcp((Connection) last, in, length);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to READ an object with NO network connection!");
}
} }
} }

View File

@ -17,8 +17,8 @@ package dorkbox.network.pipeline;
import com.esotericsoftware.kryo.KryoException; import com.esotericsoftware.kryo.KryoException;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import dorkbox.util.bytes.OptimizeUtilsByteBuf; import dorkbox.util.bytes.OptimizeUtilsByteBuf;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -28,14 +28,14 @@ import io.netty.handler.codec.MessageToByteEncoder;
public public
class KryoEncoder extends MessageToByteEncoder<Object> { class KryoEncoder extends MessageToByteEncoder<Object> {
private static final int reservedLengthIndex = 4; private static final int reservedLengthIndex = 4;
private final CryptoSerializationManager kryoWrapper; private final CryptoSerializationManager serializationManager;
private final OptimizeUtilsByteBuf optimize; private final OptimizeUtilsByteBuf optimize;
public public
KryoEncoder(CryptoSerializationManager kryoWrapper) { KryoEncoder(CryptoSerializationManager serializationManager) {
super(); super();
this.kryoWrapper = kryoWrapper; this.serializationManager = serializationManager;
this.optimize = OptimizeUtilsByteBuf.get(); this.optimize = OptimizeUtilsByteBuf.get();
} }
@ -60,7 +60,7 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
out.writeInt(0); // put an int in, which is the same size as reservedLengthIndex out.writeInt(0); // put an int in, which is the same size as reservedLengthIndex
try { try {
writeObject(this.kryoWrapper, ctx, msg, out); writeObject(this.serializationManager, ctx, msg, out);
// now set the frame (if it's TCP)! // now set the frame (if it's TCP)!
int length = out.readableBytes() - startIndex - int length = out.readableBytes() - startIndex -

View File

@ -15,9 +15,8 @@
*/ */
package dorkbox.network.pipeline; package dorkbox.network.pipeline;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
@ -28,21 +27,15 @@ public
class KryoEncoderCrypto extends KryoEncoder { class KryoEncoderCrypto extends KryoEncoder {
public public
KryoEncoderCrypto(CryptoSerializationManager kryoWrapper) { KryoEncoderCrypto(CryptoSerializationManager serializationManager) {
super(kryoWrapper); super(serializationManager);
} }
@Override @Override
protected protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) { void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
ChannelHandler last = ctx.pipeline() ChannelHandler last = ctx.pipeline()
.last(); .last();
if (last instanceof Connection) { serializationManager.writeWithCryptoTcp((ConnectionImpl) last, buffer, msg);
kryoWrapper.writeWithCryptoTcp((Connection) last, buffer, msg);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to WRITE an object with NO network connection (or wrong connection type!)!");
}
} }
} }

View File

@ -29,11 +29,11 @@ import java.util.List;
public public
class KryoDecoderUdp extends MessageToMessageDecoder<DatagramPacket> { class KryoDecoderUdp extends MessageToMessageDecoder<DatagramPacket> {
private final CryptoSerializationManager kryoWrapper; private final CryptoSerializationManager serializationManager;
public public
KryoDecoderUdp(CryptoSerializationManager kryoWrapper) { KryoDecoderUdp(CryptoSerializationManager serializationManager) {
this.kryoWrapper = kryoWrapper; this.serializationManager = serializationManager;
} }
@Override @Override
@ -46,12 +46,12 @@ class KryoDecoderUdp extends MessageToMessageDecoder<DatagramPacket> {
// there is a REMOTE possibility that UDP traffic BEAT the TCP registration traffic, which means that THIS packet // there is a REMOTE possibility that UDP traffic BEAT the TCP registration traffic, which means that THIS packet
// COULD be encrypted! // COULD be encrypted!
if (kryoWrapper.isEncrypted(data)) { if (serializationManager.isEncrypted(data)) {
throw new NetException("Encrypted UDP packet received before registration complete. WHOOPS!"); throw new NetException("Encrypted UDP packet received before registration complete. WHOOPS!");
} }
// 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 read = kryoWrapper.read(data, data.writerIndex()); Object read = serializationManager.read(data, data.writerIndex());
out.add(read); out.add(read);
} }
} }

View File

@ -15,9 +15,8 @@
*/ */
package dorkbox.network.pipeline.udp; package dorkbox.network.pipeline.udp;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
@ -31,11 +30,11 @@ import java.util.List;
public public
class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> { class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
private final CryptoSerializationManager kryoWrapper; private final CryptoSerializationManager serializationManager;
public public
KryoDecoderUdpCrypto(CryptoSerializationManager kryoWrapper) { KryoDecoderUdpCrypto(CryptoSerializationManager serializationManager) {
this.kryoWrapper = kryoWrapper; this.serializationManager = serializationManager;
} }
@Override @Override
@ -44,14 +43,8 @@ class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
ChannelHandler last = ctx.pipeline() ChannelHandler last = ctx.pipeline()
.last(); .last();
if (last instanceof Connection) {
ByteBuf data = in.content(); ByteBuf data = in.content();
Object object = kryoWrapper.readWithCryptoUdp((Connection) last, data, data.readableBytes()); Object object = serializationManager.readWithCryptoUdp((ConnectionImpl) last, data, data.readableBytes());
out.add(object); out.add(object);
} }
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to READ an object with NO network connection!");
}
}
} }

View File

@ -36,21 +36,21 @@ 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 kryoWrapper; private final CryptoSerializationManager serializationManager;
public public
KryoEncoderUdp(CryptoSerializationManager kryoWrapper) { KryoEncoderUdp(CryptoSerializationManager serializationManager) {
super(); super();
this.kryoWrapper = kryoWrapper; this.serializationManager = serializationManager;
} }
// the crypto writer will override this // the crypto writer will override this
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext context, Object msg, ByteBuf buffer) { void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext context, Object msg, ByteBuf buffer) {
// 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.
kryoWrapper.write(buffer, msg); serializationManager.write(buffer, msg);
} }
@Override @Override
@ -61,7 +61,7 @@ class KryoEncoderUdp extends MessageToMessageEncoder<Object> {
ByteBuf outBuffer = Unpooled.buffer(maxSize); ByteBuf outBuffer = Unpooled.buffer(maxSize);
// no size info, since this is UDP, it is not segmented // no size info, since this is UDP, it is not segmented
writeObject(this.kryoWrapper, ctx, msg, outBuffer); writeObject(this.serializationManager, ctx, msg, outBuffer);
// have to check to see if we are too big for UDP! // have to check to see if we are too big for UDP!

View File

@ -15,9 +15,8 @@
*/ */
package dorkbox.network.pipeline.udp; package dorkbox.network.pipeline.udp;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
@ -28,22 +27,16 @@ public
class KryoEncoderUdpCrypto extends KryoEncoderUdp { class KryoEncoderUdpCrypto extends KryoEncoderUdp {
public public
KryoEncoderUdpCrypto(CryptoSerializationManager kryoWrapper) { KryoEncoderUdpCrypto(CryptoSerializationManager serializationManager) {
super(kryoWrapper); super(serializationManager);
} }
@Override @Override
protected protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) { void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
ChannelHandler last = ctx.pipeline() ChannelHandler last = ctx.pipeline()
.last(); .last();
if (last instanceof Connection) { serializationManager.writeWithCryptoUdp((ConnectionImpl) last, buffer, msg);
kryoWrapper.writeWithCryptoUdp((Connection) last, buffer, msg);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to WRITE an object with NO network connection!");
}
} }
} }

View File

@ -15,7 +15,7 @@
*/ */
package dorkbox.network.util; package dorkbox.network.util;
import dorkbox.network.connection.Connection; import dorkbox.network.connection.ConnectionImpl;
import dorkbox.util.SerializationManager; import dorkbox.util.SerializationManager;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -37,14 +37,14 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* <p/> * <p/>
* There is a small speed penalty if there were no kryo's available to use. * There is a small speed penalty if there were no kryo's available to use.
*/ */
void writeWithCryptoTcp(Connection connection, ByteBuf buffer, Object message); void writeWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, Object message);
/** /**
* Waits until a kryo is available to write, using CAS operations to prevent having to synchronize. * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize.
* <p/> * <p/>
* There is a small speed penalty if there were no kryo's available to use. * There is a small speed penalty if there were no kryo's available to use.
*/ */
void writeWithCryptoUdp(Connection connection, ByteBuf buffer, Object message); void writeWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, Object message);
/** /**
* Reads an object from the buffer. * Reads an object from the buffer.
@ -54,7 +54,7 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* @param connection can be NULL * @param connection can be NULL
* @param length should ALWAYS be the length of the expected object! * @param length should ALWAYS be the length of the expected object!
*/ */
Object readWithCryptoTcp(Connection connection, ByteBuf buffer, int length); Object readWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, int length);
/** /**
* Reads an object from the buffer. * Reads an object from the buffer.
@ -64,5 +64,5 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* @param connection can be NULL * @param connection can be NULL
* @param length should ALWAYS be the length of the expected object! * @param length should ALWAYS be the length of the expected object!
*/ */
Object readWithCryptoUdp(Connection connection, ByteBuf buffer, int length); Object readWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, int length);
} }