//AnsEntry void ProcessCommand_AnsEntry(Message m, Host h) { if (h == null && (Config.IgnoreNoAddListFlag || !Consts.Check(m.Options, Consts.Cmd_Send_Option.NoAddList))) { h = new Host() { ClientVersion = string.Empty, GroupName = m.ExtendMessage, HasShare = false, HostSub = new HostSub() { HostName = m.HostName, Ipv4Address = m.HostAddr, UserName = m.UserName }, Index = 0, IsEnhancedContractEnabled = Consts.Check(m.Options, Consts.Cmd_All_Option.EnableNewDataContract), NickName = m.NormalMsg }; if (Consts.Check(m.Options, Consts.Cmd_All_Option.Absence)) { h.ChangeAbsenceMode(true, Resources.CommandExecutor_ProcessCommand_Br_Entry_LeaveModeText); } LivedHost.Add(m.HostAddr.Address.ToString(), h); h.AbsenceModeChanged += Host_AbsendModeChanged; //立刻查询离开消息 if (h.IsInAbsenceMode) { Host_AbsendModeChanged(h, null); } m.Host = h; } h.SupportEncrypt = Consts.Check(m.Options, Consts.Cmd_All_Option.Encrypt); h.SupportFileTransport = Consts.Check(m.Options, Consts.Cmd_All_Option.FileAttach); }
/// <summary> /// 分析网络数据包并进行转换为信息对象 /// </summary> /// <param name="packs">接收到的封包对象</param> /// <returns></returns> /// <remarks> /// 对于分包消息,如果收到的只是片段并且尚未接收完全,则不会进行解析 /// </remarks> public static IPMessager.Entity.Message ParseToMessage(params Entity.PackedNetworkMessage[] packs) { if (packs.Length == 0 || (packs[0].PackageCount > 1 && packs.Length != packs[0].PackageCount)) { return(null); } //尝试解压缩,先排序 Array.Sort(packs); //尝试解压缩 System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress); try { Array.ForEach(packs, s => zip.Write(s.Data, 0, s.Data.Length)); } catch (Exception) { OnDecompressFailed(new DecomprssFailedEventArgs(packs)); return(null); } zip.Close(); ms.Flush(); ms.Seek(0, System.IO.SeekOrigin.Begin); //构造读取流 System.IO.BinaryReader br = new System.IO.BinaryReader(ms, System.Text.Encoding.Unicode); //开始读出数据 IPMessager.Entity.Message m = new FSLib.IPMessager.Entity.Message(packs[0].RemoteIP); m.PackageNo = br.ReadUInt64(); //包编号 ulong tl = br.ReadUInt64(); m.Command = (Define.Consts.Commands)(tl & 0xFF); //命令编码 m.Options = tl & 0xFFFFFF00; //命令参数 m.UserName = br.ReadString(); //用户名 m.HostName = br.ReadString(); //主机名 int length = br.ReadInt32(); m.NormalMsgBytes = new byte[length]; br.Read(m.NormalMsgBytes, 0, length); length = br.ReadInt32(); m.ExtendMessageBytes = new byte[length]; br.Read(m.ExtendMessageBytes, 0, length); if (!Consts.Check(m.Options, Consts.Cmd_All_Option.BinaryMessage)) { m.NormalMsg = System.Text.Encoding.Unicode.GetString(m.NormalMsgBytes, 0, length); //正文 m.ExtendMessage = System.Text.Encoding.Unicode.GetString(m.ExtendMessageBytes, 0, length); //扩展消息 } return(m); }
/// <summary> /// 分析网络数据包并进行转换为信息对象 /// </summary> /// <param name="packs">接收到的封包对象</param> /// <returns></returns> /// <remarks> /// 对于分包消息,如果收到的只是片段并且尚未接收完全,则不会进行解析 /// </remarks> public static IPMessager.Entity.Message ParseToMessage(params Entity.PackedNetworkMessage[] packs) { if (packs.Length == 0 || (packs[0].PackageCount > 1 && packs.Length != packs[0].PackageCount)) { return(null); } var ms = DecompressMessagePacks(packs); if (ms == null) { OnDecompressFailed(new DecomprssFailedEventArgs(packs)); return(null); } //构造读取流 var br = new System.IO.BinaryReader(ms, System.Text.Encoding.Unicode); //开始读出数据 var m = new FSLib.IPMessager.Entity.Message(packs[0].RemoteIP); m.PackageNo = br.ReadUInt64(); //包编号 var tl = br.ReadUInt64(); m.Command = (Define.Consts.Commands)(tl & 0xFF); //命令编码 m.Options = tl & 0xFFFFFF00; //命令参数 m.UserName = br.ReadString(); //用户名 m.HostName = br.ReadString(); //主机名 var length = br.ReadInt32(); m.NormalMsgBytes = new byte[length]; br.Read(m.NormalMsgBytes, 0, length); length = br.ReadInt32(); m.ExtendMessageBytes = new byte[length]; br.Read(m.ExtendMessageBytes, 0, length); if (!Consts.Check(m.Options, Consts.Cmd_All_Option.BinaryMessage)) { m.NormalMsg = System.Text.Encoding.Unicode.GetString(m.NormalMsgBytes, 0, length); //正文 m.ExtendMessage = System.Text.Encoding.Unicode.GetString(m.ExtendMessageBytes, 0, length); //扩展消息 } return(m); }
//离开状态或昵称更改 private void ProcessCommand_BrAbsence(Message message, Host host) { if (host == null) { return; } host.ChangeAbsenceMode(Consts.Check(message.Options, Consts.Cmd_All_Option.Absence), ""); if (!string.IsNullOrEmpty(message.NormalMsg)) { host.NickName = message.NormalMsg; } if (!string.IsNullOrEmpty(message.ExtendMessage)) { host.GroupName = message.ExtendMessage; } host.HostFeature = message.Options; }
//Br_Entry void ProcessCommand_Br_Entry(Message m, Host h) { if (h == null && (Config.IgnoreNoAddListFlag || !Consts.Check(m.Options, Consts.Cmd_Send_Option.NoAddList))) { h = new Host() { ClientVersion = string.Empty, GroupName = m.ExtendMessage, HasShare = false, HostSub = new HostSub() { HostName = m.HostName, Ipv4Address = m.HostAddr, UserName = m.UserName }, Index = 0, IsEnhancedContractEnabled = Consts.Check(m.Options, Consts.Cmd_All_Option.EnableNewDataContract), NickName = m.NormalMsg, HostFeature = m.Options }; if (Consts.Check(m.Options, Consts.Cmd_All_Option.Absence)) { h.ChangeAbsenceMode(true, Resources.CommandExecutor_ProcessCommand_Br_Entry_LeaveModeText); } h.SupportEncrypt = Consts.Check(m.Options, Consts.Cmd_All_Option.Encrypt); h.SupportFileTransport = Consts.Check(m.Options, Consts.Cmd_All_Option.FileAttach); LivedHost.Add(m.HostAddr.Address.ToString(), h); h.AbsenceModeChanged += Host_AbsendModeChanged; //立刻查询离开消息 if (h.IsInAbsenceMode) { Host_AbsendModeChanged(h, null); } m.Host = h; } ulong opt = 0ul; if (Config.IsInAbsenceMode) { opt |= (ulong)Consts.Cmd_All_Option.Absence; } if (Config.EnableFileTransfer) { opt |= (ulong)Consts.Cmd_All_Option.FileAttach; } //插件 if (Config.Services != null) { Config.Services.ProviderExecute(s => opt |= s.GenerateClientFeatures()); } //回复证明自己在线 MessageProxy.SendWithNoCheck(h, Consts.Commands.AnsEntry, opt, Config.NickName, Config.GroupName); //如果开启了通知功能,则告知非本网段主机 if (h != null && Config.EnableHostNotifyBroadcast && !Config.ForceOldContract) { Host[] list = LivedHost.Values.Where(s => s.HostSub.IPHeader != h.HostSub.IPHeader && h.IsEnhancedContractEnabled).Distinct(Helper.HostSubEqualityCompare.StaticObj).ToArray(); Array.ForEach(list, s => MessageProxy.SendWithNoCheck(s, Consts.Commands.Br_Entry_Forward, 0, s.HostSub.Ipv4Address.Address.GetAddressBytes(), BitConverter.GetBytes(s.HostSub.PortNo))); } }