-
Notifications
You must be signed in to change notification settings - Fork 0
Serialization
Serialization is added like encryption to an Client and is even across all Connections. Serialization is the process of turning an Object into an String, that can be send over the Network and DeSerialization is the opposite, receiving an String and creating an Object based upon an String, received over the Network.
Like the encryption, you should use the same Serialization at the ClientStart and at the ServerStart, so that the ClientStart can understand what the Server sends and vise versa.
The most common way to provide an Serialization or DeSerializationAdapter is to add it inside an ClientConnectedHandler like this:
ServerStart serverStart = ...
SerializationAdapter serialize = ...
DeSerializationAdapter deSerialize = ...
serverStart.addClientConnectedHandler(client -> {
ObjectHandler handler = client.objectHandler();
handler.setMainSerialization(serialize);
handler.setMainDeSerialization(deSerialize);
});Since you might use more than one Serialization for Objects, the serialization is divide into 2 separate spaces.
First of there is the Main(De)SerializationAdapter. This should be the statistically most used (De)Serialization. The Main(De)SerializationAdapter is asked first to serialize the Object, that should be send over the network or was received from the network.
If an SerializationFailedException is thrown inside of the Adapter, the Connections is trying to (De)Serialize the Object/String using the so called Fallback-Stack. You can add an Serialization by stating:
ServerStart serverStart = ...
SerializationAdapter serialize = ...
DeSerializationAdapter deSerialize = ...
serverStart.addClientConnectedHandler(client -> {
client.addFallBackSerialization(serialize);
client.addFallBackDeSerialization(deSerialize);
});It will keep asking the next Adapter until no SerializationFailedException is thrown. If it cannot find any suited Adapter, it will print the whole stacktrace, of all so catched SerializationFailedException and abort the communication.
By default, NetCom2 uses the java serialization, consisting of the Serializable interface. If you do not need anything special, you simply can implement Serializable at all Objects, that should be send over the Network. This is done, because i did not want to create dependencies to other frameworks. If you want, you could use (for example) Gson like this:
Gson gson = new Gson();
serverStart serverStart = ...
SerializationAdapter serialize = object -> object.getName() + "|" + gson.toJson(object);
DeSerializationAdapter deSerialize = string -> {
String[] stringArray = string.split("\\|");
try {
Class<?> clazz = Class.fromName(stringArray[0]);
} catch(Exception e) {
throw new SerializationFailedException(e);
}
String json = stringArray[1];
gson.fromJson(json, clazz);
};
serverStart.addClientConnectedHandler(client -> {
ObjectHandler handler = client.objectHandler();
handler.setMainSerialization(serialize);
handler.setMainDeSerialization(deSerialize);
handler.addFallbackSerializationAdapter(new JavaSerializationAdapter());
handler.addFallbackDeSerializationAdapter(new JavaDeserializationAdapter());
});Note that i added fallback adapter, based upon the defaultJavaSerialization, so that the default NetCom2 Message-Objects still can be send over the Network.
Introduction
Advanced
Outdated
Further Reading
Versions explained
Examples