コード例 #1
0
        public void NativeSocket()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (var streamServer = context.CreateStreamSocket())
                {
                    streamServer.Bind("tcp://*:5557");

                    using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                    {
                        socket.Connect("127.0.0.1", 5557);

                        byte[] buffer = new byte[] { 1 };
                        socket.Send(buffer);

                        byte[] identity = streamServer.Receive();
                        byte[] message  = streamServer.Receive();

                        Assert.AreEqual(buffer[0], message[0]);

                        ManualResetEvent socketSignal = new ManualResetEvent(false);

                        Poller poller = new Poller();
                        poller.AddPollInSocket(socket, s =>
                        {
                            socket.Receive(buffer);

                            socketSignal.Set();

                            // removing the socket
                            poller.RemovePollInSocket(socket);
                        });

                        poller.PollTillCancelledNonBlocking();

                        // no message is waiting for the socket so it should fail
                        Assert.IsFalse(socketSignal.WaitOne(200));

                        // sending a message back to the socket
                        streamServer.SendMore(identity).Send("a");

                        Assert.IsTrue(socketSignal.WaitOne(200));

                        socketSignal.Reset();

                        // sending a message back to the socket
                        streamServer.SendMore(identity).Send("a");

                        // we remove the native socket so it should fail
                        Assert.IsFalse(socketSignal.WaitOne(200));

                        poller.Stop(true);
                    }
                }
            }
        }
コード例 #2
0
        public void NativeSocket()
        {
            using (var context = NetMQContext.Create())
                using (var streamServer = context.CreateStreamSocket())
                    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                    {
                        int port = streamServer.BindRandomPort("tcp://*");

                        socket.Connect("127.0.0.1", port);

                        var buffer = new byte[] { 1 };
                        socket.Send(buffer);

                        byte[] identity = streamServer.ReceiveFrameBytes();
                        byte[] message  = streamServer.ReceiveFrameBytes();

                        Assert.AreEqual(buffer[0], message[0]);

                        var socketSignal = new ManualResetEvent(false);

                        var poller = new Poller {
                            PollTimeout = TestPollTimeoutMillis
                        };
                        poller.AddPollInSocket(socket, s =>
                        {
                            socket.Receive(buffer);

                            socketSignal.Set();

                            // removing the socket
                            poller.RemovePollInSocket(socket);
                        });

                        poller.PollTillCancelledNonBlocking();

                        // no message is waiting for the socket so it should fail
                        Assert.IsFalse(socketSignal.WaitOne(100));

                        // sending a message back to the socket
                        streamServer.SendMore(identity).Send("a");

                        Assert.IsTrue(socketSignal.WaitOne(100));

                        socketSignal.Reset();

                        // sending a message back to the socket
                        streamServer.SendMore(identity).Send("a");

                        // we remove the native socket so it should fail
                        Assert.IsFalse(socketSignal.WaitOne(100));

                        poller.CancelAndJoin();
                    }
        }
コード例 #3
0
ファイル: PollerTests.cs プロジェクト: romanros/netmq
        public void NativeSocket()
        {
            using (var context = NetMQContext.Create())
            using (var streamServer = context.CreateStreamSocket())
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                int port = streamServer.BindRandomPort("tcp://*");

                socket.Connect("127.0.0.1", port);

                var buffer = new byte[] { 1 };
                socket.Send(buffer);

                byte[] identity = streamServer.ReceiveFrameBytes();
                byte[] message = streamServer.ReceiveFrameBytes();

                Assert.AreEqual(buffer[0], message[0]);

                var socketSignal = new ManualResetEvent(false);

                var poller = new Poller { PollTimeout = TestPollTimeoutMillis };
                poller.AddPollInSocket(socket, s =>
                {
                    socket.Receive(buffer);

                    socketSignal.Set();

                    // removing the socket
                    poller.RemovePollInSocket(socket);
                });

                poller.PollTillCancelledNonBlocking();

                // no message is waiting for the socket so it should fail
                Assert.IsFalse(socketSignal.WaitOne(100));

                // sending a message back to the socket
                streamServer.SendMoreFrame(identity).SendFrame("a");

                Assert.IsTrue(socketSignal.WaitOne(100));

                socketSignal.Reset();

                // sending a message back to the socket
                streamServer.SendMoreFrame(identity).SendFrame("a");

                // we remove the native socket so it should fail
                Assert.IsFalse(socketSignal.WaitOne(100));

                poller.CancelAndJoin();
            }
        }