Java Code Examples for org.apache.tomcat.jni.Status#APR_OS_START_SYSERR

The following examples show how to use org.apache.tomcat.jni.Status#APR_OS_START_SYSERR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {
    if (closed) {
        throw new IOException(sm.getString("socket.apr.closed", getSocket()));
    }

    Lock readLock = getBlockingStatusReadLock();
    WriteLock writeLock = getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    readLock.lock();
    try {
        if (getBlockingStatus() == block) {
            if (block) {
                Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
            }
            result = Socket.recvb(getSocket().longValue(), to, to.position(),
                    to.remaining());
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        writeLock.lock();
        try {
            // Set the current settings for this socket
            setBlockingStatus(block);
            if (block) {
                Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
            } else {
                Socket.timeoutSet(getSocket().longValue(), 0);
            }
            // Downgrade the lock
            readLock.lock();
            try {
                writeLock.unlock();
                result = Socket.recvb(getSocket().longValue(), to, to.position(),
                        to.remaining());
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        to.position(to.position() + result);
        return result;
    } else if (result == 0 || -result == Status.EAGAIN) {
        return 0;
    } else if ((-result) == Status.ETIMEDOUT || (-result) == Status.TIMEUP) {
        if (block) {
            throw new SocketTimeoutException(sm.getString("iib.readtimeout"));
        } else {
            // Attempting to read from the socket when the poller
            // has not signalled that there is data to read appears
            // to behave like a blocking read with a short timeout
            // on OSX rather than like a non-blocking read. If no
            // data is read, treat the resulting timeout like a
            // non-blocking read that returned no data.
            return 0;
        }
    } else if (-result == Status.APR_EOF) {
        return -1;
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("socket.apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("socket.apr.read.error",
                Integer.valueOf(-result), getSocket(), this));
    }
}
 
Example 2
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doWriteInternal(ByteBuffer from) throws IOException {
    int thisTime;

    do {
        thisTime = 0;
        if (getEndpoint().isSSLEnabled()) {
            if (sslOutputBuffer.remaining() == 0) {
                // Buffer was fully written last time around
                sslOutputBuffer.clear();
                transfer(from, sslOutputBuffer);
                sslOutputBuffer.flip();
            } else {
                // Buffer still has data from previous attempt to write
                // APR + SSL requires that exactly the same parameters are
                // passed when re-attempting the write
            }
            thisTime = Socket.sendb(getSocket().longValue(), sslOutputBuffer,
                    sslOutputBuffer.position(), sslOutputBuffer.limit());
            if (thisTime > 0) {
                sslOutputBuffer.position(sslOutputBuffer.position() + thisTime);
            }
        } else {
            thisTime = Socket.sendb(getSocket().longValue(), from, from.position(),
                    from.remaining());
            if (thisTime > 0) {
                from.position(from.position() + thisTime);
            }
        }
        if (Status.APR_STATUS_IS_EAGAIN(-thisTime)) {
            thisTime = 0;
        } else if (-thisTime == Status.APR_EOF) {
            throw new EOFException(sm.getString("socket.apr.clientAbort"));
        } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                (-thisTime == Status.APR_OS_START_SYSERR + 10053)) {
            // 10053 on Windows is connection aborted
            throw new EOFException(sm.getString("socket.apr.clientAbort"));
        } else if (thisTime < 0) {
            throw new IOException(sm.getString("socket.apr.write.error",
                    Integer.valueOf(-thisTime), getSocket(), this));
        }
    } while ((thisTime > 0 || getBlockingStatus()) && from.hasRemaining());

    // If there is data left in the buffer the socket will be registered for
    // write further up the stack. This is to ensure the socket is only
    // registered for write once as both container and user code can trigger
    // write registration.
}
 
