Example #1
0
        public async Task ShouldTriggerConnectionSentWhenSentSomeBytes()
        {
            NetworkDirection       direction = NetworkDirection.Outgoing;
            NetworkOutgoingMessage message   = new RandomMessage(113);

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket host = fixture.Pool.New())
                    using (TcpSocket socket = fixture.Pool.New())
                    {
                        TcpSocketInfo info     = host.BindAndInfo();
                        int           port     = info.Endpoint.Port;
                        IPEndPoint    endpoint = new IPEndPoint(IPAddress.Loopback, port);

                        socket.Bind();
                        host.Listen(10);
                        host.Accept(null);

                        await socket.Connect(endpoint);

                        NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint);
                        Trigger           handler    = Trigger.Bind(ref fixture.Hooks.OnConnectionSent, data =>
                        {
                            data.Remote.Should().Be(NetworkAddress.Parse(endpoint));
                            data.Connection.Should().Be(connection);
                            data.Bytes.Should().Be(message.Length);
                        });

                        connection.Send(message);
                        handler.Wait().Should().BeTrue();
                    }
        }
Example #2
0
        public async Task ShouldTriggerConnectionReceivedWhenReceivedSomeBytes()
        {
            NetworkDirection       direction = NetworkDirection.Outgoing;
            NetworkOutgoingMessage message   = new RandomMessage(113);

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket host = fixture.Pool.New())
                    using (TcpSocket socket = fixture.Pool.New())
                    {
                        TcpSocketInfo info     = host.BindAndInfo();
                        int           port     = info.Endpoint.Port;
                        IPEndPoint    endpoint = new IPEndPoint(IPAddress.Loopback, port);

                        socket.Bind();
                        host.Listen(10);

                        Task <TcpSocketAccept> task = host.Accept();
                        await socket.Connect(endpoint);

                        TcpSocketAccept accept = await task;

                        NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint);
                        NetworkBlock      block      = new NetworkBlock(new byte[1024], 0, message.Length);

                        Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionReceived, data =>
                        {
                            data.Remote.Should().Be(NetworkAddress.Parse(endpoint));
                            data.Connection.Should().NotBeNull();
                            data.Bytes.Should().Be(message.Length);
                        });

                        connection.Receive(new NullReceiver());
                        message.ToBytes(block);

                        block.With((buffer, offset, count) =>
                        {
                            accept.Connection.Send(new SocketBuffer(buffer, offset, count), null);
                        });

                        handler.Wait().Should().BeTrue();
                    }
        }