首页 > 代码库 > 自定义协议封装包头、包体

自定义协议封装包头、包体

底层通信消息类,定义消息ID、消息体,和初始化

 1 using System;  
 2   
 3 /// <summary>  
 4 /// 底层通信消息  
 5 /// </summary>  
 6 public class TSocketMessage : IDisposable  
 7 {  
 8     /// <summary>  
 9     /// 消息ID  
10     /// </summary>  
11     public int MsgID;  
12     /// <summary>  
13     /// 消息内容  
14     /// </summary>  
15     public byte[] MsgBuffer;  
16   
17     public TSocketMessage(int msgID, byte[] msg)  
18     {  
19         this.MsgID = msgID;  
20         this.MsgBuffer = msg;  
21     }  
22   
23     public void Dispose()  
24     {  
25         this.Dispose(true);  
26         GC.SuppressFinalize(this);  
27     }  
28   
29     protected virtual void Dispose(bool flag1)  
30     {  
31         if (flag1) { this.MsgBuffer = null; }  
32     }  
33 }  

消息解析器,封装包头、包体、解析包

  1 using System;  
  2 using System.Collections.Generic;  
  3 using System.IO;  
  4 using System.Text;  
  5   
  6 // 消息解析器  
  7 public class MarshalEndian  
  8 {  
  9     //用于存储剩余未解析的字节数  
 10     private List<byte> _LBuff = new List<byte>(2);  
 11     //默认是utf8的编码格式  
 12     private UTF8Encoding utf8 = new UTF8Encoding();  
 13   
 14     //包头1  
 15     const Int16 t1 = 0x55;  
 16     //包头2  
 17     const Int16 t2 = 0xAA;  
 18     //字节数常量 两个包头4个字节,一个消息id4个字节,封装消息长度 int32 4个字节  
 19     const Int32 ConstLenght = 8;  
 20   
 21     public void Dispose()  
 22     {  
 23         this.Dispose(true);  
 24         GC.SuppressFinalize(this);  
 25     }  
 26   
 27     protected virtual void Dispose(bool flag1)  
 28     {  
 29         if (flag1)  
 30         {  
 31             IDisposable disposable2 = this.utf8 as IDisposable;  
 32             if (disposable2 != null) { disposable2.Dispose(); }  
 33             IDisposable disposable = this._LBuff as IDisposable;  
 34             if (disposable != null) { disposable.Dispose(); }  
 35         }  
 36     }  
 37   
 38     public byte[] Encode(TSocketMessage msg)  
 39     {  
 40         MemoryStream ms = new MemoryStream();  
 41         BinaryWriter bw = new BinaryWriter(ms, new UTF8Encoding());  
 42         byte[] msgBuffer = msg.MsgBuffer;  
 43  
 44         #region 封装包头  
 45         bw.Write((Int16)t1);  
 46         bw.Write((Int16)t2);  
 47         #endregion  
 48  
 49         #region 包协议  
 50         if (msgBuffer != null)  
 51         {  
 52             bw.Write((Int32)(msgBuffer.Length + 4));  
 53             bw.Write(msg.MsgID);  
 54             bw.Write(msgBuffer);  
 55         }  
 56         else { bw.Write((Int32)0); }  
 57         #endregion  
 58   
 59         bw.Close();  
 60         ms.Close();  
 61         bw.Dispose();  
 62         ms.Dispose();  
 63         return ms.ToArray();  
 64     }  
 65   
 66     public List<TSocketMessage> GetDcAppMess(byte[] buff, int len)  
 67     {  
 68         //拷贝本次的有效字节  
 69         byte[] _b = new byte[len];  
 70         Array.Copy(buff, 0, _b, 0, _b.Length);  
 71         buff = _b;  
 72         if (this._LBuff.Count > 0)  
 73         {  
 74             //拷贝之前遗留的字节  
 75             this._LBuff.AddRange(_b);  
 76             buff = this._LBuff.ToArray();  
 77             this._LBuff.Clear();  
 78             this._LBuff = new List<byte>(2);  
 79         }  
 80   
 81         List<TSocketMessage> list = new List<TSocketMessage>();  
 82         MemoryStream ms = new MemoryStream(buff);  
 83         BinaryReader buffers = new BinaryReader(ms, this.utf8);  
 84         try  
 85         {  
 86             byte[] _buff;  
 87             Label_00983:  
 88  
 89             #region 包头读取  
 90             //循环读取包头             
 91             //判断本次解析的字节是否满足常量字节数   
 92             if ((buffers.BaseStream.Length - buffers.BaseStream.Position) < ConstLenght)  
 93             {  
 94                 _buff = buffers.ReadBytes((int)(buffers.BaseStream.Length - buffers.BaseStream.Position));  
 95                 this._LBuff.AddRange(_buff);  
 96                 return list;  
 97             }  
 98             Int16 tt1 = buffers.ReadInt16();  
 99             Int16 tt2 = buffers.ReadInt16();  
100             if (!(tt1 == t1 && tt2 == t2))  
101             {  
102                 long ttttt = buffers.BaseStream.Seek(-3, SeekOrigin.Current);  
103                 goto Label_00983;  
104             }  
105             #endregion  
106  
107             #region 包协议  
108             int offset = buffers.ReadInt32();  
109             #endregion  
110  
111             #region 包解析  
112             //剩余字节数大于本次需要读取的字节数  
113             if (offset <= (buffers.BaseStream.Length - buffers.BaseStream.Position))  
114             {  
115                 int msgID = buffers.ReadInt32();  
116                 _buff = buffers.ReadBytes(offset - 4);  
117                 list.Add(new TSocketMessage(msgID, _buff));  
118                 if ((buffers.BaseStream.Length - buffers.BaseStream.Position) > 0)  
119                 {  
120                     goto Label_00983;  
121                 }  
122             }  
123             else  
124             {  
125                 //剩余字节数刚好小于本次读取的字节数 存起来,等待接受剩余字节数一起解析  
126                 _buff = buffers.ReadBytes((int)(buffers.BaseStream.Length - buffers.BaseStream.Position + ConstLenght));  
127                 this._LBuff.AddRange(_buff);  
128             }  
129             #endregion  
130         }  
131         catch (Exception ex) { Console.WriteLine(ex); }  
132         finally  
133         {  
134             if (buffers != null) { buffers.Dispose(); }  
135             buffers.Close();  
136             if (buffers != null) { buffers.Dispose(); }  
137             ms.Close();  
138             if (ms != null) { ms.Dispose(); }  
139         }  
140         return list;  
141     }  
142 }  

 

自定义协议封装包头、包体