org.apache.tomcat.util.net.NioChannel Java Examples

The following examples show how to use org.apache.tomcat.util.net.NioChannel. 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: AjpNioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketChannel socket) {
    if (log.isDebugEnabled()) 
        log.debug("Iterating through our connections to release a socket channel:"+socket);
    boolean released = false;
    Iterator<java.util.Map.Entry<NioChannel, Processor<NioChannel>>> it = connections.entrySet().iterator();
    while (it.hasNext()) {
        java.util.Map.Entry<NioChannel, Processor<NioChannel>> entry = it.next();
        if (entry.getKey().getIOChannel()==socket) {
            it.remove();
            Processor<NioChannel> result = entry.getValue();
            result.recycle(true);
            unregister(result);
            released = true;
            break;
        }
    }
    if (log.isDebugEnabled()) 
        log.debug("Done iterating through our connections to release a socket channel:"+socket +" released:"+released);
}
 
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: AjpNioProtocol.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketChannel socket) {
    if (log.isDebugEnabled()) 
        log.debug("Iterating through our connections to release a socket channel:"+socket);
    boolean released = false;
    Iterator<java.util.Map.Entry<NioChannel, Processor<NioChannel>>> it = connections.entrySet().iterator();
    while (it.hasNext()) {
        java.util.Map.Entry<NioChannel, Processor<NioChannel>> entry = it.next();
        if (entry.getKey().getIOChannel()==socket) {
            it.remove();
            Processor<NioChannel> result = entry.getValue();
            result.recycle(true);
            unregister(result);
            released = true;
            break;
        }
    }
    if (log.isDebugEnabled()) 
        log.debug("Done iterating through our connections to release a socket channel:"+socket +" released:"+released);
}
 
Example #4
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean breakKeepAliveLoop(SocketWrapper<NioChannel> socketWrapper) {
    openSocket = keepAlive;
    // Do sendfile as needed: add socket to sendfile and end
    if (sendfileData != null && !getErrorState().isError()) {
        ((KeyAttachment) socketWrapper).setSendfileData(sendfileData);
        sendfileData.keepAlive = keepAlive;
        SelectionKey key = socketWrapper.getSocket().getIOChannel().keyFor(
                socketWrapper.getSocket().getPoller().getSelector());
        //do the first write on this thread, might as well
        if (socketWrapper.getSocket().getPoller().processSendfile(key,
                (KeyAttachment) socketWrapper, true)) {
            sendfileInProgress = true;
        } else {
            // Write failed
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("http11processor.sendfile.error"));
            }
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        return true;
    }
    return false;
}
 
Example #5
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 #6
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketChannel socket) {
    if (log.isDebugEnabled())
        log.debug("Iterating through our connections to release a socket channel:"+socket);
    boolean released = false;
    Iterator<java.util.Map.Entry<NioChannel, Processor<NioChannel>>> it = connections.entrySet().iterator();
    while (it.hasNext()) {
        java.util.Map.Entry<NioChannel, Processor<NioChannel>> entry = it.next();
        if (entry.getKey().getIOChannel()==socket) {
            it.remove();
            Processor<NioChannel> result = entry.getValue();
            result.recycle(true);
            unregister(result);
            released = true;
            break;
        }
    }
    if (log.isDebugEnabled())
        log.debug("Done iterating through our connections to release a socket channel:"+socket +" released:"+released);
}
 
Example #7
Source File: UpgradeNioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public UpgradeNioProcessor(SocketWrapper<NioChannel> wrapper,
        UpgradeInbound upgradeInbound, NioSelectorPool pool) {
    super(upgradeInbound);

    wrapper.setTimeout(upgradeInbound.getReadTimeout());

    this.nioChannel = wrapper.getSocket();
    this.pool = pool;
    this.maxRead = nioChannel.getBufHandler().getReadBuffer().capacity();
    this.maxWrite = nioChannel.getBufHandler().getWriteBuffer().capacity();
}
 
Example #8
Source File: InternalNioInputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void init(SocketWrapper<NioChannel> socketWrapper,
        AbstractEndpoint<NioChannel> endpoint) throws IOException {

    socket = socketWrapper.getSocket();
    socketReadBufferSize =
        socket.getBufHandler().getReadBuffer().capacity();

    int bufLength = headerBufferSize + socketReadBufferSize;
    if (buf == null || buf.length < bufLength) {
        buf = new byte[bufLength];
    }

    pool = ((NioEndpoint)endpoint).getSelectorPool();
}
 
Example #9
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void init(SocketWrapper<NioChannel> socketWrapper,
        AbstractEndpoint<NioChannel> endpoint) throws IOException {

    socket = socketWrapper.getSocket();
    pool = ((NioEndpoint)endpoint).getSelectorPool();
}
 
