/// <summary> /// Request a new real time data stream. Data will be delivered through the RealTimeDataReceived event. /// </summary> /// <returns>An ID uniquely identifying this real time data request. -1 if there was an error.</returns> public int RequestRealTimeData(RealTimeDataRequest request) { if (!Connected) { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not request real time data - not connected.")); return(-1); } if (request.Instrument == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Real Time Data Request Failed: null Instrument.")); return(-1); } request.RequestID = _requestCount++; lock (_reqSocketLock) { //two part message: //1: "RTD" //2: serialized RealTimeDataRequest var ms = new MemoryStream(); _reqSocket.SendMore(""); _reqSocket.SendMore("RTD"); _reqSocket.Send(MyUtils.ProtoBufSerialize(request, ms)); } return(request.RequestID); }
/// <summary> /// Tries to connect to the QDMS server. /// </summary> public void Connect() { Dispose(); _context = NetMQContext.Create(); _reqSocket = _context.CreateDealerSocket(); _subSocket = _context.CreateSubscriberSocket(); _dealerSocket = _context.CreateSocket(ZmqSocketType.Dealer); _reqSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _subSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _dealerSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _dealerSocket.ReceiveReady += _dealerSocket_ReceiveReady; _reqSocket.ReceiveReady += _reqSocket_ReceiveReady; _subSocket.ReceiveReady += _subSocket_ReceiveReady; _reqSocket.Connect(string.Format("tcp://{0}:{1}", _host, _realTimeRequestPort)); //start off by sending a ping to make sure everything is regular string reply = ""; try { _reqSocket.SendMore(""); _reqSocket.Send("PING"); _reqSocket.ReceiveString(TimeSpan.FromSeconds(1)); //empty frame starts the REP message //todo receive string? reply = _reqSocket.ReceiveString(TimeSpan.FromMilliseconds(50)); } catch { Dispose(); } if (reply != "PONG") //server didn't reply or replied incorrectly { _reqSocket.Disconnect(string.Format("tcp://{0}:{1}", _host, _realTimeRequestPort)); _reqSocket.Close(); { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server.")); return; } } _lastHeartBeat = DateTime.Now; _subSocket.Connect(string.Format("tcp://{0}:{1}", _host, _realTimePublishPort)); _dealerSocket.Connect(string.Format("tcp://{0}:{1}", _host, _historicalDataPort)); _running = true; //this loop sends out historical data requests and receives the data _dealerLoopThread = new Thread(DealerLoop) { Name = "Client Dealer Loop" }; _dealerLoopThread.Start(); //this loop takes care of replies to the request socket: heartbeats and data request status messages _poller = new Poller(); _poller.AddSocket(_reqSocket); _poller.AddSocket(_subSocket); _poller.AddSocket(_dealerSocket); Task.Factory.StartNew(_poller.Start, TaskCreationOptions.LongRunning); _heartBeatTimer = new Timer(1000); _heartBeatTimer.Elapsed += _timer_Elapsed; _heartBeatTimer.Start(); }