登录  /  注册

socket传输protobuf字节流实例教程

零下一度
发布: 2017-06-23 16:15:17
原创
1946人浏览过

版权声明:本文为原创文章,转载请声明 

近期在做一个棋牌项目,需要用到socket传输protobuf字节流,在网上找了一些博客和文章后发现,没有特别全面的,所以把自己研究的全部源码拿出来和大家分享,因为刚开始做,可能会有不足的地方,欢迎拍砖~~

这一篇主要是protocol buffer文件的序列化和解析,废话不多说了,直接上干货

 1 /// <summary> 2 /// 将消息序列化为二进制的方法 3 /// </summary> 4 /// <param>要序列化的对象 5 public static byte[] Serialize(IExtensible model) 6 { 7   try 8   { 9     //创建流对象10     MemoryStream ms = new MemoryStream()11     //使用ProtoBuf自带的序列化工具序列化IExtensible对象12     Serializer.Serialize<iextensible>(ms, model);13     //创建二级制数组,保存序列化后的流14     byte[] bytes = new byte[ms.Length];15     //将流的位置设为016     ms.Position = 0;17     //将流中的内容读取到二进制数组中18     ms.Read(bytes, 0, bytes.Length);19     return bytes;20   }21   catch (Exception e)22   {23     Debug.Log("序列化失败: " + e.ToString());24     return null;25   }26 }</iextensible>
登录后复制

protobuf文件中的每一条message经过protocol buffer提供的ProtoGen工具可以转成c#的中的类,例如

message Test {
    required string test1= 1;
    required string test2= 2;
}
登录后复制

经过转化后就变成了

 1   [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"SedReq")] 2   public partial class Test : global::ProtoBuf.IExtensible 3   { 4     public Test() {} 5      6     private string _test1; 7     [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"test1", DataFormat = global::ProtoBuf.DataFormat.Default)] 8     public string test1 9     {10       get { return _test1; }11       set { _test1 = value; }12     }    
13     private string _test2;14     [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"test2", DataFormat = global::ProtoBuf.DataFormat.Default)]15     public string test216     {17       get { return _test2; }18       set { _test2 = value; }19     }20     private global::ProtoBuf.IExtension extensionObject;21     global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)22       { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }23   }
登录后复制

无视所有带global的代码,你会发现,转化后的c#类和一个标准的c#实体类一模一样,并且,这些转化后的类都继承至ProtoBuf.IExtensible,所以上文中的序列化函数的参数的类型是IExtensible

有了序列化,当然还需要反序列化,也就是讲byte[]反序列化为继承至IExtensible的类型的对象

 1     /// <summary> 2     /// 将收到的消息反序列化成IExtensible对象 3     /// </summary> 4     /// <param>收到的消息的字节流. 5     /// <returns></returns> 6     public static T DeSerialize<t>(byte[] bytes) where T : IExtensible 7     { 8         try 9         {10             MemoryStream ms = new MemoryStream()11             //将消息写入流中12             ms.Write(bytes, 0, bytes.Length);13             //将流的位置归014             ms.Position = 0;15             //反序列化对象16             T result = Serializer.Deserialize<t>(ms);17             return result;18         }19         catch (Exception e)20         {21             Debug.Log("反序列化失败: " + e.ToString());22             return null;23         }24     }</t></t>
登录后复制

因为反序列化后的对象是继承至IExtensible的类的对象,所以返回值必须使用泛型约束来定义,这样才能保证函数的通用性

工具搞定,接下来就是测试代码了

1     public void Test()2     {3         Test test = new Test() { test1 = "123", test2 = "456" };4         byte[] bytes = Serialize(test);5         Test test2 = DeSerialize<test>(bytes);6         Debug.Log(test2.test1 + test2.test2);7     }</test>
登录后复制

输出结果  123456

附上protobuf-net.dll文件

预编译和转化工具

以上就是socket传输protobuf字节流实例教程的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号