/// <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); }
public void CheckSendAgainException() { using (var router = new RouterSocket()) using (var dealer = new DealerSocket()) { var port = router.BindRandomPort("tcp://127.0.0.1"); router.Options.Linger = TimeSpan.Zero; dealer.Options.SendHighWatermark = 1; dealer.Options.Linger = TimeSpan.Zero; dealer.Connect("tcp://127.0.0.1:" + port); #pragma warning disable 618 dealer.Send("1", dontWait: true, sendMore: false); dealer.Send("2", dontWait: true, sendMore: false); #pragma warning restore 618 } }
static void Main(string[] args) { Console.WriteLine(args.Length); if (args.Length < 3) { Console.WriteLine("Requeries 3 arguments: WORLD_SOCKET CRANE_SOCKET SIM_ID"); return; } var worldSocketAddr = args[0]; var craneSocketAddr = args[1]; var identity = new UTF8Encoding().GetBytes(args[2]); OptimizerType optType; if (args.Length > 3) { optType = OptimizerType.ModelBased; } else { optType = OptimizerType.RuleBased; } Console.WriteLine(optType); using (var worldSocket = new DealerSocket()) // bind using (var craneSocket = new DealerSocket()) { worldSocket.Options.Identity = identity; craneSocket.Options.Identity = identity; worldSocket.Connect(worldSocketAddr); Console.WriteLine("Connected world"); craneSocket.Connect(craneSocketAddr); Console.WriteLine("Connected crane"); while (true) { var world = World.Parser.ParseFrom(worldSocket.ReceiveFrameBytes()); var schedule = OptimizeCraneShedule(world, optType); if (schedule != null) { schedule.SequenceNr = world.Crane.Schedule.SequenceNr; Console.WriteLine(schedule); var data = schedule.ToByteArray(); Console.WriteLine(data.Length); var msg = new Msg(); msg.InitGC(data, data.Length); craneSocket.Send(ref msg, false); } } } }
/// <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(); }