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