NetworkComms网络通信框架序言

英文原文:http://www.networkcomms.net/custom-objects/

NetworkComms.Net网络库,支持发送自定义类,并可以在序列化时进行压缩和加密。序列化我们通常使用开源的Protobuf.net类库,也可以使用别的序列化方式。

简单的回顾一下,发送自定义类【契约类】的语法如下:

、首先定义契约类,(以使用protobuf.net序列化器为例)

  [ProtoContract]
  private class CustomObject
  {
      [ProtoMember()]
      public int Age { get; private set; }

      [ProtoMember()]
      public string Name { get; private set; }

     /// <summary>
     /// Parameterless constructor required for protobuf
     /// </summary>
     protected CustomObject() { }

     public CustomObject(string name, int age)
     {
         this.Name = name;
         this.Age = age;
     }
 }
 、实例化契约类,并用某个Tcp连接发送

  CustomObject myCustomObject = );
  tcpConn.SendObject(, myCustomObject);

契约类中,可以包含值类型、集合类型等原始数据类型。

某些类型没有设计为可以使用protobuf-net进行序列化,就需要做更多的工作。

下面以 Image类为例:

  [ProtoContract]
  public class ImageWrapper
  {
      /// <summary>
      /// 把Image对象存储为私有的字节数组
      /// </summary>
      [ProtoMember()]
      private byte[] _imageData;

     /// <summary>
     /// 图片名称
     /// </summary>
     [ProtoMember()]
     public string ImageName { get; set; }

     /// <summary>
      /// 图片对象
     /// </summary>
     public Image Image { get; set; }

     /// <summary>
     /// 私有的无参数构造函数 反序列化时需要使用
     /// </summary>
     private ImageWrapper() { }

     /// <summary>
     /// 创建一个新的 ImageWrapper类
     /// </summary>
     /// <param name="imageName"></param>
     /// <param name="image"></param>
     public ImageWrapper(string imageName, Image image)
     {
         this.ImageName = imageName;
         this.Image = image;
     }

     /// <summary>
     ///序列化之前,把图片转化为二进制数据
     /// </summary>
     [ProtoBeforeSerialization]
     private void Serialize()
     {
         if (Image != null)
         {
             //We need to decide how to convert our image to its raw binary form here
             using (MemoryStream inputStream = new MemoryStream())
             {
                 //For basic image types the features are part of the .net framework
                 Image.Save(inputStream, Image.RawFormat);

                 //If we wanted to include additional data processing here
                 //such as compression, encryption etc we can still use the features provided by NetworkComms.Net
                 //e.g. see DPSManager.GetDataProcessor<LZMACompressor>()

                 //Store the binary image data as bytes[]
                 _imageData = inputStream.ToArray();
             }
         }
     }

     /// <summary>
     /// 反序列化时,把二进制数据转化为图片对象
     /// </summary>
     [ProtoAfterDeserialization]
     private void Deserialize()
     {
         MemoryStream ms = new MemoryStream(_imageData);

         //If we added custom data processes we have the perform the reverse operations here before
         //trying to recreate the image object
         //e.g. DPSManager.GetDataProcessor<LZMACompressor>()

         Image = Image.FromStream(ms);
         _imageData = null;
     }
 }
 www.networkcomms.cn编辑
www.cnblogs.com/networkcomms
04-28 01:43