public void Run() { var subAddress = _settings.SubAddress; var pullAddress = _settings.PullAddress; var topic = _settings.SubTopic; using (var subscriber = new SubscriberSocket()) using (var puller = new PullSocket()) { subscriber.Connect(subAddress); subscriber.Subscribe(topic); _logger.LogInformation($"Sub socket on {subAddress} topic {topic}"); puller.Connect(pullAddress); _logger.LogInformation($"Pull socket on {pullAddress}"); using (var poller = new NetMQPoller { subscriber, puller }) { subscriber.ReceiveReady += SubReceiveReady; puller.ReceiveReady += PullReceiveReady; poller.Run(); } } }
public void Connect(string endpoint) { if (_owner.bypassPrediction || _zmqPredictedMotion != null) { return; } _zmqPredictedMotion = new PullSocket(); _zmqPredictedMotion.Connect(endpoint); _msgRecv = new NetMQ.Msg(); _msgRecv.InitPool(8 + 4 * 4); }
public override void OnRegistered(byte inDeviceID, string arguments) { base.OnRegistered(inDeviceID, arguments); Arguments args = JsonUtility.FromJson <Arguments>(arguments); Debug.Assert(string.IsNullOrEmpty(args.PredictedMotionOutputEndpoint) == false); Debug.Assert(_zmqPredictedMotion == null); _zmqPredictedMotion = new PullSocket(); _zmqPredictedMotion.Connect(args.PredictedMotionOutputEndpoint); _msgRecv = new NetMQ.Msg(); _msgRecv.InitPool(8 + 4 * 4); }
public void Start() { using (_puller = new PullSocket()) { _puller.Connect(_dealerEndpoint); while (!_cancel.IsCancellationRequested) { var messageBytes = _puller.ReceiveFrameBytes(); var message = JsonConvert.DeserializeObject <Message>(Encoding.UTF8.GetString(messageBytes)); Received.Add(message); } } }
public void SeparateControlSocketsObservedMessages() { using (var front = new RouterSocket()) using (var back = new DealerSocket()) using (var controlInPush = new PushSocket()) using (var controlInPull = new PullSocket()) using (var controlOutPush = new PushSocket()) using (var controlOutPull = new PullSocket()) { front.Bind("inproc://frontend"); back.Bind("inproc://backend"); controlInPush.Bind("inproc://controlIn"); controlInPull.Connect("inproc://controlIn"); controlOutPush.Bind("inproc://controlOut"); controlOutPull.Connect("inproc://controlOut"); var proxy = new Proxy(front, back, controlInPush, controlOutPush); Task.Factory.StartNew(proxy.Start); using (var client = new RequestSocket()) using (var server = new ResponseSocket()) { client.Connect("inproc://frontend"); server.Connect("inproc://backend"); client.SendFrame("hello"); Assert.AreEqual("hello", server.ReceiveFrameString()); server.SendFrame("reply"); Assert.AreEqual("reply", client.ReceiveFrameString()); } Assert.IsNotNull(controlInPull.ReceiveFrameBytes()); // receive identity Assert.IsEmpty(controlInPull.ReceiveFrameString()); // pull terminator Assert.AreEqual("hello", controlInPull.ReceiveFrameString()); Assert.IsNotNull(controlOutPull.ReceiveFrameBytes()); // receive identity Assert.IsEmpty(controlOutPull.ReceiveFrameString()); // pull terminator Assert.AreEqual("reply", controlOutPull.ReceiveFrameString()); proxy.Stop(); } }
static void Main(string[] args) { Console.WriteLine("====== WORKER ======"); using (var pull = new PullSocket()) using (var sink = new DealerSocket()) { pull.Connect("tcp://127.0.0.1:8888"); sink.Connect("tcp://127.0.0.1:7777"); while (true) { var workload = pull.ReceiveFrameString(); Thread.Sleep(int.Parse(workload)); Console.WriteLine("Sending to Sink"); sink.SendFrame(string.Empty); } } }
static void Main(string[] args) { // Task Sink // Bindd PULL socket to tcp://localhost:5558 // Collects results from workers via that socket Console.WriteLine("====== SINK ======"); //socket to receive messages on using (var sink = new PullSocket()) using (var receiver = new PullSocket()) { sink.Connect("tcp://localhost:5557"); receiver.Connect("tcp://localhost:5559"); //wait for start of batch (see Ventilator.csproj Program.cs) var startOfBatchTrigger = sink.ReceiveFrameString(); Console.WriteLine("Seen start of batch {0}", startOfBatchTrigger); //Start our clock now var watch = Stopwatch.StartNew(); int count = 0; for (int taskNumber = 0; taskNumber < 100; taskNumber++) { var workerDoneTrigger = receiver.ReceiveFrameString(); if (taskNumber % 10 == 0) { Console.Write(":"); } else { Console.Write("."); } count++; //Console.WriteLine("Total frame receive {0}", count); } watch.Stop(); //Calculate and report duration of batch Console.WriteLine(); Console.WriteLine("Total elapsed time {0} msec", watch.ElapsedMilliseconds); Console.ReadLine(); } }
static void Main(string[] args) { // Task Worker // Connects PULL socket to tcp://localhost:5557 // collects workload for socket from Ventilator via that socket // Connects PUSH socket to tcp://localhost:5558 // Sends results to Sink via that socket Console.WriteLine("====== WORKER ======"); using (var receiver = new PullSocket()) using (var sender = new PushSocket()) { receiver.Connect("tcp://localhost:5558"); sender.Bind("tcp://localhost:5559"); //process tasks forever int count = 0; while (true) { count++; //workload from the vetilator is a simple delay //to simulate some work being done, see //Ventilator.csproj Proram.cs for the workload sent //In real life some more meaningful work would be done string workload = receiver.ReceiveFrameString(); //simulate some work being done // Thread.Sleep(int.Parse(workload)); //send results to sink, sink just needs to know worker //is done, message content is not important, just the presence of //a message means worker is done. //See Sink.csproj Proram.cs Console.WriteLine("receive frame {1} Counts {0}", count, workload); sender.SendFrame(string.Empty); } } }
public int FanOutClientService() { if (ModFanOutClients != null) { foreach (Point p in ModFanOutClients) { if (FanoutClientRM.ContainsKey(p.name)) { Logging.logger.Warn("The fanout clisnt is exist " + p.name); } else { FanoutClientRun rr = new FanoutClientRun(); PullSocket r = new PullSocket(); string e = "tcp://" + p.ip + ":" + p.port; try { r.Connect(e); rr.name = p.name; rr.point = p; rr.pullsock = r; rr.Running = true; rr.Working = false; } catch (Exception err) { Logging.logger.Error(ModName + " connect push socket failed " + e + " " + err.Message); throw (err); } FanoutClientRM.Add(p.name, rr); } } } return(0); }
public void It_Should_Send_Message_To_Socket() { const string address = "tcp://127.0.0.1:12345"; using (var socket = new PullSocket()) using (AutoMock mock = AutoMock.GetLoose()) { mock.Provide <IOptions <MessageClientOptions> >(new OptionsWrapper <MessageClientOptions>(new MessageClientOptions { Address = address })); socket.Connect(address); var service = mock.Create <MessageClientImpl>(); service.ScheduleDelivery("A some message 1"); service.ScheduleDelivery("A some message 2"); service.ScheduleDelivery("A some message 3"); Assert.Equal("A some message 1", socket.ReceiveFrameString()); Assert.Equal("A some message 2", socket.ReceiveFrameString()); Assert.Equal("A some message 3", socket.ReceiveFrameString()); } }
public void connect() { string name = AppDomain.CurrentDomain.FriendlyName; endpoint = ConfigurationManager.AppSettings["broker"]; using (responder = new PullSocket()) { responder.Connect(endpoint); while (this.running) { try { NetMQMessage msgList = responder.ReceiveMultipartMessage(); msgList.Pop(); foreach (var msg in msgList) { string result = msg.ConvertToString(); int size = msg.MessageSize; if (size <= 5) { continue; } try { var strMsg = JsonConvert.DeserializeObject <TaskMessage>(result); if (this.onReceive != null) { this.onReceive(strMsg); } } catch (Exception ex) { if (onError != null) { this.onError(String.Format("消息:{0},反序列化异常:{1}", result, ex.Message)); } } } } catch (NetMQ.TerminatingException te) { if (onError != null) { this.onError(String.Format("连接终止异常:" + te.Message)); } break; } catch (Exception ex) { if (onError != null) { this.onError(String.Format("异常:" + ex.Message)); } break; } } } }