Esempio n. 1
0
        public void SendAndReceive()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");

                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    proxy.Stop();
                }
        }
Esempio n. 2
0
        public void StartAndStopStateValidation()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    // Send a message through to ensure the proxy has started
                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");
                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    Assert.Throws <InvalidOperationException>(proxy.Start);
                    Assert.Throws <InvalidOperationException>(proxy.Start);
                    Assert.Throws <InvalidOperationException>(proxy.Start);

                    proxy.Stop(); // blocks until stopped

                    Assert.Throws <InvalidOperationException>(proxy.Stop);
                }
        }
Esempio n. 3
0
 /// <summary>
 /// 多路接收
 /// </summary>
 /// <param name="address"></param>
 public void  Rsp(string address)
 {
     while (true)
     {
         TCPUserToken   tCPUserToken   = null;
         ResponseSocket responseSocket = null;
         if (token_Pool.Pop(out tCPUserToken))
         {
             responseSocket = tCPUserToken.Socket;
             responseSocket.Connect(address);
         }
         else
         {
             responseSocket = new ResponseSocket();
             responseSocket.Connect(address);
             tCPUserToken = new TCPUserToken(token_Pool)
             {
                 Socket = responseSocket, Address = address
             };
         }
         byte[] buf = responseSocket.ReceiveFrameBytes();
         tCPUserToken.Data = buf;
         queue.Add(tCPUserToken);
     }
 }
Esempio n. 4
0
        public void HelloWorld()
        {
            var          timer    = new Timer(60000);
            const string endpoint = "tcp://PC_ip:4505";

            //10.151.111.166       12.207.170.27
            timer.Start();
            timer.Elapsed += (sender, args) =>
            {
                timer.Stop();
                Cancel = true;
            };

            using (var replierSocket = new ResponseSocket())
            {
                replierSocket.Connect(endpoint);
                Thread.Sleep(2000);
                while (true)
                {
                    var replyFromRequester = replierSocket.ReceiveFrameString();
                    Debug.WriteLine("Got reply " + replyFromRequester);
                    replierSocket.SendFrame("Response socket say hello");
                }
            }
        }
Esempio n. 5
0
 public AsyncWorker(int id, NetMQContext context, string address) : base(id)
 {
     _responseSocket = context.CreateResponseSocket();
     _responseSocket.Options.ReceiveHighWatermark = 2;
     _responseSocket.Options.SendHighWatermark = 2;
     _responseSocket.Options.Identity = BitConverter.GetBytes(id);
     _responseSocket.Connect(address);
 }
Esempio n. 6
0
        private static void Main()
        {
            using (var worker = new ResponseSocket())
            {
                worker.Connect(WorkerEndpoint);

                while (true)
                {
                    var msg = worker.ReceiveMultipartMessage();
                    Console.WriteLine("Processing Message {0}", msg.Last.ConvertToString());

                    Thread.Sleep(500);

                    worker.SendFrame(msg.Last.ConvertToString());
                }
            }
        }
Esempio n. 7
0
        public void SeparateControlSocketsObservedMessages()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                    using (var controlInPush = new PushSocket())
                        using (var controlInPull = new PullSocket())
                            using (var controlOutPush = new PushSocket())
                                using (var controlOutPull = new PullSocket())
                                {
                                    front.Bind("inproc://frontend");
                                    back.Bind("inproc://backend");

                                    controlInPush.Bind("inproc://controlIn");
                                    controlInPull.Connect("inproc://controlIn");
                                    controlOutPush.Bind("inproc://controlOut");
                                    controlOutPull.Connect("inproc://controlOut");

                                    var proxy = new Proxy(front, back, controlInPush, controlOutPush);
                                    Task.Factory.StartNew(proxy.Start);

                                    using (var client = new RequestSocket())
                                        using (var server = new ResponseSocket())
                                        {
                                            client.Connect("inproc://frontend");
                                            server.Connect("inproc://backend");

                                            client.SendFrame("hello");
                                            Assert.AreEqual("hello", server.ReceiveFrameString());
                                            server.SendFrame("reply");
                                            Assert.AreEqual("reply", client.ReceiveFrameString());
                                        }

                                    Assert.IsNotNull(controlInPull.ReceiveFrameBytes()); // receive identity
                                    Assert.IsEmpty(controlInPull.ReceiveFrameString());  // pull terminator
                                    Assert.AreEqual("hello", controlInPull.ReceiveFrameString());

                                    Assert.IsNotNull(controlOutPull.ReceiveFrameBytes()); // receive identity
                                    Assert.IsEmpty(controlOutPull.ReceiveFrameString());  // pull terminator
                                    Assert.AreEqual("reply", controlOutPull.ReceiveFrameString());

                                    proxy.Stop();
                                }
        }
Esempio n. 8
0
        public void StartAgainAfterStop()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    // Send a message through to ensure the proxy has started
                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");
                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    proxy.Stop(); // blocks until stopped

                    // Start it again
                    Task.Factory.StartNew(proxy.Start);

                    // Send a message through to ensure the proxy has started
                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");
                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());
                        }

                    proxy.Stop(); // blocks until stopped
                }
        }
