Esempio n. 1
0
        public void Run(int serverPort)
        {
            client = new MessageClient(
                     new FramedClient(new SocketClient()),
                     new ProtoBufStacksSerializer(),
                     new ClientMessageHandler());

            client.Connect(new IPEndPoint(IPAddress.Loopback, serverPort))
                  .ContinueWith(t =>
                  {
                      Console.WriteLine("Querying for temperature in London, Warsaw, Madrid");
                      client.Send(1, new TemperatureRequest { City = "London" });
                      client.Send(1, new TemperatureRequest { City = "Warsaw" });
                      client.Send(1, new TemperatureRequest { City = "Madrid" });
                  });
        }
Esempio n. 2
0
        public void Run(int serverPort)
        {
            client = new MessageClient(
                     new FramedClient(new SocketClient(useIPv6: true)),
                     new ProtoBufStacksSerializer(),
                     new ClientMessageHandler());

            client.PreLoadTypesFromAssemblyOfType<TemperatureResponse>();

            client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, serverPort))
                  .Subscribe(_ =>
                  {
                      Console.WriteLine("Querying for temperature in London, Warsaw, Madrid");
                      client.Send(new TemperatureRequest { City = "London" });
                      client.Send(new TemperatureRequest { City = "Warsaw" });
                      client.Send(new TemperatureRequest { City = "Madrid" });
                  });
        }
Esempio n. 3
0
            public void Sending_packet_should_send_serialized_data_with_proper_header()
            {
                var c = new MessageClient(framedClient, serializer.Object, messageHandler.Object);

                serializer.Setup(s => s.Serialize(It.IsAny<TestData>(), It.IsAny<MemoryStream>()))
                          .Callback((TestData d, MemoryStream ms) =>
                          {
                              ms.Write(new byte[] { 0, 1, 2, 3, 4 }, 0, 5);
                          });

                rawClient.Setup(rc => rc.Send(It.IsAny<byte[]>())).Callback((byte[] b) =>
                {
                    var length = BitConverter.ToInt32(b, 0);
                    var messageId = BitConverter.ToInt32(b, 4);

                    Assert.Equal(4 + 4 + 5, length);
                    Assert.Equal(4 + 4 + 5, b.Length);
                    Assert.Equal(3, messageId);
                    Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, new ArraySegment<byte>(b, 8, 5));
                });

                c.Send(CreateSampleTestData());
            }
Esempio n. 4
0
            public void Sending_message_should_throw_data_exception_if_sent_message_has_no_MessageId()
            {
                var c = new MessageClient(framedClient, serializer.Object, messageHandler.Object);

                Assert.Throws(typeof(InvalidDataException), () =>
                    {
                        c.Send(new TestDataWithoutMessageId());
                    });
            }
Esempio n. 5
0
            public void Sending_message_should_succeed_when_packet_types_are_preloaded()
            {
                var c = new MessageClient(framedClient, serializer.Object, messageHandler.Object);

                c.PreLoadTypesFromAssemblyOfType<TestData>();

                serializer.Setup(s => s.Serialize(It.IsAny<TestData>(), It.IsAny<MemoryStream>()))
                          .Callback((TestData d, MemoryStream ms) =>
                          {
                              ms.Write(new byte[] { 0, 1, 2, 3, 4 }, 0, 5);
                          });

                rawClient.Setup(rc => rc.Send(It.IsAny<byte[]>())).Callback((byte[] b) =>
                {
                    var length = BitConverter.ToInt32(b, 0);
                    var messageId = BitConverter.ToInt32(b, 4);

                    Assert.Equal(4 + 4 + 5, length);
                    Assert.Equal(4 + 4 + 5, b.Length);
                    Assert.Equal(3, messageId);
                    Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, new ArraySegment<byte>(b, 8, 5));
                });

                c.Send(CreateSampleTestData());
            }
Esempio n. 6
0
            public void Sending_message_should_succeed_if_message_was_declared_imperatively()
            {
                var c = new MessageClient(framedClient, serializer.Object, new Mock<TestDataWithoutMessageIdHandler>().Object,
                    r => r.RegisterMessage<TestDataWithoutMessageId>(3));

                serializer.Setup(s => s.Serialize(It.IsAny<TestDataWithoutMessageId>(), It.IsAny<MemoryStream>()))
                         .Callback((TestDataWithoutMessageId d, MemoryStream ms) =>
                         {
                             ms.Write(new byte[] { 0, 1, 2, 3, 4 }, 0, 5);
                         });

                rawClient.Setup(rc => rc.Send(It.IsAny<byte[]>())).Callback((byte[] b) =>
                {
                    var length = BitConverter.ToInt32(b, 0);
                    var messageId = BitConverter.ToInt32(b, 4);

                    Assert.Equal(4 + 4 + 5, length);
                    Assert.Equal(4 + 4 + 5, b.Length);
                    Assert.Equal(3, messageId);
                    Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, new ArraySegment<byte>(b, 8, 5));
                });

                c.Send(new TestDataWithoutMessageId());
            }