void OpenAndSendWithWCF(BinaryWriter ProcWriter) { if (briefMakerClient.State != CommunicationState.Opened) { loggerWCF.Debug("WCF Not opened. Trying to open a connection..."); try { briefMakerClient.ChannelFactory.BeginOpen( (result) => { loggerWCF.Debug("WCF received async result"); briefMakerClient.ChannelFactory.EndOpen(result); }, null); loggerWCF.Debug("BriefMaker Connection Established"); } catch (Exception ex) { loggerWCF.Debug("Exception when opening connection:(this can be normal)(Calling Abort,Close,recreate next) {0}", ex.Message); //briefMakerClient.Abort(); //briefMakerClient.Close(); briefMakerClient = new BriefMakerServiceReference.BriefMakerClient(); loggerWCF.Debug("Abort,Close,recreate completed"); } } if (briefMakerClient.State == CommunicationState.Opened) { loggerWCF.Debug("WCF is open, calling AddDataStreamMomentUsingWCFAsync()"); try { briefMakerClient.AddDataStreamMomentUsingWCFAsync(((MemoryStream)ProcWriter.BaseStream).ToArray()); loggerWCF.Debug("AddDataStreamMomentUsingWCFAsync() completed"); } catch (Exception ex) { loggerWCF.Debug("Exception when calling AddDataStreamMomentUsingWCFAsync(): " + ex.Message); } } }
public Recorder(RecorderView view) { this.view = view; view.recorder = this; /////////// Setup logger /////////// logger = NLog.LogManager.GetCurrentClassLogger(); loggerWCF = NLog.LogManager.GetLogger("Capture.Recorder.WCF"); /////////// Setup WCF notification to a BriefMaker /////////// briefMakerClient = new BriefMakerServiceReference.BriefMakerClient(); /////////// Download Symbols /////////// logger.Debug("Downloading symbols"); var dbSymbols = from s in dc.Symbols select s; foreach (var s in dbSymbols) { if (String.IsNullOrWhiteSpace(s.Name)) { logger.Error("SymbolID:" + s.SymbolID + " does not have a name(symbol). Item will be skipped."); continue; } if (s.SymbolID > 255 || s.SymbolID < 0) { logger.Error("SymbolID:" + s.SymbolID + " range is not valid. Supported(0-255). Item will be skipped."); continue; } SecurityType secType = s.Type.Trim() == "STK" ? SecurityType.Stock : SecurityType.Index; string market = s.Market.Trim(); var new_symb = new MarketSymbol() { SymbolID = s.SymbolID, Symbol = s.Name.Trim(), securityType = s.Type.Trim() == "STK" ? SecurityType.Stock : SecurityType.Index, Market = s.Market }; symbols.Add(new_symb); } // Setup BinaryWriters logger.Debug("Downloading symbols"); capturingWriter = new BinaryWriter(new MemoryStream(MemoryStreamReserveSpace)); capturingWriter.Write((long)0); // leave some space at the beginning for time later manufacturedWriterForFuture = new BinaryWriter(new MemoryStream(MemoryStreamReserveSpace)); manufacturedWriterForFuture.Write((long)0); // leave some space at the beginning for time later // Run this thread will a little higher priority since it is dealing with real-time information. Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; view.timer.Tick += new EventHandler(timer1Sec_Tick); while (true) { TimeSpan timespan = new TimeSpan(DateTime.Now.Ticks - lastRecievedUpdate.Ticks); if (timespan.Seconds > 30) { logger.Info("No Data received in over 30 seconds. Requesting Reconnect..."); lastRecievedUpdate = DateTime.Now; ConnectToIB(); } if (terminateRequested || (DateTime.Now.TimeOfDay > settings.ShutDownTime)) { logger.Info("Close requested or automatic end of day shutdown."); ShutdownRecorder(); break; } view.BeginInvoke((MethodInvoker) delegate { view.toolStripStatusLabelEventCt.Text = totalCaptureEventsForDisplay.ToString() + " (" + (totalCaptureEventsForDisplay - lastTotalCaptureEventsForDisplay).ToString() + "/sec)"; // runs on UI thread lastTotalCaptureEventsForDisplay = totalCaptureEventsForDisplay; view.toolStripStatusLabelLastBrfID.Text = lastUpdateTime.ToString(); }); // runs on UI thread Thread.Sleep(1000); } }