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.

publicclassPureProtoServer:GenericServer<ProtoSerializer>{publicPureProtoServer(intport,boolwriteLenghtPrefix=true):base(port,writeLenghtPrefix){}}publicclassPureProtoClient: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.

publicclassPureSecureProtoServer:GenericSecureServer<ProtoSerializer>{publicPureSecureProtoServer(intport,X509Certificate2certificate,boolwriteLenghtPrefix=true):base(port,certificate,writeLenghtPrefix){}}publicclassPureSecureProtoClient:GenericSecureClient<ProtoSerializer>{publicPureSecureProtoClient(X509Certificate2certificate,boolwriteLenghtPrefix=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]classSampleMessage:IProtoMessage{[ProtoMember(1)]publicstringsample;}privatestaticvoidExamplePureProto(){PureProtoServerserver=newPureProtoServer(11234);server.StartServer();server.BytesReceived+=(clientId,bytes,offset,count)=>{SampleMessagemsg=server.Serializer.Deserialize<SampleMessage>(bytes,offset,count);Console.WriteLine(msg.sample);msg.sample="Jesse Lets cook";server.SendAsync(clientId,msg);};PureProtoClientclient=newPureProtoClient();client.Connect("127.0.0.1",11234);client.BytesReceived+=(bytes,offset,count)=>{SampleMessagemsg=client.Serializer.Deserialize<SampleMessage>(bytes,offset,count);Console.WriteLine(msg.sample);};client.SendAsync(newSampleMessage(){sample="Yo! Mr White"});Console.ReadLine();}

Clone this wiki locally