Class registration verification now includes serializer class (but not it's configuration).
This commit is contained in:
parent
63d3a586ff
commit
4c40fd38f3
|
@ -17,6 +17,9 @@ package dorkbox.network.serialization;
|
|||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import com.esotericsoftware.kryo.Registration;
|
||||
import com.esotericsoftware.kryo.Serializer;
|
||||
|
||||
import dorkbox.network.connection.CryptoConnection;
|
||||
import dorkbox.network.connection.KryoExtra;
|
||||
import dorkbox.network.rmi.RemoteObjectSerializer;
|
||||
|
@ -24,18 +27,25 @@ import dorkbox.network.rmi.RemoteObjectSerializer;
|
|||
class ClassRegistration {
|
||||
Class<?> clazz;
|
||||
int id;
|
||||
Serializer serializer;
|
||||
|
||||
ClassRegistration(final Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
<C extends CryptoConnection> void register(final KryoExtra<C> kryo, final RemoteObjectSerializer remoteObjectSerializer) {
|
||||
Registration registration;
|
||||
|
||||
if (clazz.isInterface()) {
|
||||
id = kryo.register(clazz, remoteObjectSerializer).getId();
|
||||
registration = kryo.register(clazz, remoteObjectSerializer);
|
||||
|
||||
}
|
||||
else {
|
||||
id = kryo.register(clazz).getId();
|
||||
registration = kryo.register(clazz);
|
||||
}
|
||||
|
||||
id = registration.getId();
|
||||
serializer = registration.getSerializer();
|
||||
}
|
||||
|
||||
void log(final Logger logger) {
|
||||
|
|
|
@ -24,8 +24,6 @@ import dorkbox.network.connection.KryoExtra;
|
|||
import dorkbox.network.rmi.RemoteObjectSerializer;
|
||||
|
||||
class ClassSerializer extends ClassRegistration {
|
||||
final Serializer<?> serializer;
|
||||
|
||||
ClassSerializer(final Class<?> clazz, final Serializer<?> serializer) {
|
||||
super(clazz);
|
||||
this.serializer = serializer;
|
||||
|
|
|
@ -22,7 +22,6 @@ import dorkbox.network.connection.KryoExtra;
|
|||
import dorkbox.network.rmi.RemoteObjectSerializer;
|
||||
|
||||
class ClassSerializer1 extends ClassRegistration {
|
||||
|
||||
ClassSerializer1(final Class<?> clazz, final int id) {
|
||||
super(clazz);
|
||||
this.id = id;
|
||||
|
|
|
@ -24,8 +24,6 @@ import dorkbox.network.connection.KryoExtra;
|
|||
import dorkbox.network.rmi.RemoteObjectSerializer;
|
||||
|
||||
class ClassSerializer2 extends ClassRegistration {
|
||||
final Serializer<?> serializer;
|
||||
|
||||
ClassSerializer2(final Class<?> clazz, final Serializer<?> serializer, final int id) {
|
||||
super(clazz);
|
||||
this.serializer = serializer;
|
||||
|
|
|
@ -30,7 +30,8 @@ class ClassSerializerRmi extends ClassRegistration {
|
|||
}
|
||||
|
||||
<C extends CryptoConnection> void register(final KryoExtra<C> kryo, final RemoteObjectSerializer remoteObjectSerializer) {
|
||||
id = kryo.register(clazz, remoteObjectSerializer).getId();
|
||||
this.id = kryo.register(clazz, remoteObjectSerializer).getId();
|
||||
this.serializer = remoteObjectSerializer;
|
||||
}
|
||||
|
||||
void log(final Logger logger) {
|
||||
|
|
|
@ -480,7 +480,7 @@ class Serialization<C extends CryptoConnection> implements CryptoSerializationMa
|
|||
registration.log(logger);
|
||||
|
||||
// now save all of the registration IDs for quick verification/access
|
||||
registrationDetails[i] = new Object[] {registration.id, registration.clazz.getName()};
|
||||
registrationDetails[i] = new Object[] {registration.id, registration.clazz.getName(), registration.serializer.getClass().getName()};
|
||||
|
||||
|
||||
|
||||
|
@ -505,7 +505,11 @@ class Serialization<C extends CryptoConnection> implements CryptoSerializationMa
|
|||
writer.setBuffer(buffer);
|
||||
|
||||
kryo.setRegistrationRequired(false);
|
||||
kryo.writeObject(writer, registrationDetails);
|
||||
try {
|
||||
kryo.writeCompressed(null, buffer, registrationDetails);
|
||||
} catch (Exception e) {
|
||||
logger.error("Unable to write compressed data for registration details");
|
||||
}
|
||||
|
||||
savedRegistrationDetails = new byte[buffer.writerIndex()];
|
||||
buffer.getBytes(0, savedRegistrationDetails);
|
||||
|
@ -523,6 +527,7 @@ class Serialization<C extends CryptoConnection> implements CryptoSerializationMa
|
|||
/**
|
||||
* @return true if kryo registration is required for all classes sent over the wire
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
@Override
|
||||
public
|
||||
boolean verifyKryoRegistration(byte[] otherRegistrationData) {
|
||||
|
@ -562,13 +567,15 @@ class Serialization<C extends CryptoConnection> implements CryptoSerializationMa
|
|||
Object[] classNew = classRegistrations[index];
|
||||
int idNew = (Integer) classNew[0];
|
||||
String nameNew = (String) classNew[1];
|
||||
String serializerNew = (String) classNew[2];
|
||||
|
||||
int idOrg = classOrg.id;
|
||||
String nameOrg = classOrg.clazz.getName();
|
||||
String serializerOrg = classOrg.serializer.getClass().getName();
|
||||
|
||||
if (idNew != idOrg || !nameOrg.equals(nameNew)) {
|
||||
logger.error("Server registration : {} -> {}", idOrg, nameOrg);
|
||||
logger.error("Client registration : {} -> {}", idNew, nameNew);
|
||||
if (idNew != idOrg || !nameOrg.equals(nameNew) || !serializerNew.equalsIgnoreCase(serializerOrg)) {
|
||||
logger.error("Server registration : {} -> {} ({})", idOrg, nameOrg, serializerOrg);
|
||||
logger.error("Client registration : {} -> {} ({})", idNew, nameNew, serializerNew);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -579,8 +586,9 @@ class Serialization<C extends CryptoConnection> implements CryptoSerializationMa
|
|||
Object[] holderClass = classRegistrations[index];
|
||||
int id = (Integer) holderClass[0];
|
||||
String name = (String) holderClass[1];
|
||||
String serializer = (String) holderClass[2];
|
||||
|
||||
logger.error("Missing server registration : {} -> {}", id, name);
|
||||
logger.error("Missing server registration : {} -> {} ({})", id, name, serializer);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user