Skip to content

5.SerialisedNetwork

ReferenceType edited this page Jul 15, 2024 · 3 revisions

Serialized Network

Set of generic classes that define client server models for sending and receiving serialized messages instead of raw bytes.

  • Advantage of using this instead of serializing and reserializing manually from bytes is the extra performance.
  • Serialization and deserialization avoids extra copy.
  • All features of AsyncTcpClient/Server and SSLClient/Server applies.

How it works

All serialized network types share same base generic class.

 public class PureProtoServer : GenericServer<ProtoSerializer>
 {
     public PureProtoServer(int port, bool writeLenghtPrefix = true) : base(port, writeLenghtPrefix)
     {
     }
 }

 public class PureProtoClient : GenericClient<ProtoSerializer>
 {
 }

Generic models serialises according to provided serialiser.

Implementation classes exist on their assemblies. They are kept as separate assemblies to avoid unnecessary dependencies. You can implement your own serializer too, provided that it implements ISerializer interface.

build-in models consist of :

  • JsonServer/Client
  • NetSerialiserServer/Client
  • MessagePackServer/Client
  • PureProtoServer/Client

The API is identical for all types.

Secure Variants

Secure variants have the same API as the regular ones, except for the certificates. Which follows the same analogy of SSL Client/Server for Initialisation and CertificateValidationCallback.

public class PureSecureProtoServer : GenericSecureServer<ProtoSerializer>
{
    public PureSecureProtoServer(int port, X509Certificate2 certificate, bool writeLenghtPrefix = true)
           : base(port, certificate, writeLenghtPrefix) {}
}

public class PureSecureProtoClient : GenericSecureClient<ProtoSerializer>
 {
     public PureSecureProtoClient(X509Certificate2 certificate, bool writeLenghtPrefix = true) 
            : base(certificate, writeLenghtPrefix) {}
 }

build-in models consist of:

  • SecureJsonServer/Client
  • SecureNetSerialiserServer/Client
  • SecureMessagePackServer/Client
  • PureSecureProtoServer/Client

Example

Sinse all models have the same API, for the sake of simplicity, Only the Protobuf example is provided. The API is identical for all types.

 [ProtoContract]
 class SampleMessage : IProtoMessage
 {
     [ProtoMember(1)]
     public string sample;
 }
 private static void ExamplePureProto()
 {
     PureProtoServer server = new PureProtoServer(11234);
     server.StartServer();

     server.BytesReceived += (clientId,bytes, offset, count) => 
     {
         SampleMessage msg = server.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
         Console.WriteLine(msg.sample);
         msg.sample = "Jesse Lets cook";
         server.SendAsync(clientId,msg);
     };

     PureProtoClient client = new PureProtoClient();
     client.Connect("127.0.0.1", 11234);

     client.BytesReceived += (bytes, offset, count) =>
     {
         SampleMessage msg = client.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
         Console.WriteLine(msg.sample);
     };

     client.SendAsync(new SampleMessage() { sample = "Yo! Mr White" });
     Console.ReadLine();
 }