Exemplo n.º 1
0
        public async Task <string> Send(IPEndPoint endpoint, string message)
        {
            int progress = 0;

            byte[] output = new byte[message.Length];
            byte[] bytes  = Encoding.ASCII.GetBytes(message);

            using (TcpSocket socket = factory.Tcp())
            {
                socket.Bind();

                TcpSocketConnect connected = await socket.Connect(endpoint);

                TcpSocketSend sent = await socket.Send(bytes);

                while (progress < output.Length)
                {
                    SocketBuffer     buffer   = new SocketBuffer(output, progress);
                    TcpSocketReceive received = await socket.Receive(buffer);

                    if (received.Count == 0)
                    {
                        break;
                    }

                    progress += received.Count;
                }
            }

            return(Encoding.ASCII.GetString(output));
        }
Exemplo n.º 2
0
 private void OnConnected(PeerConnectorContext context, TcpSocketConnect data)
 {
     if (data.Status == SocketStatus.OK)
     {
         context.Queue.Add(new PeerConnectorTaskAccept(hash, data.Socket, data.Endpoint));
     }
     else
     {
         context.Queue.Add(new PeerConnectorTaskReject(remote));
         data.Socket.Dispose();
     }
 }
Exemplo n.º 3
0
 private void OnConnected(TcpSocketConnect data)
 {
     if (data.Status == SocketStatus.OK)
     {
         socket.Send(outgoing, OnSent);
     }
     else
     {
         Console.WriteLine("OnConnected");
         socket.Dispose();
     }
 }
Exemplo n.º 4
0
        public async Task CanHandleNotBoundSocket()
        {
            using (CompletionThread worker = new CompletionThread())
            {
                SocketFactory factory = new SocketFactory(worker);

                using (TcpSocket socket = factory.Tcp())
                {
                    IPEndPoint       endpoint  = new IPEndPoint(IPAddress.Loopback, 80);
                    TcpSocketConnect connected = await socket.Connect(endpoint);

                    Assert.That(connected.Status, Is.Not.EqualTo(SocketStatus.OK));
                }
            }
        }
Exemplo n.º 5
0
        public async Task CanConnectUsingTasksToNotAvailableSite()
        {
            IPAddress[] addresses = Dns.GetHostAddresses("www.example.com");
            IPEndPoint  endpoint  = new IPEndPoint(addresses[0], 8123);

            using (CompletionThread worker = new CompletionThread())
            {
                SocketFactory factory = new SocketFactory(worker);
                TcpSocket     socket  = factory.Tcp();

                socket.Bind();
                worker.Start();

                TcpSocketConnect connect = await socket.Connect(endpoint);

                SocketStatus expected = SocketStatus.TimedOut;

                Assert.That(connect.Status, Is.EqualTo(expected));
            }
        }
Exemplo n.º 6
0
        public async Task ShouldTriggerConnectionTerminatedWhenSending()
        {
            NetworkDirection direction = NetworkDirection.Outgoing;

            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();
                        TcpSocketConnect       connect = await socket.Connect(endpoint);

                        TcpSocketAccept accept = await task;

                        connect.Status.Should().Be(SocketStatus.OK);
                        accept.Status.Should().Be(SocketStatus.OK);
                        accept.Connection.Dispose();

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

                        for (int i = 0; i < 10; i++)
                        {
                            connection.Send(new OneByteMessage());
                        }

                        handler.Wait().Should().BeTrue();
                    }
        }
Exemplo n.º 7
0
        public async Task <NegotiatorFixturePair> Create()
        {
            int?port;

            TcpSocket host   = pool.New();
            TcpSocket client = pool.New();

            client.Bind();
            host.Bind(out port);
            host.Listen(1);

            Task <TcpSocketAccept>  accept  = host.Accept();
            Task <TcpSocketConnect> connect = client.Connect(port.Value);

            TcpSocketAccept  accepted  = await accept;
            TcpSocketConnect connected = await connect;

            NetworkConnection local  = pool.Create(connected.Socket, NetworkDirection.Outgoing, connected.Endpoint);
            NetworkConnection remote = pool.Create(accepted.Connection, NetworkDirection.Incoming, accepted.GetRemote());

            return(new NegotiatorFixturePair(local, remote));
        }