Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 dorkbox.network.connection;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.factories.ReflectionSerializerFactory;
import com.esotericsoftware.kryo.factories.SerializerFactory;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import com.esotericsoftware.kryo.util.MapReferenceResolver;
import dorkbox.network.connection.ping.PingMessage;
import dorkbox.network.rmi.CachedMethod;
import dorkbox.network.rmi.InvocationHandlerSerializer;
import dorkbox.network.rmi.InvocationResultSerializer;
import dorkbox.network.rmi.InvokeMethod;
import dorkbox.network.rmi.InvokeMethodResult;
import dorkbox.network.rmi.InvokeMethodSerializer;
import dorkbox.network.rmi.RemoteObjectSerializer;
import dorkbox.network.rmi.RmiRegistration;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.objectPool.ObjectPool;
import dorkbox.objectPool.PoolableObject;
import dorkbox.util.Property;
import dorkbox.util.serialization.ArraysAsListSerializer;
import dorkbox.util.serialization.EccPrivateKeySerializer;
import dorkbox.util.serialization.EccPublicKeySerializer;
import dorkbox.util.serialization.FieldAnnotationAwareSerializer;
import dorkbox.util.serialization.IesParametersSerializer;
import dorkbox.util.serialization.IesWithCipherParametersSerializer;
import dorkbox.util.serialization.IgnoreSerialization;
import dorkbox.util.serialization.UnmodifiableCollectionsSerializer;
import io.netty.buffer.ByteBuf;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.IESParameters;
import org.bouncycastle.crypto.params.IESWithCipherParameters;
import org.slf4j.Logger;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* Threads reading/writing, it messes up a single instance. it is possible to use a single kryo with the use of synchronize, however - that
* defeats the point of multi-threaded
*/
@SuppressWarnings({"unused", "StaticNonFinalField"})
public
class KryoCryptoSerializationManager implements CryptoSerializationManager {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(KryoCryptoSerializationManager.class);
/**
* Specify if we want KRYO to use unsafe memory for serialization, or to use the ASM backend. Unsafe memory use is WAY faster, but is
* limited to the "same endianess" on all endpoints, and unsafe DOES NOT work on android.
*/
@Property
public static boolean useUnsafeMemory = false;
private static final String OBJECT_ID = "objectID";
/**
* The default serialization manager. This is static, since serialization must be consistent within the JVM. This can be changed.
*/
public static KryoCryptoSerializationManager DEFAULT = DEFAULT();
public static
KryoCryptoSerializationManager DEFAULT() {
return DEFAULT(true, true);
}
public static
KryoCryptoSerializationManager DEFAULT(final boolean references, final boolean registrationRequired) {
// ignore fields that have the "@IgnoreSerialization" annotation.
Collection> marks = new ArrayList>();
marks.add(IgnoreSerialization.class);
SerializerFactory disregardingFactory = new FieldAnnotationAwareSerializer.Factory(marks, true);
final KryoCryptoSerializationManager serializationManager = new KryoCryptoSerializationManager(references,
registrationRequired,
disregardingFactory);
serializationManager.register(PingMessage.class);
serializationManager.register(byte[].class);
serializationManager.register(IESParameters.class, new IesParametersSerializer());
serializationManager.register(IESWithCipherParameters.class, new IesWithCipherParametersSerializer());
serializationManager.register(ECPublicKeyParameters.class, new EccPublicKeySerializer());
serializationManager.register(ECPrivateKeyParameters.class, new EccPrivateKeySerializer());
serializationManager.register(dorkbox.network.connection.registration.Registration.class);
// necessary for the transport of exceptions.
serializationManager.register(ArrayList.class, new CollectionSerializer());
serializationManager.register(StackTraceElement.class);
serializationManager.register(StackTraceElement[].class);
// extra serializers
//noinspection ArraysAsListWithZeroOrOneArgument
serializationManager.register(Arrays.asList("")
.getClass(), new ArraysAsListSerializer());
UnmodifiableCollectionsSerializer.registerSerializers(serializationManager);
return serializationManager;
}
private static class ClassSerializer {
final Class> clazz;
final Serializer> serializer;
ClassSerializer(final Class> clazz, final Serializer> serializer) {
this.clazz = clazz;
this.serializer = serializer;
}
}
private static class ClassSerializer2 {
final Class> clazz;
final Serializer> serializer;
final int id;
ClassSerializer2(final Class> clazz, final Serializer> serializer, final int id) {
this.clazz = clazz;
this.serializer = serializer;
this.id = id;
}
}
private static class RemoteClass {
private final Class ifaceClass;
private final Class implClass;
RemoteClass(final Class ifaceClass, final Class implClass) {
this.ifaceClass = ifaceClass;
this.implClass = implClass;
}
}
private boolean initialized = false;
private final ObjectPool kryoPool;
// used by operations performed during kryo initialization, which are by default package access (since it's an anon-inner class
private final List> classesToRegister = new ArrayList>();
private final List classSerializerToRegister = new ArrayList();
private final List classSerializer2ToRegister = new ArrayList();
private final List remoteClassToRegister = new ArrayList();
private boolean shouldInitRMI = false;
private InvokeMethodSerializer methodSerializer = null;
private Serializer