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. windows系统调用 互斥体mutex

    #include "iostream" #include "windows.h" using namespace std; class CCountUpDown ...

  2. 在Repeater中嵌套使用Repeater

    在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品.最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加 ...

  3. javascript的语法结构

    字符规范: javascript程序是采用的Unicode字符集编写的,并且区分大小写.但是html代码不区分大小写,比如,在html中点击事件就可以写成onClick或则onclick,但是在jav ...

  4. oracle触发器及异常处理 简单例子

    create sequence person_seq start with 1 increment by 1 order                     --按顺序 nocycle       ...

  5. 【转】PowerShell入门(一):PowerShell能干什么?

    转至:http://www.cnblogs.com/ceachy/archive/2013/01/30/WhatCanPowerShellDo.html PowerShell能干什么呢?就像序言中提到 ...

  6. 自定义view 画圆

    package com.exaple.day01rikao; import android.content.Context; import android.graphics.Canvas; impor ...

  7. 机器学习实战-边学边读python代码(3)

    程序清单2-3 归一化特征值: def autoNorm(dataSet): /* >>> barray([[ 1., 2., 3.], [ 2., 3., 4.], [ 10., ...

  8. vm虚拟机启动失败 Global\vmx86

    workstation12 PRO 启动虚拟机异常报错:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件 解决方法,启动windows系统服务:

  9. 腾讯数据总监:运营人员必须掌握的APP基础数据分析体系(没有比这篇更系统全面的)

    导读:在互联网企业,任何一个APP都要事先规划好数据体系,才允许上线运营,有了数据才可以更好的科学运营.因此本文将为大家介绍APP的基础数据指标体系.主要分为五个维度,包括用户规模与质量.参与度分析. ...

  10. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...