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. Ubuntu 12.04安装vim和配置

    问题: ubuntu默认没有安装vim,出现: jyg@ubuntu:~$ vim test.cThe program 'vim' can be found in the following pack ...

  2. ajax 代码

    function ajax(){ var aj=null; if(window.ActiveXObject){ aj = new ActiveXObject("Microsoft.XMLHT ...

  3. (转) Eclipse连接MySQL数据库(傻瓜篇)

    Eclipse连接MySQL数据库(傻瓜篇) 原帖地址: http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html Posted on 2 ...

  4. 如何从github上面拷贝源码

    有好奇心的朋友们一定都想看一看很多开源项目的源码,那么github就不用说啦,太多的开源项目都把源码放到上面. 博主最近为了学习angularjs也不得不去github上面弄源码,下面将会介绍如何做: ...

  5. Unity在编辑器状态下清空控制台信息

    public static void ClearConsole() { var assembly = System.Reflection.Assembly.GetAssembly(typeof(Uni ...

  6. 如何使用R语言解决可恶的脏数据

    转自:http://shujuren.org/article/45.html 在数据分析过程中最头疼的应该是如何应付脏数据,脏数据的存在将会对后期的建模.挖掘等工作造成严重的错误,所以必须谨慎的处理那 ...

  7. FIFO页面置换算法

    本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4:为例: #include <stdio.h> #define Init ...

  8. How To Use DBLink In Oracle Forms 6i

    You want to connect multiple databases in oracle forms to perform certain tasks, for example you nee ...

  9. 运行时c函数

    // 修改isa,本质就是改变当前对象的类名    object_setClass(self, [XMGKVONotifying_Person class]); // self动态添加关联    // ...

  10. lua table remove元素的问题

    当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...