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


Java SelectionKey.OP_WRITE属性代码示例

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


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

示例1: Client

public Client(Selector selector, SocketChannel clientChannel, CloseListener<Client> closeListener) throws ClosedChannelException {
    id = nextId++;
    this.clientChannel = clientChannel;
    router = new Router(this, selector);
    pendingIdBuffer = createIntBuffer(id);

    SelectionHandler selectionHandler = (selectionKey) -> {
        if (selectionKey.isValid() && selectionKey.isWritable()) {
            processSend();
        }
        if (selectionKey.isValid() && selectionKey.isReadable()) {
            processReceive();
        }
        if (selectionKey.isValid()) {
            updateInterests();
        }
    };
    // on start, we are interested only in writing (we must first send the client id)
    interests = SelectionKey.OP_WRITE;
    selectionKey = clientChannel.register(selector, interests, selectionHandler);

    this.closeListener = closeListener;
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:23,代码来源:Client.java

示例2: 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

示例3: modify

@Override
public void modify(TCPClientSocket clientSocket, boolean writable) throws IOException {

    SelectionKey key = clientSocket.getChannel().keyFor(selector);
    if (key == null)
        return; // not registered with this selector

    int interestOps = key.interestOps();
    if (writable) {
        interestOps |= SelectionKey.OP_WRITE;
    } else {
        interestOps &= ~SelectionKey.OP_WRITE;
    }

    key.interestOps(interestOps);
}
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:16,代码来源:SocketSelectorImpl.java

示例4: getInterestOps

@Override
public int getInterestOps() {
    if (!isSelectable()) {
        return 0;
    }
    int interestOps = 0;
    if (getReadInterest()) {
        interestOps |= SelectionKey.OP_READ;
    }
    if (getWriteInterest()) {
        interestOps |= SelectionKey.OP_WRITE;
    }
    return interestOps;
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:14,代码来源:NIOServerCnxn.java

示例5: internalSendBuffer

/**
 * This method implements the internals of sendBuffer. We
 * have separated it from send buffer to be able to catch
 * exceptions when testing.
 *
 * @param bb Buffer to send.
 */
protected void internalSendBuffer(ByteBuffer bb) {
    if (bb != ServerCnxnFactory.closeConn) {
        // We check if write interest here because if it is NOT set,
        // nothing is queued, so we can try to send the buffer right
        // away without waking up the selector
        if(sk.isValid() &&
                ((sk.interestOps() & SelectionKey.OP_WRITE) == 0)) {
            try {
                sock.write(bb);
            } catch (IOException e) {
                // we are just doing best effort right now
            }
        }
        // if there is nothing left to send, we are done
        if (bb.remaining() == 0) {
            packetSent();
            return;
        }
    }

    synchronized(this.factory){
        sk.selector().wakeup();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Add a buffer to outgoingBuffers, sk " + sk
                    + " is valid: " + sk.isValid());
        }
        outgoingBuffers.add(bb);
        if (sk.isValid()) {
            sk.interestOps(sk.interestOps() | SelectionKey.OP_WRITE);
        }
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:39,代码来源:NIOServerCnxn.java

示例6: write

public synchronized void write(byte[] data) throws ConnectionCloseException, ClosedChannelException {
	if (readyToClose)
		throw new ConnectionCloseException();
	ContextBean bean = context.getChanToContextBean().get(channel);
	ByteBuffer buffer = ByteBuffer.allocate(data.length + 4);
	buffer.putInt(data.length);
	buffer.put(data);
	buffer.flip();
	readyToWrite.add(buffer);
	int ops = bean.getOps();
	ops |= SelectionKey.OP_WRITE;
	bean.setOps(ops);
	this.channel.register(this.selector, ops);
	this.selector.wakeup();
}
 
开发者ID:luohaha,项目名称:LightComm4J,代码行数:15,代码来源:Connection.java

示例7: 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:l294265421,项目名称:ZooKeeper,代码行数:34,代码来源:ClientCnxnSocketNIO.java

示例8: 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

示例9: 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:SunburstApps,项目名称:OpenJSharp,代码行数:11,代码来源:SctpChannelImpl.java

示例10: enableWrite

@Override
synchronized void enableWrite() {
    int i = sockKey.interestOps();
    if ((i & SelectionKey.OP_WRITE) == 0) {
        sockKey.interestOps(i | SelectionKey.OP_WRITE);
    }
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:7,代码来源:ClientCnxnSocketNIO.java

示例11: 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

示例12: 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

示例13: disableWrite

@Override
public synchronized void disableWrite() {
    int i = sockKey.interestOps();
    if ((i & SelectionKey.OP_WRITE) != 0) {
        sockKey.interestOps(i & (~SelectionKey.OP_WRITE));
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:7,代码来源:ClientCnxnSocketNIO.java

示例14: 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);
        /* No need to poll again in checkConnect,
         * the error will be detected there */
        readyToConnect = true;
        return (newOps & ~oldOps) != 0;
    }

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

    if (((ops & Net.POLLCONN) != 0) &&
        ((intOps & SelectionKey.OP_CONNECT) != 0) &&
        ((state == ChannelState.UNCONNECTED) || (state == ChannelState.PENDING))) {
        newOps |= SelectionKey.OP_CONNECT;
        readyToConnect = true;
    }

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

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:44,代码来源:SctpChannelImpl.java

示例15: doSelect

protected int doSelect(long timeout) throws IOException
{
    if (channelArray == null)
        throw new ClosedSelectorException();
    processDeregisterQueue();
    if (interruptTriggered)
    {
        resetWakeupSocket();
        return 0;
    }

    ArrayList read = new ArrayList();
    ArrayList write = new ArrayList();
    ArrayList error = new ArrayList();
    for (int i = 0; i < channelArray.get_Count(); i++)
    {
        SelectionKeyImpl ski = (SelectionKeyImpl)channelArray.get_Item(i);
        int ops = ski.interestOps();
        if (ski.channel() instanceof SocketChannelImpl)
        {
            // TODO there's a race condition here...
            if (((SocketChannelImpl)ski.channel()).isConnected())
            {
                ops &= SelectionKey.OP_READ | SelectionKey.OP_WRITE;
            }
            else
            {
                ops &= SelectionKey.OP_CONNECT;
            }
        }
        if ((ops & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0)
        {
            read.Add(ski.getSocket());
        }
        if ((ops & (SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT)) != 0)
        {
            write.Add(ski.getSocket());
        }
        if ((ops & SelectionKey.OP_CONNECT) != 0)
        {
            error.Add(ski.getSocket());
        }
    }
    read.Add(wakeupSourceFd);
    try
    {
        begin();
        int microSeconds = 1000 * (int)Math.min(Integer.MAX_VALUE / 1000, timeout);
        try
        {
            if (false) throw new SocketException();
            // FXBUG docs say that -1 is infinite timeout, but that doesn't appear to work
            Socket.Select(read, write, error, timeout < 0 ? Integer.MAX_VALUE : microSeconds);
        }
        catch (SocketException _)
        {
            read.Clear();
            write.Clear();
            error.Clear();
        }
    }
    finally
    {
        end();
    }
    processDeregisterQueue();
    int updated = updateSelectedKeys(read, write, error);
    // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
    resetWakeupSocket();
    return updated;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:71,代码来源:DotNetSelectorImpl.java


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