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 HasInTest() { using (var server = new RouterSocket()) using (var client = new DealerSocket()) { var port = server.BindRandomPort("tcp://*"); // no one sent a message so it should be false Assert.IsFalse(server.HasIn); client.Connect("tcp://localhost:" + port); // wait for the client to connect Thread.Sleep(100); // now we have one client connected but didn't send a message yet Assert.IsFalse(server.HasIn); client.SendFrame("1"); // wait for the message to arrive Thread.Sleep(100); // the has in should indicate a message is ready Assert.IsTrue(server.HasIn); server.SkipFrame(); // identity string message = server.ReceiveFrameString(); Assert.AreEqual(message, "1"); // we read the message, it should false again Assert.IsFalse(server.HasIn); } }
public void RouterDealerMessaging() { using (var server = new RouterSocket()) using (var client = new DealerSocket()) { int port = server.BindRandomPort("tcp://127.0.0.1"); client.Connect("tcp://127.0.0.1:" + port); var clientOutgoingMessage = new NetMQMessage(); clientOutgoingMessage.Append("Hello"); client.SendMultipartMessage(clientOutgoingMessage); NetMQMessage serverIncomingMessage = server.ReceiveMultipartMessage(); // number of frames should be one because first message should be identity of client Assert.Equal(2, serverIncomingMessage.FrameCount); Assert.Equal("Hello", serverIncomingMessage[1].ConvertToString()); var serverOutgoingMessage = new NetMQMessage(); // first adding the identity serverOutgoingMessage.Append(serverIncomingMessage[0]); serverOutgoingMessage.Append("World"); server.SendMultipartMessage(serverOutgoingMessage); var incomingClientMessage = client.ReceiveMultipartMessage(); Assert.Equal(1, incomingClientMessage.FrameCount); Assert.Equal("World", incomingClientMessage[0].ConvertToString()); } }
public void CheckReceiveAgainException() { using (var router = new RouterSocket()) { router.BindRandomPort("tcp://127.0.0.1"); router.Receive(SendReceiveOptions.DontWait); } }
public void SimpleTimer() { // TODO it is not really clear what this test is actually testing -- maybe split it into a few smaller tests using (var router = new RouterSocket()) using (var dealer = new DealerSocket()) using (var poller = new Poller(router) { PollTimeout = TestPollTimeoutMillis }) { int port = router.BindRandomPort("tcp://127.0.0.1"); dealer.Connect("tcp://127.0.0.1:" + port); bool messageArrived = false; router.ReceiveReady += (s, e) => { Assert.IsFalse(messageArrived); router.SkipFrame(); router.SkipFrame(); messageArrived = true; }; bool timerTriggered = false; int count = 0; const int timerIntervalMillis = 100; var timer = new NetMQTimer(TimeSpan.FromMilliseconds(timerIntervalMillis)); timer.Elapsed += (a, s) => { // the timer should jump before the message Assert.IsFalse(messageArrived); timerTriggered = true; timer.Enable = false; count++; }; poller.AddTimer(timer); poller.PollTillCancelledNonBlocking(); Thread.Sleep(150); dealer.SendFrame("hello"); Thread.Sleep(300); poller.CancelAndJoin(); Assert.IsTrue(messageArrived); Assert.IsTrue(timerTriggered); Assert.AreEqual(1, count); } }
public void AddSocketDuringWork() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var poller = new NetMQPoller { router1 }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); bool router1Arrived = false; bool router2Arrived = false; var signal1 = new ManualResetEvent(false); var signal2 = new ManualResetEvent(false); router1.ReceiveReady += (s, e) => { router1.SkipFrame(); router1.SkipFrame(); router1Arrived = true; poller.Add(router2); signal1.Set(); }; router2.ReceiveReady += (s, e) => { router2.SkipFrame(); router2.SkipFrame(); router2Arrived = true; signal2.Set(); }; poller.RunAsync(); dealer1.SendFrame("1"); Assert.IsTrue(signal1.WaitOne(300)); // Give the pollset a chance to update Thread.Sleep(50); dealer2.SendFrame("2"); Assert.IsTrue(signal1.WaitOne(300)); poller.Stop(); Assert.IsTrue(router1Arrived); Assert.IsTrue(router2Arrived); } }
public void Mandatory() { using (var router = new RouterSocket()) { router.Options.RouterMandatory = true; router.BindRandomPort("tcp://*"); Assert.Throws <HostUnreachableException>(() => router.SendMoreFrame("UNKNOWN").SendFrame("Hello")); } }
public void CheckTryReceive() { using (var router = new RouterSocket()) { router.BindRandomPort("tcp://127.0.0.1"); var msg = new Msg(); msg.InitEmpty(); Assert.IsFalse(router.TryReceive(ref msg, TimeSpan.Zero)); } }
public void ReceiveReadyDot35Bug() { // In .NET 3.5, we saw an issue where ReceiveReady would be raised every second despite nothing being received using (var server = new RouterSocket()) { server.BindRandomPort("tcp://127.0.0.1"); server.ReceiveReady += (s, e) => Assert.Fail("Should not receive"); Assert.IsFalse(server.Poll(TimeSpan.FromMilliseconds(1500))); } }
public void AddSocketDuringWork() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var poller = new Poller(router1) { PollTimeout = TestPollTimeoutMillis }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); bool router1Arrived = false; bool router2Arrived = false; var signal1 = new ManualResetEvent(false); var signal2 = new ManualResetEvent(false); router1.ReceiveReady += (s, e) => { router1.SkipFrame(); router1.SkipFrame(); router1Arrived = true; poller.AddSocket(router2); signal1.Set(); }; router2.ReceiveReady += (s, e) => { router2.SkipFrame(); router2.SkipFrame(); router2Arrived = true; signal2.Set(); }; poller.PollTillCancelledNonBlocking(); dealer1.SendFrame("1"); Assert.IsTrue(signal1.WaitOne(300)); dealer2.SendFrame("2"); Assert.IsTrue(signal1.WaitOne(300)); poller.CancelAndJoin(); Assert.IsTrue(router1Arrived); Assert.IsTrue(router2Arrived); } }
public LRUBroker(string frontConnectionString, string backendConnectionString) { _frontend = new RouterSocket(frontConnectionString); _backend = new RouterSocket(); BackendPort = _backend.BindRandomPort(backendConnectionString.TrimStart(new char[] { '@' })); _frontend.ReceiveReady += FrontendReceiveReady; _backend.ReceiveReady += BackendReceiveReady; _poller.Add(_frontend); _poller.Add(_backend); }
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(); } }
public void RemoveTimer() { using (var router = new RouterSocket()) using (var dealer = new DealerSocket()) using (var poller = new Poller(router) { PollTimeout = TestPollTimeoutMillis }) { int port = router.BindRandomPort("tcp://127.0.0.1"); dealer.Connect("tcp://127.0.0.1:" + port); bool timerTriggered = false; var timer = new NetMQTimer(TimeSpan.FromMilliseconds(100)); timer.Elapsed += (a, s) => { timerTriggered = true; }; // The timer will fire after 100ms poller.AddTimer(timer); bool messageArrived = false; router.ReceiveReady += (s, e) => { router.SkipFrame(); router.SkipFrame(); messageArrived = true; // Remove timer poller.RemoveTimer(timer); }; poller.PollTillCancelledNonBlocking(); Thread.Sleep(20); dealer.SendFrame("hello"); Thread.Sleep(300); poller.CancelAndJoin(); Assert.IsTrue(messageArrived); Assert.IsFalse(timerTriggered); } }
public void CheckTrySendSucceeds() { 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); Thread.Sleep(100); Assert.IsTrue(dealer.TrySendFrame("1")); } }
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 } }
/// <summary> /// Start node. Use beacon discovery. /// We get a new _inbox (RouterSocket listening to peers) and a new _beacon on every Start(). /// Also a new _port and _endpoint at each Start() /// </summary> /// <param name="interfaceName">A particular name, or null </param> /// <returns>true if OK, false if not possible or if already running</returns> private bool Start(string interfaceName) { if (_isRunning) { return(false); } // Create the _beacon and bind the _inbox _beacon = new NetMQBeacon(); _beacon.ReceiveReady += OnBeaconReady; _beacon.Configure(_beaconPort, interfaceName); // Bind our router port to the host. Our hostName is provided by the beacon. var address = $"tcp://{_beacon.BoundTo}"; _inbox = new RouterSocket(); _inbox.ReceiveReady += OnInboxReady; _port = _inbox.BindRandomPort(address); if (_port <= 0) { // Die on bad interface or port exhaustion return(false); } _endpoint = $"{address}:{_port}"; _loggerDelegate?.Invoke($"The _inbox RouterSocket for node {_uuid.ToShortString6()} is bound to _endpoint={_endpoint}"); _loggerDelegate?.Invoke($"Starting {_name} {_uuid.ToShortString6()}. Publishing beacon on port {_port}. Adding _beacon and _inbox to poller."); // Start polling on _inbox and _beacon _poller.Add(_inbox); _poller.Add(_beacon); // Set broadcast/listen beacon PublishBeacon(_port); _beacon.Subscribe("ZRE"); _isRunning = true; return(true); }
protected override void _SocketBuilder() { reportLog("Starting Router name " + Name, LogType.INFO, section); socket = new RouterSocket(); socket.Options.TcpKeepalive = true; socket.Options.TcpKeepaliveIdle = TimeSpan.FromMilliseconds(100); socket.Options.TcpKeepaliveInterval = TimeSpan.FromMilliseconds(100); local_port = socket.BindRandomPort("tcp://*"); monitor = new NetMQMonitor(socket, "inproc://router" + Net2.GetAddressIndex(), SocketEvents.All); monitor.Disconnected += Monitor_Disconnected; monitor.Accepted += Monitor_Connected; monitor.Timeout = TimeSpan.FromMilliseconds(100); task = monitor.StartAsync(); setState(Net2State.STARTED); start_time = GetTime().sec; reportLog("Router is ready on " + local_port, LogType.INFO, section); }
public void AddSocketAfterRemoving() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var router3 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var dealer3 = new DealerSocket()) using (var poller = new NetMQPoller { router1, router2 }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); int port3 = router3.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); dealer3.Connect("tcp://127.0.0.1:" + port3); bool router1Arrived = false; bool router2Arrived = false; bool router3Arrived = false; var signal1 = new ManualResetEvent(false); var signal2 = new ManualResetEvent(false); var signal3 = new ManualResetEvent(false); router1.ReceiveReady += (s, e) => { router1Arrived = true; router1.SkipFrame(); router1.SkipFrame(); poller.Remove(router1); signal1.Set(); }; router2.ReceiveReady += (s, e) => { router2Arrived = true; router2.SkipFrame(); router2.SkipFrame(); poller.Add(router3); signal2.Set(); }; router3.ReceiveReady += (s, e) => { router3Arrived = true; router3.SkipFrame(); router3.SkipFrame(); signal3.Set(); }; poller.RunAsync(); dealer1.SendFrame("1"); Assert.IsTrue(signal1.WaitOne(300)); dealer2.SendFrame("2"); Assert.IsTrue(signal2.WaitOne(300)); dealer3.SendFrame("3"); Assert.IsTrue(signal3.WaitOne(300)); poller.Stop(); Assert.IsTrue(router1Arrived); Assert.IsTrue(router2Arrived); Assert.IsTrue(router3Arrived); } }
public void AddTwoSocketAfterRemoving() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var router3 = new RouterSocket()) using (var router4 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var dealer3 = new DealerSocket()) using (var dealer4 = new DealerSocket()) using (var poller = new NetMQPoller { router1, router2 }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); int port3 = router3.BindRandomPort("tcp://127.0.0.1"); int port4 = router4.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); dealer3.Connect("tcp://127.0.0.1:" + port3); dealer4.Connect("tcp://127.0.0.1:" + port4); int router1Arrived = 0; int router2Arrived = 0; bool router3Arrived = false; bool router4Arrived = false; var signal1 = new ManualResetEvent(false); var signal2 = new ManualResetEvent(false); var signal3 = new ManualResetEvent(false); var signal4 = new ManualResetEvent(false); router1.ReceiveReady += (s, e) => { router1Arrived++; router1.SkipFrame(); // identity router1.SkipFrame(); // message poller.Remove(router1); signal1.Set(); }; router2.ReceiveReady += (s, e) => { router2Arrived++; router2.SkipFrame(); // identity router2.SkipFrame(); // message if (router2Arrived == 1) { poller.Add(router3); poller.Add(router4); signal2.Set(); } }; router3.ReceiveReady += (s, e) => { router3.SkipFrame(); // identity router3.SkipFrame(); // message router3Arrived = true; signal3.Set(); }; router4.ReceiveReady += (s, e) => { router4.SkipFrame(); // identity router4.SkipFrame(); // message router4Arrived = true; signal4.Set(); }; poller.RunAsync(); dealer1.SendFrame("1"); Assert.IsTrue(signal1.WaitOne(300)); dealer2.SendFrame("2"); Assert.IsTrue(signal2.WaitOne(300)); dealer3.SendFrame("3"); dealer4.SendFrame("4"); dealer2.SendFrame("2"); dealer1.SendFrame("1"); Assert.IsTrue(signal3.WaitOne(300)); Assert.IsTrue(signal4.WaitOne(300)); poller.Stop(); router1.SkipFrame(); bool more; Assert.AreEqual("1", router1.ReceiveFrameString(out more)); Assert.IsFalse(more); Assert.AreEqual(1, router1Arrived); Assert.AreEqual(2, router2Arrived); Assert.IsTrue(router3Arrived); Assert.IsTrue(router4Arrived); } }
public void RemoveSocket() { using (var router1 = new RouterSocket()) using (var router2 = new RouterSocket()) using (var router3 = new RouterSocket()) using (var dealer1 = new DealerSocket()) using (var dealer2 = new DealerSocket()) using (var dealer3 = new DealerSocket()) using (var poller = new NetMQPoller { router1, router2, router3 }) { int port1 = router1.BindRandomPort("tcp://127.0.0.1"); int port2 = router2.BindRandomPort("tcp://127.0.0.1"); int port3 = router3.BindRandomPort("tcp://127.0.0.1"); dealer1.Connect("tcp://127.0.0.1:" + port1); dealer2.Connect("tcp://127.0.0.1:" + port2); dealer3.Connect("tcp://127.0.0.1:" + port3); bool first = true; router1.ReceiveReady += (s, e) => { if (!first) { Assert.Fail("This should not happen because we cancelled the socket"); } first = false; // identity e.Socket.SkipFrame(); bool more; Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more)); Assert.False(more); // cancelling the socket poller.Remove(e.Socket); // remove self }; router2.ReceiveReady += (s, e) => { // identity byte[] identity = e.Socket.ReceiveFrameBytes(); // message e.Socket.SkipFrame(); e.Socket.SendMoreFrame(identity); e.Socket.SendFrame("2"); }; router3.ReceiveReady += (s, e) => { // identity byte[] identity = e.Socket.ReceiveFrameBytes(); // message e.Socket.SkipFrame(); e.Socket.SendMoreFrame(identity).SendFrame("3"); }; Task pollerTask = Task.Factory.StartNew(poller.Run); // Send three messages. Only the first will be processed, as then handler removes // the socket from the poller. dealer1.SendFrame("Hello"); dealer1.SendFrame("Hello2"); dealer1.SendFrame("Hello3"); // making sure the socket defined before the one cancelled still works dealer2.SendFrame("1"); Assert.AreEqual("2", dealer2.ReceiveFrameString()); // making sure the socket defined after the one cancelled still works dealer3.SendFrame("1"); Assert.AreEqual("3", dealer3.ReceiveFrameString()); poller.Stop(); Assert.IsTrue(pollerTask.IsCompleted); } }