protected void CoSynSendRequest(RequestCommunicatePackage communicator, int timeMillionsecond)
        {
            if (communicator.WaitResponse)
            {
                RequestPackage reqtPkg = new RequestPackage(_selfDevice,
                                                            communicator.CommunicationName,
                                                            communicator.WaitResponse,
                                                            communicator.ParamPackage.ParamDefalutValues);

                C2CRequestPackage c2cReqtPkg = new C2CRequestPackage(_selfDevice,
                                                                     "outside message sent",
                                                                     communicator.WaitResponse,
                                                                     null);
                c2cReqtPkg.OutSideMessage = reqtPkg;
                RequestMTPackage mtReqtPkg = new RequestMTPackage(c2cReqtPkg,
                                                                  _selfDevice,
                                                                  communicator.TargetDevice,
                                                                  c2cReqtPkg.WaittingResponse);
                MiddlewareTransferPackage mtReplyBasePkg = null;

                //set param-time to wait this c2c response.
                mtReplyBasePkg = _middlewareCommunicateProcessor.SynSendMessage(mtReqtPkg, timeMillionsecond);

                if (null != (mtReplyBasePkg as ReplyMTPackage))
                {
                    ReplyMTPackage  mtReplyPkg  = mtReplyBasePkg as ReplyMTPackage;
                    C2CReplyPackage c2cReplyPkg = mtReplyPkg.C2CReplyPackage;
                    if ((null != c2cReplyPkg) && (null != c2cReplyPkg.OutSideMessage))
                    {
                        ReplyPackage outsideReplyPkg = c2cReplyPkg.OutSideMessage;
                        communicator.ResponsePackage = outsideReplyPkg;
                    }
                    else
                    {
                        throw new ArgumentNullException("应答报文C2CReplyPackage/C2CReplyPackage.OutSideMessage字段空");
                    }
                }
                else
                {
                    throw new NotImplementedException("暂不支持打回RequestMTPackage类型");
                }
            }
            else
            {
                throw new MiddlewareCommunicatErrorExcetion("同步调用而不请求应答是一个错误");
            }
        }
        /// <summary>
        /// 中间件传输层请求消息通知
        /// </summary>
        /// <param name="mtPkg">新到的中间件通讯数据包</param>
        internal void MiddlewareTransferMessageRecived(RequestMTPackage reqtMTPkg)
        {
            C2CRequestPackage c2cReqtPkg = reqtMTPkg.C2CNormalTransPackage;

            if (!c2cReqtPkg.OutSideMessage.IsEmpty)
            {
                if (c2cReqtPkg.WaittingResponse)
                {
                    //_waitResponCCReqtPkg2ReqtMTPkg.Add(c2cReqtPkg, reqtMTPkg);
                    _waitResponCCReqtPkg2ReqtMTPkg[c2cReqtPkg] = reqtMTPkg;
                }

                //index waitting response
                RequestPackage outsideReqtPkg = c2cReqtPkg.OutSideMessage;
                if (outsideReqtPkg.WaittingResponse)
                {
                    //_waitResponOutsideReqtPkg2CCReqtPkg.Add(outsideReqtPkg, c2cReqtPkg);
                    _waitResponOutsideReqtPkg2CCReqtPkg[outsideReqtPkg] = c2cReqtPkg;
                }
                //outside notify
                this.CoRemotReqtRecived_OutsideNotify(outsideReqtPkg);
            }
            else
            {
                //消息模块验证消息
                if (c2cReqtPkg.OperatName.Equals("ListenerVertificationRequest"))
                {
                    C2CReplyPackage replyPkg   = mMiddlewareMessenger.VertificationInfoRecived(c2cReqtPkg);
                    ReplyMTPackage  mtReplyPkg = new ReplyMTPackage(replyPkg,
                                                                    _selfDevice,
                                                                    reqtMTPkg.SourceDevice,
                                                                    reqtMTPkg.MessageId);
                    try
                    {
                        _middlewareCommunicateProcessor.AsynSendMessage(mtReplyPkg);
                    }catch (Exception ex)
                    {
                        //TODO.
                    }
                }
            }
        }
        private void __AsynFeedbackCommunicateReplyMessageRunning(ReplyCommunicatePackage communicator)
        {
            if (_waitResponOutsideReqtPkg2CCReqtPkg.ContainsKey(communicator.RemotReqtPkg) &&
                _waitResponCCReqtPkg2ReqtMTPkg.ContainsKey(_waitResponOutsideReqtPkg2CCReqtPkg[communicator.RemotReqtPkg]))
            {
                ReplyPackage replyPkg = new ReplyPackage(communicator.RemotRet,
                                                         communicator.ParamPackage.ParamDefalutValues);

                C2CReplyPackage c2cReplyPkg = new C2CReplyPackage(ReplyPackage.Middleware_ReplyInfo.S_OK,
                                                                  null);
                c2cReplyPkg.OutSideMessage = replyPkg;

                C2CRequestPackage sourC2CReqtPkg    = _waitResponOutsideReqtPkg2CCReqtPkg[communicator.RemotReqtPkg] as C2CRequestPackage;
                string            mtReplyPkgReplyid = (_waitResponCCReqtPkg2ReqtMTPkg[sourC2CReqtPkg] as RequestMTPackage).MessageId;

                ReplyMTPackage mtReplyPkg = new ReplyMTPackage(c2cReplyPkg,
                                                               _selfDevice,
                                                               communicator.RemotReqtPkg.SourDevice,
                                                               mtReplyPkgReplyid);
                try
                {
                    _middlewareCommunicateProcessor.AsynSendMessage(mtReplyPkg);
                }
                catch (System.Exception ex)
                {
                    //throw excetion "ex"
                    this.CoMiddleware2MiddlewareAsynReplyCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                              new MiddlewareCommunicatErrorExcetion(ex.ToString()));
                }
                _waitResponOutsideReqtPkg2CCReqtPkg.Remove(communicator.RemotReqtPkg);
                _waitResponCCReqtPkg2ReqtMTPkg.Remove(sourC2CReqtPkg);
            }
            else
            {
                //throw excetion "在尝试匹配应答报文的时候,查询源报文索引失败"
                this.CoMiddleware2MiddlewareAsynReplyCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                          new MiddlewareCommunicatErrorExcetion("在尝试匹配应答报文的时候,查询源报文索引失败"));
            }
        }
