Exemplo n.º 1
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);
        }
      }
    }