Java Code Examples for org.apache.tomcat.jni.Socket#timeoutSet()

The following examples show how to use org.apache.tomcat.jni.Socket#timeoutSet() . 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: UpgradeAprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public UpgradeAprProcessor(SocketWrapper<Long> wrapper,
        UpgradeInbound upgradeInbound) {
    super(upgradeInbound);

    Socket.timeoutSet(wrapper.getSocket().longValue(),
            upgradeInbound.getReadTimeout());

    this.socket = wrapper.getSocket().longValue();
}
 
Example 2
Source File: AprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public AprProcessor(SocketWrapper<Long> wrapper,
        HttpUpgradeHandler httpUpgradeProcessor, AprEndpoint endpoint,
        int asyncWriteBufferSize) {
    super(httpUpgradeProcessor,
            new AprServletInputStream(wrapper),
            new AprServletOutputStream(wrapper, asyncWriteBufferSize, endpoint));

    Socket.timeoutSet(wrapper.getSocket().longValue(), INFINITE_TIMEOUT);
}
 
Example 3
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Process the specified connection.
 */
protected boolean setSocketOptions(long socket) {
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }
        }

    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 4
Source File: UpgradeAprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public UpgradeAprProcessor(SocketWrapper<Long> wrapper,
        UpgradeInbound upgradeInbound) {
    super(upgradeInbound);

    Socket.timeoutSet(wrapper.getSocket().longValue(),
            upgradeInbound.getReadTimeout());

    this.socket = wrapper.getSocket().longValue();
}
 
Example 5
Source File: AprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public AprProcessor(SocketWrapper<Long> wrapper,
        HttpUpgradeHandler httpUpgradeProcessor, AprEndpoint endpoint,
        int asyncWriteBufferSize) {
    super(httpUpgradeProcessor,
            new AprServletInputStream(wrapper),
            new AprServletOutputStream(wrapper, asyncWriteBufferSize, endpoint));

    Socket.timeoutSet(wrapper.getSocket().longValue(), INFINITE_TIMEOUT);
}
 
Example 6
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Process the specified connection.
 */
protected boolean setSocketOptions(long socket) {
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }
        }

    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 7
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Process the specified connection.
 * @param socketWrapper The socket wrapper
 * @return <code>true</code> if the socket was correctly configured
 *  and processing may continue, <code>false</code> if the socket needs to be
 *  close immediately
 */
protected boolean setSocketOptions(SocketWrapperBase<Long> socketWrapper) {
    long socket = socketWrapper.getSocket().longValue();
    // Process the connection
    int step = 1;
    try {

        // 1: Set socket options: timeout, linger, etc
        if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0)
            Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
        if (socketProperties.getTcpNoDelay())
            Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
        Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);

        // 2: SSL handshake
        step = 2;
        if (sslContext != 0) {
            SSLSocket.attach(sslContext, socket);
            if (SSLSocket.handshake(socket) != 0) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
                }
                return false;
            }

            if (negotiableProtocols.size() > 0) {
                byte[] negotiated = new byte[256];
                int len = SSLSocket.getALPN(socket, negotiated);
                String negotiatedProtocol =
                        new String(negotiated, 0, len, StandardCharsets.UTF_8);
                if (negotiatedProtocol.length() > 0) {
                    socketWrapper.setNegotiatedProtocol(negotiatedProtocol);
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("endpoint.alpn.negotiated", negotiatedProtocol));
                    }
                }
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled()) {
            if (step == 2) {
                log.debug(sm.getString("endpoint.err.handshake"), t);
            } else {
                log.debug(sm.getString("endpoint.err.unexpected"), t);
            }
        }
        // Tell to close the socket
        return false;
    }
    return true;
}
 
Example 8
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public SendfileState add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (sendfileRunning) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.length, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return SendfileState.ERROR;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos += nw;
                data.length -= nw;
                if (data.length == 0) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(data.socket, getConnectionTimeout() * 1000);
                    return SendfileState.DONE;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return SendfileState.ERROR;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return SendfileState.PENDING;
}
 
