Package org.apache.tomcat.util.net.NioEndpoint

Examples of org.apache.tomcat.util.net.NioEndpoint.KeyAttachment


            }
            request.updateCounters();
           
            // Do sendfile as needed: add socket to sendfile and end
            if (sendfileData != null && !error) {
                KeyAttachment ka = (KeyAttachment)socket.getAttachment(false);
                ka.setSendfileData(sendfileData);
                sendfileData.keepAlive = keepAlive;
                SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
                //do the first write on this thread, might as well
                openSocket = socket.getPoller().processSendfile(key,ka,true);
                break;
View Full Code Here


                        time = System.currentTimeMillis(); //reset our timeout timer
                        continue; //we successfully wrote, try again without a selector
                    }
                }
                if ( key == null ) throw new IOException("Key no longer registered");
                KeyAttachment att = (KeyAttachment) key.attachment();
                try {
                    if ( att.getLatch()==null || att.getLatch().getCount()==0) att.startLatch(1);
                    if ( att.interestOps() == 0) socket.getPoller().add(socket,SelectionKey.OP_WRITE);
                    att.getLatch().await(writeTimeout,TimeUnit.MILLISECONDS);
                }catch (InterruptedException ignore) {
                    Thread.interrupted();
                }
                if ( att.getLatch()!=null && att.getLatch().getCount()> 0) {
                    //we got interrupted, but we haven't received notification from the poller.
                    keycount = 0;
                }else {
                    //latch countdown has happened
                    keycount = 1;
                    att.resetLatch();
                }

                if (writeTimeout > 0 && (keycount == 0))
                    timedout = (System.currentTimeMillis() - time) >= writeTimeout;
            } //while
View Full Code Here

                        throw new EOFException();
                    read += cnt;
                    if (cnt > 0)
                        break;
                }
                KeyAttachment att = (KeyAttachment) key.attachment();
                try {
                    if ( att.getLatch()==null || att.getLatch().getCount()==0) att.startLatch(1);
                    if ( att.interestOps() == 0) socket.getPoller().add(socket,SelectionKey.OP_READ);
                    att.getLatch().await(readTimeout,TimeUnit.MILLISECONDS);
                }catch (InterruptedException ignore) {
                    Thread.interrupted();
                }
                if ( att.getLatch()!=null && att.getLatch().getCount()> 0) {
                    //we got interrupted, but we haven't received notification from the poller.
                    keycount = 0;
                }else {
                    //latch countdown has happened
                    keycount = 1;
                    att.resetLatch();
                }
                if (readTimeout > 0 && (keycount == 0))
                    timedout = (System.currentTimeMillis() - time) >= readTimeout;
            } //while
            if (timedout)
