Example #1
0
        /// <summary>
        /// 非阻塞式执行一次消息发送请求
        /// </summary>
        /// <param name="mtPkg">发往远程中间件设备的消息包</param>
        internal void AsynSendMessage(MiddlewareTransferPackage mtPkg)
        {
            //will send a message
            if ((mtPkg is RequestMTPackage) && ((mtPkg as RequestMTPackage).WattingResponse))
            {
                __ActiveMiddlewareMessageWattinghere(mtPkg);
            }

            bool mustWaitReponse = true;
            ServerRequestPackage wilSendScsMsg = new ServerRequestPackage(mustWaitReponse);

            //配置为Middleware传输包
            IMiddleware2MiddlewareCommunicatRequest statSeter = wilSendScsMsg.Active_MiddlewareCommunicatRequest();

            statSeter.WilSendMiddleware2MiddlewareMessage(mtPkg.TargetDevice, mtPkg);

            BinTransferLayer   btlprocessor = _middlewareCorelogicLayer.BinTransferProcessor;
            ServerReplyPackage replyServPkg = null;

            try
            {
                replyServPkg = btlprocessor.SynSendMessage(wilSendScsMsg, 100000);
            }
            catch (System.Exception ex)
            {
                //TODO
                throw new MiddlewareTransferErrorExcetion(ex);
            }
            //分析服务端报文
            IMiddleware2MiddlewareCommunicatReply replyInterface = replyServPkg.Active_Middleware2MiddlewareCommunicatReply();
            bool   ret;
            string detail;

            try
            {
                replyInterface.SendMiddlewareMsgOprRet(out ret, out detail);
            }
            catch (Exception ex)
            {
                throw new MiddlewareTransferErrorExcetion(ex.ToString());
            }
            if (true == ret)
            {
                //noting to do
            }
            else
            {
                throw new MiddlewareTransferErrorExcetion(detail);
            }
        }
Example #2
0
        private MiddlewareTransferPackage __SendMessageAndWattingForResponse(MiddlewareTransferPackage msg, int timeoutMilliseconds)
        {
            WaitingMessage waitMsg = new WaitingMessage();

            lock (_synWriteLockObject)
            {
                _synWattingMiddlewareReplyMessages[msg.MessageId] = waitMsg;
            }

            //send message
            bool mustWaitReponse = true;
            ServerRequestPackage wilSendServMsg = new ServerRequestPackage(mustWaitReponse);

            //配置为中间件传输包
            IMiddleware2MiddlewareCommunicatRequest statSetter = wilSendServMsg.Active_MiddlewareCommunicatRequest();

            statSetter.WilSendMiddleware2MiddlewareMessage(msg.TargetDevice, msg);

            BinTransferLayer   btlpeocessor = _middlewareCorelogicLayer.BinTransferProcessor;
            ServerReplyPackage replyServPkg = null;

            try
            {
                replyServPkg = btlpeocessor.SynSendMessage(wilSendServMsg, 100000);
            }
            catch (System.Exception ex)
            {
                //释放锁记录
                lock (_synWriteLockObject)
                {
                    _synWattingMiddlewareReplyMessages.Remove(msg.MessageId);
                }
                throw new MiddlewareTransferErrorExcetion(ex.ToString());
            }
            //分析服务端报文
            IMiddleware2MiddlewareCommunicatReply replyInterface = replyServPkg.Active_Middleware2MiddlewareCommunicatReply();
            bool   ret;
            string detail;

            replyInterface.SendMiddlewareMsgOprRet(out ret, out detail);
            if (true == ret)
            {
                //noting to do
            }
            else
            {
                throw new MiddlewareTransferErrorExcetion(detail);
            }

            //wait for the recived event
            waitMsg.WaitEvent.Wait(timeoutMilliseconds);

            //Check for exceptions
            switch (waitMsg.State)
            {
            case MiddlewareTransferServerStateInfo.WaittingForResponse:
            {
                //同步超时
                lock (_synWriteLockObject)
                {
                    _synWattingMiddlewareReplyMessages.Remove(msg.MessageId);
                }
                throw new MiddlewareTransferErrorExcetion(MiddlewareTransferServerStateInfo.TimeOut.ToString());
            }

            case MiddlewareTransferServerStateInfo.ResponseReceived:
            {
                lock (_synWriteLockObject)
                {
                    _synWattingMiddlewareReplyMessages.Remove(msg.MessageId);
                }
                break;
            }
            }
            return(waitMsg.ResponseMessage);
        }