private void StatelessListenerLoop() { OrLog(LogLevel.Verbose, "StatelessListenerLoop start tcp://" + Room.ListenAddrV4 + ":" + Room.StatelessDealPort); AsyncIO.ForceDotNet.Force(); using (var dealerSocket = new DealerSocket()) { dealerSocket.Options.ReceiveHighWatermark = 1; dealerSocket.Connect("tcp://" + Room.ListenAddrV4 + ":" + Room.StatelessDealPort); while (!statelessListenerCancelled) { if (!IsMessageQueueRunning) { continue; } byte[] message; if (statelessQueue.TryDequeue(out message)) { dealerSocket.SendFrame(message, false); } Thread.Yield(); } dealerSocket.Close(); } NetMQConfig.Cleanup(); OrLog(LogLevel.Verbose, "StatelessListenerLoop end"); }
public void Shutdown() { if (_socket != null) { try { _socket.Close(); } catch (Exception) { } try { _socket.Dispose(); } catch (Exception) { } } _socket = null; _finished = true; _worker.Join(); NetMQConfig.Cleanup(true); }
public override Task Stop() { _cancel.Cancel(); _poller.Stop(); _client.Close(); _client.Dispose(); _isConnected.OnCompleted(); _isConnected.Dispose(); return(Task.CompletedTask); }
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 OnSessionEnded(object sender, EventArgs e) { mPoller.Stop(); if (mDealerSocket != null) { mDealerSocket.Close(); } if (mVisualDealerSockets != null && mVisualDealerSockets.Count > 0) { foreach (DealerSocket socket in mVisualDealerSockets) { socket.Close(); } } }
private Task Cleanup() { _cancel.Cancel(); if (null != _getMarketState) { _getMarketState.Close(); _getMarketState.Dispose(); } if (null != _getMarketUpdates) { _getMarketUpdates.Close(); _getMarketUpdates.Dispose(); } return(Task.CompletedTask); }
private void StopRealTimeRequestSocket() { lock (realTimeRequestSocketLock) { if (realTimeRequestSocket != null) { try { realTimeRequestSocket.Disconnect(realTimeRequestConnectionString); } finally { realTimeRequestSocket.ReceiveReady -= RealTimeRequestSocketReceiveReady; realTimeRequestSocket.Close(); realTimeRequestSocket = null; } } } }
private void StopHistoricalDataSocket() { lock (historicalDataSocket) { if (historicalDataSocket != null) { try { historicalDataSocket.Disconnect(historicalDataConnectionString); } finally { historicalDataSocket.ReceiveReady -= HistoricalDataSocketReceiveReady; historicalDataSocket.Close(); historicalDataSocket = null; } } } }
/// <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(); }
public void Stop() { _proxy.Stop(); _router?.Close(); _dealer?.Close(); }
/// <summary> /// Disconnects from the server. /// </summary> public void Disconnect(bool cancelStreams = true) { if (!PollerRunning) { return; } // Start by canceling all active real time streams if (cancelStreams) { while (RealTimeDataStreams.Count > 0) { CancelRealTimeData(RealTimeDataStreams.First().Instrument); } } _poller?.Stop(); _poller?.Dispose(); lock (_realTimeRequestSocketLock) { if (_realTimeRequestSocket != null) { try { _realTimeRequestSocket.Disconnect(_realTimeRequestConnectionString); } finally { _realTimeRequestSocket.ReceiveReady -= RealTimeRequestSocketReceiveReady; _realTimeRequestSocket.Close(); _realTimeRequestSocket = null; } } } lock (_realTimeDataSocketLock) { if (_realTimeDataSocket != null) { try { _realTimeDataSocket.Disconnect(_realTimeDataConnectionString); } finally { _realTimeDataSocket.ReceiveReady -= RealTimeDataSocketReceiveReady; _realTimeDataSocket.Close(); _realTimeDataSocket = null; } } } lock (_historicalDataSocket) { if (_historicalDataSocket != null) { try { _historicalDataSocket.Disconnect(_historicalDataConnectionString); } finally { _historicalDataSocket.ReceiveReady -= HistoricalDataSocketReceiveReady; _historicalDataSocket.Close(); _historicalDataSocket = null; } } } _poller = null; }
/// <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(); }
/// <summary> /// 停止 /// </summary> public void Stop() { frontEnd.Close(); backEnd.Close(); proxy.Stop(); }