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.
                    }
                }
            }
        }
Ejemplo n.º 3
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("二进制数据指向无法识别的类型");
            }
            }
        }
        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()));
                }
            }
        }
Ejemplo n.º 5
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"]));
            }
        }