Exemplo n.º 1
0
        public void AddressNotAvailable(string ipAddress)
        {
            IPAddress address  = IPAddress.Parse(ipAddress);
            var       endPoint = new IPEndPoint(address, Port);
            Tcp       tcp      = this.loop.CreateTcp();

            //
            //
            // It seems that Linux is broken here - bind succeeds
            // for "127.255.255.255"
            //
            if (ipAddress == "127.255.255.255" && Platform.IsLinux)
            {
                tcp.Bind(endPoint);
            }
            else
            {
                var error = Assert.Throws <OperationException>(() => tcp.Bind(endPoint));
                Assert.Equal(ErrorCode.EADDRNOTAVAIL, error.ErrorCode);
            }

            tcp.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(1, this.closeCalled);
        }
Exemplo n.º 2
0
        public void LocalHost()
        {
            if (!Platform.OSSupportsIPv6)
            {
                return;
            }

            var endPoint = new IPEndPoint(IPAddress.IPv6Loopback, Port);
            Tcp tcp      = this.loop.CreateTcp();

            try
            {
                tcp.Bind(endPoint);
            }
            catch (OperationException exception)
            {
                // IPv6 loop back not available happens on some Linux
                Assert.Equal(ErrorCode.EADDRNOTAVAIL, exception.ErrorCode);
                return;
            }

            tcp.CloseHandle(this.OnClose);

            this.loop.RunDefault();
            Assert.Equal(1, this.closeCount);
        }
Exemplo n.º 3
0
 private void OnConnected(Tcp client, Exception exception)
 {
     if (exception != null)
     {
         log($"client error: {exception.Message}");
         client.CloseHandle(this.OnClosed);
     }
     else
     {
         var writableBuffer = WritableBuffer.From(new byte[1024]);
         client.QueueWriteStream(
             writableBuffer,
             (streamHandle, error) =>
         {
             writableBuffer.Dispose();
             if (error != null)
             {
                 log($"write error {error}");
                 streamHandle.CloseHandle(this.OnClosed);
             }
             else
             {
                 client.OnRead(
                     this.OnAccept,
                     (_h, _e) => { log($"read error {_e.Message}"); });
             }
         });
         ////log("client wrote 1024");
     }
 }
Exemplo n.º 4
0
        void OnConnection(Tcp tcp, Exception exception)
        {
            this.connectionError = exception;
            this.connection++;

            tcp.CloseHandle(this.OnClose);
            this.server.CloseHandle(this.OnClose);
        }
Exemplo n.º 5
0
 static void OnConnection(Tcp client, Exception error)
 {
     if (error != null)
     {
         client.CloseHandle(OnClosed);
     }
     else
     {
         client.OnRead(OnAccept, OnError);
     }
 }
Exemplo n.º 6
0
        public void TcpListen2()
        {
            Tcp tcp         = this.loop.CreateTcp();
            var anyEndPoint = new IPEndPoint(IPAddress.Loopback, IPEndPoint.MinPort);

            tcp.Listen(anyEndPoint, this.OnConnection);
            tcp.RemoveReference();
            tcp.CloseHandle(this.OnClose);

            this.loop.RunDefault();
            Assert.Equal(1, this.closeCount);
        }
Exemplo n.º 7
0
        void OnConnected(Tcp tcp, Exception exception)
        {
            Assert.True(exception == null);

            byte[] content = Encoding.UTF8.GetBytes("PING");
            for (int i = 0; i < NumberOfWriteRequests; i++)
            {
                tcp.QueueWrite(content, this.OnWriteCompleted);
            }

            tcp.CloseHandle(this.OnClose);
        }
Exemplo n.º 8
0
        void OnConnected(Tcp tcp, Exception exception)
        {
            this.connectedError = exception;
            this.connectedCount++;

            if (exception == null)
            {
                // Send PING
                byte[] content = Encoding.UTF8.GetBytes("PING");
                do
                {
                    try
                    {
                        tcp.TryWrite(content);
                        this.bytesWritten += content.Length;
                        break; // Try write success
                    }
                    catch (OperationException error)
                    {
                        if (error.ErrorCode != ErrorCode.EAGAIN)
                        {
                            this.bytesWritten = 0;
                            break;
                        }
                    }
                }while (true);

                // Send Empty
                content = Encoding.UTF8.GetBytes("");
                do
                {
                    try
                    {
                        tcp.TryWrite(content);
                        break; // Try write success
                    }
                    catch (OperationException error)
                    {
                        if (error.ErrorCode != ErrorCode.EAGAIN)
                        {
                            this.bytesWritten = 0;
                            break;
                        }
                    }
                }while (true);

                tcp.CloseHandle(this.OnClose);
            }
            else
            {
                this.server.CloseHandle(this.OnClose);
            }
        }
