示例#1
0
        public async Task CanHandleTerminatedStream()
        {
            IPAddress  localhost = IPAddress.Loopback;
            IPEndPoint endpoint  = new IPEndPoint(localhost, 1234);

            using (CompletionThread worker = new CompletionThread())
            {
                SocketFactory factory = new SocketFactory(worker);

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

                        server.Bind(endpoint.Port);
                        server.Listen(1);

                        Task <TcpSocketAccept> acceptable = server.Accept();
                        await socket.Connect(endpoint);

                        TcpSocketAccept accepted = await acceptable;
                        accepted.Connection.Dispose();

                        byte[]           buffer   = new byte[10];
                        TcpSocketReceive received = await socket.Receive(buffer);

                        Assert.That(received.Status, Is.Not.EqualTo(SocketStatus.OK));
                    }
            }
        }
示例#2
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));
        }
示例#3
0
        private void OnReceived(TcpSocketReceive data)
        {
            if (data.Status == SocketStatus.OK)
            {
                incoming = new SocketBuffer(data.Buffer.Data, data.Buffer.Offset + data.Count, data.Buffer.Count - data.Count);

                if (outgoing.Count > 0)
                {
                    socket.Send(outgoing, OnSent);
                }
                else if (incoming.Count > 0)
                {
                    socket.Receive(incoming, OnReceived);
                }
                else
                {
                    incoming = new SocketBuffer(incoming.Data);
                    outgoing = new SocketBuffer(outgoing.Data);

                    if (session.Elapsed > TimeSpan.FromMinutes(1))
                    {
                        socket  = factory.Tcp();
                        session = Stopwatch.StartNew();

                        socket.Bind();
                        socket.Connect(endpoint, OnConnected);
                        data.Socket.Dispose();
                    }
                    else
                    {
                        socket.Send(outgoing, OnSent);
                    }
                }
            }
            else
            {
                Console.WriteLine("OnReceived");
                socket.Dispose();
            }

            counter += data.Count;

            if (counter - previous > 1024 * 1024 * 1024)
            {
                previous = counter;

                Console.WriteLine($"{counter}: {counter / watch.ElapsedMilliseconds}");
            }
        }
示例#4
0
        public async Task CanHandleNotBoundSocket()
        {
            using (CompletionThread worker = new CompletionThread())
            {
                SocketFactory factory = new SocketFactory(worker);

                using (TcpSocket socket = factory.Tcp())
                {
                    byte[]           buffer   = new byte[10];
                    TcpSocketReceive received = await socket.Receive(buffer);

                    Assert.That(received.Status, Is.Not.EqualTo(SocketStatus.OK));
                }
            }
        }
示例#5
0
        private void OnReceived(TcpSocketReceive received)
        {
            if (received.Count > 0 && received.Status == SocketStatus.OK)
            {
                int          count  = received.Count;
                SocketBuffer buffer = new SocketBuffer(data, 0, count);

                received.Socket.Send(buffer, OnSent);
            }
            else
            {
                factory.Release(this);
                received.Socket.Dispose();
            }
        }
示例#6
0
        private async void OnAccepted(TcpSocketAccept data)
        {
            if (data.Status == SocketStatus.OK)
            {
                socket.Accept(OnAccepted);
            }

            if (data.Status == SocketStatus.OK)
            {
                TcpSocket other = data.Connection;
                byte[]    bytes = new byte[1024];

                while (true)
                {
                    TcpSocketReceive received = await other.Receive(bytes);

                    SocketBuffer buffer = new SocketBuffer(bytes, 0, received.Count);

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

                    if (received.Status != SocketStatus.OK)
                    {
                        break;
                    }

                    TcpSocketSend sent = await other.Send(buffer);

                    if (sent.Status != SocketStatus.OK)
                    {
                        break;
                    }
                }

                other.Dispose();
            }
        }
示例#7
0
        public async Task CanReceiveDataUsingTasksToHostedEchoServerInTwoPieces()
        {
            string request = "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n";

            byte[] input  = Encoding.ASCII.GetBytes(request);
            byte[] output = new byte[input.Length];

            byte[] part1 = new byte[input.Length / 2];
            byte[] part2 = new byte[input.Length - part1.Length];

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

                using (EchoServer server = new EchoServer(factory))
                {
                    socket.Bind();
                    worker.Start();
                    server.Start();

                    await socket.Connect(server.Endpoint);

                    await socket.Send(input);

                    TcpSocketReceive received1 = await socket.Receive(part1);

                    TcpSocketReceive received2 = await socket.Receive(part2);

                    Array.Copy(part1, 0, output, 0, received1.Count);
                    Array.Copy(part2, 0, output, received1.Count, received2.Count);

                    Assert.That(input, Is.EqualTo(output));
                }
            }
        }
示例#8
0
 private void OnReceived(TcpSocketReceive received, NetworkIncomingMessageHandler handler)
 {
     listener.Schedule(new NetworkPoolDecrypt(listener, identifier, this, handler, received.Count));
 }