private void DoSendImsMsg(ImsResponse responseMsg) { try { if (ImsNetManager.Instance.IsImsSocketConnect()) { IMsgInfo msg = responseMsg; MsgSend msgSend = new MsgSend(); int res = msgSend.ConvertFrom(msg); if (0 == res) { byte[] data = msgSend.CloneBytes(); ImsNetManager.Instance.NowImsStation.SendBytes(data); // Logger.LogInfo(null, "向IMS发送响应消息 :【" + BitConverter.ToString(responseMsg.CloneMsgBytes()) + "】"); } else { // Logger.LogError(null, "向IMS发送响应消息 :【" + "消息不合法" + "】"); } } } catch (SocketException ex) { ImsNetManager.Instance.NowImsStation.CloseImsConn(); // Logger.LogError(null, "IMS发送响应消息时捕获到SOCKET异常:【" + ex.Message+"】"); } catch (Exception ex) { ImsNetManager.Instance.NowImsStation.CloseImsConn(); // Logger.LogError(null, "IMS发送响应消息时捕获到未知异常:【" + ex.Message+"】"); } }
/// <summary> /// 获取不支持异常 /// </summary> /// <param name="info">异常信息</param> /// <param name="msgId">消息Id</param> /// <returns>异常</returns> private Exception GetNotSupportedException(IMsgInfo info) { if (info != null) { throw new NotSupportedException("No suitable protocol processor for [" + info.Name + "]"); } throw new NotSupportedException("No suitable protocol processor , sent model or unknow msgId"); }
/// <summary> /// 解包 /// </summary> /// <param name="msgInfo">消息信息</param> /// <param name="buffer">需要解包的字节流</param> /// <returns>协议对象</returns> public object Unpacked(IMsgInfo msgInfo, ArraySegment <byte> buffer) { return(unpackedPolicy.Do(new ReceivePackage { MsgInfo = msgInfo, Body = buffer }, package => { throw GetNotSupportedException(package.MsgInfo); })); }
/// <summary> /// 打包 /// </summary> /// <param name="buffer">缓冲数据流</param> /// <param name="msgInfo">消息信息</param> /// <param name="packet">输入的协议对象</param> public void Packed(ref ArraySegment <byte> buffer, IMsgInfo msgInfo, object packet) { var sent = packedPolicy.Do(new SentPackage { MsgInfo = msgInfo, Packet = packet, SentBuffer = buffer }, package => { throw GetNotSupportedException(package.MsgInfo); }); buffer = sent; }
/// <summary> /// 对象生成 /// </summary> /// <param name="msgInfo">基础消息信息</param> /// <returns></returns> private static object Generate(IMsgInfo msgInfo) { if (msgInfo.Generate != null) { return(msgInfo.Generate.Invoke(null, null)); } if (msgInfo.ProtocolType != null) { return(Activator.CreateInstance(msgInfo.ProtocolType)); } return(null); }