/// <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="e">事件数据</param> protected virtual void OnPackageSended(PackageEventArgs e) { if (PackageSended == null) { return; } if (!IPMClient.NeedPostMessage) { PackageSended(this, e); } else { if (callPackageSended == null) { callPackageSended = s => PackageSended(this, s as PackageEventArgs); } IPMClient.SendASynchronizeMessage(callPackageSended, e); } }
/// <summary> /// 触发数据包已发送事件 /// </summary> /// <param name="e">事件数据</param> protected virtual void OnPackageSended(PackageEventArgs e) { if (PackageSended == null) return; if (!IPMClient.NeedPostMessage) { PackageSended(this, e); } else { if (callPackageSended == null) callPackageSended = s => PackageSended(this, s as PackageEventArgs); IPMClient.SendASynchronizeMessage(callPackageSended, 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; }