View Full Code Here

        int keepAliveTimeout = endpoint.getKeepAliveTimeout();

        boolean keptAlive = false;
        boolean openSocket = false;
        boolean recycle = true;
        final KeyAttachment ka = (KeyAttachment)socket.getAttachment(false);
       
        while (!error && keepAlive && !comet && !async) {
            //always default to our soTimeout
            ka.setTimeout(soTimeout);
            // Parsing the request header
            try {
                if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) {
                    socket.getIOChannel().socket().setSoTimeout((int)soTimeout);
                }
                if (!inputBuffer.parseRequestLine(keptAlive)) {
                    //no data available yet, since we might have read part
                    //of the request line, we can't recycle the processor
                    openSocket = true;
                    recycle = false;
                    if (inputBuffer.getParsingRequestLinePhase()<2) {
                        //keep alive timeout here
                        if (keepAliveTimeout>0) ka.setTimeout(keepAliveTimeout);
                    }
                    break;
                }
                keptAlive = true;
                if ( !inputBuffer.parseHeaders() ) {
                    //we've read part of the request, don't recycle it
                    //instead associate it with the socket
                    openSocket = true;
                    recycle = false;
                    break;
                }
                request.setStartTime(System.currentTimeMillis());
                if (!disableUploadTimeout) { //only for body, not for request headers
                    socket.getIOChannel().socket().setSoTimeout(timeout);
                }
            } catch (IOException e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("http11processor.header.parse"), e);
                }
                error = true;
                break;
            } catch (Throwable t) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("http11processor.header.parse"), t);
                }
                // 400 - Bad Request
                response.setStatus(400);
                adapter.log(request, response, 0);
                error = true;
            }

            if (!error) {
                // Setting up filters, and parse some request headers
                rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);
                try {
                    prepareRequest();
                } catch (Throwable t) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("http11processor.request.prepare"), t);
                    }
                    // 400 - Internal Server Error
                    response.setStatus(400);
                    adapter.log(request, response, 0);
                    error = true;
                }
            }
           
            if (maxKeepAliveRequests == 1 )
                keepAlive = false;
            if (maxKeepAliveRequests > 0 && ka.decrementKeepAlive() <= 0)
                keepAlive = false;

            // Process the request in the adapter
            if (!error) {
                try {
                    rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
                    adapter.service(request, response);
                    // Handle when the response was committed before a serious
                    // error occurred.  Throwing a ServletException should both
                    // set the status to 500 and set the errorException.
                    // If we fail here, then the response is likely already
                    // committed, so we can't try and set headers.
                    if(keepAlive && !error) { // Avoid checking twice.
                        error = response.getErrorException() != null ||
                                statusDropsConnection(response.getStatus());
                    }
                    // Comet support
                    SelectionKey key = socket.getIOChannel().keyFor(socket.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.tomcat.comet.timeout");
                                if (comettimeout != null) attach.setTimeout(comettimeout.longValue());
                            }
                        }
                    }
                } catch (InterruptedIOException e) {
                    error = true;
                } catch (Throwable t) {
                    log.error(sm.getString("http11processor.request.process"), t);
                    // 500 - Internal Server Error
                    response.setStatus(500);
                    adapter.log(request, response, 0);
                    error = true;
                }
            }

            // Finish the handling of the request
            if (!comet && !async) {
                // If we know we are closing the connection, don't drain input.
                // This way uploading a 100GB file doesn't tie up the thread
                // if the servlet has rejected it.
                if(error)
                    inputBuffer.setSwallowInput(false);
                endRequest();
            }

            // If there was an error, make sure the request is counted as
            // and error, and update the statistics counter
            if (error) {
                response.setStatus(500);
            }
            request.updateCounters();

            if (!comet && !async) {
                // Next request
                inputBuffer.nextRequest();
                outputBuffer.nextRequest();
            }
           
            // Do sendfile as needed: add socket to sendfile and end
            if (sendfileData != null && !error) {
                ka.setSendfileData(sendfileData);
                sendfileData.keepAlive = keepAlive;
                SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
                //do the first write on this thread, might as well
                openSocket = socket.getPoller().processSendfile(key,ka,true,true);
                break;
View Full Code Here

                } catch (Exception x) {
                    log.error("", x);
                }
            } else {
                final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
                final KeyAttachment att = (KeyAttachment) key.attachment();
                try {
                    if (key != null) {
                        key.interestOps(interestOps);
                        att.interestOps(interestOps);
                    }
                }
                catch (CancelledKeyException ckx) {
                    try {
                        if (key != null && key.attachment() != null) {
                            KeyAttachment ka = (KeyAttachment) key.attachment();
                            ka.setError(true); //set to collect this socket immediately
                        }
                        try {
                            socket.close();
                        }
                        catch (Exception ignore) {}
View Full Code Here

        }
       
        public void register(final NioChannel socket)
        {
            socket.setPoller(this);
            KeyAttachment key = keyCache.poll();
            final KeyAttachment ka = key!=null?key:new KeyAttachment();
            ka.reset(this,socket);
            PollerEvent r = eventCache.poll();
            ka.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER turns into.
            if ( r==null) r = new PollerEvent(socket,ka,OP_REGISTER);
            else r.reset(socket,ka,OP_REGISTER);
            addEvent(r);
        }
View Full Code Here

            addEvent(r);
        }
       
        public void cancelledKey(SelectionKey key, SocketStatus status) {
            try {
                KeyAttachment ka = (KeyAttachment) key.attachment();
                if (ka != null && ka.getComet()) {
                    //the comet event takes care of clean up
                    processSocket(ka.getChannel(), status);
                }else {
                    if (key.isValid()) key.cancel();
                    if (key.channel().isOpen()) key.channel().close();
                    key.attach(null);
                }
View Full Code Here

                // Walk through the collection of ready keys and dispatch
                // any active event.
                while (iterator != null && iterator.hasNext()) {
                    SelectionKey sk = (SelectionKey) iterator.next();
                    iterator.remove();
                    KeyAttachment attachment = (KeyAttachment)sk.attachment();
                    try {
                        if ( sk.isValid() && attachment != null ) {
                            attachment.access();
                            sk.attach(attachment);
                            sk.interestOps(0); //this is a must, so that we don't have multiple threads messing with the socket
                            attachment.interestOps(0);
                            NioChannel channel = attachment.getChannel();
                            if (sk.isReadable() || sk.isWritable() ) {
                                if ( attachment.getComet() ) {
                                    if (!processSocket(channel, SocketStatus.OPEN))
                                        processSocket(channel, SocketStatus.DISCONNECT);
                                } else {
                                    boolean close = (!processSocket(channel));
                                    if ( close ) {
View Full Code Here

            //timeout
            Set<SelectionKey> keys = selector.keys();
            for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext(); ) {
                SelectionKey key = iter.next();
                try {
                    KeyAttachment ka = (KeyAttachment) key.attachment();
                    if ( ka == null ) {
                        cancelledKey(key, SocketStatus.ERROR); //we don't support any keys without attachments
                    } else if ( ka.getError() ) {
                        cancelledKey(key, SocketStatus.DISCONNECT);
                    }else if ((ka.interestOps()&SelectionKey.OP_READ) == SelectionKey.OP_READ) {
                        //only timeout sockets that we are waiting for a read from
                        long delta = now - ka.getLastAccess();
                        long timeout = (ka.getTimeout()==-1)?((long) socketProperties.getSoTimeout()):(ka.getTimeout());
                        boolean isTimedout = delta > timeout;
                        if (isTimedout) {
                            key.interestOps(0);
                            ka.interestOps(0); //avoid duplicate timeout calls
                            cancelledKey(key, SocketStatus.TIMEOUT);
                        } else {
                            long nextTime = now+(timeout-delta);
                            nextExpiration = (nextTime < nextExpiration)?nextTime:nextExpiration;
                        }
View Full Code Here

                        if ( handshake == 0 ) {
                            // Process the request from this socket
                            if ((status != null) && (handler.event(socket, status) == Handler.SocketState.CLOSED)) {
                                // Close socket and pool
                                try {
                                    KeyAttachment att = (KeyAttachment)socket.getAttachment(true);
                                    try {socket.close();}catch (Exception ignore){}
                                    if ( socket.isOpen() ) socket.close(true);
                                    key.cancel();
                                    key.attach(null);
                                    nioChannels.offer(socket);
                                    if ( att!=null ) keyCache.offer(att);
                                }catch ( Exception x ) {
                                    log.error("",x);
                                }
                            } else if ((status == null) && (handler.process(socket) == Handler.SocketState.CLOSED)) {
                                // Close socket and pool
                                try {
                                    KeyAttachment att = (KeyAttachment)socket.getAttachment(true);
                                    try {socket.close();}catch (Exception ignore){}
                                    if ( socket.isOpen() ) socket.close(true);
                                    key.cancel();
                                    key.attach(null);
                                    nioChannels.offer(socket);
                                    if ( att!=null ) keyCache.offer(att);
                                }catch ( Exception x ) {
                                    log.error("",x);
                                }
                            }
                        } else if (handshake == -1 ) {
                            socket.getPoller().cancelledKey(key,SocketStatus.DISCONNECT);
                            try {socket.close(true);}catch (IOException ignore){}
                            nioChannels.offer(socket);
                        } else {
                            final SelectionKey fk = key;
                            final int intops = handshake;
                            final KeyAttachment ka = (KeyAttachment)fk.attachment();
                            ka.getPoller().add(socket,intops);
                        }
                    }
                } finally {
                    //dereference socket to let GC do its job
                    socket = null;
View Full Code Here

TOP

Related Classes of org.apache.tomcat.util.net.NioEndpoint.KeyAttachment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.