/// <summary> /// 解析请求,并返回解析后的消息包 /// </summary> /// <param name="buffer">数据包</param> /// <param name="remoteEndPoint">远程端点位置</param> /// <returns></returns> public static IPMessager.Entity.Message TryToTranslateMessage(byte[] buffer, IPEndPoint remoteEndPoint) { if (!Test(buffer)) return null; IPMessager.Entity.Message m = new FSLib.IPMessager.Entity.Message(remoteEndPoint); int index = 0; int nextMatch = index; //查找的委托 Func<char, bool> FindIndex = (c) => { int flag = (int)c; while (nextMatch < buffer.Length && buffer[nextMatch] != flag) nextMatch++; return nextMatch <= buffer.Length; }; Func<string> GetSubString = () => { string str = System.Text.Encoding.Default.GetString(buffer, index, nextMatch - index); index = ++nextMatch; return str; }; Func<ulong> GetULong = () => { ulong t1; if (!ulong.TryParse(GetSubString(), out t1)) return 0; else return t1; }; //设置数据包开始-为了兼容飞秋 FindIndex(':'); index = ++nextMatch; //查找包编号 if (!FindIndex(':')) return null; m.PackageNo = GetULong(); //用户名 if (m.PackageNo == 0 || !FindIndex(':')) return null; m.UserName = GetSubString(); //主机名 if (!FindIndex(':')) return null; m.HostName = GetSubString(); //查找命令和选项 if (!FindIndex(':')) return null; ulong temp = ulong.Parse(GetSubString()); m.Command = (Consts.Commands)(temp & 0xFF); m.Options = temp & 0xFFFFFF00; //正文? if (FindIndex('\0')) { m.NormalMsg = GetSubString(); } else { m.NormalMsg = string.Empty; } if (index < buffer.Length) { nextMatch = buffer.Length; m.ExtendMessage = GetSubString().TrimEnd('\0'); } else { m.ExtendMessage = string.Empty; } return m; }
/// <summary> /// 解析请求,并返回解析后的消息包 /// </summary> /// <param name="buffer">数据包</param> /// <param name="remoteEndPoint">远程端点位置</param> /// <returns></returns> public static IPMessager.Entity.Message TryToTranslateMessage(byte[] buffer, IPEndPoint remoteEndPoint) { if (!Test(buffer)) { return(null); } IPMessager.Entity.Message m = new FSLib.IPMessager.Entity.Message(remoteEndPoint); int index = 0; int nextMatch = index; //查找的委托 Func <char, bool> FindIndex = (c) => { int flag = (int)c; while (nextMatch < buffer.Length && buffer[nextMatch] != flag) { nextMatch++; } return(nextMatch <= buffer.Length); }; Func <string> GetSubString = () => { string str = System.Text.Encoding.Default.GetString(buffer, index, nextMatch - index); index = ++nextMatch; return(str); }; Func <ulong> GetULong = () => { ulong t1; if (!ulong.TryParse(GetSubString(), out t1)) { return(0); } else { return(t1); } }; //设置数据包开始-为了兼容飞秋 FindIndex(':'); index = ++nextMatch; //查找包编号 if (!FindIndex(':')) { return(null); } m.PackageNo = GetULong(); //用户名 if (m.PackageNo == 0 || !FindIndex(':')) { return(null); } m.UserName = GetSubString(); //主机名 if (!FindIndex(':')) { return(null); } m.HostName = GetSubString(); //查找命令和选项 if (!FindIndex(':')) { return(null); } ulong temp = ulong.Parse(GetSubString()); m.Command = (Consts.Commands)(temp & 0xFF); m.Options = temp & 0xFFFFFF00; //正文? if (FindIndex('\0')) { m.NormalMsg = GetSubString(); } else { m.NormalMsg = string.Empty; } if (index < buffer.Length) { nextMatch = buffer.Length; m.ExtendMessage = GetSubString().TrimEnd('\0'); } else { m.ExtendMessage = string.Empty; } return(m); }