Ejemplo n.º 1
0
 private void ClientConnected(SocketClient client)
 {
     this.client = new MessageClient(
                     new FramedClient(client),
                     new ProtoBufStacksSerializer(),
                     new ServerMessageHandler(service));
 }
Ejemplo n.º 2
0
        private void ClientConnected(SocketClient client)
        {
            this.client = new MessageClient(
                            new FramedClient(client),
                            new ProtoBufStacksSerializer(),
                            new ServerMessageHandler(service));

            this.client.PreLoadTypesFromAssemblyOfType<TemperatureChanged>();
        }
Ejemplo n.º 3
0
            public void Receiving_packet_should_be_deserialized_properly()
            {
                serializer.Setup(s => s.Deserialize<TestData>(It.IsAny<MemoryStream>())).Returns(new TestData());

                var c = new MessageClient(framedClient, serializer.Object, messageHandler.Object);

                rawClientReceived.OnNext(new ArraySegment<byte>(new byte[] { 12, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0 }));

                messageHandler.Verify(m => m.HandleTestData(It.IsAny<IMessageClient>(), It.IsAny<TestData>()), Times.Once());
            }
Ejemplo n.º 4
0
            public void Receiving_packet_should_be_deserialized_properly()
            {
                serializer.Setup(s => s.CreateDeserializer<TestData>()).Returns((MemoryStream ms) => new TestData());

                var c = new MessageClient(framedClient, serializer.Object, messageHandler.Object);

                rawClient.Raise(r => r.Received += delegate { }, new ArraySegment<byte>(new byte[] { 12, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0 }));

                messageHandler.Verify(m => m.HandleTestData(It.IsAny<IMessageClient>(), It.IsAny<TestData>()), Times.Once());
            }
Ejemplo n.º 5
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" });
                  });
        }
Ejemplo n.º 6
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" });
                  });
        }
Ejemplo n.º 7
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 typeCode = BitConverter.ToInt32(b, 4);

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

                c.Send(3, CreateSampleTestData());
            }
Ejemplo n.º 8
0
 public void Sending_message_should_fail_if_message_handler_has_more_messages_than_registered_imperatively()
 {
     Assert.Throws(typeof(InvalidOperationException), () =>
         {
             var c = new MessageClient(framedClient, serializer.Object,
                             new Mock<BrokenTestDataWithoutMessageIdHandler>().Object,
                             r => r.RegisterMessage<TestDataWithoutMessageId>(2));
         });
 }
Ejemplo n.º 9
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());
                    });
            }
Ejemplo n.º 10
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());
            }
Ejemplo n.º 11
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());
            }