Example 3
Source File: AprServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private int doWriteInternal(byte[] b, int off, int len) throws IOException {

        int start = off;
        int left = len;
        int written;

        do {
            if (endpoint.isSSLEnabled()) {
                if (sslOutputBuffer.remaining() == 0) {
                    // Buffer was fully written last time around
                    sslOutputBuffer.clear();
                    if (left < SSL_OUTPUT_BUFFER_SIZE) {
                        sslOutputBuffer.put(b, start, left);
                    } else {
                        sslOutputBuffer.put(b, start, SSL_OUTPUT_BUFFER_SIZE);
                    }
                    sslOutputBuffer.flip();
                } else {
                    // Buffer still has data from previous attempt to write
                    // APR + SSL requires that exactly the same parameters are
                    // passed when re-attempting the write
                }
                written = Socket.sendb(socket, sslOutputBuffer,
                        sslOutputBuffer.position(), sslOutputBuffer.limit());
                if (written > 0) {
                    sslOutputBuffer.position(
                            sslOutputBuffer.position() + written);
                }
            } else {
                written = Socket.send(socket, b, start, left);
            }
            if (Status.APR_STATUS_IS_EAGAIN(-written)) {
                written = 0;
            } else if (-written == Status.APR_EOF) {
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                    (-written == Status.APR_OS_START_SYSERR + 10053)) {
                // 10053 on Windows is connection aborted
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if (written < 0) {
                throw new IOException(sm.getString("apr.write.error",
                        Integer.valueOf(-written), Long.valueOf(socket), wrapper));
            }
            start += written;
            left -= written;
        } while (written > 0 && left > 0);

        if (left > 0) {
            endpoint.getPoller().add(socket, -1, false, true);
        }
        return len - left;
    }
 
Example 4
Source File: AprServletInputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}
 
Example 5
Source File: AprServletOutputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private int doWriteInternal(byte[] b, int off, int len) throws IOException {

        int start = off;
        int left = len;
        int written;

        do {
            if (endpoint.isSSLEnabled()) {
                if (sslOutputBuffer.remaining() == 0) {
                    // Buffer was fully written last time around
                    sslOutputBuffer.clear();
                    if (left < SSL_OUTPUT_BUFFER_SIZE) {
                        sslOutputBuffer.put(b, start, left);
                    } else {
                        sslOutputBuffer.put(b, start, SSL_OUTPUT_BUFFER_SIZE);
                    }
                    sslOutputBuffer.flip();
                } else {
                    // Buffer still has data from previous attempt to write
                    // APR + SSL requires that exactly the same parameters are
                    // passed when re-attempting the write
                }
                written = Socket.sendb(socket, sslOutputBuffer,
                        sslOutputBuffer.position(), sslOutputBuffer.limit());
                if (written > 0) {
                    sslOutputBuffer.position(
                            sslOutputBuffer.position() + written);
                }
            } else {
                written = Socket.send(socket, b, start, left);
            }
            if (Status.APR_STATUS_IS_EAGAIN(-written)) {
                written = 0;
            } else if (-written == Status.APR_EOF) {
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
                    (-written == Status.APR_OS_START_SYSERR + 10053)) {
                // 10053 on Windows is connection aborted
                throw new EOFException(sm.getString("apr.clientAbort"));
            } else if (written < 0) {
                throw new IOException(sm.getString("apr.write.error",
                        Integer.valueOf(-written), Long.valueOf(socket), wrapper));
            }
            start += written;
            left -= written;
        } while (written > 0 && left > 0);

        if (left > 0) {
            endpoint.getPoller().add(socket, -1, false, true);
        }
        return len - left;
    }
 
Example 6
Source File: AprServletInputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected int doRead(boolean block, byte[] b, int off, int len)
        throws IOException {

    if (closed) {
        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
    }

    Lock readLock = wrapper.getBlockingStatusReadLock();
    WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

    boolean readDone = false;
    int result = 0;
    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            result = Socket.recv(socket, b, off, len);
            readDone = true;
        }
    } finally {
        readLock.unlock();
    }

    if (!readDone) {
        try {
            writeLock.lock();
            wrapper.setBlockingStatus(block);
            // Set the current settings for this socket
            Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                result = Socket.recv(socket, b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }

    if (result > 0) {
        eagain = false;
        return result;
    } else if (-result == Status.EAGAIN) {
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EGENERAL && wrapper.isSecure()) {
        // Not entirely sure why this is necessary. Testing to date has not
        // identified any issues with this but log it so it can be tracked
        // if it is suspected of causing issues in the future.
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("apr.read.sslGeneralError",
                    Long.valueOf(socket), wrapper));
        }
        eagain = true;
        return 0;
    } else if (-result == Status.APR_EOF) {
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
            (-result == Status.APR_OS_START_SYSERR + 10053)) {
        // 10053 on Windows is connection aborted
        throw new EOFException(sm.getString("apr.clientAbort"));
    } else {
        throw new IOException(sm.getString("apr.read.error",
                Integer.valueOf(-result), Long.valueOf(socket), wrapper));
    }
}