예제 #1
0
  /**
   * Handle OP_READ.
   *
   * @param ctx {@link Context}
   * @param key {@link SelectionKey}
   * @return false if handled by a {@link CallbackHandler}, otherwise true
   */
  public boolean onReadInterest(final SelectionKey key, final Context ctx) throws IOException {
    // disable OP_READ on key before doing anything else
    key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));

    if (asyncQueueReader.isReady(key)) {
      invokeAsyncQueueReader(pollContext(ctx, key, Context.OpType.OP_READ));
      return false;
    }
    Object attach = SelectionKeyAttachment.getAttachment(key);
    if (attach instanceof CallbackHandler) {
      NIOContext context = pollContext(ctx, key, Context.OpType.OP_READ);
      invokeCallbackHandler((CallbackHandler) attach, context);
      return false;
    }
    return true;
  }
예제 #2
0
  /** {@inheritDoc} */
  public void closeChannel(SelectableChannel channel) {
    // channel could be either SocketChannel or ServerSocketChannel
    if (channel instanceof SocketChannel) {
      Socket socket = ((SocketChannel) channel).socket();

      try {
        if (!socket.isInputShutdown()) socket.shutdownInput();
      } catch (IOException e) {
        logger.log(Level.FINEST, "Unexpected exception during channel inputShutdown", e);
      }

      try {
        if (!socket.isOutputShutdown()) socket.shutdownOutput();
      } catch (IOException e) {
        logger.log(Level.FINEST, "Unexpected exception during channel outputShutdown", e);
      }

      try {
        socket.close();
      } catch (IOException e) {
        logger.log(Level.FINEST, "Unexpected exception during socket close", e);
      }
    }

    try {
      channel.close();
    } catch (IOException e) {
      logger.log(Level.FINEST, "Unexpected exception during channel close", e);
    }

    if (asyncQueueReader != null) {
      asyncQueueReader.onClose(channel);
    }

    if (asyncQueueWriter != null) {
      asyncQueueWriter.onClose(channel);
    }
  }
예제 #3
0
  /** Shuntdown this instance by closing its Selector and associated channels. */
  public void shutdown() {
    // If shutdown was called for this SelectorHandler
    if (isShutDown.getAndSet(true)) return;

    stateHolder.setState(State.STOPPED);

    if (selector != null) {
      try {
        boolean isContinue = true;
        while (isContinue) {
          try {
            for (SelectionKey selectionKey : selector.keys()) {
              selectionKeyHandler.close(selectionKey);
            }

            isContinue = false;
          } catch (ConcurrentModificationException e) {
            // ignore
          }
        }
      } catch (ClosedSelectorException e) {
        // If Selector is already closed - OK
      }
    }

    try {
      if (serverSocket != null) {
        serverSocket.close();
        serverSocket = null;
      }
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "serverSocket.close", ex);
    }

    try {
      if (serverSocketChannel != null) {
        serverSocketChannel.close();
        serverSocketChannel = null;
      }
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "serverSocketChannel.close", ex);
    }

    try {
      if (selector != null) selector.close();
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "selector.close", ex);
    }

    if (asyncQueueReader != null) {
      asyncQueueReader.close();
      asyncQueueReader = null;
    }

    if (asyncQueueWriter != null) {
      asyncQueueWriter.close();
      asyncQueueWriter = null;
    }

    selectorHandlerTasks.clear();

    attributes = null;
  }