Exemplo n.º 9
0
        public void Run()
        {
            Tcp tcp = this.loop.CreateTcp();

            tcp.NoDelay(true);
            tcp.KeepAlive(true, 60);

            tcp.CloseHandle(OnClose);

            int result = this.loop.RunDefault();

            Assert.Equal(0, result);
        }
Exemplo n.º 10
0
 void OnRead(Tcp tcp, IStreamReadCompletion completion)
 {
     if (completion.Error != null ||
         completion.Completed)
     {
         tcp.CloseHandle(this.OnClose);
         this.server.CloseHandle(this.OnClose);
     }
     else
     {
         this.bytesRead += completion.Data.Count;
     }
 }
Exemplo n.º 11
0
        void OnConnection(Tcp tcp, Exception exception)
        {
            this.connectionError = exception;
            this.connectionCount++;

            if (exception == null)
            {
                tcp.OnRead(this.OnRead);
            }
            else
            {
                tcp.CloseHandle(this.OnClose);
            }
        }
Exemplo n.º 12
0
        void OnConnected(Tcp tcp, Exception error)
        {
            if (error != null)
            {
                Console.WriteLine($"Tcp ping pong : client connection failed, error {error}.");
                tcp.CloseHandle(OnClose);
            }
            else
            {
                tcp.OnRead(this.OnAccept, OnError);

                // Sending the first ping
                tcp.QueueWrite(this.content, OnWriteCompleted);
            }
        }
Exemplo n.º 13
0
        public void Invalid()
        {
            IPAddress address   = IPAddress.Loopback;
            var       endPoint1 = new IPEndPoint(address, Port);
            var       endPoint2 = new IPEndPoint(address, Port + 1);

            Tcp tcp = this.loop.CreateTcp();

            Assert.Equal(tcp.Bind(endPoint1), tcp);

            Assert.Throws <OperationException>(() => tcp.Bind(endPoint2));
            tcp.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(1, this.closeCalled);
        }
Exemplo n.º 14
0
        public void AddressInUse()
        {
            IPAddress address  = IPAddress.Loopback;
            var       endPoint = new IPEndPoint(address, Port);

            Tcp tcp1 = this.loop.CreateTcp().Bind(endPoint);
            Tcp tcp2 = this.loop.CreateTcp().Bind(endPoint);

            tcp1.Listen(OnConnection);
            Assert.Throws <OperationException>(() => tcp2.Listen(OnConnection));

            tcp1.CloseHandle(this.OnClose);
            tcp2.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(2, this.closeCalled);
        }
Exemplo n.º 15
0
        void OnConnected(Tcp tcp, Exception exception)
        {
            if (exception == null)
            {
                IPEndPoint endPoint       = tcp.GetLocalEndPoint();
                IPEndPoint remoteEndPoint = tcp.GetPeerEndPoint();

                if (Equals(endPoint.Address, IPAddress.Loopback) &&
                    Equals(remoteEndPoint.Address, IPAddress.Loopback) &&
                    remoteEndPoint.Port == Port)
                {
                    this.connectedCount++;
                }
            }

            tcp.CloseHandle(this.OnClose);
        }
Exemplo n.º 16
0
        public void AddressNotAvailable()
        {
            if (!Platform.OSSupportsIPv6)
            {
                return;
            }

            IPAddress address  = IPAddress.Parse("4:4:4:4:4:4:4:4");
            var       endPoint = new IPEndPoint(address, Port);
            Tcp       tcp      = this.loop.CreateTcp();

            Assert.Throws <OperationException>(() => tcp.Bind(endPoint));

            tcp.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(1, this.closeCount);
        }
