Code cleanup

This commit is contained in:
nathan 2016-02-21 02:15:28 +01:00
parent a84fdfc1d3
commit 30a023a682
10 changed files with 27 additions and 96 deletions

View File

@ -71,6 +71,7 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
for (int i = 0; i < split.length; i++) {
int asInt = Integer.parseInt(split[i]);
if (asInt >= 0 && asInt <= 255) {
//noinspection NumericCastThatLosesPrecision
address[i] = (byte) Integer.parseInt(split[i]);
}
else {
@ -234,8 +235,7 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
return;
}
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
if (!optimizeUtils.canReadInt(payload)) {
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
logger2.error("Invalid decryption of connection ID. Aborting.");
shutdown(registrationWrapper2, channel);
@ -243,8 +243,8 @@ class RegistrationRemoteHandlerClientTCP<C extends Connection> extends Registrat
return;
}
metaChannel.connectionID = optimizeUtils.readInt(payload, true);
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
metaChannel.connectionID = OptimizeUtilsByteArray.readInt(payload, true);
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
/*
* Diffie-Hellman-Merkle key exchange for the AES key

View File

@ -151,8 +151,7 @@ class RegistrationRemoteHandlerClientUDP<C extends Connection> extends Registrat
// now decrypt channelID using AES
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
if (!optimizeUtils.canReadInt(payload)) {
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
this.logger.error("Invalid decryption of connection ID. Aborting.");
shutdown(registrationWrapper2, channel);
@ -160,7 +159,7 @@ class RegistrationRemoteHandlerClientUDP<C extends Connection> extends Registrat
return;
}
Integer connectionID = optimizeUtils.readInt(payload, true);
Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true);
MetaChannel metaChannel2 = null;
try {

View File

@ -144,8 +144,7 @@ class RegistrationRemoteHandlerClientUDT<C extends Connection> extends Registrat
// now decrypt channelID using AES
byte[] payload = CryptoAES.decrypt(getAesEngine(), metaChannel.aesKey, metaChannel.aesIV, registration.payload, logger);
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
if (!optimizeUtils.canReadInt(payload)) {
if (!OptimizeUtilsByteArray.canReadInt(payload)) {
logger2.error("Invalid decryption of connection ID. Aborting.");
shutdown(registrationWrapper2, channel);
@ -153,7 +152,7 @@ class RegistrationRemoteHandlerClientUDT<C extends Connection> extends Registrat
return;
}
Integer connectionID = optimizeUtils.readInt(payload, true);
Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true);
MetaChannel metaChannel2 = null;
try {

View File

@ -206,7 +206,6 @@ class RegistrationRemoteHandlerServerTCP<C extends Connection> extends Registrat
// save off encryption handshake info
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
/*
@ -224,9 +223,9 @@ class RegistrationRemoteHandlerServerTCP<C extends Connection> extends Registrat
// 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];
optimizeUtils.writeInt(idAsBytes, connectionID, true);
OptimizeUtilsByteArray.writeInt(idAsBytes, connectionID, true);
byte[] combinedBytes = Arrays.concatenate(idAsBytes, pubKeyAsBytes);

View File

@ -251,10 +251,9 @@ class RegistrationRemoteHandlerServerUDP<C extends Connection> extends MessageTo
Registration register = new Registration();
// save off the connectionID as a byte array, then encrypt it
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
byte[] idAsBytes = new byte[intLength];
optimizeUtils.writeInt(idAsBytes, metaChannel.connectionID, true);
OptimizeUtilsByteArray.writeInt(idAsBytes, metaChannel.connectionID, true);
// now encrypt payload via AES
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),

View File

@ -121,10 +121,9 @@ class RegistrationRemoteHandlerServerUDT<C extends Connection> extends Registrat
Registration register = new Registration();
// save off the connectionID as a byte array, then encrypt it
OptimizeUtilsByteArray optimizeUtils = OptimizeUtilsByteArray.get();
int intLength = optimizeUtils.intLength(metaChannel.connectionID, true);
int intLength = OptimizeUtilsByteArray.intLength(metaChannel.connectionID, true);
byte[] idAsBytes = new byte[intLength];
optimizeUtils.writeInt(idAsBytes, metaChannel.connectionID, true);
OptimizeUtilsByteArray.writeInt(idAsBytes, metaChannel.connectionID, true);
// now encrypt payload via AES
register.payload = CryptoAES.encrypt(RegistrationRemoteHandler.getAesEngine(),

View File

@ -26,14 +26,12 @@ import java.util.List;
public
class KryoDecoder extends ByteToMessageDecoder {
private final OptimizeUtilsByteBuf optimize;
private final CryptoSerializationManager serializationManager;
public
KryoDecoder(CryptoSerializationManager serializationManager) {
super();
this.serializationManager = serializationManager;
this.optimize = OptimizeUtilsByteBuf.get();
}
@SuppressWarnings("unused")
@ -51,11 +49,10 @@ class KryoDecoder extends ByteToMessageDecoder {
@Override
protected
void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
OptimizeUtilsByteBuf optimize = this.optimize;
// Make sure if the length field was received,
// 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.
if (lengthLength == 0 || readableBytes < 2 || readableBytes < lengthLength) {
@ -78,7 +75,7 @@ class KryoDecoder extends ByteToMessageDecoder {
// 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.
@ -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.
// (That's how we got here to begin with)
while (readableBytes > 0) {
if (optimize.canReadInt(in) > 0) {
length = optimize.readInt(in, true);
if (OptimizeUtilsByteBuf.canReadInt(in) > 0) {
length = OptimizeUtilsByteBuf.readInt(in, true);
if (length <= 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!
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
out.add(readObject(this.serializationManager, ctx, in, length));

View File

@ -30,14 +30,12 @@ public
class KryoEncoder extends MessageToByteEncoder<Object> {
private static final int reservedLengthIndex = 4;
private final CryptoSerializationManager serializationManager;
private final OptimizeUtilsByteBuf optimize;
public
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.optimize = OptimizeUtilsByteBuf.get();
}
// the crypto writer will override this
@ -72,12 +70,11 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
writeObject(this.serializationManager, context, msg, out);
// now set the frame (if it's TCP)!
int length = out.readableBytes() - startIndex -
reservedLengthIndex; // (reservedLengthLength) 4 is the reserved space for the integer.
// (reservedLengthLength) 4 is the reserved space for the integer.
int length = out.readableBytes() - startIndex - reservedLengthIndex;
// specify the header.
OptimizeUtilsByteBuf optimize = this.optimize;
int lengthOfTheLength = optimize.intLength(length, true);
int lengthOfTheLength = OptimizeUtilsByteBuf.intLength(length, true);
// 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;
@ -86,7 +83,7 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
out.writerIndex(newIndex);
// do the optimized length thing!
optimize.writeInt(out, length, true);
OptimizeUtilsByteBuf.writeInt(out, length, true);
out.setIndex(newIndex, oldIndex);
} catch (KryoException ex) {
context.fireExceptionCaught(new IOException("Unable to serialize object of type: " + msg.getClass()

View File

@ -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();
}
}

View File

@ -54,7 +54,7 @@ class RmiSendObjectTest extends BaseTest {
public
void received(Connection connection, OtherObjectImpl object) {
// The test is complete when the client sends the OtherObject instance.
if (object.value() == 12.34f) {
if (object.value() == 12.34F) {
stopEndPoints();
} else {
fail("Incorrect object value");
@ -86,14 +86,14 @@ class RmiSendObjectTest extends BaseTest {
test.setOther(43.21f);
// 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.
OtherObject otherObject = test.getOtherObject();
// Normal remote method call on the second object.
otherObject.setValue(12.34f);
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.
// we have to manually flush, since we are in a separate thread that does not auto-flush.