Example #10
Source File: NioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public NioProcessor(SocketWrapper<NioChannel> wrapper,
        HttpUpgradeHandler httpUpgradeProcessor, NioSelectorPool pool,
        int asyncWriteBufferSize) {
    super(httpUpgradeProcessor,
            new NioServletInputStream(wrapper, pool),
            new NioServletOutputStream(wrapper, asyncWriteBufferSize, pool));

    wrapper.setTimeout(INFINITE_TIMEOUT);
}
 
Example #11
Source File: NioServletOutputStream.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public NioServletOutputStream(SocketWrapper<NioChannel> socketWrapper,
        int asyncWriteBufferSize, NioSelectorPool pool) {
    super(asyncWriteBufferSize);
    channel = socketWrapper.getSocket();
    this.pool = pool;
    maxWrite = channel.getBufHandler().getWriteBuffer().capacity();
}
 
Example #12
Source File: UpgradeNioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public UpgradeNioProcessor(SocketWrapper<NioChannel> wrapper,
        UpgradeInbound upgradeInbound, NioSelectorPool pool) {
    super(upgradeInbound);

    wrapper.setTimeout(upgradeInbound.getReadTimeout());

    this.nioChannel = wrapper.getSocket();
    this.pool = pool;
    this.maxRead = nioChannel.getBufHandler().getReadBuffer().capacity();
    this.maxWrite = nioChannel.getBufHandler().getWriteBuffer().capacity();
}
 
Example #13
Source File: AjpNioProtocol.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the handler once the processor is no longer
 * required.
 */
@Override
public void release(SocketWrapper<NioChannel> socket,
        Processor<NioChannel> processor, boolean isSocketClosing,
        boolean addToPoller) {
    processor.recycle(isSocketClosing);
    recycledProcessors.offer(processor);
    if (addToPoller) {
        // The only time this method is called with addToPoller == true
        // is when the socket is in keep-alive so set the appropriate
        // timeout.
        socket.setTimeout(getProtocol().getKeepAliveTimeout());
        socket.getSocket().getPoller().add(socket.getSocket());
    }
}
 
Example #14
Source File: AjpNioProtocol.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketWrapper<NioChannel> socket) {
    Processor<NioChannel> processor =
            connections.remove(socket.getSocket());
    if (processor != null) {
        processor.recycle(true);
        recycledProcessors.offer(processor);
    }
}
 
Example #15
Source File: AjpNioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketWrapper<NioChannel> socket) {
    Processor<NioChannel> processor =
            connections.remove(socket.getSocket());
    if (processor != null) {
        processor.recycle(true);
        recycledProcessors.offer(processor);
    }
}
 
Example #16
Source File: NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public NioProcessor(SocketWrapper<NioChannel> wrapper,
        HttpUpgradeHandler httpUpgradeProcessor, NioSelectorPool pool,
        int asyncWriteBufferSize) {
    super(httpUpgradeProcessor,
            new NioServletInputStream(wrapper, pool),
            new NioServletOutputStream(wrapper, asyncWriteBufferSize, pool));

    wrapper.setTimeout(INFINITE_TIMEOUT);
}
 
Example #17
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean breakKeepAliveLoop(SocketWrapper<NioChannel> socketWrapper) {
    openSocket = keepAlive;
    // Do sendfile as needed: add socket to sendfile and end
    if (sendfileData != null && !getErrorState().isError()) {
        ((KeyAttachment) socketWrapper).setSendfileData(sendfileData);
        sendfileData.keepAlive = keepAlive;
        SelectionKey key = socketWrapper.getSocket().getIOChannel().keyFor(
                socketWrapper.getSocket().getPoller().getSelector());
        //do the first write on this thread, might as well
        switch (socketWrapper.getSocket().getPoller().processSendfile(
                key, (KeyAttachment) socketWrapper, true)) {
        case DONE:
            // If sendfile is complete, no need to break keep-alive loop
            sendfileData = null;
            return false;
        case PENDING:
            sendfileInProgress = true;
            return true;
        case ERROR:
            // Write failed
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("http11processor.sendfile.error"));
            }
            setErrorState(ErrorState.CLOSE_NOW, null);
            return true;
        }
    }
    return false;
}
 
Example #18
Source File: AjpNioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the handler once the processor is no longer
 * required.
 */
@Override
public void release(SocketWrapper<NioChannel> socket,
        Processor<NioChannel> processor, boolean isSocketClosing,
        boolean addToPoller) {
    processor.recycle(isSocketClosing);
    recycledProcessors.offer(processor);
    if (addToPoller) {
        // The only time this method is called with addToPoller == true
        // is when the socket is in keep-alive so set the appropriate
        // timeout.
        socket.setTimeout(getProtocol().getKeepAliveTimeout());
        socket.getSocket().getPoller().add(socket.getSocket());
    }
}
 
