private void CheckFailureAction(DealRptChannelState drcs, ref string strMessage)
 {
     LogHelper.WriteInfo(
         "===================ServiceChannelManger.CheckCallbackChannel:回送通道检查失败!关闭失败的通道,重新创建新通道。");
     drcs.DealRptChannel.DoClose();
     drcs.DealRptChannel = this.CreateDealRptChannel(drcs.ServiceAddress, ref strMessage);
     drcs.State          = ChannelState.CSNormal;
 }
        /// <summary>
        /// 回送通道检查
        /// </summary>
        private void CheckCallbackChannel()
        {
            string strMessage = string.Empty;

            lock (((ICollection)_wcfDealRptChannelList).SyncRoot)
            {
                foreach (var pair in _wcfDealRptChannelList.Keys)
                {
                    DealRptChannelState drcs = null;
                    try
                    {
                        drcs = _wcfDealRptChannelList[pair];
                        if (drcs == null)
                        {
                            continue;
                        }

                        if (drcs.State == ChannelState.CSError)
                        {
                            CheckFailureAction(drcs, ref strMessage);
                            continue;
                        }

                        //LogHelper.WriteDebug("ServiceChannelManger.CheckCallbackChannel");
                        drcs.State = ChannelState.CSChecking;

                        OrderDealRptClient odrc = drcs.DealRptChannel;
                        if (odrc == null)
                        {
                            CheckFailureAction(drcs, ref strMessage);
                            continue;
                        }

                        odrc.CheckChannel();
                    }
                    catch (Exception ex)
                    {
                        if (drcs != null)
                        {
                            drcs.State = ChannelState.CSError;
                        }
                        LogHelper.WriteError("GT-2160:[报盘处理]定时检查成交回送通道异常", ex);
                    }
                }
            }
        }
        /// <summary>
        /// 创建报盘通道,每一个服务地址维护一个实例
        /// 即一个撮合中心一个下单连接
        /// </summary>
        /// <param name="strIp">服务器</param>
        /// <param name="strPort">服务器端口</param>
        /// <param name="strDoOrderServiceName">下单服务名</param>
        /// <param name="strDealRptServiceName">成交回送服务名</param>
        ///  <param name="state">成交回送通道状态</param>
        /// <param name="strMessage"></param>
        /// <returns>报盘通道实例</returns>
        public DoOrderClient GetDoOrderChannel(string strIp, string strPort,
                                               string strDoOrderServiceName, string strDealRptServiceName,
                                               ref ChannelState state, ref string strMessage)
        {
            LogHelper.WriteDebug("ServiceChannelManger.GetDoOrderChannel");
            DoOrderClient resultObj = null;

            if (_wcfDoOrderChannelList != null)
            {
                var strDoOrderAddress = string.Format("net.tcp://{0}:{1}/{2}", strIp, strPort, strDoOrderServiceName);

                string strPort2 = "";
                try
                {
                    int dealPort = int.Parse(strPort);
                    dealPort++;
                    strPort2 = dealPort.ToString();
                }
                catch (Exception ex)
                {
                    LogHelper.WriteError(ex.Message, ex);
                    strPort2 = strPort;
                }

                var strDealRptAddress = string.Format("net.tcp://{0}:{1}/{2}", strIp, strPort2,
                                                      strDealRptServiceName);

                _wcfDealRptChannelListLock.EnterUpgradeableReadLock();
                try
                {
                    //如果缓存中没有,那么新建
                    if (!_wcfDoOrderChannelList.TryGetValue(strDoOrderAddress, out resultObj))
                    {
                        _wcfDealRptChannelListLock.EnterWriteLock();
                        try
                        {
                            var doOrderChannel = CreateDoOrderChannel(strDoOrderAddress, ref strMessage);
                            if (doOrderChannel != null)
                            {
                                resultObj = doOrderChannel;
                                _wcfDoOrderChannelList[strDoOrderAddress] = doOrderChannel;

                                var dealBackChannel = this.CreateDealRptChannel(strDealRptAddress, ref strMessage);
                                if (dealBackChannel != null)
                                {
                                    _wcfDealRptChannelList[strDealRptAddress] = new DealRptChannelState(dealBackChannel,
                                                                                                        ChannelState.
                                                                                                        CSNormal,
                                                                                                        strDealRptAddress);
                                }
                            }
                        }
                        finally
                        {
                            _wcfDealRptChannelListLock.ExitWriteLock();
                        }
                    }
                    else
                    {
                        if (resultObj != null &&
                            ((_wcfDealRptChannelList[strDealRptAddress].State == ChannelState.CSError) ||
                             (resultObj.State == CommunicationState.Faulted ||
                              resultObj.State == CommunicationState.Closed)))
                        {
                            _wcfDealRptChannelListLock.EnterWriteLock();
                            try
                            {
                                LogHelper.WriteInfo("ServiceChannelManager.GetDoOrderChannel下单通道出错,状态为" +
                                                    resultObj.State);
                                RemoveDoOrderAndRptChannel(strDoOrderAddress, strDealRptAddress);

                                resultObj = null;
                            }
                            finally
                            {
                                _wcfDealRptChannelListLock.ExitWriteLock();
                            }
                        }
                    }
                }
                finally
                {
                    _wcfDealRptChannelListLock.ExitUpgradeableReadLock();
                }

                #region old code

                //lock (((ICollection) _wcfDealRptChannelList).SyncRoot)
                //{
                //    var strDoOrderAddress = string.Format("net.tcp://{0}:{1}/{2}", strIp, strPort, strDoOrderServiceName);

                //    string strPort2 = "";
                //    try
                //    {
                //        int dealPort = int.Parse(strPort);
                //        dealPort++;
                //        strPort2 = dealPort.ToString();
                //    }
                //    catch (Exception ex)
                //    {
                //        LogHelper.WriteError(ex.Message, ex);
                //        strPort2 = strPort;
                //    }

                //    var strDealRptAddress = string.Format("net.tcp://{0}:{1}/{2}", strIp, strPort2,
                //                                          strDealRptServiceName);

                //    //如果缓存中没有,那么新建
                //    if (!_wcfDoOrderChannelList.ContainsKey(strDoOrderAddress))
                //    {
                //        var doOrderChannel = CreateDoOrderChannel(strDoOrderAddress, ref strMessage);
                //        if (doOrderChannel != null)
                //        {
                //            resultObj = doOrderChannel;
                //            _wcfDoOrderChannelList[strDoOrderAddress] = doOrderChannel;

                //            var dealBackChannel = this.CreateDealRptChannel(strDealRptAddress, ref strMessage);
                //            if (dealBackChannel != null)
                //            {
                //                _wcfDealRptChannelList[strDealRptAddress] = new DealRptChannelState(dealBackChannel,
                //                                                                                    ChannelState.
                //                                                                                        CSNormal,
                //                                                                                    strDealRptAddress);
                //            }
                //        }
                //    }
                //        //if (_wcfDoOrderChannelList.ContainsKey(strDoOrderAddress))
                //    else
                //    {
                //        resultObj = _wcfDoOrderChannelList[strDoOrderAddress];

                //        if (resultObj != null &&
                //            ((_wcfDealRptChannelList[strDealRptAddress].State == ChannelState.CSError) ||
                //             (resultObj.State == CommunicationState.Faulted ||
                //              resultObj.State == CommunicationState.Closed)))
                //        {
                //            //_wcfDoOrderChannelList.Remove(strDoOrderAddress);
                //            //_wcfDealRptChannelList.Remove(strDealRptAddress);
                //            LogHelper.WriteDebug("ServiceChannelManager.GetDoOrderChannel下单通道出错,状态为" + resultObj.State);
                //            RemoveDoOrderAndRptChannel(strDoOrderAddress, strDealRptAddress);

                //            resultObj = null;
                //        }
                //    }
                //}

                #endregion
            }
            return(resultObj);
        }