本人開發socket通訊多年了,一直乾著“重覆發明輪子”工作,這種工作方式效率低下,容易出錯! 重覆的事情做多了,也會出現“靈光“!何不自己設計一套中間件,在此基礎上,再設計應用層協議。就可以避免“重覆發明輪子”。 ...
.net 平臺下,實現通訊處理有很多方法(見下表),各有利弊:
序號 | 實現方式 | 特點 |
1 | WCF | 優點:封裝好,方便。缺點:難學,不跨平臺 |
2 |
RocketMQ,SuperSocket等中間件 |
優點:輕便 缺點:用戶群體少 |
3 | 直接使用winsocket | 優點:全部在自己掌控之下,協議靈活。缺點:實現時間長,易於出錯。 |
本人開發socket通訊多年了,一直乾著“重覆發明輪子”工作,這種工作方式效率低下,容易出錯!
重覆的事情做多了,也會出現“靈光“!何不自己設計一套中間件,在此基礎上,再設計應用層協議。就可以避免“重覆發明輪子”。
源碼下載地址:http://download.csdn.net/download/qq_29939347/10209713
先看下圖,協議棧:
本文講述的就是綠色部分如何設計。
這層協議設計原則有:
- 要簡單 有兩層意思:一是協議簡單;再者使用起來簡單。並且可以滿足大部分應用場景。
- 可以跨平臺 .net core本身可以跨平臺。 如果對方使用c、c++開發,用其他語言實現該協議也不難。
- 隱藏底層細節 應用層,處理對象都是.net類,而不是位元組流。
- 可以大數據傳輸 無論傳輸多大的數據,不必考慮分包合包處理。
設計思路
總的原則是,傳輸的是.net平臺下的類,而不是位元組流。直接處理.net類要比位元組流要方便,安全很多。
.net平臺下類型很多,我提取了最常用的幾種,達到即簡單,又滿足大部分應用場景的要求。
可以傳輸的類型有:int、string、short、long,byte;
以及對應鏈表類型: List<int>、List<string>、List<short>、List<long>、byte[];
協議總的包體:
public class NetPacket { public int PacketType { get; set; } // 包類型 public int Param1 { get; set; } // 參數1 ,可以根據實際情況使用 public int Param2 { get; set; } // 參數2 ,可以根據實際情況使用
public List<NetValuePair> Items { get; set; } //傳輸的key value 列表
}
NetValuePair 定義如下:
public class NetValuePair { public string Key { get; set; } public NetValueBase Value { get; set; } public NetValuePair() { } }
NetValueBase 包含子類型,分別對應string、int等。以string類型舉例:
public class NetValueBase { public EN_DataType ValueType { get;protected set; } public virtual object GetValue() { return null; } }
public class NetValueString: NetValueBase { public string Value { get; set; } = string.Empty; public override object GetValue() { return Value; } public NetValueString() { ValueType = EN_DataType.en_string; } public NetValueString(string value) { ValueType = EN_DataType.en_string; Value = value; } }
NetValueString值最終要以位元組流方式傳送出去,採用如下方式序列化:
string值採用utf8傳輸,先將字元串轉換成位元組流;分別寫入位元組流的長度,實際的位元組流;
在序列化中,沒用多餘欄位。比.net 自帶的序列化類處理要高效的多,大家可以對比下。
internal static void WriteStringValue(Stream stream, string value) { byte[] bytes = Encoding.UTF8.GetBytes(value); WriteInt32(stream, bytes.Length); stream.Write(bytes, 0, bytes.Length); }
其它類型的序列化,與此類似,不在累述。反序列化如何操作,也不難想像。
傳輸
序列化後的數據要發送出去,需要下一層來處理。
這層的主要功能就是分包和合包。(數據很小的時候就不需要分包了)
public class RawNetPacket { public static int HeadLen = 14; public UInt16 PacketLen; public UInt32 PacketId; //一個完整的包 唯一id public UInt32 TotalNO; //共有多少個包 public UInt32 PacketNO; //包序列號 public byte[] Body; //承載NetPacket序列化的數據,有可能分包發送
}
具體如何分包和合包,可以參考附件源碼。
使用舉例
1 傳送文件
private NetPacket GetPacketByFile(string fileName) { using (FileStream stream = new FileInfo(fileName).OpenRead()) { NetPacket result = new NetPacket(); result.PacketType = 2; result.Param1 = 2; result.Param2 = 3; result.Items = new List<NetValuePair>(); //string NetValuePair pair = new NetValuePair(); pair.Key = "文件名稱"; pair.Value = new NetValueString(fileName); result.Items.Add(pair); //byte pair = new NetValuePair(); pair.Key = "文件二進位數據"; NetValueListByte fileBuffer = new NetValueListByte(); fileBuffer.Value = new byte[stream.Length]; stream.Read(fileBuffer.Value, 0, Convert.ToInt32(stream.Length)); pair.Value = fileBuffer; result.Items.Add(pair); return result; } }
2 傳輸對象
可以將對象序列化為json字元串,再傳送。