NetworkComms V3 之自定义对象
能够发送自定义对象,并且在发送的时候对发送的对象进行加密,压缩是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 之自定义对象的更多相关文章
- NetworkComms V3 模拟登陆
演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...
- NetworkComms V3 之支持TCP连接和UDP连接
NetworkComms V3 无缝的支持TCP连接和UDP连接. 您可以很容易的创建这两种连接 //创建一个连接信息对象 ConnectionInfo connInfo = ); //创建一个TCP ...
- NetworkComms V3 使用Json序列化器进行网络通信
刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...
- JavaScript 自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- Sqlite 存储自定义对象
在iOS中如果想保存自定义对象,要让自定义对象实现NSCoding接口并实现方法-(id)initWithCoder:(NSCoder *)coder和-(void)encodeWithCoder:( ...
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
摘要: NSUserDefaults适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等,NSUserDefaults肯定是首选,但是如果我们自定义了一个对象,对象保存的 ...
- js自定义对象
一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在Javascrip ...
- iOS 自定义对象转NSDictionary
我们在向后台Post数据的时候,常常需要把某个对象作为参数,比如在AF的框架中,我们进行Post时,其中的para参数就是需要NSdictionary的 Alamofire.request(.POST ...
- iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档
1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...
随机推荐
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- Python数据
读取文件中数据的最高分数 highest_score=0 result_f=open("results.txt") for line in result_f: (name,scor ...
- Ext学习
一.Ext对原有JavaScript对象的扩展 1.Ext.Array数组 2.Ext.Date日期 3.Ext.Function函数 4.Ext.Number数字 5.Ext.String字符串 二 ...
- Windows Azure Azure 简介
平台介绍 Windows Azure作为一个微软公有云平台,被寄予了厚望. 可以说Windows Azure与Windows RT一样是微软战略转型的重点. 2012年9月微软与中国本土的电信服务提供 ...
- Signalr简单例子
一.需要引用的 Js: 二.编码 用的是signalr2,需要新建Startup.cs类,编码如下: using Microsoft.Owin; using Owin; using System; u ...
- ubuntu 如何 su 到 root(作为 root 用户操作)
ubuntu 安装后,root用户默认被锁定,不允许登录,也不允许"su"到 root.对于桌面用户来说,这样安全性更高一些,但对于服务器可以设置成"允许 su 到roo ...
- linux查看修改线程默认栈空间大小(ulimit -s)
linux查看修改线程默认栈空间大小 ulimit -s 1.通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2.通过命令 ulimit -s 设置大 ...
- 原生JS--COOKIE
原生JS--COOKIE: COOKIE基础及应用:1.什么是COOKIE==>页面用来保存信息,比如:自动登录,记住用户名2.COOKIE的特性: --同一个网站中,所有的页面共享同一套co ...
- 上传预览图片自己做的.md
1.无插件预览(window.URL.createObjectURL) ```javascript //demo 图片预览 单个 $(".demo input#demo_file" ...
- php入门
最近公司招了几个应届毕业生,他们对前端的了解还挺多,但是对后端的技术一无所知,我觉得,作为一个前端攻城狮,如果你有远大的抱负,就应该雨露均沾... 今天我就跟大家讲一讲PHP最基本的入门,至少别人问起 ...