Java Code Examples for org.apache.tomcat.util.net.NioEndpoint#KeyAttachment

The following examples show how to use org.apache.tomcat.util.net.NioEndpoint#KeyAttachment . 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: UpgradeNioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void flush() throws IOException {
    NioEndpoint.KeyAttachment att =
            (NioEndpoint.KeyAttachment) nioChannel.getAttachment();
    if (att == null) {
        throw new IOException("Key must be cancelled");
    }
    long writeTimeout = att.getTimeout();
    Selector selector = null;
    try {
        selector = pool.get();
    } catch ( IOException x ) {
        //ignore
    }
    try {
        do {
            if (nioChannel.flush(true, selector, writeTimeout)) {
                break;
            }
        } while (true);
    } finally {
        if (selector != null) {
            pool.put(selector);
        }
    }
}
 
Example 2
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
protected void setCometTimeouts(SocketWrapper<NioChannel> socketWrapper) {
    // Comet support
    SelectionKey key = socketWrapper.getSocket().getIOChannel().keyFor(
            socketWrapper.getSocket().getPoller().getSelector());
    if (key != null) {
        NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
        if (attach != null)  {
            attach.setComet(comet);
            if (comet) {
                Integer comettimeout = (Integer) request.getAttribute(
                        org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                if (comettimeout != null) {
                    attach.setTimeout(comettimeout.longValue());
                }
            }
        }
    }
}
 
Example 3
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Callback to write data from the buffer.
 */
private void flushBuffer() throws IOException {

    //prevent timeout for async,
    SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
    if (key != null) {
        NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
        attach.access();
    }

    //write to the socket, if there is anything to write
    if (socket.getBufHandler().getWriteBuffer().position() > 0) {
        socket.getBufHandler().getWriteBuffer().flip();
        writeToSocket(socket.getBufHandler().getWriteBuffer(),true, false);
    }
}
 
Example 4
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected void setCometTimeouts(SocketWrapper<NioChannel> socketWrapper) {
    // Comet support
    SelectionKey key = socketWrapper.getSocket().getIOChannel().keyFor(
            socketWrapper.getSocket().getPoller().getSelector());
    if (key != null) {
        NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
        if (attach != null)  {
            attach.setComet(comet);
            if (comet) {
                Integer comettimeout = (Integer) request.getAttribute(
                        org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                if (comettimeout != null) {
                    attach.setTimeout(comettimeout.longValue());
                }
            }
        }
    }
}
 
Example 5
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Callback to write data from the buffer.
 */
private void flushBuffer() throws IOException {

    //prevent timeout for async,
    SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
    if (key != null) {
        NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
        attach.access();
    }

    //write to the socket, if there is anything to write
    if (socket.getBufHandler().getWriteBuffer().position() > 0) {
        socket.getBufHandler().getWriteBuffer().flip();
        writeToSocket(socket.getBufHandler().getWriteBuffer(),true, false);
    }
}
 
Example 6
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException {
    while (length > 0) {
        int thisTime = length;
        if (socket.getBufHandler().getWriteBuffer().position() ==
                socket.getBufHandler().getWriteBuffer().capacity()
                || socket.getBufHandler().getWriteBuffer().remaining()==0) {
            flushBuffer();
        }
        if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
            thisTime = socket.getBufHandler().getWriteBuffer().remaining();
        }
        socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime);
        length = length - thisTime;
        offset = offset + thisTime;
    }
    NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment)socket.getAttachment();
    if ( ka!= null ) ka.access();//prevent timeouts for just doing client writes
}
 
Example 7
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param bytebuffer ByteBuffer
 * @param flip boolean
 * @return int
 * @throws IOException
 * TODO Fix non blocking write properly
 */
