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


Java SelectionKey.OP_READ属性代码示例

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


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

示例1: updateInterests

protected void updateInterests() {
    if (!selectionKey.isValid()) {
        return;
    }
    int interestOps = 0;
    if (mayRead()) {
        interestOps |= SelectionKey.OP_READ;
    }
    if (mayWrite()) {
        interestOps |= SelectionKey.OP_WRITE;
    }
    if (mayConnect()) {
        interestOps |= SelectionKey.OP_CONNECT;
    }
    if (interests != interestOps) {
        // interests must be changed
        interests = interestOps;
        selectionKey.interestOps(interestOps);
    }
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:20,代码来源:TCPConnection.java

示例2: timeoutExceptionString

private static String timeoutExceptionString(SelectableChannel channel,
                                             long timeout, int ops) {
  
  String waitingFor;
  switch(ops) {
  
  case SelectionKey.OP_READ :
    waitingFor = "read"; break;
    
  case SelectionKey.OP_WRITE :
    waitingFor = "write"; break;      
    
  case SelectionKey.OP_CONNECT :
    waitingFor = "connect"; break;
    
  default :
    waitingFor = "" + ops;  
  }
  
  return timeout + " millis timeout while " +
         "waiting for channel to be ready for " + 
         waitingFor + ". ch : " + channel;    
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:23,代码来源:SocketIOWithTimeout.java

示例3: setInterestedInRead

/**
 * {@inheritDoc}
 */
@Override
protected void setInterestedInRead(NioSession session, boolean isInterested) throws Exception {
    SelectionKey key = session.getSelectionKey();
    int oldInterestOps = key.interestOps();
    int newInterestOps = oldInterestOps;

    if (isInterested) {
        newInterestOps |= SelectionKey.OP_READ;
    } else {
        newInterestOps &= ~SelectionKey.OP_READ;
    }

    if (oldInterestOps != newInterestOps) {
        key.interestOps(newInterestOps);
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:19,代码来源:NioProcessor.java

示例4: registerEvent

@Override
public void registerEvent(RawEvent event) throws IOException {
    int ops = event.interestOps();
    if ((ops & SelectionKey.OP_WRITE) != 0) {
        synchronized (stateLock) {
            checkOpen();
            executor.execute(event::handle);
        }
    } else if ((ops & SelectionKey.OP_READ) != 0) {
        CompletionStage<?> cs;
        synchronized (readLock) {
            cs = currentRule().whenReady();
            synchronized (stateLock) {
                checkOpen();
                cs.thenRun(() -> executor.execute(event::handle));
            }
        }
    } else {
        throw new RuntimeException("Unexpected registration: " + ops);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:MockChannel.java

示例5: UDPConnection

public UDPConnection(ConnectionId id, Client client, Selector selector, IPv4Header ipv4Header, UDPHeader udpHeader) throws IOException {
    super(id, client);

    networkToClient = new Packetizer(ipv4Header, udpHeader);
    networkToClient.getResponseIPv4Header().swapSourceAndDestination();
    networkToClient.getResponseTransportHeader().swapSourceAndDestination();

    touch();

    SelectionHandler selectionHandler = (selectionKey) -> {
        touch();
        if (selectionKey.isValid() && selectionKey.isReadable()) {
            processReceive();
        }
        if (selectionKey.isValid() && selectionKey.isWritable()) {
            processSend();
        }
        updateInterests();
    };
    channel = createChannel();
    interests = SelectionKey.OP_READ;
    selectionKey = channel.register(selector, interests, selectionHandler);
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:23,代码来源:UDPConnection.java

示例6: translateReadyOps

/**
 * Translates native poll revent ops into a ready operation ops
 */
private boolean translateReadyOps(int ops, int initialOps,
                                  SelectionKeyImpl sk) {
    int intOps = sk.nioInterestOps();
    int oldOps = sk.nioReadyOps();
    int newOps = initialOps;

    if ((ops & Net.POLLNVAL) != 0) {
        /* This should only happen if this channel is pre-closed while a
         * selection operation is in progress
         * ## Throw an error if this channel has not been pre-closed */
        return false;
    }

    if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
        newOps = intOps;
        sk.nioReadyOps(newOps);
        return (newOps & ~oldOps) != 0;
    }

    if (((ops & Net.POLLIN) != 0) &&
        ((intOps & SelectionKey.OP_READ) != 0))
        newOps |= SelectionKey.OP_READ;

    if (((ops & Net.POLLOUT) != 0) &&
        ((intOps & SelectionKey.OP_WRITE) != 0))
        newOps |= SelectionKey.OP_WRITE;

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:SctpMultiChannelImpl.java

示例7: translateAndSetInterestOps

@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    sk.selector.putEventOps(sk, newOps);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:9,代码来源:SctpMultiChannelImpl.java

示例8: doTransport

@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, LinkedList<Packet> outgoingQueue,
                 ClientCnxn cnxn)
        throws IOException, InterruptedException {
    selector.select(waitTimeOut);
    Set<SelectionKey> selected;
    synchronized (this) {
        selected = selector.selectedKeys();
    }
    // Everything below and until we get back to the select is
    // non blocking, so time is effectively a constant. That is
    // Why we just have to do this once, here
    updateNow();
    for (SelectionKey k : selected) {
        SocketChannel sc = ((SocketChannel) k.channel());
        if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
            if (sc.finishConnect()) {
                updateLastSendAndHeard();
                sendThread.primeConnection();
            }
        } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
            doIO(pendingQueue, outgoingQueue, cnxn);
        }
    }
    if (sendThread.getZkState().isConnected()) {
        synchronized(outgoingQueue) {
            if (findSendablePacket(outgoingQueue,
                    cnxn.sendThread.clientTunneledAuthenticationInProgress()) != null) {
                enableWrite();
            }
        }
    }
    selected.clear();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:34,代码来源:ClientCnxnSocketNIO.java

示例9: registerForEvent

public void registerForEvent(EventHandler eventHandler)
{
    if (orb.transportDebugFlag) {
        dprint(".registerForEvent: " + eventHandler);
    }

    if (isClosed()) {
        if (orb.transportDebugFlag) {
            dprint(".registerForEvent: closed: " + eventHandler);
        }
        return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
        synchronized (deferredRegistrations) {
            deferredRegistrations.add(eventHandler);
        }
        if (! selectorStarted) {
            startSelector();
        }
        selector.wakeup();
        return;
    }

    switch (eventHandler.getInterestOps()) {
    case SelectionKey.OP_ACCEPT :
        createListenerThread(eventHandler);
        break;
    case SelectionKey.OP_READ :
        createReaderThread(eventHandler);
        break;
    default:
        if (orb.transportDebugFlag) {
            dprint(".registerForEvent: default: " + eventHandler);
        }
        throw new RuntimeException(
            "SelectorImpl.registerForEvent: unknown interest ops");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:SelectorImpl.java

示例10: registerForRead

protected void registerForRead(final SelectionKey key, ObjectReader reader) {
	if (log.isTraceEnabled())
		log.trace("Adding key for read event:" + key);
	reader.finish();
	// register our OP_READ interest
	Runnable r = new Runnable() {
		@Override
		public void run() {
			try {
				if (key.isValid()) {
					// cycle the selector so this key is active again
					key.selector().wakeup();
					// resume interest in OP_READ, OP_WRITE
					int resumeOps = key.interestOps() | SelectionKey.OP_READ;
					key.interestOps(resumeOps);
					if (log.isTraceEnabled())
						log.trace("Registering key for read:" + key);
				}
			} catch (CancelledKeyException ckx) {
				NioReceiver.cancelledKey(key);
				if (log.isTraceEnabled())
					log.trace("CKX Cancelling key:" + key);

			} catch (Exception x) {
				log.error("Error registering key for read:" + key, x);
			}
		}
	};
	receiver.addEvent(r);
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:30,代码来源:NioReplicationTask.java

示例11: remove

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,代码行数:39,代码来源:NioBlockingSelector.java

示例12: translateAndSetInterestOps

@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;
    if ((ops & SelectionKey.OP_READ) != 0)
        newOps |= Net.POLLIN;
    if ((ops & SelectionKey.OP_WRITE) != 0)
        newOps |= Net.POLLOUT;
    if ((ops & SelectionKey.OP_CONNECT) != 0)
        newOps |= Net.POLLCONN;
    sk.selector.putEventOps(sk, newOps);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:SctpChannelImpl.java

示例13: cancel

public void cancel(SelectionKey sk, KeyAttachment key, int ops){
    if (sk!=null) {
        sk.cancel();
        sk.attach(null);
        if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
        if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
    }
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:8,代码来源:NioBlockingSelector.java

示例14: getInterestOps

public int getInterestOps()
{
    return SelectionKey.OP_READ;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:4,代码来源:SocketOrChannelConnectionImpl.java

示例15: remove

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:how2j,项目名称:lazycat,代码行数:46,代码来源:NioBlockingSelector.java


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