From 20d4a25232edfeb6398e39523a64e2617e495407 Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 11 Aug 2020 16:15:49 +0200 Subject: [PATCH] Serialization now initializes using the Serialization defaults class --- .../network/serialization/Serialization.kt | 81 ++----------------- 1 file changed, 7 insertions(+), 74 deletions(-) diff --git a/src/dorkbox/network/serialization/Serialization.kt b/src/dorkbox/network/serialization/Serialization.kt index 00f7e9b3..e023aa33 100644 --- a/src/dorkbox/network/serialization/Serialization.kt +++ b/src/dorkbox/network/serialization/Serialization.kt @@ -15,12 +15,14 @@ */ package dorkbox.network.serialization -import com.esotericsoftware.kryo.* +import com.esotericsoftware.kryo.ClassResolver +import com.esotericsoftware.kryo.Kryo +import com.esotericsoftware.kryo.Serializer +import com.esotericsoftware.kryo.SerializerFactory import com.esotericsoftware.kryo.io.Input import com.esotericsoftware.kryo.io.Output import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy import com.esotericsoftware.kryo.util.IdentityMap -import de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer import dorkbox.network.connection.KryoExtra import dorkbox.network.connection.ping.PingMessage import dorkbox.network.rmi.CachedMethod @@ -28,7 +30,8 @@ import dorkbox.network.rmi.RmiUtils import dorkbox.network.rmi.messages.* import dorkbox.objectPool.ObjectPool import dorkbox.objectPool.PoolableObject -import dorkbox.util.OS +import dorkbox.os.OS +import dorkbox.util.serialization.SerializationDefaults import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled import org.agrona.collections.Int2ObjectHashMap @@ -38,7 +41,6 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import java.io.IOException import java.lang.reflect.InvocationHandler -import java.util.* /** * Threads reading/writing at the same time a single instance of kryo. it is possible to use a single kryo with the use of @@ -66,26 +68,6 @@ class Serialization(references: Boolean, companion object { const val CLASS_REGISTRATION_VALIDATION_FRAGMENT_SIZE = 400 - private val UNMODIFIABLE_COLLECTION_SERIALIZERS: Array, Serializer>> - - init { - val unmodSerializers = mutableListOf, Serializer>>() - - // hacky way to register unmodifiable serializers. This MUST be done here, because we ONLY want internal objects created once - @Suppress("UNCHECKED_CAST") - val kryo: Kryo = object : Kryo() { - override fun register(type: Class<*>, serializer: Serializer<*>): Registration { - val type1 = type as Class - val serializer1 = serializer as Serializer - unmodSerializers.add(Pair(type1, serializer1)) - return super.register(type, serializer) - } - } - UnmodifiableCollectionsSerializer.registerSerializers(kryo) - - UNMODIFIABLE_COLLECTION_SERIALIZERS = unmodSerializers.toTypedArray() - // end hack - } /** * Additionally, this serialization manager will register the entire class+interface hierarchy for an object. If you want to specify a @@ -165,55 +147,7 @@ class Serialization(references: Boolean, kryo.references = references // All registration MUST happen in-order of when the register(*) method was called, otherwise there are problems. - - // these are registered using the default serializers. We don't customize these, because we don't care about it. - kryo.register(String::class.java) - kryo.register(Array::class.java) - - kryo.register(IntArray::class.java) - kryo.register(ShortArray::class.java) - kryo.register(FloatArray::class.java) - kryo.register(DoubleArray::class.java) - kryo.register(LongArray::class.java) - kryo.register(ByteArray::class.java) - kryo.register(CharArray::class.java) - kryo.register(BooleanArray::class.java) - - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - - - kryo.register(Array::class.java) - kryo.register(Array>::class.java) - kryo.register(Class::class.java) - - // necessary for the transport of exceptions. - kryo.register(StackTraceElement::class.java) - kryo.register(Array::class.java) - - kryo.register(arrayListOf().javaClass) - kryo.register(hashMapOf().javaClass) - kryo.register(hashSetOf().javaClass) - - kryo.register(emptyList().javaClass) - kryo.register(emptySet().javaClass) - kryo.register(emptyMap().javaClass) - - kryo.register(Collections.EMPTY_LIST::class.java) - kryo.register(Collections.EMPTY_SET::class.java) - kryo.register(Collections.EMPTY_MAP::class.java) - kryo.register(Collections.emptyNavigableSet().javaClass) - kryo.register(Collections.emptyNavigableMap().javaClass) - - UNMODIFIABLE_COLLECTION_SERIALIZERS.forEach { - kryo.register(it.first, it.second) - } + SerializationDefaults.register(kryo) // RMI stuff! kryo.register(GlobalObjectCreateRequest::class.java) @@ -228,7 +162,6 @@ class Serialization(references: Boolean, @Suppress("UNCHECKED_CAST") kryo.register(InvocationHandler::class.java as Class, objectRequestSerializer) - // check to see which interfaces are mapped to RMI (otherwise, the interface requires a serializer) classesToRegister.forEach { registration -> registration.register(kryo)