本文整理汇总了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);
}
}
示例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);
}
}
}
示例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();
}
示例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);
}
}
}
示例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;
}
}
示例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(); }
}
}
}
示例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();
}
}
}
示例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();
}
}
示例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();
}
示例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:<- ");
}
}
示例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();
}
示例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;
}
示例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);
}
}
}
示例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);
}
}
示例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();
}
}