/// <summary> /// Cancel a real time data stream and clean up after it. /// </summary> /// <returns>True if the stream was canceled, False if subscribers remain.</returns> public bool CancelRtdStream(int instrumentID) { lock (activeStreamsLock) { //make sure there is a data stream for this instrument if (ActiveStreams.Collection.Any(x => x.Instrument.ID == instrumentID)) { var streamInfo = ActiveStreams.Collection.First(x => x.Instrument.ID == instrumentID); var instrument = streamInfo.Instrument; //log the request Log(LogLevel.Info, $"RTD Cancelation request: {instrument.Symbol} from {instrument.Datasource.Name}"); lock (subscriberCountLock) { StreamSubscribersCount[streamInfo]--; if (StreamSubscribersCount[streamInfo] == 0) { //there are no clients subscribed to this stream anymore //cancel it and remove it from all the places StreamSubscribersCount.Remove(streamInfo); ActiveStreams.TryRemove(streamInfo); DataSources[streamInfo.Datasource].CancelRealTimeData(streamInfo.RequestID); return(true); } } } return(false); } }
/// <summary> /// Sends a real time data request to the correct data source, logs it, and updates subscriber counts /// </summary> /// <param name="request"></param> private void ForwardRtdRequest(RealTimeDataRequest request) { //send the request to the correct data source int reqID; try { reqID = DataSources[request.Instrument.Datasource.Name].RequestRealTimeData(request); } catch (Exception ex) { Log(LogLevel.Error, "Error requesting real time data: " + ex.Message); return; } //log the request Log(LogLevel.Info, $"RTD Request: {request.Instrument.Symbol} from {request.Instrument.Datasource.Name} @ {Enum.GetName(typeof(BarSize), request.Frequency)} ID:{reqID}"); //add the request to the active streams, though it's not necessarily active yet var streamInfo = new RealTimeStreamInfo( request.Instrument, reqID, request.Instrument.Datasource.Name, request.Frequency, request.RTHOnly); lock (activeStreamsLock) { ActiveStreams.TryAdd(streamInfo); } lock (subscriberCountLock) { StreamSubscribersCount.Add(streamInfo, 1); } }