Beispiel #4
0
        internal MiddlewareTransferPackage DeserializeMessage(byte[] bytes)
        {
            if ((null == bytes) || (0 == bytes.Length))
            {
                throw new Exception("Bin数据不存在或为空");
            }
            byte bytOpjTypeCodec = bytes[0];

            switch (bytOpjTypeCodec)
            {
            case (byte)SerializObjectType.MiddlewareTransferPackage:
            {
                try
                {
                    byte[] bytObjContent = new byte[bytes.Length - 1];
                    Buffer.BlockCopy(bytes, 1, bytObjContent, 0, bytObjContent.Length);

                    CCCommunicateClass.Seria_MiddlewareTransferPackage middlewareSerializeObj = null;
                    MiddlewareTransferPackage retPkg = new MiddlewareTransferPackage();
                    using (MemoryStream m = new MemoryStream(bytObjContent))
                    {
                        CJNet_SerializeTool deSerializeTool = new CJNet_SerializeTool();
                        middlewareSerializeObj = deSerializeTool.Deserialize(m, null, typeof(CCCommunicateClass.Seria_MiddlewareTransferPackage))
                                                 as CCCommunicateClass.Seria_MiddlewareTransferPackage;
                    }
                    retPkg.ParseSerializeData(middlewareSerializeObj);
                    return(retPkg);
                }
                catch (System.Exception ex)
                {
                    throw new Exception("针对Bin数据尝试反序列失败,请检验数据格式: " + ex.ToString());
                }
            }

            case (byte)SerializObjectType.RequestMTPackage:
            {
                try
                {
                    byte[] bytObjContent = new byte[bytes.Length - 1];
                    Buffer.BlockCopy(bytes, 1, bytObjContent, 0, bytObjContent.Length);

                    CCCommunicateClass.Seria_RequestMTPackage reqtMTPSerializeObj = null;
                    RequestMTPackage retPkg = new RequestMTPackage();
                    using (MemoryStream m = new MemoryStream(bytObjContent))
                    {
                        CJNet_SerializeTool deSerializeTool = new CJNet_SerializeTool();
                        reqtMTPSerializeObj = deSerializeTool.Deserialize(m, null, typeof(CCCommunicateClass.Seria_RequestMTPackage)) as CCCommunicateClass.Seria_RequestMTPackage;
                    }
                    retPkg.ParseSerializeData(reqtMTPSerializeObj);
                    return(retPkg);
                }
                catch (System.Exception ex)
                {
                    throw new Exception("针对Bin数据尝试反序列失败,请检验数据格式: " + ex.ToString());
                }
            }

            case (byte)SerializObjectType.ReplyMTPackage:
            {
                try
                {
                    byte[] bytObjContent = new byte[bytes.Length - 1];
                    Buffer.BlockCopy(bytes, 1, bytObjContent, 0, bytObjContent.Length);

                    CCCommunicateClass.Seria_ReplyMTPackage replyMTPSerializeObj = null;
                    ReplyMTPackage retPkg = new ReplyMTPackage();
                    using (MemoryStream m = new MemoryStream(bytObjContent))
                    {
                        CJNet_SerializeTool deSerializeTool = new CJNet_SerializeTool();
                        replyMTPSerializeObj = deSerializeTool.Deserialize(m, null, typeof(CCCommunicateClass.Seria_ReplyMTPackage)) as CCCommunicateClass.Seria_ReplyMTPackage;
                    }
                    retPkg.ParseSerializeData(replyMTPSerializeObj);
                    return(retPkg);
                }
                catch (System.Exception ex)
                {
                    throw new Exception("针对Bin数据尝试反序列失败,请检验数据格式: " + ex.ToString());
                }
            }

            default:
            {
                throw new NotImplementedException("二进制数据指向无法识别的类型");
            }
            }
        }
