//处理接收到消息事件 void MessageProxy_MessageReceived(object sender, MessageEventArgs e) { if (e.Message == null || e.IsHandled) return; MessageEventArgs me = new MessageEventArgs(e.Message, e.Host); OnMessageProcessing(me); if (me.IsHandled) return; //分析请求 switch (e.Message.Command) { case Consts.Commands.NoOperaiton: ProcessCommand_Nop(e.Message, e.Host); break; case Consts.Commands.Br_Entry: ProcessCommand_Br_Entry(e.Message, e.Host); break; case Consts.Commands.Br_Exit: ProcessCommand_Br_Exit(e.Message, e.Host); break; case Consts.Commands.AnsEntry: ProcessCommand_AnsEntry(e.Message, e.Host); break; case Consts.Commands.Br_Absence: ProcessCommand_BrAbsence(e.Message, e.Host); break; case Consts.Commands.IsGetList: ProcessCommand_IsGetList(e.Message, e.Host); break; case Consts.Commands.OkGetList: ProcessCommand_OkGetList(e.Message, e.Host); break; case Consts.Commands.GetList: ProcessCommand_GetList(e.Message, e.Host); break; case Consts.Commands.AnsList: ProcessCommand_AnsList(e.Message, e.Host); break; case Consts.Commands.IsGetList2: ProcessCommand_IsGetList2(e.Message, e.Host); break; case Consts.Commands.SendMsg: ProcessCommand_SendMsg(e.Message, e.Host); break; case Consts.Commands.RecvMsg: ProcessCommand_RecvMsg(e.Message, e.Host); break; case Consts.Commands.ReadMsg: ProcessCommand_ReadMsg(e.Message, e.Host); break; case Consts.Commands.DelMsg: ProcessCommand_DelMsg(e.Message, e.Host); break; case Consts.Commands.AnsReadMsg: ProcessCommand_AnsReadMsg(e.Message, e.Host); break; case Consts.Commands.GetInfo: ProcessCommand_GetInfo(e.Message, e.Host); break; case Consts.Commands.SendInfo: ProcessCommand_SendInfo(e.Message, e.Host); break; case Consts.Commands.GetAbsenceInfo: ProcessCommand_GetAbsenceInfo(e.Message, e.Host); break; case Consts.Commands.SendAbsenceInfo: ProcessCommand_SendAbsenceInfo(e.Message, e.Host); break; case Consts.Commands.GetFileData: ProcessCommand_GetFileData(e.Message, e.Host); break; case Consts.Commands.ReleaseFiles: ProcessCommand_ReleaseFiles(e.Message, e.Host); break; case Consts.Commands.GetDirFiles: ProcessCommand_GetDirFiles(e.Message, e.Host); break; case Consts.Commands.Ex_PackageRecevied: ProcessCommand_Ex_PackageRecevied(e.Message, e.Host); break; default: break; } // OnMessageProcessed(me); }
/// <summary> /// 触发消息对方消息已经打开事件 /// </summary> /// <param name="e">事件参数</param> protected virtual void OnTextMessageOpened(MessageEventArgs e) { if (TextMessageOpened == null) return; if (!IPMClient.NeedPostMessage) TextMessageOpened(this, e); else { if (textMessageOpenedCall == null) textMessageOpenedCall = (s) => TextMessageOpened(this, s as MessageEventArgs); IPMClient.SendSynchronizeMessage(textMessageOpenedCall, e); } }
/// <summary> /// 触发收到文本消息事件 /// </summary> /// <param name="e">事件参数</param> protected virtual void OnTextMessageReceived(MessageEventArgs e) { if (TextMessageReceived == null) return; if (IPMClient.NeedPostMessage) { if (textMessageReceivedCall == null) textMessageReceivedCall = s => TextMessageReceived(this, s as MessageEventArgs); IPMClient.SendSynchronizeMessage(textMessageReceivedCall, e); } else { TextMessageReceived(this, e); } }
/// <summary> /// 触发消息已处理的事件 /// </summary> /// <param name="e">事件参数</param> protected virtual void OnMessageProcessed(MessageEventArgs e) { if (MessageProcessed == null) return; if (IPMClient.NeedPostMessage) { if (messageProcessedCall == null) messageProcessedCall = (s) => MessageProcessed(this, s as MessageEventArgs); IPMClient.SendSynchronizeMessage(messageProcessedCall, e); } else { MessageProcessed(this, e); } }
//接收到文字消息 private void ProcessCommand_SendMsg(Message message, Host host) { if (host == null) return; //确认是否需要自动回复? if (!message.IsAutoSendMessage && (Config.AutoReply || (Config.AutoReplyWhenAbsence && Config.IsInAbsenceMode))) { string msg = string.IsNullOrEmpty(Config.AutoReplyMessage) ? Config.AutoReplyMessage : Config.AbsenceMessage; if (string.IsNullOrEmpty(msg)) msg = Resources.CommandExecutor_ProcessCommand_SendMsg_AutoReplyMessage; MessageProxy.SendWithNoCheck(host, Consts.Commands.SendMsg, (ulong)Consts.Cmd_Send_Option.AutoRet, msg, string.Empty); message.AutoReplyTime = DateTime.Now; } //触发事件 MessageEventArgs e = new MessageEventArgs(message, host); OnTextMessageReceiving(e); if (!e.IsHandled) OnTextMessageReceived(e); }
/// <summary> /// 引发消息将要发送事件 /// </summary> /// <param name="e"></param> protected virtual void OnMessageSending(MessageEventArgs e) { if (MessageSending == null) return; if (!IPMClient.NeedPostMessage) { MessageSending(this, e); } else { if (messageSendingCallBack == null) messageSendingCallBack = s => MessageSending(this, s as MessageEventArgs); IPMClient.SendSynchronizeMessage(messageSendingCallBack, e); } }
/// <summary> /// 以二进制模式发送消息 /// </summary> /// <param name="host">关联的远程主机,不可以为null</param> /// <param name="remoteEndPoint">远程主机地址</param> /// <param name="cmd">命令</param> /// <param name="options">参数</param> /// <param name="normalMsg">常规信息</param> /// <param name="extendMessage">扩展消息</param> /// <param name="sendCheck">是否检查发送到</param> /// <exception cref="InvalidOperationException">如果对方主机不在列表中,或未知是否支持增强协议,则会抛出此异常</exception> /// <returns>返回发出的消息包编号</returns> public ulong Send(Host host, IPEndPoint remoteEndPoint, Define.Consts.Commands cmd, ulong options, byte[] normalMsg, byte[] extendMessage, bool sendCheck) { if (!Client.IsInitialized) return 0ul; //判断远程主机是否支持这个模式 if (host == null || !host.IsEnhancedContractEnabled) throw new InvalidOperationException("尚不知道主机是否支持增强协议模式,无法以二进制模式发送消息!"); Message cm = Message.Create(host, remoteEndPoint, Config.GetRandomTick(), Config.HostName, Config.NickName, cmd, options, "", ""); cm.ExtendMessageBytes = extendMessage; cm.NormalMsgBytes = normalMsg; cm.IsRequireReceiveCheck = sendCheck; //设置选项 if (sendCheck) { cm.Options |= (ulong)Define.Consts.Cmd_All_Option.RequireReceiveCheck; } cm.Options |= (ulong)Define.Consts.Cmd_All_Option.EnableNewDataContract | (ulong)Define.Consts.Cmd_All_Option.BinaryMessage; MessageEventArgs mea = new MessageEventArgs(cm) { Message = cm, IsHandled = false, Host = host }; OnMessageSending(mea); if (mea.IsHandled) return mea.Message.PackageNo; Entity.PackedNetworkMessage[] pnm = MessagePackerV2.BuildNetworkMessage(cm); PackageEventArgs pea = new PackageEventArgs(pnm.Length > 1, pnm[0], pnm); OnPckageSending(pea); if (!pea.IsHandled) { Array.ForEach(pnm, s => { Client.Send(s); }); OnPackageSended(pea); } OnMessageSended(mea); return cm.PackageNo; }
/// <summary> /// 发送打包好的消息 /// </summary> /// <param name="cm"></param> /// <returns>返回发出的消息包编号</returns> public ulong Send(Message cm) { if (!Client.IsInitialized) return 0ul; cm.Options |= (ulong)Define.Consts.Cmd_All_Option.EnableNewDataContract; MessageEventArgs mea = new MessageEventArgs(cm) { Message = cm, IsHandled = false, Host = cm.Host }; OnMessageSending(mea); if (mea.IsHandled) return mea.Message.PackageNo; //判断远程主机是否支持这个模式 if (!Config.ForceOldContract && cm.Host != null && cm.Host.IsEnhancedContractEnabled) { Entity.PackedNetworkMessage[] pnm = MessagePackerV2.BuildNetworkMessage(cm); PackageEventArgs pea = new PackageEventArgs(pnm.Length > 1, pnm[0], pnm); OnPckageSending(pea); if (!pea.IsHandled) { Array.ForEach(pnm, s => { Client.Send(s); }); OnPackageSended(pea); } } else { Entity.PackedNetworkMessage pn = MessagePacker.BuildNetworkMessage(cm); PackageEventArgs pe = new PackageEventArgs(false, pn, null); OnPckageSending(pe); if (!pe.IsHandled) { Client.Send(pn); OnPackageSended(pe); } } OnMessageSended(mea); return cm.PackageNo; }
/// <summary> /// 预处理信息,检测是否有文件请求,有就处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Commander_TextMessageReceived(object sender, MessageEventArgs e) { if (e.IsHandled || !e.Message.IsFileAttached) return; var files = FileTaskItemHelper.DecompileTaskInfo(e.Host, e.Message); if (files != null) { var ea = new FileReceivedEventArgs(e.Host, e.Message, files); this.FileTaskManager.OnFileReceived(ea); //如果没有正文信息,就标记为已处理 if (string.IsNullOrEmpty(e.Message.NormalMsg.Trim())) e.IsHandled = true; } }