当前位置: 首页>>代码示例>>C#>>正文


C# RouterSocket.BindRandomPort方法代码示例

本文整理汇总了C#中NetMQ.Sockets.RouterSocket.BindRandomPort方法的典型用法代码示例。如果您正苦于以下问题:C# RouterSocket.BindRandomPort方法的具体用法?C# RouterSocket.BindRandomPort怎么用?C# RouterSocket.BindRandomPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetMQ.Sockets.RouterSocket的用法示例。


在下文中一共展示了RouterSocket.BindRandomPort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CheckReceiveAgainException

        public void CheckReceiveAgainException()
        {
            using (var router = new RouterSocket())
            {
                router.BindRandomPort("tcp://127.0.0.1");

                router.Receive(SendReceiveOptions.DontWait);
            }
        }
开发者ID:hdxhan,项目名称:netmq,代码行数:9,代码来源:SocketTests.cs

示例2: Mandatory

        public void Mandatory()
        {
            using (var router = new RouterSocket())
            {
                router.Options.RouterMandatory = true;
                router.BindRandomPort("tcp://*");

                Assert.Throws<HostUnreachableException>(() => router.SendMoreFrame("UNKNOWN").SendFrame("Hello"));
            }
        }
开发者ID:NetMQ,项目名称:NetMQ3-x,代码行数:10,代码来源:RouterTests.cs

示例3: ReceiveReadyDot35Bug

        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)));
            }
        }
开发者ID:NetMQ,项目名称:NetMQ3-x,代码行数:11,代码来源:RouterTests.cs

示例4: CheckTryReceive

        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));
            }
        }
开发者ID:GrabCAD,项目名称:netmq,代码行数:11,代码来源:SocketTests.cs

示例5: CheckTrySendSucceeds

        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"));
            }
        }
开发者ID:GrabCAD,项目名称:netmq,代码行数:17,代码来源:SocketTests.cs

示例6: CheckSendAgainException

        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
            }
        }
开发者ID:hdxhan,项目名称:netmq,代码行数:18,代码来源:SocketTests.cs

示例7: TwoMessagesFromRouterToDealer

        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();
            }
        }
开发者ID:NetMQ,项目名称:NetMQ3-x,代码行数:44,代码来源:RouterTests.cs

示例8: RawSocket

        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");
            }
        }
开发者ID:hdxhan,项目名称:netmq,代码行数:29,代码来源:SocketTests.cs

示例9: AddSocketDuringWork

        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));

                dealer2.SendFrame("2");
                Assert.IsTrue(signal2.WaitOne(300));

                poller.Stop();

                Assert.IsTrue(router1Arrived);
                Assert.IsTrue(router2Arrived);
            }
        }
开发者ID:cjkao,项目名称:netmq,代码行数:51,代码来源:NetMQPollerTest.cs

示例10: SimpleTimer

        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 NetMQPoller { router })
            {
                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 += (s, a) =>
                {
                    // the timer should jump before the message
                    Assert.IsFalse(messageArrived);
                    timerTriggered = true;
                    timer.Enable = false;
                    count++;
                };
                poller.Add(timer);

                poller.RunAsync();

                Thread.Sleep(150);

                dealer.SendFrame("hello");

                Thread.Sleep(300);

                poller.Stop();

                Assert.IsTrue(messageArrived);
                Assert.IsTrue(timerTriggered);
                Assert.AreEqual(1, count);
            }
        }
开发者ID:cjkao,项目名称:netmq,代码行数:54,代码来源:NetMQPollerTest.cs

示例11: RemoveSocket

        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);
            }
        }
开发者ID:cjkao,项目名称:netmq,代码行数:80,代码来源:NetMQPollerTest.cs

示例12: AddTwoSocketAfterRemoving

        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);
            }
        }
开发者ID:cjkao,项目名称:netmq,代码行数:97,代码来源:NetMQPollerTest.cs

示例13: Start

        /// <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;
        }
开发者ID:NetMQ,项目名称:Zyre,代码行数:45,代码来源:ZyreNode.cs

示例14: AddSocketDuringWork

        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);
            }
        }
开发者ID:hdxhan,项目名称:netmq,代码行数:50,代码来源:PollerTests.cs

示例15: Messages_From_Dealer_To_Router

		public void Messages_From_Dealer_To_Router()
		{
			int maxMessage = 5;
			CountdownEvent 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();
				};
				byte[] 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);

				byte[] serverId = publisher.ReceiveFrameBytes();
				Assert.AreEqual(request, publisher.ReceiveFrameString());

				for (int 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.");
				}
			}
		}
开发者ID:dalebrubaker,项目名称:NetMQ.ReactiveExtensions,代码行数:50,代码来源:RouterDealerTest2.cs


注:本文中的NetMQ.Sockets.RouterSocket.BindRandomPort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。