本文整理汇总了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);
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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);
}
示例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;
}
示例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);
}
示例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();
}
示例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");
}
}
示例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);
}
示例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();
}
示例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);
}
示例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());
}
}
示例14: getInterestOps
public int getInterestOps()
{
return SelectionKey.OP_READ;
}
示例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();
}