Example 9
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 10
Source File: AprEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected void doWrite(boolean block, ByteBuffer from) throws IOException {
    if (closed) {
        throw new IOException(sm.getString("socket.apr.closed", getSocket()));
    }

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

    readLock.lock();
    try {
        if (getBlockingStatus() == block) {
            if (block) {
                Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000);
            }
            doWriteInternal(from);
            return;
        }
    } finally {
        readLock.unlock();
    }

    writeLock.lock();
    try {
        // Set the current settings for this socket
        setBlockingStatus(block);
        if (block) {
            Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000);
        } else {
            Socket.timeoutSet(getSocket().longValue(), 0);
        }

        // Downgrade the lock
        readLock.lock();
        try {
            writeLock.unlock();
            doWriteInternal(from);
        } finally {
            readLock.unlock();
        }
    } finally {
        // Should have been released above but may not have been on some
        // exception paths
        if (writeLock.isHeldByCurrentThread()) {
            writeLock.unlock();
        }
    }
}
 
Example 11
Source File: AprServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doWrite(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();

    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            return doWriteInternal(b, off, len);
        }
    } finally {
        readLock.unlock();
    }

    try {
        writeLock.lock();
        // Set the current settings for this socket
        wrapper.setBlockingStatus(block);
        if (block) {
            Socket.timeoutSet(socket, endpoint.getSoTimeout() * 1000);
        } else {
            Socket.timeoutSet(socket, 0);
        }

        // Downgrade the lock
        try {
            readLock.lock();
            writeLock.unlock();
            return doWriteInternal(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();
        }
    }
}
 
Example 12
Source File: Http11AprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected void setSocketTimeout(int timeout) {
    Socket.timeoutSet(socketWrapper.getSocket().longValue(), timeout * 1000);
}
 
Example 13
Source File: AprEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public boolean add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        data.pos = data.start;
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (true) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.end - data.pos, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return false;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos = data.pos + nw;
                if (data.pos >= data.end) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(
                            data.socket, getSoTimeout() * 1000);
                    return true;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return false;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return false;
}
 
Example 14
Source File: AprServletOutputStream.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected int doWrite(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();

    try {
        readLock.lock();
        if (wrapper.getBlockingStatus() == block) {
            return doWriteInternal(b, off, len);
        }
    } finally {
        readLock.unlock();
    }

    try {
        writeLock.lock();
        // Set the current settings for this socket
        wrapper.setBlockingStatus(block);
        if (block) {
            Socket.timeoutSet(socket, endpoint.getSoTimeout() * 1000);
        } else {
            Socket.timeoutSet(socket, 0);
        }

        // Downgrade the lock
        try {
            readLock.lock();
            writeLock.unlock();
            return doWriteInternal(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();
        }
    }
}
 
Example 15
Source File: Http11AprProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected void setSocketTimeout(int timeout) {
    Socket.timeoutSet(socketWrapper.getSocket().longValue(), timeout * 1000);
}
 
Example 16
Source File: AprEndpoint.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Add the sendfile data to the sendfile poller. Note that in most cases,
 * the initial non blocking calls to sendfile will return right away, and
 * will be handled asynchronously inside the kernel. As a result,
 * the poller will never be used.
 *
 * @param data containing the reference to the data which should be snet
 * @return true if all the data has been sent right away, and false
 *              otherwise
 */
public boolean add(SendfileData data) {
    // Initialize fd from data given
    try {
        data.fdpool = Socket.pool(data.socket);
        data.fd = File.open
            (data.fileName, File.APR_FOPEN_READ
             | File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
             0, data.fdpool);
        data.pos = data.start;
        // Set the socket to nonblocking mode
        Socket.timeoutSet(data.socket, 0);
        while (true) {
            long nw = Socket.sendfilen(data.socket, data.fd,
                                       data.pos, data.end - data.pos, 0);
            if (nw < 0) {
                if (!(-nw == Status.EAGAIN)) {
                    Pool.destroy(data.fdpool);
                    data.socket = 0;
                    return false;
                } else {
                    // Break the loop and add the socket to poller.
                    break;
                }
            } else {
                data.pos = data.pos + nw;
                if (data.pos >= data.end) {
                    // Entire file has been sent
                    Pool.destroy(data.fdpool);
                    // Set back socket to blocking mode
                    Socket.timeoutSet(
                            data.socket, getSoTimeout() * 1000);
                    return true;
                }
            }
        }
    } catch (Exception e) {
        log.warn(sm.getString("endpoint.sendfile.error"), e);
        return false;
    }
    // Add socket to the list. Newly added sockets will wait
    // at most for pollTime before being polled
    synchronized (this) {
        addS.add(data);
        this.notify();
    }
    return false;
}