Esempio n. 9
0
 private static void WorkerRoutine()
 {
     try
     {
         using (ResponseSocket rep = new ResponseSocket())
         {
             rep.Options.Identity = Encoding.Unicode.GetBytes(Guid.NewGuid().ToString());
             rep.Connect("tcp://localhost:5556");
             //rep.Connect("inproc://workers");
             rep.ReceiveReady += RepOnReceiveReady;
             while (!s_token.IsCancellationRequested)
             {
                 rep.Poll(TimeSpan.FromMilliseconds(100));
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception on WorkerRoutine: {0}", ex.Message);
         throw;
     }
 }
Esempio n. 10
0
 private static void WorkerRoutine()
 {
     try
     {
         using (ResponseSocket rep = new ResponseSocket())
         {
             rep.Options.Identity = Encoding.Unicode.GetBytes(Guid.NewGuid().ToString());
             rep.Connect("tcp://localhost:5556");
             //rep.Connect("inproc://workers");
             rep.ReceiveReady += RepOnReceiveReady;
             while (!s_token.IsCancellationRequested)
             {
                 rep.Poll(TimeSpan.FromMilliseconds(100));
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception on WorkerRoutine: {0}", ex.Message);
         throw;
     }
 }
Esempio n. 11
0
        public void TestProxySendAndReceiveWithExternalPoller()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                    using (var poller = new NetMQPoller {
                        front, back
                    })
                    {
                        front.Bind("inproc://frontend");
                        back.Bind("inproc://backend");

                        var proxy = new Proxy(front, back, null, poller);

                        poller.RunAsync();

                        proxy.Start();

                        using (var client = new RequestSocket())
                            using (var server = new ResponseSocket())
                            {
                                client.Connect("inproc://frontend");
                                server.Connect("inproc://backend");

                                client.SendFrame("hello");
                                Assert.AreEqual("hello", server.ReceiveFrameString());
                                server.SendFrame("reply");
                                Assert.AreEqual("reply", client.ReceiveFrameString());

                                // Now stop the external poller
                                poller.Stop();

                                client.SendFrame("anyone there?");

                                // Should no longer receive any messages
                                Assert.IsFalse(server.TrySkipFrame(TimeSpan.FromMilliseconds(50)));
                            }
                    }
        }
Esempio n. 12
0
        public void StoppingProxyDisengagesFunctionality()
        {
            using (var front = new RouterSocket())
                using (var back = new DealerSocket())
                {
                    front.Bind("inproc://frontend");
                    back.Bind("inproc://backend");

                    var proxy = new Proxy(front, back);
                    Task.Factory.StartNew(proxy.Start);

                    // Send a message through to ensure the proxy has started
                    using (var client = new RequestSocket())
                        using (var server = new ResponseSocket())
                        {
                            client.Connect("inproc://frontend");
                            server.Connect("inproc://backend");
                            client.SendFrame("hello");
                            Assert.AreEqual("hello", server.ReceiveFrameString());
                            server.SendFrame("reply");
                            Assert.AreEqual("reply", client.ReceiveFrameString());

                            proxy.Stop(); // blocks until stopped

                            using (var poller = new NetMQPoller {
                                front, back
                            })
                            {
                                poller.RunAsync();

                                client.SendFrame("anyone there?");

                                // Should no longer receive any messages
                                Assert.IsFalse(server.TrySkipFrame(TimeSpan.FromMilliseconds(50)));
                            }
                        }
                }
        }
Esempio n. 13
0
        private void BackgroundTask(object state)
        {
            var            cancellationToken = (CancellationToken)state;
            ResponseSocket socket            = _ctx.CreateResponseSocket();

            try
            {
                switch (_cxMode)
                {
                case ConnectionMode.Connect:
                    socket.Connect(_address);
                    break;

                case ConnectionMode.Bind:
                    socket.Bind(_address);
                    break;
                }

                byte[] receiveBuffer;
                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        receiveBuffer = socket.Receive();
                        if (receiveBuffer == null)
                        {
                            continue; // NetMQ > 3.3.0.11
                        }
                    }
                    catch (AgainException)
                    {
                        continue; // NetMQ = 3.3.0.11
                    }

                    #region Always send a reply...

                    try
                    {
                        Thread.Sleep(500); // simulates processing...
                        socket.Send(string.Format("Reply ({0})", Encoding.UTF8.GetString(receiveBuffer)));
                    }
                    catch (TerminatingException)
                    {
                        try
                        {
                            socket.Send("Exit...");
                        }
                        catch
                        {
                        }
                        throw;
                    }

                    #endregion
                }
            }
            catch (TerminatingException)
            {
            }
            finally
            {
                try
                {
                    socket.Dispose();
                }
                catch (NetMQException)
                {
                }
            }
        }