Exemplo n.º 17
0
        void OnConnected(Tcp tcp, Exception error)
        {
            if (error != null)
            {
                Console.WriteLine($"Tcp write batch : Write request connection failed {error}.");
                tcp.CloseHandle(OnClosed);
                return;
            }

            tcp.OnRead(OnAccept, OnError);
            for (int i = 0; i < NumberOfRequests; i++)
            {
                tcp.QueueWriteStream(this.dataBuffer, this.OnWriteComplete);
            }

            this.batchWriteCommit = this.loop.NowInHighResolution;
            tcp.Shutdown(this.OnShutdown);
        }
Exemplo n.º 18
0
        void OnConnected(Tcp handle, Exception exception)
        {
            var error = exception as OperationException;

            this.connectionErrorValid =
                error != null &&
                error.ErrorCode == ErrorCode.ECONNREFUSED &&
                this.closeCount == 0;

            this.connectCount++;

            if (this.closeHandle)
            {
                handle.CloseHandle(this.OnClose);
            }
            else
            {
                this.timer.Start(this.OnTimer, 100, 0);
            }
        }
Exemplo n.º 19
0
        public void Invalid()
        {
            if (!Platform.OSSupportsIPv6)
            {
                return;
            }

            IPAddress address   = IPAddress.Parse("::");
            var       endPoint1 = new IPEndPoint(address, Port);
            var       endPoint2 = new IPEndPoint(address, Port + 1);

            Tcp tcp = this.loop.CreateTcp();

            Assert.Equal(tcp.Bind(endPoint1), tcp);

            Assert.Throws <OperationException>(() => tcp.Bind(endPoint2));
            tcp.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(1, this.closeCount);
        }
Exemplo n.º 20
0
        public void AddressInUse()
        {
            if (!Platform.OSSupportsIPv6)
            {
                return;
            }

            IPAddress address  = IPAddress.Parse("::");
            var       endPoint = new IPEndPoint(address, Port);

            Tcp tcp1 = this.loop.CreateTcp().Bind(endPoint);
            Tcp tcp2 = this.loop.CreateTcp().Bind(endPoint);

            tcp1.Listen(OnConnection);
            Assert.Throws <OperationException>(() => tcp2.Listen(OnConnection));

            tcp1.CloseHandle(this.OnClose);
            tcp2.CloseHandle(this.OnClose);
            this.loop.RunDefault();
            Assert.Equal(2, this.closeCount);
        }
Exemplo n.º 21
0
 void OnConnected(Tcp tcp, Exception exception)
 {
     this.connected++;
     tcp.CloseHandle(this.OnClose);
 }
Exemplo n.º 22
0
        internal void Close(bool force_close)
        {
            state = 98;

            if (sock != null)
            {
#if !DNXCORE50
                ResponseStream st = GetResponseStream();

                if (st != null)
                {
                    st.Close();
                }
#else
                ResponseStream rs = GetResponseStream();
                if (rs != null)
                {
                    rs.Close();
                }
#endif

                o_stream = null;
            }

            if (sock != null)
            {
                force_close |= !context.Request.KeepAlive;
                if (!force_close)
                {
                    force_close = (context.Response.Headers ["connection"] == "close");
                }

                /*
                 * if (!force_close) {
                 * //					bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                 * //							status_code == 413 || status_code == 414 || status_code == 500 ||
                 * //							status_code == 503);
                 *
                 *      force_close |= (context.Request.ProtocolVersion <= HttpVersion.Version10);
                 * }
                 */

                if (!force_close && context.Request.FlushInput())
                {
                    if (chunked && context.Response.ForceCloseChunked == false)
                    {
                        // Don't close. Keep working.
                        reuses++;
                        Unbind();
                        Init();
                        ReadLibuv();
                        return;
                    }

                    reuses++;
                    Unbind();
                    Init();
                    ReadLibuv();
                    return;
                }

                Tcp s = sock;
                sock = null;
                try {
                    if (s != null)
                    {
                        s.CloseHandle();
                    }
                } catch {
                }
                finally {
#if !DNXCORE50
                    if (s != null)
                    {
                        s.Dispose();
                    }
#endif
                }
                Unbind();
                RemoveConnection();
                return;
            }
        }