自定義協議封裝包頭、包體


底層通信消息類,定義消息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 }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM