using System; using System.Text; using System.Text.RegularExpressions; namespace Rocky.Net { /// <summary> /// 数据报文分析器,通过分析接收到的原始数据,得到完整的数据报文. /// 通常的报文识别方法包括:固定长度,长度标记,标记符等方法 /// </summary> internal static class DatagramResolver { public const int MaxDatagramSize = 1024 * 8; public const string EndFlag = "<EOF>"; /// <summary> /// 解析报文 /// </summary> /// <returns>报文数组,原始数据可能包含多个报文</returns> public static string[] Resolve(StringBuilder sb) { if (sb.Length == 0) { return new string[0]; } string buffer = sb.ToString(); sb.Length = 0; string[] resolved = Regex.Split(buffer, EndFlag); int size = resolved.Length - 1; if (resolved[size].Length < MaxDatagramSize) { sb.Append(resolved[size]); } Array.Resize<string>(ref resolved, size); return resolved; } } }