public void RawSocket() { using (var router = new RouterSocket()) using (var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { router.Options.RouterRawSocket = true; var port = router.BindRandomPort("tcp://127.0.0.1"); clientSocket.Connect("127.0.0.1", port); clientSocket.NoDelay = true; byte[] clientMessage = Encoding.ASCII.GetBytes("HelloRaw"); int bytesSent = clientSocket.Send(clientMessage); Assert.Greater(bytesSent, 0); byte[] id = router.ReceiveFrameBytes(); byte[] message = router.ReceiveFrameBytes(); router.SendMoreFrame(id).SendMoreFrame(message); // SendMore option is ignored var buffer = new byte[16]; int bytesRead = clientSocket.Receive(buffer); Assert.Greater(bytesRead, 0); Assert.AreEqual(Encoding.ASCII.GetString(buffer, 0, bytesRead), "HelloRaw"); } }
public void Messages_From_Router_To_Dealer_With_Subscription() { Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n"); var maxMessage = 5; var cd = new CountdownEvent(maxMessage); string endpoint; using (var publisher = new RouterSocket()) using (var subscriber = new DealerSocket()) using (var poller = new NetMQPoller { subscriber }) { publisher.Bind("tcp://127.0.0.1:0"); endpoint = publisher.Options.LastEndpoint; subscriber.Connect(endpoint); subscriber.ReceiveReady += (sender, e) => { var strs = e.Socket.ReceiveMultipartStrings(); foreach (var str in strs) { Console.WriteLine("Subscribe: " + str); } cd.Signal(); }; var clientId = Encoding.Unicode.GetBytes("ClientIdTheIsLongerThen32BytesForSureAbsolutelySure"); subscriber.Options.Identity = clientId; const string request = "Ping"; // Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it // sends at least one message which makes it necessary to open the connection. I believe this is a // low-level feature of the TCP/IP transport. subscriber.SendFrame(request); // Ping. var serverId = publisher.ReceiveFrameBytes(); //Assert.AreEqual(request, publisher.ReceiveFrameString()); for (var i = 0; i < maxMessage; i++) { var msg = string.Format("[message: {0}]", i); Console.Write("Publish: {0}\n", msg); publisher.SendMoreFrame(serverId).SendFrame(msg); //publisher.SendMoreFrame("").SendFrame(msg); } poller.RunAsync(); if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called. { Assert.Fail("Timed out, this test should complete in less than 10 seconds."); } } }
private static Message GetNextMessage() { var message = new Message(); // There may be additional ZMQ identities attached; read until the delimiter <IDS|MSG>" // and store them in message.identifiers // http://ipython.org/ipython-doc/dev/development/messaging.html#the-wire-protocol var delimAsBytes = Encoding.ASCII.GetBytes(Constants.Delimiter); while (true) { var delim = ShellRouterSocket.ReceiveFrameBytes(); if (delim.SequenceEqual(delimAsBytes)) { break; } message.Identifiers.Add(delim); } // Getting Hmac message.HMac = ShellRouterSocket.ReceiveFrameString(); Log.Info(message.HMac); // Getting Header var header = ShellRouterSocket.ReceiveFrameString(); Log.Info(header); message.Header = JsonSerializer.Deserialize <Header>(header); // Getting parent header var parentHeader = ShellRouterSocket.ReceiveFrameString(); Log.Info(parentHeader); message.ParentHeader = JsonSerializer.Deserialize <Header>(parentHeader); // Getting metadata var metadata = ShellRouterSocket.ReceiveFrameString(); Log.Info(metadata); message.Metadata = JsonSerializer.Deserialize <Dictionary <string, object> >(metadata); // Getting content var content = ShellRouterSocket.ReceiveFrameString(); Log.Info(content); message.Content = content; return(message); }
public void Messages_From_Dealer_To_Router() { var maxMessage = 5; var cd = new CountdownEvent(maxMessage); Console.Write("Test sending message from subscribers (dealer) to publisher(router).\n"); using (var publisher = new RouterSocket()) using (var subscriber = new DealerSocket()) using (var poller = new NetMQPoller { subscriber }) { var port = publisher.BindRandomPort("tcp://*"); subscriber.Connect("tcp://127.0.0.1:" + port); subscriber.ReceiveReady += (sender, e) => { var strs = e.Socket.ReceiveMultipartStrings(); foreach (var str in strs) { Console.WriteLine(str); } cd.Signal(); }; var clientId = Encoding.Unicode.GetBytes("ClientId"); subscriber.Options.Identity = clientId; const string request = "GET /\r\n"; const string response = "HTTP/1.0 200 OK\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Hello, World!"; subscriber.SendFrame(request); var serverId = publisher.ReceiveFrameBytes(); Assert.AreEqual(request, publisher.ReceiveFrameString()); for (var i = 0; i < maxMessage; i++) { publisher.SendMoreFrame(serverId).SendFrame(response); } poller.RunAsync(); if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called. { Assert.Fail("Timed out, this test should complete in less than 10 seconds."); } } }
public void TwoMessagesFromRouterToDealer() { using (var server = new RouterSocket()) using (var client = new DealerSocket()) using (var poller = new NetMQPoller { client }) { var port = server.BindRandomPort("tcp://*"); client.Connect("tcp://127.0.0.1:" + port); var cnt = 0; client.ReceiveReady += (sender, e) => { var strs = e.Socket.ReceiveMultipartStrings(); foreach (var str in strs) { Console.WriteLine(str); } cnt++; if (cnt == 2) { poller.Stop(); } }; byte[] clientId = Encoding.Unicode.GetBytes("ClientId"); client.Options.Identity = clientId; const string request = "GET /\r\n"; const string response = "HTTP/1.0 200 OK\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Hello, World!"; client.SendFrame(request); byte[] serverId = server.ReceiveFrameBytes(); Assert.AreEqual(request, server.ReceiveFrameString()); // two messages in a row, not frames server.SendMoreFrame(serverId).SendFrame(response); server.SendMoreFrame(serverId).SendFrame(response); poller.Run(); } }