Пример #1
0
        public void DesktopClient_WillAccept_SinglePartMessages()
        {
            Queue<Packet> packets = new Queue<Packet>();
            // Mock the the message handler processor
            var messageHandler = new Mock<IDesktopMessageHandler>();
            messageHandler.Setup(mh => mh.DisplayLogPacket(It.IsAny<LogPacket>())).Callback(
                (LogPacket packet) =>
                {
                    packets.Enqueue(packet);
                }).Verifiable();

            // Setup Client
            StartClientThread(messageHandler.Object);

            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(JsonConvert.SerializeObject(new LogPacket()));

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(500);

            Assert.IsTrue(packets.Count == 1);
        }
        public void DesktopClient_WillNotAccept_MoreThanOnePartMessages()
        {
            Queue <Packet> packets = new Queue <Packet>();
            // Mock the the message handler processor
            var messageHandler = new Mock <IDesktopMessageHandler>();

            messageHandler.Setup(mh => mh.DisplayLogPacket(It.IsAny <LogPacket>()))
            .Callback((LogPacket packet) =>
            {
                packets.Enqueue(packet);
            })
            .Verifiable();

            // Setup Client
            StartClientThread(messageHandler.Object);

            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(typeof(LogPacket).Name);
                message.Append(JsonConvert.SerializeObject(new LogPacket()));
                message.Append("hello!");

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(500);

            Assert.IsTrue(packets.Count == 0);
        }
Пример #3
0
 private void WorkerThread()
 {
     while (true)
     {
         NetMQMessage message = _toSend.Take();
         _pushSocket.SendMultipartMessage(message);
     }
 }
Пример #4
0
        /// <summary>
        /// Send a message to the _server using ZeroMQ
        /// </summary>
        /// <param name="packet">Packet to transmit</param>
        public void Transmit(Packet packet)
        {
            var payload = JsonConvert.SerializeObject(packet, _orderEventJsonConverter);

            var message = new NetMQMessage();

            message.Append(payload);

            _server.SendMultipartMessage(message);
        }
 public void PushOrdersInfo(object objectToSend, PushMessageType messageType)
 {
     using (var ms = new MemoryStream())
     {
         var messageToSend = new NetMQMessage(2);
         messageToSend.Append(BitConverter.GetBytes((byte)messageType));
         messageToSend.Append(MyUtils.ProtoBufSerialize(objectToSend, ms));
         pushSocket.SendMultipartMessage(messageToSend);
     }
 }
Пример #6
0
        public void testconnect()
        {
            var sessionid = Guid.NewGuid().ToString();
            var version   = (byte)1;

            var msg = new NetMQMessage();

            msg.Append(new byte[] { version });                                                                           //版本号
            msg.Append("action");                                                                                         //动作
            msg.Append(sessionid);                                                                                        //sessionid
            msg.Append(new byte[] { 61, 2, 3 });                                                                          //客户端密钥
            msg.AppendEmptyFrame();
            msg.Append(Encoding.UTF8.GetBytes($"c=newseed&r=false&o={++O}&uid=xxxxxx&t=123123&token={GetToken(Token)}")); //信息头
            msg.Append(Encoding.UTF8.GetBytes("seed=123123"));                                                            //信息体

            PushSocket.SendMultipartMessage(msg);

            NetMQMessage receiveMsg = null;

            PullSocket.TryReceiveMultipartMessage(new TimeSpan(0, 0, 0, 20), ref receiveMsg);

            Console.WriteLine(receiveMsg);

            Assert.IsNotNull(receiveMsg);

            Assert.AreEqual(version, receiveMsg[0].Buffer[0]);
            Assert.AreEqual("action", receiveMsg[1].ConvertToString());
            Assert.AreEqual(sessionid, receiveMsg[2].ConvertToString());
            Assert.GreaterOrEqual(receiveMsg[3].BufferSize, 1);

            var headers = receiveMsg[5].ConvertToString().ToQueryDic();

            Assert.AreEqual(Convert.ToString((int)StatusCode.Ok), headers["code"]);
        }
Пример #7
0
        public virtual void SetUp()
        {
            PullSocket = new PullSocket(config.PullSocketString);
            PullSocket.ReceiveReady += ProcessTaskPullSocket;

            PushSocket = new PushSocket(config.PushsocketString);
            Poller     = new NetMQPoller()
            {
                PullSocket, PushSocket, MessageQueue
            };

            MessageQueue.ReceiveReady += (sender, args) => { PushSocket.SendMultipartMessage(args.Queue.Dequeue()); };
        }