Beispiel #5
0
        private void __ProcessMiddlewareMessageRecived(MiddlewareTransferPackage mtMsg)
        {
            if (mtMsg is RequestMTPackage)
            {
                //向上请求消息
                _middlewareCorelogicLayer.MiddlewareTransferMessageRecived(mtMsg as RequestMTPackage);
            }
            if (mtMsg is ReplyMTPackage)
            {
                //Noting to do, push up
                ReplyMTPackage mtReplyPkg         = mtMsg as ReplyMTPackage;
                bool           bAsynArrIndexExist = false;
                bool           bSynArrIndexExist  = false;
                lock (_synReadLockObject)
                {
                    if (false == string.IsNullOrEmpty(mtReplyPkg.RepliedMessageId))
                    {
                        bAsynArrIndexExist = _asynWattingMiddlewareReplyMessages.ContainsKey(mtReplyPkg.RepliedMessageId);
                        bSynArrIndexExist  = _synWattingMiddlewareReplyMessages.ContainsKey(mtReplyPkg.RepliedMessageId);
                    }
                }
                if ((false == bAsynArrIndexExist) &&
                    (false == bSynArrIndexExist))
                {
                    //一个未知的应答消息,没有索引,丢弃
                }
                else
                {
                    if (true == bSynArrIndexExist)
                    {
                        //处理同步消息
                        WaitingMessage waitingMessage = null;
                        lock (_synReadLockObject)
                        {
                            if (_synWattingMiddlewareReplyMessages.ContainsKey(mtReplyPkg.RepliedMessageId))
                            {
                                waitingMessage = _synWattingMiddlewareReplyMessages[mtReplyPkg.RepliedMessageId] as WaitingMessage;
                            }
                        }

                        //If there is a thread waiting for this response message, pulse it
                        if (waitingMessage != null)
                        {
                            waitingMessage.ResponseMessage = mtReplyPkg;
                            waitingMessage.State           = MiddlewareTransferServerStateInfo.ResponseReceived;
                            waitingMessage.WaitEvent.Set();
                            return;
                        }
                    }
                    else
                    {
                        //处理异步消息
                        MiddlewareTransferPackage sourMtPkg = _asynWattingMiddlewareReplyMessages[mtReplyPkg.RepliedMessageId] as MiddlewareTransferPackage;
                        _middlewareCorelogicLayer.MiddlewareTransferMessageRecived(sourMtPkg, mtReplyPkg);

                        //清理资源
                        bool havWattingRecored;
                        lock (_synReadLockObject)
                        {
                            havWattingRecored = _asynWattingMiddlewareReplyMessages.ContainsKey(mtReplyPkg.RepliedMessageId);
                        }
                        if (havWattingRecored)
                        {
                            _asynWattingMiddlewareReplyMessages.Remove(mtReplyPkg.RepliedMessageId);
                        }
                    }
                }
            }
        }
        private void __CoAsynSendRequestRunning(RequestCommunicatePackage communicator)
        {
            RequestPackage reqtPkg = new RequestPackage(_selfDevice,
                                                        communicator.CommunicationName,
                                                        communicator.WaitResponse,
                                                        communicator.ParamPackage.ParamDefalutValues);

            C2CRequestPackage c2cReqtPkg = new C2CRequestPackage(_selfDevice,
                                                                 "outside message sent",
                                                                 communicator.WaitResponse,
                                                                 null);

            c2cReqtPkg.OutSideMessage = reqtPkg;
            RequestMTPackage mtReqtPkg = new RequestMTPackage(c2cReqtPkg,
                                                              _selfDevice,
                                                              communicator.TargetDevice,
                                                              c2cReqtPkg.WaittingResponse);

            if (communicator.WaitResponse)
            {
                MiddlewareTransferPackage mtReplyBasePkg = null;
                try
                {
                    //100s limited time to wait this c2c response.
                    mtReplyBasePkg = _middlewareCommunicateProcessor.SynSendMessage(mtReqtPkg, 10000);
                }
                catch (System.Exception ex)
                {
                    //throw new MiddlewareCommunicatErrorExcetion(ex.ToString());
                    this.CoMiddleware2MiddlewareAsynReqtCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                             new MiddlewareCommunicatErrorExcetion(ex.ToString()));
                }
                if (null != (mtReplyBasePkg as ReplyMTPackage))
                {
                    ReplyMTPackage  mtReplyPkg  = mtReplyBasePkg as ReplyMTPackage;
                    C2CReplyPackage c2cReplyPkg = mtReplyPkg.C2CReplyPackage;
                    if ((null != c2cReplyPkg) && (null != c2cReplyPkg.OutSideMessage))
                    {
                        ReplyPackage outsideReplyPkg = c2cReplyPkg.OutSideMessage;
                        communicator.ResponsePackage = outsideReplyPkg;
                        //asyn outside notify
                        if (null != communicator.AsynchronousReponseCame)
                        {
                            communicator.AsynchronousReponseCame(communicator, new AsynReplyCommunicatePackage(outsideReplyPkg));
                        }
                    }
                    else
                    {
                        ArgumentNullException ex = new ArgumentNullException("应答报文C2CReplyPackage/C2CReplyPackage.OutSideMessage字段空");
                        this.CoMiddleware2MiddlewareAsynReqtCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                                 new MiddlewareCommunicatErrorExcetion(ex.ToString()));
                    }
                }
                else
                {
                    NotImplementedException ex = new NotImplementedException("暂不支持打回RequestMTPackage类型");
                    this.CoMiddleware2MiddlewareAsynReqtCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                             new MiddlewareCommunicatErrorExcetion(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    _middlewareCommunicateProcessor.AsynSendMessage(mtReqtPkg);
                }
                catch (System.Exception ex)
                {
                    //throw new MiddlewareCommunicatErrorExcetion(ex.ToString());
                    this.CoMiddleware2MiddlewareAsynReqtCommunicatErrorRecived_OutsideNotify(communicator,
                                                                                             new MiddlewareCommunicatErrorExcetion(ex.ToString()));
                }
            }
        }
