Code cleanup
This commit is contained in:
parent
a84fdfc1d3
commit
30a023a682
@ -71,6 +71,7 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
|
|||||||
for (int i = 0; i < split.length; i++) {
|
for (int i = 0; i < split.length; i++) {
|
||||||
int asInt = Integer.parseInt(split[i]);
|
int asInt = Integer.parseInt(split[i]);
|
||||||
if (asInt >= 0 && asInt <= 255) {
|
if (asInt >= 0 && asInt <= 255) {
|
||||||
|
//noinspection NumericCastThatLosesPrecision
|
||||||
address[i] = (byte) Integer.parseInt(split[i]);
|
address[i] = (byte) Integer.parseInt(split[i]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -234,8 +235,7 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
|
||||||
if (!optimizeUtils.canReadInt(payload)) {
|
|
||||||
logger2.error("Invalid decryption of connection ID. Aborting.");
|
logger2.error("Invalid decryption of connection ID. Aborting.");
|
||||||
shutdown(registrationWrapper2, channel);
|
shutdown(registrationWrapper2, channel);
|
||||||
|
|
||||||
@ -243,8 +243,8 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
metaChannel.connectionID = optimizeUtils.readInt(payload, true);
|
metaChannel.connectionID = OptimizeUtilsByteArray.readInt(payload, true);
|
||||||
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
|
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Diffie-Hellman-Merkle key exchange for the AES key
|
* Diffie-Hellman-Merkle key exchange for the AES key
|
||||||
|
@ -151,8 +151,7 @@ class RegistrationRemoteHandlerClientUDP<C extends Connection> extends Registrat
|
|||||||
// now decrypt channelID using AES
|
// now decrypt channelID using AES
|
||||||
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
|
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
|
||||||
|
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
|
||||||
if (!optimizeUtils.canReadInt(payload)) {
|
|
||||||
this.logger.error("Invalid decryption of connection ID. Aborting.");
|
this.logger.error("Invalid decryption of connection ID. Aborting.");
|
||||||
shutdown(registrationWrapper2, channel);
|
shutdown(registrationWrapper2, channel);
|
||||||
|
|
||||||
@ -160,7 +159,7 @@ class RegistrationRemoteHandlerClientUDP<C extends Connection> extends Registrat
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer connectionID = optimizeUtils.readInt(payload, true);
|
Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true);
|
||||||
|
|
||||||
MetaChannel metaChannel2 = null;
|
MetaChannel metaChannel2 = null;
|
||||||
try {
|
try {
|
||||||
|
@ -144,8 +144,7 @@ class RegistrationRemoteHandlerClientUDT<C extends Connection> extends Registrat
|
|||||||
// now decrypt channelID using AES
|
// now decrypt channelID using AES
|
||||||
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
|
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
|
||||||
|
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
|
||||||
if (!optimizeUtils.canReadInt(payload)) {
|
|
||||||
logger2.error("Invalid decryption of connection ID. Aborting.");
|
logger2.error("Invalid decryption of connection ID. Aborting.");
|
||||||
shutdown(registrationWrapper2, channel);
|
shutdown(registrationWrapper2, channel);
|
||||||
|
|
||||||
@ -153,7 +152,7 @@ class RegistrationRemoteHandlerClientUDT<C extends Connection> extends Registrat
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer connectionID = optimizeUtils.readInt(payload, true);
|
Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true);
|
||||||
|
|
||||||
MetaChannel metaChannel2 = null;
|
MetaChannel metaChannel2 = null;
|
||||||
try {
|
try {
|
||||||
|
@ -206,7 +206,6 @@ class RegistrationRemoteHandlerServerTCP<C extends Connection> extends Registrat
|
|||||||
// save off encryption handshake info
|
// save off encryption handshake info
|
||||||
metaChannel.publicKey = registration.publicKey;
|
metaChannel.publicKey = registration.publicKey;
|
||||||
|
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
|
||||||
// use ECC to create an AES key, which is used to encrypt the ECDH public key and the connectionID
|
// use ECC to create an AES key, which is used to encrypt the ECDH public key and the connectionID
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -224,9 +223,9 @@ class RegistrationRemoteHandlerServerTCP<C extends Connection> extends Registrat
|
|||||||
|
|
||||||
// save off the connectionID as a byte array
|
// save off the connectionID as a byte array
|
||||||
|
|
||||||
int intLength = optimizeUtils.intLength(connectionID, true);
|
int intLength = OptimizeUtilsByteArray.intLength(connectionID, true);
|
||||||
byte[] idAsBytes = new byte[intLength];
|
byte[] idAsBytes = new byte[intLength];
|
||||||
optimizeUtils.writeInt(idAsBytes, connectionID, true);
|
OptimizeUtilsByteArray.writeInt(idAsBytes, connectionID, true);
|
||||||
|
|
||||||
byte[] combinedBytes = Arrays.concatenate(idAsBytes, pubKeyAsBytes);
|
byte[] combinedBytes = Arrays.concatenate(idAsBytes, pubKeyAsBytes);
|
||||||
|
|
||||||
|
@ -251,10 +251,9 @@ class RegistrationRemoteHandlerServerUDP<C extends Connection> extends MessageTo
|
|||||||
Registration register = new Registration();
|
Registration register = new Registration();
|
||||||
|
|
||||||
// save off the connectionID as a byte array, then encrypt it
|
// save off the connectionID as a byte array, then encrypt it
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
|
||||||
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
|
|
||||||
byte[] idAsBytes = new byte[intLength];
|
byte[] idAsBytes = new byte[intLength];
|
||||||
optimizeUtils.writeInt(idAsBytes, metaChannel.connectionID, true);
|
OptimizeUtilsByteArray.writeInt(idAsBytes, metaChannel.connectionID, true);
|
||||||
|
|
||||||
// now encrypt payload via AES
|
// now encrypt payload via AES
|
||||||
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),
|
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),
|
||||||
|
@ -121,10 +121,9 @@ class RegistrationRemoteHandlerServerUDT<C extends Connection> extends Registrat
|
|||||||
Registration register = new Registration();
|
Registration register = new Registration();
|
||||||
|
|
||||||
// save off the connectionID as a byte array, then encrypt it
|
// save off the connectionID as a byte array, then encrypt it
|
||||||
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
|
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
|
||||||
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
|
|
||||||
byte[] idAsBytes = new byte[intLength];
|
byte[] idAsBytes = new byte[intLength];
|
||||||
optimizeUtils.writeInt(idAsBytes, metaChannel.connectionID, true);
|
OptimizeUtilsByteArray.writeInt(idAsBytes, metaChannel.connectionID, true);
|
||||||
|
|
||||||
// now encrypt payload via AES
|
// now encrypt payload via AES
|
||||||
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),
|
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),
|
||||||
|
@ -26,14 +26,12 @@ import java.util.List;
|
|||||||
|
|
||||||
public
|
public
|
||||||
class KryoDecoder extends ByteToMessageDecoder {
|
class KryoDecoder extends ByteToMessageDecoder {
|
||||||
private final OptimizeUtilsByteBuf optimize;
|
|
||||||
private final CryptoSerializationManager serializationManager;
|
private final CryptoSerializationManager serializationManager;
|
||||||
|
|
||||||
public
|
public
|
||||||
KryoDecoder(CryptoSerializationManager serializationManager) {
|
KryoDecoder(CryptoSerializationManager serializationManager) {
|
||||||
super();
|
super();
|
||||||
this.serializationManager = serializationManager;
|
this.serializationManager = serializationManager;
|
||||||
this.optimize = OptimizeUtilsByteBuf.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@ -51,11 +49,10 @@ class KryoDecoder extends ByteToMessageDecoder {
|
|||||||
@Override
|
@Override
|
||||||
protected
|
protected
|
||||||
void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||||
OptimizeUtilsByteBuf optimize = this.optimize;
|
|
||||||
|
|
||||||
// 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 = optimize.canReadInt(in);
|
int lengthLength = OptimizeUtilsByteBuf.canReadInt(in);
|
||||||
int readableBytes = in.readableBytes(); // full length of available bytes.
|
int readableBytes = in.readableBytes(); // full length of available bytes.
|
||||||
|
|
||||||
if (lengthLength == 0 || readableBytes < 2 || readableBytes < lengthLength) {
|
if (lengthLength == 0 || readableBytes < 2 || readableBytes < lengthLength) {
|
||||||
@ -78,7 +75,7 @@ class KryoDecoder extends ByteToMessageDecoder {
|
|||||||
|
|
||||||
|
|
||||||
// Read the length field.
|
// Read the length field.
|
||||||
int length = optimize.readInt(in, true);
|
int length = OptimizeUtilsByteBuf.readInt(in, true);
|
||||||
readableBytes = in.readableBytes(); // have to adjust readable bytes, since we just read an int off the buffer.
|
readableBytes = in.readableBytes(); // have to adjust readable bytes, since we just read an int off the buffer.
|
||||||
|
|
||||||
|
|
||||||
@ -119,8 +116,8 @@ class KryoDecoder extends ByteToMessageDecoder {
|
|||||||
// how many more objects?? The first time, it can be off, because we already KNOW it's > 0.
|
// how many more objects?? The first time, it can be off, because we already KNOW it's > 0.
|
||||||
// (That's how we got here to begin with)
|
// (That's how we got here to begin with)
|
||||||
while (readableBytes > 0) {
|
while (readableBytes > 0) {
|
||||||
if (optimize.canReadInt(in) > 0) {
|
if (OptimizeUtilsByteBuf.canReadInt(in) > 0) {
|
||||||
length = optimize.readInt(in, true);
|
length = OptimizeUtilsByteBuf.readInt(in, true);
|
||||||
|
|
||||||
if (length <= 0) {
|
if (length <= 0) {
|
||||||
// throw new IllegalStateException("Kryo DecoderTCP had a read length of 0");
|
// throw new IllegalStateException("Kryo DecoderTCP had a read length of 0");
|
||||||
@ -153,7 +150,7 @@ class KryoDecoder extends ByteToMessageDecoder {
|
|||||||
// NOW add each one of the NEW objects to the array!
|
// NOW add each one of the NEW objects to the array!
|
||||||
|
|
||||||
for (int i = 0; i < objectCount; i++) {
|
for (int i = 0; i < objectCount; i++) {
|
||||||
length = optimize.readInt(in, true); // object LENGTH
|
length = OptimizeUtilsByteBuf.readInt(in, true); // object LENGTH
|
||||||
|
|
||||||
// however many we need to
|
// however many we need to
|
||||||
out.add(readObject(this.serializationManager, ctx, in, length));
|
out.add(readObject(this.serializationManager, ctx, in, length));
|
||||||
|
@ -30,14 +30,12 @@ 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 serializationManager;
|
private final CryptoSerializationManager serializationManager;
|
||||||
private final OptimizeUtilsByteBuf optimize;
|
|
||||||
|
|
||||||
|
|
||||||
public
|
public
|
||||||
KryoEncoder(final CryptoSerializationManager serializationManager) {
|
KryoEncoder(final CryptoSerializationManager serializationManager) {
|
||||||
super();
|
super(false); // just use direct buffers anyways. When using Heap buffers, they because chunked and the backing array is invalid.
|
||||||
this.serializationManager = serializationManager;
|
this.serializationManager = serializationManager;
|
||||||
this.optimize = OptimizeUtilsByteBuf.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the crypto writer will override this
|
// the crypto writer will override this
|
||||||
@ -72,12 +70,11 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
|
|||||||
writeObject(this.serializationManager, context, msg, out);
|
writeObject(this.serializationManager, context, msg, out);
|
||||||
|
|
||||||
// now set the frame (if it's TCP)!
|
// now set the frame (if it's TCP)!
|
||||||
int length = out.readableBytes() - startIndex -
|
// (reservedLengthLength) 4 is the reserved space for the integer.
|
||||||
reservedLengthIndex; // (reservedLengthLength) 4 is the reserved space for the integer.
|
int length = out.readableBytes() - startIndex - reservedLengthIndex;
|
||||||
|
|
||||||
// specify the header.
|
// specify the header.
|
||||||
OptimizeUtilsByteBuf optimize = this.optimize;
|
int lengthOfTheLength = OptimizeUtilsByteBuf.intLength(length, true);
|
||||||
int lengthOfTheLength = optimize.intLength(length, true);
|
|
||||||
|
|
||||||
// 4 was the position specified by the kryoEncoder. It was to make room for the integer. DOES NOT SUPPORT NEGATIVE NUMBERS!
|
// 4 was the position specified by the kryoEncoder. It was to make room for the integer. DOES NOT SUPPORT NEGATIVE NUMBERS!
|
||||||
int newIndex = startIndex + reservedLengthIndex - lengthOfTheLength;
|
int newIndex = startIndex + reservedLengthIndex - lengthOfTheLength;
|
||||||
@ -86,7 +83,7 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
|
|||||||
out.writerIndex(newIndex);
|
out.writerIndex(newIndex);
|
||||||
|
|
||||||
// do the optimized length thing!
|
// do the optimized length thing!
|
||||||
optimize.writeInt(out, length, true);
|
OptimizeUtilsByteBuf.writeInt(out, length, true);
|
||||||
out.setIndex(newIndex, oldIndex);
|
out.setIndex(newIndex, oldIndex);
|
||||||
} catch (KryoException ex) {
|
} catch (KryoException ex) {
|
||||||
context.fireExceptionCaught(new IOException("Unable to serialize object of type: " + msg.getClass()
|
context.fireExceptionCaught(new IOException("Unable to serialize object of type: " + msg.getClass()
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010 dorkbox, llc
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package io.netty.handler.codec.compression;
|
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
|
|
||||||
public
|
|
||||||
class SnappyAccess {
|
|
||||||
|
|
||||||
public static
|
|
||||||
int calculateChecksum(ByteBuf data, int offset, int length) {
|
|
||||||
return Snappy.calculateChecksum(data, offset, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static
|
|
||||||
int calculateChecksum(ByteBuf slice) {
|
|
||||||
return Snappy.calculateChecksum(slice);
|
|
||||||
}
|
|
||||||
// oh well. At least we can still get to it.
|
|
||||||
private final Snappy snappy = new Snappy();
|
|
||||||
|
|
||||||
public
|
|
||||||
SnappyAccess() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
void encode(ByteBuf slice, ByteBuf outputBuffer, short maxValue) {
|
|
||||||
snappy.encode(slice, outputBuffer, maxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
void encode(ByteBuf slice, ByteBuf outputBuffer, int dataLength) {
|
|
||||||
snappy.encode(slice, outputBuffer, dataLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
void decode(ByteBuf inputBuffer, ByteBuf outputBuffer) {
|
|
||||||
snappy.decode(inputBuffer, outputBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
void reset() {
|
|
||||||
snappy.reset();
|
|
||||||
}
|
|
||||||
}
|
|
@ -54,7 +54,7 @@ class RmiSendObjectTest extends BaseTest {
|
|||||||
public
|
public
|
||||||
void received(Connection connection, OtherObjectImpl object) {
|
void received(Connection connection, OtherObjectImpl object) {
|
||||||
// The test is complete when the client sends the OtherObject instance.
|
// The test is complete when the client sends the OtherObject instance.
|
||||||
if (object.value() == 12.34f) {
|
if (object.value() == 12.34F) {
|
||||||
stopEndPoints();
|
stopEndPoints();
|
||||||
} else {
|
} else {
|
||||||
fail("Incorrect object value");
|
fail("Incorrect object value");
|
||||||
@ -86,14 +86,14 @@ class RmiSendObjectTest extends BaseTest {
|
|||||||
|
|
||||||
test.setOther(43.21f);
|
test.setOther(43.21f);
|
||||||
// Normal remote method call.
|
// Normal remote method call.
|
||||||
assertEquals(43.21f, test.other(), .0001f);
|
assertEquals(43.21f, test.other(), 0.0001F);
|
||||||
|
|
||||||
// Make a remote method call that returns another remote proxy object.
|
// Make a remote method call that returns another remote proxy object.
|
||||||
OtherObject otherObject = test.getOtherObject();
|
OtherObject otherObject = test.getOtherObject();
|
||||||
// Normal remote method call on the second object.
|
// Normal remote method call on the second object.
|
||||||
otherObject.setValue(12.34f);
|
otherObject.setValue(12.34f);
|
||||||
float value = otherObject.value();
|
float value = otherObject.value();
|
||||||
assertEquals(12.34f, value, .0001f);
|
assertEquals(12.34f, value, 0.0001F);
|
||||||
|
|
||||||
// When a remote proxy object is sent, the other side receives its actual remote object.
|
// When a remote proxy object is sent, the other side receives its actual remote object.
|
||||||
// we have to manually flush, since we are in a separate thread that does not auto-flush.
|
// we have to manually flush, since we are in a separate thread that does not auto-flush.
|
||||||
|
Loading…
Reference in New Issue
Block a user