NetworkComms网络通信框架序言

能够发送自定义对象,并且在发送的时候对发送的对象进行加密,压缩是networkComms v3框架的一个重要特性。

具体可以参考源码中 ExampleConsole 工程文件

使用NetworkComms V3 框架发送自定义对象的语法如下:

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

如果您使用的是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;
    }
}

对于一些不支持序列化的类,比如image 类的序列化,要进行一些转换,如下:

[ProtoContract]
public class ImageWrapper
{
    /// <summary>
    /// Private store of the image data as a byte[]
    /// This will be populated automatically when the object is serialised
    /// </summary>
    [ProtoMember()]
    private byte[] _imageData;

    /// <summary>
    /// The image name
    /// </summary>
    [ProtoMember()]
    public string ImageName { get; set; }

    /// <summary>
    /// The public accessor for the image. This will be populated
    /// automatically when the object is deserialised.
    /// </summary>
    public Image Image { get; set; }

    /// <summary>
    /// Private parameterless constructor required for deserialisation
    /// </summary>
    private ImageWrapper() { }

    /// <summary>
    /// Create a new ImageWrapper
    /// </summary>
    /// <param name="imageName"></param>
    /// <param name="image"></param>
    public ImageWrapper(string imageName, Image image)
    {
        this.ImageName = imageName;
        this.Image = image;
    }

    /// <summary>
    /// Before serialising this object convert the image into binary data
    /// </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>
    /// When deserialising the object convert the binary data back into an image object
    /// </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;
    }
}

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

www.networkcomms.cn整理

NetworkComms V3 之自定义对象的更多相关文章

  1. NetworkComms V3 模拟登陆

    演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...

  2. NetworkComms V3 之支持TCP连接和UDP连接

    NetworkComms V3 无缝的支持TCP连接和UDP连接. 您可以很容易的创建这两种连接 //创建一个连接信息对象 ConnectionInfo connInfo = ); //创建一个TCP ...

  3. NetworkComms V3 使用Json序列化器进行网络通信

    刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...

  4. JavaScript 自定义对象

    在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...

  5. Sqlite 存储自定义对象

    在iOS中如果想保存自定义对象,要让自定义对象实现NSCoding接口并实现方法-(id)initWithCoder:(NSCoder *)coder和-(void)encodeWithCoder:( ...

  6. NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象

    摘要: NSUserDefaults适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等,NSUserDefaults肯定是首选,但是如果我们自定义了一个对象,对象保存的 ...

  7. js自定义对象

    一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在Javascrip ...

  8. iOS 自定义对象转NSDictionary

    我们在向后台Post数据的时候,常常需要把某个对象作为参数,比如在AF的框架中,我们进行Post时,其中的para参数就是需要NSdictionary的 Alamofire.request(.POST ...

  9. iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档

    1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...

随机推荐

  1. ReentrantReadWriteLock读写锁详解

    一.读写锁简介 现实中有这样一种场景:对共享资源有读和写的操作,且写操作没有读操作那么频繁.在没有写操作的时候,多个线程同时读一个资源没有任何问题,所以应该允许多个线程同时读取共享资源:但是如果一个线 ...

  2. Python快速建站系列-Part.Two-结构化和布局

    |版权声明:本文为博主原创文章,未经博主允许不得转载. 首先明确我们要建一个什么样的站,作为教程(也算自己使用tornado的一个小总结),自然功能不能太多,但又满足一个普通网站需要的就行了. 目前想 ...

  3. logstash5.x配置

    logstash --help --可以通过此命令看到所有命令 -f, --path.config --配置文件路径 -e, --config.string --可直接运行的配置字符串 -w, --p ...

  4. CentOS7下安装Mysql和Memcached 以及 使用C#操作Mysql和Memcached

    我本身是学.net的,但是现在很多主流SQL和NOSQL都是部置在linux下,本着好学的精神,前段时间装了个虚拟机,在其装上CentOS64位的服务器系统,对于英文0基础,linux0基础的我来说, ...

  5. python核心编程学习记录之函数与函数式编程

    @func function 意思是func(function) @func(a) function 意思是func(a)这是个函数对象,在去调用function函数 如果要传额外的值,只传值用*tu ...

  6. Prince2的七大原则(6)

    Prince2科普_Prince2的七大原则(6) 按照惯例我们先来回顾一下,PRINCE2七大原则分别是指:持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 今天讲第六个 ...

  7. android 内存处理工具

    1. LeakCanary 检查内存泄露 LeakCanary 是一个开源的在debug版本中检测内存泄漏的java库. 让我们来看看一个cait的例子: 1 2 3 4 5 6 7 8 9 10 1 ...

  8. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  9. Linux phpwind论坛的安装

    1:新建文件夹phpwind

  10. [Machine-Learning] 机器学习中的几个度量指标

    Several classification metrics for ML/DM methods. 主要解释下机器学习(或数据挖掘)中的几个度量指标. 1. 关于 "TN/TP/FN/FP&q ...