List of usage examples for java.nio.channels SelectionKey OP_WRITE
int OP_WRITE
To view the source code for java.nio.channels SelectionKey OP_WRITE.
Click Source Link
From source file:com.facebook.infrastructure.net.TcpConnection.java
public void modifyKeyForWrite(SelectionKey key) { key.interestOps(key_.interestOps() | SelectionKey.OP_WRITE); }
From source file:com.taobao.gecko.core.nio.impl.Reactor.java
final void dispatchEvent(final Set<SelectionKey> selectedKeySet) { final Iterator<SelectionKey> it = selectedKeySet.iterator(); boolean skipOpRead = false; // while (it.hasNext()) { final SelectionKey key = it.next(); it.remove();// w ww.j ava 2 s . co m if (!key.isValid()) { if (key.attachment() != null) { this.controller.closeSelectionKey(key); } else { key.cancel(); } continue; } try { if (key.isAcceptable()) { this.controller.onAccept(key); continue; } if ((key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) { // Remove write interest key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE); this.controller.onWrite(key); if (!this.controller.isHandleReadWriteConcurrently()) { skipOpRead = true; } } if (!skipOpRead && (key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // read key.interestOps(key.interestOps() & ~SelectionKey.OP_READ); // if (!this.controller.getStatistics().isReceiveOverFlow()) { // Remove read interest this.controller.onRead(key);// continue; } else { key.interestOps(key.interestOps() // | SelectionKey.OP_READ); } } if ((key.readyOps() & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) { this.controller.onConnect(key); continue; } } catch (final RejectedExecutionException e) { // if (key.attachment() instanceof AbstractNioSession) { ((AbstractSession) key.attachment()).onException(e); } this.controller.notifyException(e); if (this.selector.isOpen()) { continue; } else { break; } } catch (final CancelledKeyException e) { // ignore } catch (final Exception e) { if (key.attachment() instanceof AbstractNioSession) { ((AbstractSession) key.attachment()).onException(e); } this.controller.closeSelectionKey(key); this.controller.notifyException(e); log.error("Reactor dispatch events error", e); if (this.selector.isOpen()) { continue; } else { break; } } } }
From source file:com.alibaba.napoli.gecko.core.nio.impl.Reactor.java
final void dispatchEvent(final Set<SelectionKey> selectedKeySet) { final Iterator<SelectionKey> it = selectedKeySet.iterator(); boolean skipOpRead = false; // ? while (it.hasNext()) { final SelectionKey key = it.next(); it.remove();/*from w w w .j av a 2s. c om*/ if (!key.isValid()) { if (key.attachment() != null) { this.controller.closeSelectionKey(key); } else { key.cancel(); } continue; } try { if (key.isAcceptable()) { this.controller.onAccept(key); continue; } if ((key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) { // Remove write interest key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE); this.controller.onWrite(key); if (!this.controller.isHandleReadWriteConcurrently()) { skipOpRead = true; } } if (!skipOpRead && (key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // read key.interestOps(key.interestOps() & ~SelectionKey.OP_READ); // ??? if (!this.controller.getStatistics().isReceiveOverFlow()) { // Remove read interest this.controller.onRead(key);// ? continue; } else { key.interestOps(key.interestOps() // | SelectionKey.OP_READ); } } if ((key.readyOps() & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) { this.controller.onConnect(key); continue; } } catch (final RejectedExecutionException e) { // ??? if (key.attachment() instanceof AbstractNioSession) { ((AbstractSession) key.attachment()).onException(e); } this.controller.notifyException(e); if (this.selector.isOpen()) { continue; } else { break; } } catch (final CancelledKeyException e) { // ignore } catch (final Exception e) { if (key.attachment() instanceof AbstractNioSession) { ((AbstractSession) key.attachment()).onException(e); } this.controller.closeSelectionKey(key); this.controller.notifyException(e); log.error("Reactor dispatch events error", e); if (this.selector.isOpen()) { continue; } else { break; } } } }
From source file:de.kapsi.net.daap.nio.DaapServerNIO.java
/** * Notify all clients about an update of the Library *///from w w w.jav a2 s . c o m private void processUpdate() { Set keys = selector.keys(); Iterator it = keys.iterator(); while (it.hasNext()) { SelectionKey sk = (SelectionKey) it.next(); SelectableChannel channel = (SelectableChannel) sk.channel(); if (channel instanceof SocketChannel) { DaapConnection connection = (DaapConnection) sk.attachment(); if (connection.isDaapConnection()) { try { connection.update(); if (sk.isValid()) { try { sk.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); } catch (CancelledKeyException err) { cancel(sk); LOG.error("SelectionKey.interestOps()", err); } } } catch (ClosedChannelException err) { cancel(sk); LOG.error("DaapConnection.update()", err); } catch (IOException err) { cancel(sk); LOG.error("DaapConnection.update()", err); } } } } }
From source file:Proxy.java
String printSelectionOps(SelectionKey key) { StringBuilder sb = new StringBuilder(); if ((key.readyOps() & SelectionKey.OP_ACCEPT) != 0) sb.append("OP_ACCEPT "); if ((key.readyOps() & SelectionKey.OP_CONNECT) != 0) sb.append("OP_CONNECT "); if ((key.readyOps() & SelectionKey.OP_READ) != 0) sb.append("OP_READ "); if ((key.readyOps() & SelectionKey.OP_WRITE) != 0) sb.append("OP_WRITE "); return sb.toString(); }
From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java
protected void updateWriteInterest(NIOClientSocketHandler clientHandler, Selector selector) { SocketChannel socketChannel = clientHandler.getSocketChannel(); if (!socketChannel.isOpen()) return;// ww w .ja va 2 s . c o m if (!socketChannel.isConnected()) return; // We are interested in writing only if there is something in the output buffer or // the handler expresses the need to write something if (clientHandler.getOutputBuffer().position() > 0 || clientHandler.hasWriteInterest()) addInterest(socketChannel, SelectionKey.OP_WRITE, null, selector); else removeInterest(socketChannel, SelectionKey.OP_WRITE, selector); }
From source file:net.ymate.platform.serv.nio.support.NioSession.java
public void send(Object message) throws IOException { if (__selectionKey != null) { ByteBufferBuilder _msgBuffer = __eventGroup.codec().encode(message); if (_msgBuffer != null) { if (__writeBufferQueue.offer(_msgBuffer.buffer())) { __selectionKey.interestOps(__selectionKey.interestOps() | SelectionKey.OP_WRITE); __selectionKey.selector().wakeup(); }// w w w . j ava 2 s .c o m } } }
From source file:org.apache.catalina.cluster.tcp.TcpReplicationThread.java
/** * Called to initiate a unit of work by this worker thread * on the provided SelectionKey object. This method is * synchronized, as is the run() method, so only one key * can be serviced at a given time./*from w w w . ja v a2 s. co m*/ * Before waking the worker thread, and before returning * to the main selection loop, this key's interest set is * updated to remove OP_READ. This will cause the selector * to ignore read-readiness for this channel while the * worker thread is servicing it. */ synchronized void serviceChannel(SelectionKey key, boolean synchronous) { this.key = key; this.synchronous = synchronous; key.interestOps(key.interestOps() & (~SelectionKey.OP_READ)); key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE)); this.notify(); // awaken the thread }
From source file:org.apache.htrace.impl.PackedBufferManager.java
/** * Send the provided ByteBuffer objects. * * We use non-blocking I/O because Java does not provide write timeouts. * Without a write timeout, the socket could get hung and we'd never recover. * We also use the GatheringByteChannel#write method which calls the pread() * system call under the covers. This ensures that even if TCP_NODELAY is on, * we send the minimal number of packets. *//*from ww w .j ava 2s. co m*/ private void doSend(SelectionKey sockKey, ByteBuffer[] bufs) throws IOException { long totalWritten = 0; sockKey.interestOps(SelectionKey.OP_WRITE); SocketChannel sock = (SocketChannel) sockKey.attachment(); long startMs = TimeUtil.nowMs(); long remainingMs = conf.ioTimeoutMs; while (true) { selector.select(remainingMs); int firstBuf = 0; for (SelectionKey key : selector.selectedKeys()) { if (key.isWritable()) { long written = sock.write(bufs, firstBuf, bufs.length - firstBuf); if (LOG.isTraceEnabled()) { LOG.trace("Sent " + written + " bytes to " + conf.endpointStr); } totalWritten += written; } } while (true) { if (firstBuf == bufs.length) { if (LOG.isTraceEnabled()) { LOG.trace("Finished sending " + totalWritten + " bytes to " + conf.endpointStr); } return; } if (bufs[firstBuf].remaining() > 0) { break; } firstBuf++; } remainingMs = updateRemainingMs(startMs, conf.ioTimeoutMs); if (remainingMs == 0) { throw new IOException("Attempt to write to " + conf.endpointStr + " timed out after " + TimeUtil.deltaMs(startMs, TimeUtil.nowMs()) + " ms."); } } }
From source file:org.apache.synapse.transport.utils.logging.LoggingIOSession.java
private static String formatOps(int ops) { StringBuilder buffer = new StringBuilder(6); buffer.append('['); if ((ops & SelectionKey.OP_READ) > 0) { buffer.append('r'); }//from ww w . java 2 s. c om if ((ops & SelectionKey.OP_WRITE) > 0) { buffer.append('w'); } if ((ops & SelectionKey.OP_ACCEPT) > 0) { buffer.append('a'); } if ((ops & SelectionKey.OP_CONNECT) > 0) { buffer.append('c'); } buffer.append(']'); return buffer.toString(); }