Пример #8
0
        public void test()
        {
            Thread.Sleep(1000);
            var msg = new NetMQMessage();

            msg.Append(new byte[] { 1 });                 //版本号
            msg.Append("action");                         //动作
            msg.Append("sessionid");                      //sessionid
            msg.Append(new byte[] { 14, 2, 3 });          //客户端密钥
            msg.Append(new byte[] { 61, 2, 3 });          //服务端密钥
            msg.Append(Encoding.UTF8.GetBytes("c=test")); //信息头
            msg.Append(Encoding.UTF8.GetBytes("c=test")); //信息体

            PushSocket.SendMultipartMessage(msg);
        }
        public void DesktopClient_WillShutDown_WhenStopServerIsCalled_FromAnotherThread()
        {
            Queue <Packet> packets        = new Queue <Packet>();
            var            messageHandler = new Mock <IDesktopMessageHandler>();

            messageHandler.Setup(mh => mh.DisplayLogPacket(It.IsAny <LogPacket>()))
            .Callback((LogPacket packet) =>
            {
                packets.Enqueue(packet);
            })
            .Verifiable();

            StartClientThread(messageHandler.Object);

            // Try to send a message when the DesktopClient is listening
            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(JsonConvert.SerializeObject(new LogPacket()));

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(2000);
            Assert.IsTrue(packets.Count == 1);

            // Shut down the server
            _desktopMessageHandler.StopServer();
            Thread.Sleep(2000);

            // Try to send another message when the DesktopClient is not listening
            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(JsonConvert.SerializeObject(new LogPacket()));

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(2000);
            // Nothing should have made it so the count should be the same
            Assert.IsTrue(packets.Count == 1);
        }
Пример #10
0
        public virtual void Setup(INetManager netManager)
        {
            if (string.IsNullOrEmpty(PushsocketString))
            {
                throw new ArgumentNullException(nameof(PushsocketString));
            }
            if (string.IsNullOrEmpty(PullSocketString))
            {
                throw new ArgumentNullException(nameof(PullSocketString));
            }
            if (netManager == null)
            {
                throw new ArgumentNullException(nameof(netManager));
            }

            if (!string.IsNullOrEmpty(XPubSocketString) && !string.IsNullOrEmpty(XSubSocketString))
            {
                var PublisherSocket = new XPublisherSocket(XPubSocketString);

                var SubscriberSocket = new XSubscriberSocket(XSubSocketString);

                GlobalServerProxy = new Proxy(SubscriberSocket, PublisherSocket);
            }

            sender         = new PushSocket(PushsocketString);
            responseSocket = new PullSocket(PullSocketString);

            Poller = new NetMQPoller()
            {
                sender,
                responseSocket,
                PushQueue
            };

            NetSend = netManager;

            PushQueue.ReceiveReady += (o, args) =>
            {
                var msg = PushQueue.Dequeue();
                sender.SendMultipartMessage(msg);
            };

            responseSocket.ReceiveReady += ProcessResponse;

            IsSetuped = true;
        }
Пример #11
0
        public void DesktopClient_WillShutDown_WhenStopServerIsCalled_FromAnotherThread()
        {
            Queue<Packet> packets = new Queue<Packet>();
            var messageHandler = new Mock<IDesktopMessageHandler>();
            messageHandler.Setup(mh => mh.DisplayLogPacket(It.IsAny<LogPacket>()))
            .Callback((LogPacket packet) =>
            {
                packets.Enqueue(packet);
            })
            .Verifiable();

            StartClientThread(messageHandler.Object);

            // Try to send a message when the DesktopClient is listening
            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(JsonConvert.SerializeObject(new LogPacket()));

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(2000);
            Assert.IsTrue(packets.Count == 1);

            // Shut down the server
            _desktopMessageHandler.StopServer();
            Thread.Sleep(2000);

            // Try to send another message when the DesktopClient is not listening
            using (PushSocket server = new PushSocket("@tcp://*:" + _port))
            {
                var message = new NetMQMessage();

                message.Append(JsonConvert.SerializeObject(new LogPacket()));

                server.SendMultipartMessage(message);
            }

            // Give NetMQ time to send the message
            Thread.Sleep(2000);
            // Nothing should have made it so the count should be the same
            Assert.IsTrue(packets.Count == 1);
        }