Beispiel #7
0
        public void Listen(ClientDevice messenger, BaseMessageType typMsg)
        {
            if (mListeningDevice2MsgID.ContainsKey(messenger.Detail) &&
                (mListeningDevice2MsgID[messenger.Detail] as List <uint>).Contains(typMsg.Id))
            {
                throw new InvalidOperationException("该设备消息已被监听");
            }

            C2CRequestPackage listenerVertificationRequestPkg =
                new C2CRequestPackage(mCoreLogicProcessor.SelfDevice,
                                      "ListenerVertificationRequest",
                                      true,
                                      new Dictionary <string, byte[]>()
            {
                { "MessageType",
                  BitConverter.GetBytes(typMsg.Id) }
            });
            RequestMTPackage mtReqtPkg = new RequestMTPackage(listenerVertificationRequestPkg,
                                                              mCoreLogicProcessor.SelfDevice,
                                                              messenger,
                                                              true);

            //验证流程
            MiddlewareTransferPackage mtReplyBasePkg = null;

            try
            {
                //100s limited time to wait this c2c response.
                mtReplyBasePkg = mMiddlewareCommunicateProcessor.SynSendMessage(mtReqtPkg, 10000);
            }catch (Exception ex)
            {
                throw new Exception("尝试监听一个设备消息时遭遇网络异常:" + ex.ToString());
            }
            ReplyMTPackage  mtReplyPkg  = mtReplyBasePkg as ReplyMTPackage;
            C2CReplyPackage c2cReplyPkg = mtReplyPkg.C2CReplyPackage;

            if (Communication.Package.ReplyPackage.Middleware_ReplyInfo.S_OK == c2cReplyPkg.ReplyState)
            {
                //加入监听群组
                string      gourp_detail   = Encoding.ASCII.GetString(c2cReplyPkg.ParamDefalutValues["group_detail"]);
                GroupDevice wilListenGroup = null;
                try
                {
                    wilListenGroup = mGroupCommunicateProcessor.GetGroup(gourp_detail);
                    mGroupCommunicateProcessor.JoinGroup(wilListenGroup,
                                                         Communication.CommunicationConfig.GroupMemberRole.Listener);
                }catch (Exception ex)
                {
                    throw new Exception("尝试监听一个设备消息时遭遇网络异常:" + ex.ToString());
                }

                if (!mListeningDevice2MsgID.ContainsKey(messenger.Detail))
                {
                    mListeningDevice2MsgID.Add(messenger.Detail, new List <uint>());
                }
                (mListeningDevice2MsgID[messenger.Detail] as List <uint>).Add(typMsg.Id);

                mListeningGroup2Device.Add(wilListenGroup.Detail, messenger);
            }
            else
            {
                throw new Exception("尝试监听一个设备消息时遭遇网络异常:" +
                                    Encoding.UTF8.GetString(c2cReplyPkg.ParamDefalutValues["excetion_detail"]));
            }
        }