protected override void ProducerAction() { using (NetMQContext ctx = NetMQContext.Create()) { using (RouterSocket socket = ctx.CreateRouterSocket()) { socket.Bind(Address); while (true) { NetMQMessage message = socket.ReceiveMessage(); if (!IsEmpty) { Queue.Add(message); } } } } }
private void RouterAction() { using (NetMQContext ctx = NetMQContext.Create()) { using (RouterSocket socket = ctx.CreateRouterSocket()) { socket.Bind(Address); while (true) { NetMQMessage message = socket.ReceiveMessage(); string topic = message.Last.ConvertToString(); byte[] snapshot = _snapshotFactory(topic); var response = new NetMQMessage(); response.Append(message.First); response.AppendEmptyFrame(); response.Append(snapshot); socket.SendMessage(response); } } } }
public void Router_Dealer_Demonstrating_Messages_From_Subscribers_To_Publisher() { // NOTES // 1. Use ThreadLocal<DealerSocket> where each thread has // its own client DealerSocket to talk to server // 2. Each thread can send using it own socket // 3. Each thread socket is added to poller const int delay = 500; // millis var clientSocketPerThread = new ThreadLocal <DealerSocket>(); using (var server = new RouterSocket("@tcp://127.0.0.1:5556")) { using (var poller = new NetMQPoller()) { // Start some threads, each with its own DealerSocket // to talk to the server socket. Creates lots of sockets, // but no nasty race conditions no shared state, each // thread has its own socket, happy days. for (var i = 0; i < 4; i++) { Task.Factory.StartNew(state => { DealerSocket client = null; if (!clientSocketPerThread.IsValueCreated) { client = new DealerSocket(); client.Options.Identity = Encoding.Unicode.GetBytes(state.ToString()); client.Connect("tcp://127.0.0.1:5556"); client.ReceiveReady += Client_ReceiveReady; clientSocketPerThread.Value = client; poller.Add(client); } else { client = clientSocketPerThread.Value; } while (true) { var messageToServer = new NetMQMessage(); messageToServer.AppendEmptyFrame(); messageToServer.Append(state.ToString()); Console.WriteLine("======================================"); Console.WriteLine(" OUTGOING MESSAGE TO SERVER "); Console.WriteLine("======================================"); PrintFrames("Client Sending", messageToServer); client.SendMultipartMessage(messageToServer); Thread.Sleep(delay); } }, string.Format("client {0}", i), TaskCreationOptions.LongRunning); } // start the poller poller.RunAsync(); // server loop for (var i = 0; i < 6; i++) { var clientMessage = server.ReceiveMessage(); Console.WriteLine("======================================"); Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT "); Console.WriteLine("======================================"); PrintFrames("Server receiving", clientMessage); if (clientMessage.FrameCount == 3) { var clientAddress = clientMessage[0]; var clientOriginalMessage = clientMessage[2].ConvertToString(); var response = string.Format("{0} back from server {1}", clientOriginalMessage, DateTime.Now.ToLongTimeString()); var messageToClient = new NetMQMessage(); messageToClient.Append(clientAddress); messageToClient.AppendEmptyFrame(); messageToClient.Append(response); server.SendMultipartMessage(messageToClient); } } } } }