Example #1
0
 public override void SetConnectionReset()
 {
     Impl.SetConnectionReset();
 }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: int read(byte b[] , int off, int length, int timeout) throws java.io.IOException
        internal virtual int Read(sbyte[] b, int off, int length, int timeout)
        {
            int n;

            // EOF already encountered
            if (Eof)
            {
                return(-1);
            }

            // connection reset
            if (Impl.ConnectionReset)
            {
                throw new SocketException("Connection reset");
            }

            // bounds check
            if (length <= 0 || off < 0 || off + length > b.Length)
            {
                if (length == 0)
                {
                    return(0);
                }
                throw new ArrayIndexOutOfBoundsException();
            }

            bool gotReset = false;

            // acquire file descriptor and do the read
            FileDescriptor fd = Impl.AcquireFD();

            try
            {
                n = SocketRead(fd, b, off, length, timeout);
                if (n > 0)
                {
                    return(n);
                }
            }
            catch (ConnectionResetException)
            {
                gotReset = true;
            }
            finally
            {
                Impl.ReleaseFD();
            }

            /*
             * We receive a "connection reset" but there may be bytes still
             * buffered on the socket
             */
            if (gotReset)
            {
                Impl.SetConnectionResetPending();
                Impl.AcquireFD();
                try
                {
                    n = SocketRead(fd, b, off, length, timeout);
                    if (n > 0)
                    {
                        return(n);
                    }
                }
                catch (ConnectionResetException)
                {
                }
                finally
                {
                    Impl.ReleaseFD();
                }
            }

            /*
             * If we get here we are at EOF, the socket has been closed,
             * or the connection has been reset.
             */
            if (Impl.ClosedOrPending)
            {
                throw new SocketException("Socket closed");
            }
            if (Impl.ConnectionResetPending)
            {
                Impl.SetConnectionReset();
            }
            if (Impl.ConnectionReset)
            {
                throw new SocketException("Connection reset");
            }
            Eof = true;
            return(-1);
        }