private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block, boolean flip) throws IOException {
    if ( flip ) bytebuffer.flip();

    int written = 0;
    NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment();
    if ( att == null ) throw new IOException("Key must be cancelled");
    long writeTimeout = att.getWriteTimeout();
    Selector selector = null;
    try {
        selector = pool.get();
    } catch ( IOException x ) {
        //ignore
    }
    try {
        written = pool.write(bytebuffer, socket, selector, writeTimeout, block);
        //make sure we are flushed 
        do {
            if (socket.flush(true,selector,writeTimeout)) break;
        }while ( true );
    }finally { 
        if ( selector != null ) pool.put(selector);
    }
    if ( block ) bytebuffer.clear(); //only clear
    return written;
}
 
Example 8
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException {
    while (length > 0) {
        int thisTime = length;
        if (socket.getBufHandler().getWriteBuffer().position() ==
                socket.getBufHandler().getWriteBuffer().capacity()
                || socket.getBufHandler().getWriteBuffer().remaining()==0) {
            flushBuffer();
        }
        if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
            thisTime = socket.getBufHandler().getWriteBuffer().remaining();
        }
        socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime);
        length = length - thisTime;
        offset = offset + thisTime;
    }
    NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment)socket.getAttachment();
    if ( ka!= null ) ka.access();//prevent timeouts for just doing client writes
}
 
Example 9
Source File: NioServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected void doFlush() throws IOException {
    NioEndpoint.KeyAttachment att =
            (NioEndpoint.KeyAttachment) channel.getAttachment();
    if (att == null) {
        throw new IOException("Key must be cancelled");
    }
    long writeTimeout = att.getWriteTimeout();
    Selector selector = null;
    try {
        selector = pool.get();
    } catch ( IOException x ) {
        //ignore
    }
    try {
        do {
            if (channel.flush(true, selector, writeTimeout)) {
                break;
            }
        } while (true);
    } finally {
        if (selector != null) {
            pool.put(selector);
        }
    }
}
 
Example 10
Source File: NioServletInputStream.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private int fillReadBuffer(boolean block) throws IOException {
    int nRead;
    if (block) {
        Selector selector = null;
        try {
            selector = pool.get();
        } catch ( IOException x ) {
            // Ignore
        }
        try {
            NioEndpoint.KeyAttachment att =
                    (NioEndpoint.KeyAttachment) channel.getAttachment();
            if (att == null) {
                throw new IOException("Key must be cancelled.");
            }
            nRead = pool.read(channel.getBufHandler().getReadBuffer(),
                    channel, selector, att.getTimeout());
        } catch (EOFException eof) {
            nRead = -1;
        } finally {
            if (selector != null) {
                pool.put(selector);
            }
        }
    } else {
        nRead = channel.read(channel.getBufHandler().getReadBuffer());
    }
    return nRead;
}
 
Example 11
Source File: NioServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private int doWriteInternal (boolean block, byte[] b, int off, int len)
        throws IOException {
    channel.getBufHandler().getWriteBuffer().clear();
    channel.getBufHandler().getWriteBuffer().put(b, off, len);
    channel.getBufHandler().getWriteBuffer().flip();

    int written = 0;
    NioEndpoint.KeyAttachment att =
            (NioEndpoint.KeyAttachment) channel.getAttachment();
    if (att == null) {
        throw new IOException("Key must be cancelled");
    }
    long writeTimeout = att.getWriteTimeout();
    Selector selector = null;
    try {
        selector = pool.get();
    } catch ( IOException x ) {
        //ignore
    }
    try {
        written = pool.write(channel.getBufHandler().getWriteBuffer(),
                channel, selector, writeTimeout, block);
    } finally {
        if (selector != null) {
            pool.put(selector);
        }
    }
    if (written < len) {
        channel.getPoller().add(channel, SelectionKey.OP_WRITE);
    }
    return written;
}
 
Example 12
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void resetTimeouts() {
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    if (!getErrorState().isError() && attach != null &&
            asyncStateMachine.isAsyncDispatching()) {
        long soTimeout = endpoint.getSoTimeout();

        //reset the timeout
        if (keepAlive) {
            attach.setTimeout(keepAliveTimeout);
        } else {
            attach.setTimeout(soTimeout);
        }
    }
}
 
