private static IRealtimeQuotationProcessorService CreateRealtimeQuotationProcessorService(string url, int timeoutInSecond = 30)
        {
            EndpointAddress address = new EndpointAddress(url);
            NetTcpBinding   binding = new NetTcpBinding(SecurityMode.None);

            binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = binding.MaxBufferSize = 16 * 1024 * 1024;
            binding.SendTimeout       = TimeSpan.FromSeconds(timeoutInSecond);
            binding.OpenTimeout       = TimeSpan.FromSeconds(timeoutInSecond);
            ChannelFactory <IRealtimeQuotationProcessorService> factory = new ChannelFactory <IRealtimeQuotationProcessorService>(binding, address);
            IRealtimeQuotationProcessorService service = factory.CreateChannel();

            return(service);
        }
        private QuotationBroadcastHelper(StateServer stateServer, TransactionServer.Service transactionServerService)
        {
            this._StateServer = stateServer;
            this._TransactionServerService = transactionServerService;

            string host = new Uri(transactionServerService.Url).Host;
            string port = ConfigurationManager.AppSettings["iExchange.StateServer.TransactionServer.RealtimeQuotationServicePort"];

            this.realtimeQuotationProcessorServiceUrl = string.Format("net.tcp://{0}:{1}/TransactionServer/RealtimeQuotationProcessService", host, string.IsNullOrEmpty(port) ? "9090" : port);
            this.realtimeQuotationProcessorService    = CreateRealtimeQuotationProcessorService(this.realtimeQuotationProcessorServiceUrl);

            this._TransactionServerService.Timeout = 1800000;
            this._QuotationForBroadcastQueue       = new Queue <QuotationForBroadcast>();
            this._BroadcastThread = new Thread(this.Broadcast);
            this._BroadcastThread.IsBackground = true;
            this._BroadcastThread.Start();
        }
        private void Broadcast()
        {
            while (true)
            {
                this._QuotationArriveEvent.WaitOne();
                try
                {
                    while (true)
                    {
                        QuotationForBroadcast    quotation = null;
                        AutoFillResultInString[] autoFillResults;
                        lock (this._QuotationForBroadcastQueue)
                        {
                            //if (this._QuotationForBroadcastQueue.Count == 4)
                            {
                                quotation = this._Merger.MergeAndGetQuotationToBroadcast(this._QuotationForBroadcastQueue);
                                this._QuotationForBroadcastQueue.Clear();
                            }
                        }
                        if (quotation == null)
                        {
                            break;
                        }
                        if (EnableLog)
                        {
                            AppDebug.LogEvent("StateServer.QuotationBroadcastHelper",
                                              string.Format("Quotation send to TransactionServer: {0}", quotation), EventLogEntryType.Information);
                        }

                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        //XmlNode xmlHitOrders = this._TransactionServerService.SetQuotation(quotation.Token, quotation.OriginQuotations, quotation.OverridedQuotations, out autoFillResults);
                        string  hitOrders    = this.realtimeQuotationProcessorService.SetQuotation(quotation.Token, quotation.OriginQuotations, quotation.OverridedQuotations, out autoFillResults);
                        XmlNode xmlHitOrders = null;
                        if (!string.IsNullOrEmpty(hitOrders))
                        {
                            XmlDocument document = new XmlDocument();
                            document.LoadXml(hitOrders);
                            xmlHitOrders = document.DocumentElement;
                        }
                        watch.Stop();
                        AppDebug.LogEvent("StateServer",
                                          string.Format("QuotationBroadcastHepler.Broadcast, call TransactionServer.SetQuotation consume time = {0} ms", watch.ElapsedMilliseconds), EventLogEntryType.Information);
                        AutoFillResult[] autoFillResults2 = autoFillResults == null ? null : new AutoFillResult[autoFillResults.Length];
                        if (autoFillResults != null)
                        {
                            int index = 0;
                            foreach (AutoFillResultInString source in autoFillResults)
                            {
                                autoFillResults2[index++] = source.ToAutoFillResult();
                            }
                        }
                        Token token = quotation.Token == null ? StateServer.Token: quotation.Token;
                        this._StateServer.AfterBroadcastQuotationToTransactionServer(token, xmlHitOrders, autoFillResults2, this._TransactionServerService);
                    }
                }
                catch (Exception ex)
                {
                    AppDebug.LogEvent("StateServer",
                                      string.Format("QuotationBroadcastHepler.Broadcast to {0} call TransactionServer.SetQuotation error {1}", this.realtimeQuotationProcessorServiceUrl, ex), EventLogEntryType.Error);
                    this.realtimeQuotationProcessorService = CreateRealtimeQuotationProcessorService(this.realtimeQuotationProcessorServiceUrl);
                }
            }
        }