/// <summary> /// Pushes data to local storage. /// </summary> public void PushData(DataAdditionRequest request) { if (request == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Request cannot be null.")); return; } if (request.Instrument?.ID == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Instrument must be set and have an ID.")); return; } if (!Connected) { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not push historical data to local storage - not connected.")); return; } lock (_historicalDataSocketLock) { if (_historicalDataSocket != null) { _historicalDataSocket.SendMoreFrame("HISTPUSH"); using (var ms = new MemoryStream()) { _historicalDataSocket.SendFrame(MyUtils.ProtoBufSerialize(request, ms)); } } } }
/// <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 (request == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Real Time Data Request Failed: Request cannot be null.")); return(-1); } if (request.Instrument == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Real Time Data Request Failed: null Instrument.")); return(-1); } if (!Connected) { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not request real time data - not connected.")); return(-1); } request.RequestID = Interlocked.Increment(ref _requestCount); lock (_realTimeRequestSocketLock) { if (_realTimeRequestSocket != null) { // Two part message: // 1: "RTD" // 2: serialized RealTimeDataRequest _realTimeRequestSocket.SendMoreFrame(string.Empty); _realTimeRequestSocket.SendMoreFrame(MessageType.RTDRequest); using (var ms = new MemoryStream()) { _realTimeRequestSocket.SendFrame(MyUtils.ProtoBufSerialize(request, ms)); } } } return(request.RequestID); }
private bool CreateRealTimeRequestSocket() { realTimeRequestSocket = new DealerSocket(realTimeRequestConnectionString); realTimeRequestSocket.Options.Identity = Encoding.UTF8.GetBytes(name); // Start off by sending a ping to make sure everything is regular byte[] reply = new byte[] { }; try { realTimeRequestSocket.SendMoreFrame(string.Empty) .SendFrame(BitConverter.GetBytes((byte)DataRequestMessageType.Ping)); if (realTimeRequestSocket.TryReceiveFrameBytes(TimeSpan.FromSeconds(1), out reply)) { realTimeRequestSocket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(50), out reply); } } catch { Disconnect(); } if (reply?.Length > 0) { DataRequestMessageType type = (DataRequestMessageType)BitConverter.ToInt16(reply, 0); if (type != DataRequestMessageType.Pong) { try { realTimeRequestSocket.Disconnect(realTimeRequestConnectionString); } finally { realTimeRequestSocket.Close(); realTimeRequestSocket = null; } RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server.")); return(true); } } else { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server.")); } realTimeRequestSocket.ReceiveReady += RealTimeRequestSocketReceiveReady; return(false); }
public void DropMultipartMessages() { using (var peer1 = new PeerSocket("@inproc://peertopeer3")) using (var dealer = new DealerSocket(">inproc://peertopeer3")) { dealer.SendMoreFrame("This should be dropped"); dealer.SendFrame("This as well"); dealer.SendFrame("Hello"); peer1.ReceiveFrameBytes(); var message = peer1.ReceiveFrameString(); Assert.Equal("Hello", message); } }
/// <summary> /// Pushes data to local storage. /// </summary> public void PushData(DataAdditionRequest request) { if (request == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Request cannot be null.")); return; } if (request.Instrument?.ID == null) { RaiseEvent(Error, this, new ErrorArgs(-1, "Instrument must be set and have an ID.")); return; } if (!ClientRunningAndIsConnected) { RaiseEvent(Error, this, new ErrorArgs(-1, "Could not push historical data to local storage - not connected.")); return; } lock (historicalDataSocketLock) { if (historicalDataSocket != null) { historicalDataSocket.SendMoreFrame(BitConverter.GetBytes((byte)DataRequestMessageType.HistPush)); using (MemoryStream ms = new MemoryStream()) { historicalDataSocket.SendFrame(MyUtils.ProtoBufSerialize(request, ms)); } } } }
public void Handover() { using (var router = new RouterSocket()) using (var dealer1 = new DealerSocket()) { router.Options.RouterHandover = true; router.Bind("inproc://127.0.0.1:5555"); dealer1.Options.Identity = Encoding.ASCII.GetBytes("ID"); dealer1.Connect("inproc://127.0.0.1:5555"); dealer1.SendMoreFrame("Hello").SendFrame("World"); var identity = router.ReceiveFrameString(); Assert.AreEqual("ID", identity); using (var dealer2 = new DealerSocket()) { dealer2.Options.Identity = Encoding.ASCII.GetBytes("ID"); dealer2.Connect("inproc://127.0.0.1:5555"); // We have new peer which should take over, however we are still reading a message var message = router.ReceiveFrameString(); Assert.AreEqual("Hello", message); message = router.ReceiveFrameString(); Assert.AreEqual("World", message); dealer2.SendMoreFrame("Hello").SendFrame("World"); identity = router.ReceiveFrameString(); Assert.AreEqual("ID", identity); message = router.ReceiveFrameString(); Assert.AreEqual("Hello", message); message = router.ReceiveFrameString(); Assert.AreEqual("World", message); } } }
/// <summary> /// Tries to connect to the QDMS server. /// </summary> public void Connect() { if (Connected) { return; } lock (_realTimeRequestSocketLock) { _realTimeRequestSocket = new DealerSocket(_realTimeRequestConnectionString); _realTimeRequestSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); // Start off by sending a ping to make sure everything is regular var reply = string.Empty; try { _realTimeRequestSocket.SendMoreFrame(string.Empty).SendFrame(MessageType.Ping); if (_realTimeRequestSocket.TryReceiveFrameString(TimeSpan.FromSeconds(1), out reply)) { _realTimeRequestSocket.TryReceiveFrameString(TimeSpan.FromMilliseconds(50), out reply); } } catch { Disconnect(); } if (reply == null || !reply.Equals(MessageType.Pong, StringComparison.InvariantCultureIgnoreCase)) { try { _realTimeRequestSocket.Disconnect(_realTimeRequestConnectionString); } finally { _realTimeRequestSocket.Close(); _realTimeRequestSocket = null; } RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server.")); return; } _realTimeRequestSocket.ReceiveReady += RealTimeRequestSocketReceiveReady; } lock (_realTimeDataSocketLock) { _realTimeDataSocket = new SubscriberSocket(_realTimeDataConnectionString); _realTimeDataSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _realTimeDataSocket.ReceiveReady += RealTimeDataSocketReceiveReady; } lock (_historicalDataSocketLock) { _historicalDataSocket = new DealerSocket(_historicalDataConnectionString); _historicalDataSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _historicalDataSocket.ReceiveReady += HistoricalDataSocketReceiveReady; } _lastHeartBeat = DateTime.Now; _heartBeatTimer = new NetMQTimer(TimeSpan.FromSeconds(HeartBeatPeriodInSeconds)); _heartBeatTimer.Elapsed += HeartBeatTimerElapsed; _historicalDataTimer = new NetMQTimer(TimeSpan.FromSeconds(HistoricalDataRequestsPeriodInSeconds)); _historicalDataTimer.Elapsed += HistoricalDataTimerElapsed; _poller = new NetMQPoller { _realTimeRequestSocket, _realTimeDataSocket, _historicalDataSocket, _heartBeatTimer, _historicalDataTimer }; _poller.RunAsync(); }