Example 13
Source File: NioServletInputStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private int fillReadBuffer(boolean block) throws IOException {
    int nRead;
    if (block) {
        Selector selector = null;
        try {
            selector = pool.get();
        } catch ( IOException x ) {
            // Ignore
        }
        try {
            NioEndpoint.KeyAttachment att =
                    (NioEndpoint.KeyAttachment) channel.getAttachment();
            if (att == null) {
                throw new IOException("Key must be cancelled.");
            }
            nRead = pool.read(channel.getBufHandler().getReadBuffer(),
                    channel, selector, att.getTimeout());
        } catch (EOFException eof) {
            nRead = -1;
        } finally {
            if (selector != null) {
                pool.put(selector);
            }
        }
    } else {
        nRead = channel.read(channel.getBufHandler().getReadBuffer());
    }
    return nRead;
}
 
Example 14
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void resetTimeouts() {
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    if (!getErrorState().isError() && attach != null &&
            asyncStateMachine.isAsyncDispatching()) {
        long soTimeout = endpoint.getSoTimeout();

        //reset the timeout
        if (keepAlive) {
            attach.setTimeout(keepAliveTimeout);
        } else {
            attach.setTimeout(soTimeout);
        }
    }
}
 
Example 15
Source File: UpgradeNioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private int fillReadBuffer(boolean block) throws IOException {
    int nRead;
    if (block) {
        Selector selector = null;
        try {
            selector = pool.get();
        } catch ( IOException x ) {
            // Ignore
        }
        try {
            NioEndpoint.KeyAttachment att =
                    (NioEndpoint.KeyAttachment) nioChannel.getAttachment();
            if (att == null) {
                throw new IOException("Key must be cancelled.");
            }
            nRead = pool.read(nioChannel.getBufHandler().getReadBuffer(),
                    nioChannel, selector, att.getTimeout());
        } catch (EOFException eof) {
            nRead = -1;
        } finally {
            if (selector != null) {
                pool.put(selector);
            }
        }
    } else {
        nRead = nioChannel.read(nioChannel.getBufHandler().getReadBuffer());
    }
    return nRead;
}
 
Example 16
Source File: UpgradeNioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private synchronized int writeToSocket(byte[] bytes, int off, int len)
        throws IOException {

    nioChannel.getBufHandler().getWriteBuffer().clear();
    nioChannel.getBufHandler().getWriteBuffer().put(bytes, off, len);
    nioChannel.getBufHandler().getWriteBuffer().flip();

    int written = 0;
    NioEndpoint.KeyAttachment att =
            (NioEndpoint.KeyAttachment) nioChannel.getAttachment();
    if (att == null) {
        throw new IOException("Key must be cancelled");
    }
    long writeTimeout = att.getTimeout();
    Selector selector = null;
    try {
        selector = pool.get();
    } catch ( IOException x ) {
        //ignore
    }
    try {
        written = pool.write(nioChannel.getBufHandler().getWriteBuffer(),
                nioChannel, selector, writeTimeout, true);
    } finally {
        if (selector != null) {
            pool.put(selector);
        }
    }
    return written;
}
 