Example #19
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected Processor<NioChannel> createUpgradeProcessor(
        SocketWrapper<NioChannel> socket,
        HttpUpgradeHandler httpUpgradeProcessor)
        throws IOException {
    return new NioProcessor(socket, httpUpgradeProcessor,
            proto.getEndpoint().getSelectorPool(),
            proto.getUpgradeAsyncWriteBufferSize());
}
 
Example #20
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated  Will be removed in Tomcat 8.0.x.
 */
@Deprecated
@Override
protected Processor<NioChannel> createUpgradeProcessor(
        SocketWrapper<NioChannel> socket,
        org.apache.coyote.http11.upgrade.UpgradeInbound inbound)
        throws IOException {
    return new org.apache.coyote.http11.upgrade.UpgradeNioProcessor(
            socket, inbound,
            ((Http11NioProtocol) getProtocol()).getEndpoint().getSelectorPool());
}
 
Example #21
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void longPoll(SocketWrapper<NioChannel> socket,
        Processor<NioChannel> processor) {

    if (processor.isAsync()) {
        socket.setAsync(true);
    } else {
        // Either:
        //  - this is comet request
        //  - this is an upgraded connection
        //  - the request line/headers have not been completely
        //    read
        socket.getSocket().getPoller().add(socket.getSocket());
    }
}
 
Example #22
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void initSsl(SocketWrapper<NioChannel> socket,
        Processor<NioChannel> processor) {
    if (proto.isSSLEnabled() &&
            (proto.sslImplementation != null)
            && (socket.getSocket() instanceof SecureNioChannel)) {
        SecureNioChannel ch = (SecureNioChannel)socket.getSocket();
        processor.setSslSupport(
                proto.sslImplementation.getSSLSupport(
                        ch.getSslEngine().getSession()));
    } else {
        processor.setSslSupport(null);
    }

}
 
Example #23
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the handler once the processor is no longer
 * required.
 *
 * @param socket
 * @param processor
 * @param isSocketClosing   Not used in HTTP
 * @param addToPoller
 */
@Override
public void release(SocketWrapper<NioChannel> socket,
        Processor<NioChannel> processor, boolean isSocketClosing,
        boolean addToPoller) {
    processor.recycle(isSocketClosing);
    recycledProcessors.offer(processor);
    if (addToPoller) {
        // The only time this method is called with addToPoller == true
        // is when the socket is in keep-alive so set the appropriate
        // timeout.
        socket.setTimeout(getProtocol().getKeepAliveTimeout());
        socket.getSocket().getPoller().add(socket.getSocket());
    }
}
 
Example #24
Source File: Http11NioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Expected to be used by the Poller to release resources on socket
 * close, errors etc.
 */
@Override
public void release(SocketWrapper<NioChannel> socket) {
    Processor<NioChannel> processor =
        connections.remove(socket.getSocket());
    if (processor != null) {
        processor.recycle(true);
        recycledProcessors.offer(processor);
    }
}
 
Example #25
Source File: NioServletOutputStream.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public NioServletOutputStream(SocketWrapper<NioChannel> socketWrapper,
        int asyncWriteBufferSize, NioSelectorPool pool) {
    super(asyncWriteBufferSize);
    channel = socketWrapper.getSocket();
    this.pool = pool;
    maxWrite = channel.getBufHandler().getWriteBuffer().capacity();
}
 
Example #26
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void init(SocketWrapper<NioChannel> socketWrapper,
        AbstractEndpoint<NioChannel> endpoint) throws IOException {

    socket = socketWrapper.getSocket();
    pool = ((NioEndpoint)endpoint).getSelectorPool();
}
 
Example #27
Source File: InternalNioInputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void init(SocketWrapper<NioChannel> socketWrapper,
        AbstractEndpoint<NioChannel> endpoint) throws IOException {

    socket = socketWrapper.getSocket();
    socketReadBufferSize =
        socket.getBufHandler().getReadBuffer().capacity();

    int bufLength = headerBufferSize + socketReadBufferSize;
    if (buf == null || buf.length < bufLength) {
        buf = new byte[bufLength];
    }

    pool = ((NioEndpoint)endpoint).getSelectorPool();
}
 
Example #28
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractOutputBuffer<NioChannel> getOutputBuffer() {
    return outputBuffer;
}
 
Example #29
Source File: AjpNioProtocol.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractProtocol<NioChannel> getProtocol() {
    return proto;
}
 
Example #30
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractInputBuffer<NioChannel> getInputBuffer() {
    return inputBuffer;
}