diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientTCP.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientTCP.java index 319f4468..029d931b 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientTCP.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientTCP.java @@ -71,6 +71,7 @@ class RegistrationRemoteHandlerClientTCP 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 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 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 diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDP.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDP.java index 5e10fbbc..87463036 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDP.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDP.java @@ -151,8 +151,7 @@ class RegistrationRemoteHandlerClientUDP 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 extends Registrat return; } - Integer connectionID = optimizeUtils.readInt(payload, true); + Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true); MetaChannel metaChannel2 = null; try { diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDT.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDT.java index b670fb12..b1013cdd 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDT.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerClientUDT.java @@ -144,8 +144,7 @@ class RegistrationRemoteHandlerClientUDT 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 extends Registrat return; } - Integer connectionID = optimizeUtils.readInt(payload, true); + Integer connectionID = OptimizeUtilsByteArray.readInt(payload, true); MetaChannel metaChannel2 = null; try { diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerTCP.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerTCP.java index f656fad3..5fd1eff3 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerTCP.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerTCP.java @@ -206,7 +206,6 @@ class RegistrationRemoteHandlerServerTCP 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 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); diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDP.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDP.java index 0b762ece..7668166e 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDP.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDP.java @@ -251,10 +251,9 @@ class RegistrationRemoteHandlerServerUDP 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(), diff --git a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDT.java b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDT.java index 6f997e1e..dd015356 100644 --- a/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDT.java +++ b/Dorkbox-Network/src/dorkbox/network/connection/registration/remote/RegistrationRemoteHandlerServerUDT.java @@ -121,10 +121,9 @@ class RegistrationRemoteHandlerServerUDT 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(), diff --git a/Dorkbox-Network/src/dorkbox/network/pipeline/KryoDecoder.java b/Dorkbox-Network/src/dorkbox/network/pipeline/KryoDecoder.java index b7e125a8..47113059 100644 --- a/Dorkbox-Network/src/dorkbox/network/pipeline/KryoDecoder.java +++ b/Dorkbox-Network/src/dorkbox/network/pipeline/KryoDecoder.java @@ -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 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)); diff --git a/Dorkbox-Network/src/dorkbox/network/pipeline/KryoEncoder.java b/Dorkbox-Network/src/dorkbox/network/pipeline/KryoEncoder.java index 3e139579..3e360fde 100644 --- a/Dorkbox-Network/src/dorkbox/network/pipeline/KryoEncoder.java +++ b/Dorkbox-Network/src/dorkbox/network/pipeline/KryoEncoder.java @@ -30,14 +30,12 @@ public class KryoEncoder extends MessageToByteEncoder { 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 { 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 { 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() diff --git a/Dorkbox-Network/src/io/netty/handler/codec/compression/SnappyAccess.java b/Dorkbox-Network/src/io/netty/handler/codec/compression/SnappyAccess.java deleted file mode 100644 index ad511a29..00000000 --- a/Dorkbox-Network/src/io/netty/handler/codec/compression/SnappyAccess.java +++ /dev/null @@ -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(); - } -} diff --git a/Dorkbox-Network/test/dorkbox/network/rmi/RmiSendObjectTest.java b/Dorkbox-Network/test/dorkbox/network/rmi/RmiSendObjectTest.java index 4cbee65a..89880fb0 100644 --- a/Dorkbox-Network/test/dorkbox/network/rmi/RmiSendObjectTest.java +++ b/Dorkbox-Network/test/dorkbox/network/rmi/RmiSendObjectTest.java @@ -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.