当前位置: 首页>>代码示例>>Java>>正文


Java SelectionKey.isValid方法代码示例

本文整理汇总了Java中java.nio.channels.SelectionKey.isValid方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.isValid方法的具体用法?Java SelectionKey.isValid怎么用?Java SelectionKey.isValid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.SelectionKey的用法示例。


在下文中一共展示了SelectionKey.isValid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: finishConnection

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private final void finishConnection(final SelectionKey key, final MMOConnection<T> con)
{
    try
    {
        ((SocketChannel) key.channel()).finishConnect();
    }
    catch (IOException e)
    {
        con.getClient().onForcedDisconnection();
        closeConnectionImpl(key, con);
    }

    // key might have been invalidated on finishConnect()
    if (key.isValid())
    {
        key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT);
    }
}
 
开发者ID:L2jBrasil,项目名称:L2jBrasil,代码行数:20,代码来源:SelectorThread.java

示例2: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
	final Selector selector = this.selector;
	for (;;) {
		++acceptCount;
		try {
			selector.select( 1000L );
			Set<SelectionKey> keys = selector.selectedKeys();
			try {
				for (SelectionKey key : keys) {
					if (key.isValid() && key.isAcceptable()) {
						accept();							
					} else {
						key.cancel();
					}
				}
			} finally {
				keys.clear();
			}
		} catch (Throwable e) {
			LOGGER.warn(getName(), e);
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:25,代码来源:NIOAcceptor.java

示例3: isReadable

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
protected boolean isReadable(DatagramChannel handle) {
    SelectionKey key = handle.keyFor(selector);

    if ((key == null) || (!key.isValid())) {
        return false;
    }

    return key.isReadable();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:11,代码来源:NioDatagramAcceptor.java

示例4: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
    final Selector tSelector = this.selector;
    for (; ; ) {
        try {
            tSelector.select(1000L);
            Set<SelectionKey> keys = tSelector.selectedKeys();
            try {
                for (SelectionKey key : keys) {
                    if (key.isValid() && key.isAcceptable()) {
                        accept();
                    } else {
                        key.cancel();
                    }
                }
            } finally {
                keys.clear();
            }
        } catch (Exception e) {
            LOGGER.info(getName(), e);
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:24,代码来源:NIOAcceptor.java

示例5: getState

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected SessionState getState(NioSession session) {
    SelectionKey key = session.getSelectionKey();

    if (key == null) {
        // The channel is not yet registred to a selector
        return SessionState.OPENING;
    }

    if (key.isValid()) {
        // The session is opened
        return SessionState.OPENED;
    } else {
        // The session still as to be closed
        return SessionState.CLOSING;
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:21,代码来源:NioProcessor.java

示例6: pauseSelects

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void pauseSelects( AbstractSelectableChannel channel ) {

      //System.out.println( "pauseSelects: " + channel + " - " + Debug.getCompressedStackTrace() );

      if( channel == null ) {
        return;
      }

      SelectionKey key = channel.keyFor( selector );

      if( key != null && key.isValid() ) {
        key.interestOps( key.interestOps() & ~INTEREST_OP );
      }
      else {  //channel not (yet?) registered
        if( channel.isOpen() ) {  //only bother if channel has not already been closed
          try{  register_cancel_list_mon.enter();

            paused_states.put( channel, Boolean.TRUE);  //ensure the op is paused upon reg select-time reg

          }
          finally{  register_cancel_list_mon.exit();  }
        }
      }
    }
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:25,代码来源:VirtualChannelSelectorImpl.java

示例7: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void run() {
	while (true) {
		try {
			selector.select();
			Iterator<SelectionKey> it = selector.selectedKeys().iterator();
			while (it.hasNext()) {
				SelectionKey k = it.next();
				it.remove();
				if (!k.isValid())
					continue;
				if (k.isReadable()) {
					read(k);
				}
				continue;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
开发者ID:biancso,项目名称:Mevius-IO,代码行数:22,代码来源:MeviusClient.java

示例8: registerForWrite

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void registerForWrite(final SelectionKey key, final boolean needWrite) {
	if(key == null || key.isValid() == false){
		return;
	}
	
	if(needWrite == true){
		if((key.interestOps() & SelectionKey.OP_WRITE) > 0){
			return;
		}
	}else{
		if((key.interestOps() & SelectionKey.OP_WRITE) == 0){
			return;
		}
	}
	
	
	Runnable r = new Runnable() {
           public void run() {
           	if(key == null || !key.isValid()){
           		return;
           	}
           	key.selector().wakeup();
           	if(needWrite == true){
           		key.interestOps(key.interestOps()  & (~SelectionKey.OP_READ) | SelectionKey.OP_WRITE);
           	}else{
           		key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE) | SelectionKey.OP_READ);
           	}
           }
       };
       listener.addEvent(r);
       try{
       	key.selector().wakeup();
       }catch(Exception e){
       	e.printStackTrace();
       }
}
 
开发者ID:ahhblss,项目名称:ddpush,代码行数:37,代码来源:MessengerTask.java

示例9: remove

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void remove(final KeyAttachment key, final int ops) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            if ( key == null ) return;
            NioChannel nch = key.getChannel();
            if ( nch == null ) return;
            SocketChannel ch = nch.getIOChannel();
            if ( ch == null ) return;
            SelectionKey sk = ch.keyFor(selector);
            try {
                if (sk == null) {
                    if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                    if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                } else {
                    if (sk.isValid()) {
                        sk.interestOps(sk.interestOps() & (~ops));
                        if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                        if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                        if (sk.interestOps()==0) {
                            sk.cancel();
                            sk.attach(null);
                        }
                    }else {
                        sk.cancel();
                        sk.attach(null);
                    }
                }
            }catch (CancelledKeyException cx) {
                if (sk!=null) {
                    sk.cancel();
                    sk.attach(null);
                }
            }
        }
    };
    events.offer(r);
    wakeup();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:40,代码来源:NioBlockingSelector.java

示例10: registerInterestOps

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void registerInterestOps(EventHandler eventHandler)
{
    if (orb.transportDebugFlag) {
        dprint(".registerInterestOps:-> " + eventHandler);
    }

    SelectionKey selectionKey = eventHandler.getSelectionKey();
    if (selectionKey.isValid()) {
        int ehOps = eventHandler.getInterestOps();
        SelectionKeyAndOp keyAndOp = new SelectionKeyAndOp(selectionKey, ehOps);
        synchronized(interestOpsList) {
            interestOpsList.add(keyAndOp);
        }
        // tell Selector Thread there's an update to a SelectorKey's Ops
        selector.wakeup();
    }
    else {
        wrapper.selectionKeyInvalid(eventHandler.toString());
        if (orb.transportDebugFlag) {
            dprint(".registerInterestOps: EventHandler SelectionKey not valid " + eventHandler);
        }
    }

    if (orb.transportDebugFlag) {
        dprint(".registerInterestOps:<- ");
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:28,代码来源:SelectorImpl.java

示例11: getConnectionRequest

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected ConnectionRequest getConnectionRequest(SocketChannel handle) {
    SelectionKey key = handle.keyFor(selector);

    if ((key == null) || (!key.isValid())) {
        return null;
    }

    return (ConnectionRequest) key.attachment();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:14,代码来源:NioSocketConnector.java

示例12: next

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * Get the next SocketChannel in the operator we have built from
 * the selected-key et for this selector.
 * 
 * @return The next SocketChannel in the iterator
 */
public ServerSocketChannel next() {
    SelectionKey key = iterator.next();

    if (key.isValid() && key.isAcceptable()) {
        return (ServerSocketChannel) key.channel();
    }

    return null;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:16,代码来源:NioSocketAcceptor.java

示例13: run

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
	final Selector selector = this.selector;
	for (;;) {
		++connectCount;
		try {
			//查看有无连接就绪
			selector.select( 1000L );
			connect(selector);
			Set<SelectionKey> keys = selector.selectedKeys();
			try {
				for (SelectionKey key: keys) {
					Object att = key.attachment();
					if (att != null && key.isValid() && key.isConnectable()) {
						finishConnect(key, att);
					} else {
						key.cancel();
					}
				}
			} finally {
				keys.clear();
			}
		} catch (Exception e) {
			LOGGER.warn(name, e);
		}
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:28,代码来源:NIOConnector.java

示例14: clearSelectionKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void clearSelectionKey() {
    try {
        SelectionKey key = this.processKey;
        if (key != null && key.isValid()) {
            key.attach(null);
            key.cancel();
        }
    } catch (Exception e) {
        AbstractConnection.LOGGER.info("clear selector keys err:" + e);
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:12,代码来源:NIOSocketWR.java

示例15: registerForWrite

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void registerForWrite(final SelectionKey key, final boolean needWrite) {
	if(key == null || key.isValid() == false){
		return;
	}
	
	if(needWrite == true){
		if((key.interestOps() & SelectionKey.OP_WRITE) > 0){
			return;
		}
	}else{
		if((key.interestOps() & SelectionKey.OP_WRITE) == 0){
			return;
		}
	}
	
	Runnable r = new Runnable() {
           public void run() {
           	if(key == null || !key.isValid()){
           		return;
           	}
           	key.selector().wakeup();
           	if(needWrite == true){
           		key.interestOps(key.interestOps()  & (~SelectionKey.OP_READ) | SelectionKey.OP_WRITE);
           	}else{
           		key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE) | SelectionKey.OP_READ);
           	}
           }
       };
       listener.addEvent(r);
       try{
       	key.selector().wakeup();
       }catch(Exception e){
       	e.printStackTrace();
       }
}
 
开发者ID:ahhblss,项目名称:ddpush,代码行数:36,代码来源:PushTask.java


注:本文中的java.nio.channels.SelectionKey.isValid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。