Network Configuration
NetworkConfig
class allows to define basic network configuration like encoding or serializer.
Server
or Client
instance can be created with custom configuration by passing NetworkConfig
instance to their constructor.
To change default network configuration:
NetworkConfig.Default = new NetworkConfig(...);
Encoding
By default all text messages are encoded using UTF8
.
Serializer
By default Json.NET is used for serialization, with following implementation:
public class NewtonsoftJsonSerializer : INetworkSerializer
{
private readonly JsonSerializer _serializer;
public NewtonsoftJsonSerializer()
{
var settings = new JsonSerializerSettings
{
// Required for internal dynamic type handling
TypeNameHandling = TypeNameHandling.Auto
};
_serializer = JsonSerializer.CreateDefault(settings);
}
public void Serialize(Stream stream, object value)
{
using var writer = new StreamWriter(stream);
using var jsonWriter = new JsonTextWriter(writer);
_serializer.Serialize(jsonWriter, value);
}
public object Deserialize(Stream stream, Type type)
{
using var reader = new StreamReader(stream);
using var jsonReader = new JsonTextReader(reader);
var value = _serializer.Deserialize(jsonReader, type);
return value;
}
}