Example 17
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status) throws IOException {

    long soTimeout = endpoint.getSoTimeout();

    RequestInfo rp = request.getRequestProcessor();
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        if (!getErrorState().isError()) {
            if (attach != null) {
                attach.setComet(comet);
                if (comet) {
                    Integer comettimeout = (Integer) request.getAttribute(
                            org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                    if (comettimeout != null) {
                        attach.setTimeout(comettimeout.longValue());
                    }
                } else {
                    //reset the timeout
                    if (keepAlive) {
                        attach.setTimeout(keepAliveTimeout);
                    } else {
                        attach.setTimeout(soTimeout);
                    }
                }

            }
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        log.error(sm.getString("http11processor.request.process"), t);
        getAdapter().log(request, response, 0);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        if (keepAlive) {
            inputBuffer.nextRequest();
            outputBuffer.nextRequest();
            return SocketState.OPEN;
        } else {
            return SocketState.CLOSED;
        }
    } else {
        return SocketState.LONG;
    }
}
 
Example 18
Source File: InternalNioInputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Perform blocking read with a timeout if desired
 * @param timeout boolean - if we want to use the timeout data
 * @param block - true if the system should perform a blocking read, false otherwise
 * @return boolean - true if data was read, false is no data read, EOFException if EOF is reached
 * @throws IOException if a socket exception occurs
 * @throws EOFException if end of stream is reached
 */

private int readSocket(boolean timeout, boolean block) throws IOException {
    int nRead = 0;
    socket.getBufHandler().getReadBuffer().clear();
    if ( block ) {
        Selector selector = null;
        try {
            selector = pool.get();
        } catch ( IOException x ) {
            // Ignore
        }
        try {
            NioEndpoint.KeyAttachment att =
                    (NioEndpoint.KeyAttachment) socket.getAttachment();
            if (att == null) {
                throw new IOException("Key must be cancelled.");
            }
            nRead = pool.read(socket.getBufHandler().getReadBuffer(),
                    socket, selector,
                    socket.getIOChannel().socket().getSoTimeout());
        } catch ( EOFException eof ) {
            nRead = -1;
        } finally {
            if ( selector != null ) pool.put(selector);
        }
    } else {
        nRead = socket.read(socket.getBufHandler().getReadBuffer());
    }
    if (nRead > 0) {
        socket.getBufHandler().getReadBuffer().flip();
        socket.getBufHandler().getReadBuffer().limit(nRead);
        expand(nRead + pos);
        socket.getBufHandler().getReadBuffer().get(buf, pos, nRead);
        lastValid = pos + nRead;
        return nRead;
    } else if (nRead == -1) {
        //return false;
        throw new EOFException(sm.getString("iib.eof.error"));
    } else {
        return 0;
    }
}
 
Example 19
Source File: AjpNioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private int readSocket(byte[] buf, int pos, int n, boolean block)
        throws IOException {
    int nRead = 0;
    ByteBuffer readBuffer =
            socketWrapper.getSocket().getBufHandler().getReadBuffer();
    readBuffer.clear();
    if (n < readBuffer.capacity()) {
        readBuffer.limit(n);
    }
    if ( block ) {
        Selector selector = null;
        try {
            selector = pool.get();
        } catch ( IOException x ) {
            // Ignore
        }
        try {
            NioEndpoint.KeyAttachment att =
                    (NioEndpoint.KeyAttachment) socketWrapper.getSocket().getAttachment();
            if ( att == null ) throw new IOException("Key must be cancelled.");
            nRead = pool.read(readBuffer, socketWrapper.getSocket(),
                    selector, att.getTimeout());
        } catch ( EOFException eof ) {
            nRead = -1;
        } finally { 
            if ( selector != null ) pool.put(selector);
        }
    } else {
        nRead = socketWrapper.getSocket().read(readBuffer);
    }
    if (nRead > 0) {
        readBuffer.flip();
        readBuffer.limit(nRead);
        readBuffer.get(buf, pos, nRead);
        return nRead;
    } else if (nRead == -1) {
        //return false;
        throw new EOFException(sm.getString("iib.eof.error"));
    } else {
        return 0;
    }
}
 
Example 20
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status) throws IOException {

    long soTimeout = endpoint.getSoTimeout();

    RequestInfo rp = request.getRequestProcessor();
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        if (!getErrorState().isError()) {
            if (attach != null) {
                attach.setComet(comet);
                if (comet) {
                    Integer comettimeout = (Integer) request.getAttribute(
                            org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                    if (comettimeout != null) {
                        attach.setTimeout(comettimeout.longValue());
                    }
                } else {
                    //reset the timeout
                    if (keepAlive) {
                        attach.setTimeout(keepAliveTimeout);
                    } else {
                        attach.setTimeout(soTimeout);
                    }
                }

            }
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        log.error(sm.getString("http11processor.request.process"), t);
        getAdapter().log(request, response, 0);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        if (keepAlive) {
            inputBuffer.nextRequest();
            outputBuffer.nextRequest();
            return SocketState.OPEN;
        } else {
            return SocketState.CLOSED;
        }
    } else {
        return SocketState.LONG;
    }
}