// method is called when there are new data to update public Array RefreshData(ref int topicCount) { object[,] data = null; try { TwsRtdServerConnection connection = null; TwsRtdServerMktDataRequest mktDataRequest = null; TwsRtdServerTopic topic = null; // in loop - update all topics from updatedTopicIds array int[] updatedTopicIds; lock (m_updatedTopicIds) { updatedTopicIds = m_updatedTopicIds.ToArray(); m_updatedTopicIds.Clear(); } topicCount = updatedTopicIds.Length; data = new object[2, topicCount]; int n = 0; foreach(var topicId in updatedTopicIds) { TwsRtdServerTopicIdMap twsRtdServerTopicIdMap = null; if (m_topicIdMap.TryGetValue(topicId, out twsRtdServerTopicIdMap)) { // get appropriate connection if (m_connections.TryGetValue(twsRtdServerTopicIdMap.ConnectionStr(), out connection)) { // get appropriate mktDataRequest mktDataRequest = connection.GetMktDataRequest(twsRtdServerTopicIdMap.TwsReqId()); if (mktDataRequest != null) { // get appropriate topic topic = mktDataRequest.GetTopic(twsRtdServerTopicIdMap.TopicStr()); } if (topic != null) { // update data array with topic.Id and topic.Value data[0, n] = topic.TopicId(); data[1, n] = topic.TopicValue(); n++; } } } } } catch //(COMException comException) { // comException.Message = "The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))" // Console.WriteLine(comException.Message); } return data; }
void GetTopicAndAddUpdate(string topicStr, TwsRtdServerMktDataRequest mktDataRequest, object value) { TwsRtdServerTopic topic = mktDataRequest.GetTopic(topicStr); if (topic != null) { // set topic's new value topic.TopicValue(value); m_server.AddUpdatedTopicId(topic.TopicId()); // add topic to updatedTopicIds array } // save latest value mktDataRequest.SetMktDataTickValue(topicStr, value); }
// this method is called when topic is removed public void DisconnectData(int topicId) { try { // find appropriate connection, mktDataRequest and topic TwsRtdServerTopicIdMap twsRtdServerTopicIdMap = null; if (!m_topicIdMap.TryGetValue(topicId, out twsRtdServerTopicIdMap)) { return; } // get appropriate connection TwsRtdServerConnection connection = null; if (!m_connections.TryGetValue(twsRtdServerTopicIdMap.ConnectionStr(), out connection)) { return; } // get appropriate mktDataRequest TwsRtdServerMktDataRequest mktDataRequest = connection.GetMktDataRequest(twsRtdServerTopicIdMap.TwsReqId()); if (mktDataRequest != null) { // get appropriate topic TwsRtdServerTopic topic = mktDataRequest.GetTopic(twsRtdServerTopicIdMap.TopicStr()); if (topic != null) { // remove topic mktDataRequest.RemoveTopic(twsRtdServerTopicIdMap.TopicStr()); } m_topicIdMap.Remove(topicId); // remove topicId from updatedTopicIds lock (m_updatedTopicIds) { m_updatedTopicIds.Remove(topicId); } // try to remove mktDataRequest connection.RemoveMktDataRequest(twsRtdServerTopicIdMap.TwsReqId()); } } catch { // error disconnecting data } }