예제 #1
0
 @Override
 public void run() {
   if (interestOps == OP_REGISTER) {
     try {
       socket
           .getIOChannel()
           .register(socket.getPoller().getSelector(), SelectionKey.OP_READ, socketWrapper);
     } catch (Exception x) {
       log.error(sm.getString("endpoint.nio.registerFail"), x);
     }
   } else {
     final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
     try {
       if (key == null) {
         // The key was cancelled (e.g. due to socket closure)
         // and removed from the selector while it was being
         // processed. Count down the connections at this point
         // since it won't have been counted down when the socket
         // closed.
         socket.socketWrapper.getEndpoint().countDownConnection();
       } else {
         final NioSocketWrapper socketWrapper = (NioSocketWrapper) key.attachment();
         if (socketWrapper != null) {
           // we are registering the key to start with, reset the fairness counter.
           int ops = key.interestOps() | interestOps;
           socketWrapper.interestOps(ops);
           key.interestOps(ops);
         } else {
           socket.getPoller().cancelledKey(key);
         }
       }
     } catch (CancelledKeyException ckx) {
       try {
         socket.getPoller().cancelledKey(key);
       } catch (Exception ignore) {
       }
     }
   }
 }
예제 #2
0
 private void close(NioChannel socket, SelectionKey key) {
   try {
     if (socket.getPoller().cancelledKey(key) != null) {
       // SocketWrapper (attachment) was removed from the
       // key - recycle the key. This can only happen once
       // per attempted closure so it is used to determine
       // whether or not to return the key to the cache.
       // We do NOT want to do this more than once - see BZ
       // 57340 / 57943.
       if (running && !paused) {
         if (!nioChannels.push(socket)) {
           socket.free();
         }
       }
     }
   } catch (Exception x) {
     log.error("", x);
   }
 }
예제 #3
0
    @Override
    protected void doRun() {
      NioChannel socket = socketWrapper.getSocket();
      SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());

      try {
        int handshake = -1;

        try {
          if (key != null) {
            // For STOP there is no point trying to handshake as the
            // Poller has been stopped.
            if (socket.isHandshakeComplete() || event == SocketEvent.STOP) {
              handshake = 0;
            } else {
              handshake = socket.handshake(key.isReadable(), key.isWritable());
              // The handshake process reads/writes from/to the
              // socket. status may therefore be OPEN_WRITE once
              // the handshake completes. However, the handshake
              // happens when the socket is opened so the status
              // must always be OPEN_READ after it completes. It
              // is OK to always set this as it is only used if
              // the handshake completes.
              event = SocketEvent.OPEN_READ;
            }
          }
        } catch (IOException x) {
          handshake = -1;
          if (log.isDebugEnabled()) log.debug("Error during SSL handshake", x);
        } catch (CancelledKeyException ckx) {
          handshake = -1;
        }
        if (handshake == 0) {
          SocketState state = SocketState.OPEN;
          // Process the request from this socket
          if (event == null) {
            state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
          } else {
            state = getHandler().process(socketWrapper, event);
          }
          if (state == SocketState.CLOSED) {
            close(socket, key);
          }
        } else if (handshake == -1) {
          close(socket, key);
        } else if (handshake == SelectionKey.OP_READ) {
          socketWrapper.registerReadInterest();
        } else if (handshake == SelectionKey.OP_WRITE) {
          socketWrapper.registerWriteInterest();
        }
      } catch (CancelledKeyException cx) {
        socket.getPoller().cancelledKey(key);
      } catch (VirtualMachineError vme) {
        ExceptionUtils.handleThrowable(vme);
      } catch (Throwable t) {
        log.error("", t);
        socket.getPoller().cancelledKey(key);
      } finally {
        socketWrapper = null;
        event = null;
        // return to cache
        if (running && !paused) {
          processorCache.push(this);
        }
      }
    }