java.nio.channels.SelectableChannel#keyFor()源码实例Demo

下面列出了java.nio.channels.SelectableChannel#keyFor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bt   文件: NIOConnectionManager.java
void connectionChecks() throws IOException {
	if((iterations & 0x0F) != 0)
		return;

	long now = System.currentTimeMillis();
	
	if(now - lastConnectionCheck < 500)
		return;
	lastConnectionCheck = now;
	
	for(Selectable conn : new ArrayList<>(connections)) {
		conn.doStateChecks(now);
		SelectableChannel ch = conn.getChannel();
		SelectionKey k;
		if(ch == null || (k = ch.keyFor(selector)) == null || !k.isValid())
			connections.remove(conn);
	}
}
 
源代码2 项目: baratine   文件: PollTcpManagerNio.java
private boolean removeConnection(PollController conn)
{
  if (conn == null) {
    return false;
  }
  
  SocketBar socket = conn.getSocket();
  SelectableChannel channel = socket.selectableChannel();
  
  SelectionKey key = channel.keyFor(_selector);
  
  if (key != null) {
    key.cancel();
  }
  
  remove(conn);
      
  return true;
}
 
源代码3 项目: mldht   文件: NIOConnectionManager.java
void connectionChecks() throws IOException {
	if((iterations & 0x0F) != 0)
		return;

	long now = System.currentTimeMillis();
	
	if(now - lastConnectionCheck < 500)
		return;
	lastConnectionCheck = now;
	
	for(Selectable conn : new ArrayList<>(connections)) {
		conn.doStateChecks(now);
		SelectableChannel ch = conn.getChannel();
		SelectionKey k;
		if(ch == null || (k = ch.keyFor(selector)) == null || !k.isValid())
			connections.remove(conn);
	}
}
 
源代码4 项目: twister2   文件: Progress.java
private void addInterest(SelectableChannel channel,
                           int operation,
                           SelectHandler callback)
      throws ClosedChannelException {

    SelectionKey key = channel.keyFor(selector);

    if (key == null) {
      channel.register(selector, operation, callback);
    } else if (!key.isValid()) {
      throw new RuntimeException(
          String.format("Unable to add %d in %s due to key is invalid", operation, channel));
    } else {
      // Key is not null and key is valid
      if ((key.interestOps() & operation) != 0) {
        LOG.severe(String.format("%d has been registered in %s", operation, channel));
//        throw new RuntimeException(
//            String.format("%d has been registered in %s", operation, channel));
      }
      if (key.attachment() == null) {
        key.attach(callback);
      } else {
        if (callback != key.attachment()) {
          throw new RuntimeException("Unmatched SelectHandler has already been attached"
              + " for other operation");
        }
      }
      key.interestOps(key.interestOps() | operation);
    }
  }
 
源代码5 项目: twister2   文件: Progress.java
private void removeInterest(SelectableChannel channel, int operation) {
  SelectionKey key = channel.keyFor(selector);

  // Exception would be thrown if key is null or key is inValid
  // We do not need double check it ahead
  key.interestOps(key.interestOps() & (~operation));
}
 
源代码6 项目: incubator-heron   文件: NIOLooper.java
/**
 * Register an operation interest on a SelectableChannel, with ISelectHandler as callback attachment
 * There are two cases when trying to register an interest
 * 1. The whole key does not exist; no interests ever registered for this channel
 * 2. The key exists due to other interests registered but not the one we are adding
 * <p>
 * In 1st case, we just register this channel with operation on the given Selector
 * In 2nd case, we have to make sure the state of NIOLooper is clean:
 * 1. Key has to be valid
 * 2. The interest has not yet been registered
 * 3. If old attached ISelectHandler exists, it has to be the same as new one
 * If any one of above 3 conditions are not met, RuntimeException would be thrown.
 *
 * @param channel The Selectable to register operation interest
 * @param operation The operation interest to register
 * @param callback The Callback to handle
 * @throws ClosedChannelException if Channel is closed when trying to register an interest
 */
private void addInterest(SelectableChannel channel,
                         int operation,
                         ISelectHandler callback)
    throws ClosedChannelException {

  SelectionKey key = channel.keyFor(selector);

  if (key == null) {
    channel.register(selector, operation, callback);
  } else if (!key.isValid()) {
    throw new RuntimeException(
        String.format("Unable to add %d in %s due to key is invalid", operation, channel));
  } else {
    // Key is not null and key is valid
    if ((key.interestOps() & operation) != 0) {
      throw new RuntimeException(
          String.format("%d has been registered in %s", operation, channel));
    }
    if (key.attachment() == null) {
      key.attach(callback);
    } else {
      if (callback != key.attachment()) {
        throw new RuntimeException("Unmatched SelectHandler has already been attached"
            + " for other operation");
      }
      // If call == key.attachment
      // Just skip
    }
    key.interestOps(key.interestOps() | operation);
  }
}
 
源代码7 项目: craft-atom   文件: NioAcceptor.java
private void close(SelectableChannel sc) throws IOException {
	if (sc != null) {
		SelectionKey key = sc.keyFor(selector);
		if (key != null) {
			key.cancel();
		}
		sc.close();
	}
}
 
源代码8 项目: twister2   文件: Progress.java
private boolean isInterestRegistered(SelectableChannel channel, int operation) {
  SelectionKey key = channel.keyFor(selector);

  return key != null && (key.interestOps() & operation) != 0;
}
 
源代码9 项目: twister2   文件: Progress.java
public void removeAllInterest(SelectableChannel channel) {
  SelectionKey key = channel.keyFor(selector);
  if (key != null) {
    key.cancel();
  }
}
 
源代码10 项目: incubator-heron   文件: NIOLooper.java
public void removeAllInterest(SelectableChannel channel) {
  SelectionKey key = channel.keyFor(selector);
  if (key != null) {
    key.cancel();
  }
}
 
源代码11 项目: incubator-heron   文件: NIOLooper.java
public boolean isChannelValid(SelectableChannel channel) {
  SelectionKey key = channel.keyFor(selector);
  return key != null && key.isValid();
}
 
源代码12 项目: incubator-heron   文件: NIOLooper.java
/**
 * Remove one operation interest on a SelectableChannel.
 * The SelectableChannel has to be registered with Selector ahead.
 * Otherwise, NullPointerExceptions would throw
 * The key for SelectableChannel has to be valid.
 * Otherwise, InvalidValid Exception would throw.
 *
 * @param channel the SelectableChannel to remove operation interest
 * @param operation the interest to remove
 */
private void removeInterest(SelectableChannel channel, int operation) {
  SelectionKey key = channel.keyFor(selector);

  // Exception would be thrown if key is null or key is inValid
  // We do not need double check it ahead
  key.interestOps(key.interestOps() & (~operation));
}
 
源代码13 项目: incubator-heron   文件: NIOLooper.java
/**
 * Check whether an operation interest was registered on a SelectableChannel
 * There are two cases that interest is not registered
 * 1. The whole key does not exist; no interests ever registered for this channel
 * 2. The key exists due to other interests registered but not the one we are adding
 * If the key exists, the key for SelectableChannel has to be valid.
 * Otherwise, InvalidValid Exception would throw.
 *
 * @param channel The Selectable to check
 * @param operation The operation interest to check
 */
private boolean isInterestRegistered(SelectableChannel channel, int operation) {
  SelectionKey key = channel.keyFor(selector);

  return key != null && (key.interestOps() & operation) != 0;
}