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


Java SelectionKey.OP_CONNECT属性代码示例

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


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

示例4: TCPConnection

public TCPConnection(ConnectionId id, Client client, Selector selector, IPv4Header ipv4Header, TCPHeader tcpHeader) throws IOException {
    super(id, client);

    TCPHeader shrinkedTcpHeader = tcpHeader.copy();
    shrinkedTcpHeader.shrinkOptions(); // no TCP options

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

    SelectionHandler selectionHandler = (selectionKey) -> {
        if (selectionKey.isValid() && selectionKey.isConnectable()) {
            processConnect();
        }
        if (selectionKey.isValid() && selectionKey.isReadable()) {
            processReceive();
        }
        if (selectionKey.isValid() && selectionKey.isWritable()) {
            processSend();
        }
        updateInterests();
    };
    channel = createChannel();
    // interests will be set on the first packet received
    // set the initial value now so that they won't need to be updated
    interests = SelectionKey.OP_CONNECT;
    selectionKey = channel.register(selector, interests, selectionHandler);
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:28,代码来源:TCPConnection.java

示例5: doTransport

@Override
void doTransport(int waitTimeOut, List<Packet> pendingQueue, 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();
                updateSocketAddresses();
                sendThread.primeConnection();
            }
        } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
            doIO(pendingQueue, cnxn);
        }
    }
    if (sendThread.getZkState().isConnected()) {
        if (findSendablePacket(outgoingQueue,
                sendThread.tunnelAuthInProgress()) != null) {
            enableWrite();
        }
    }
    selected.clear();
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:32,代码来源:ClientCnxnSocketNIO.java

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

示例7: register

/**
 * This method has to be called once before using the channel.
 * @param interestOps operations that are going to be handled. OP_Connect need not be set because it is automatically added for client connections.
 * @throws ClosedChannelException 
 * @throws ExecutionException 
 * @throws InterruptedException 
 */
protected void register(int interestOps) throws ClosedChannelException
{
	if(clientSocket)
	{
		interestOps|=SelectionKey.OP_CONNECT;
	}
	if(Thread.currentThread()==t)
	{
		key=t.register(c, interestOps, ChannelProcessor.this);
	}else
	{
		final int interestOpsFinal=interestOps;
		t.addTask(new Runnable() {
			
			@Override
			public void run() {
				try {
					key=t.register(c, interestOpsFinal, ChannelProcessor.this);
				} catch (ClosedChannelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:34,代码来源:ChannelProcessor.java

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

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

示例10: handleClients

private void handleClients() throws Exception {
    int selectCount = 0;
    while (true) {
        int createdCount = 0;
        synchronized (this) {
            if (connectionsNeeded > 0) {

                while (connectionsNeeded > 0 && createdCount < 20) {
                    connectionsNeeded--;
                    createdCount++;
                    totalCreated++;

                    SocketChannel channel = SocketChannel.open();
                    channel.configureBlocking(false);
                    channel.connect(address);
                    if (!channel.finishConnect()) {
                        channel.register(selector,
                                         SelectionKey.OP_CONNECT);
                    }
                }

                log("Started total of " +
                    totalCreated + " client connections");
                Thread.sleep(200);
            }
        }

        if (createdCount > 0) {
            selector.selectNow();
        } else {
            selectCount++;
            long startTime = System.nanoTime();
            selector.select();
            long duration = durationMillis(startTime);
            log("Exited clientSelector.select(), loop #"
                + selectCount + ", duration = " + duration + "ms");
        }

        int keyCount = -1;
        Iterator<SelectionKey> keys =
            selector.selectedKeys().iterator();
        while (keys.hasNext()) {
            SelectionKey key = keys.next();
            synchronized (key) {
                keyCount++;
                keys.remove();
                if (!key.isValid()) {
                    log("Ignoring client key #" + keyCount);
                    continue;
                }
                int readyOps = key.readyOps();
                if (readyOps == SelectionKey.OP_CONNECT) {
                    key.interestOps(0);
                    ((SocketChannel) key.channel()).finishConnect();
                } else {
                    log("readyOps() on client key #" + keyCount +
                        " returned " + readyOps);
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:62,代码来源:LotsOfCancels.java

示例11: validOps

/**
 * Returns an operation set identifying this channel's supported operations.
 *
 * <P> SCTP channels support connecting, reading, and writing, so this
 * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
 * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
 * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return (SelectionKey.OP_READ |
            SelectionKey.OP_WRITE |
            SelectionKey.OP_CONNECT);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:16,代码来源:SctpChannel.java

示例12: validOps

/**
 * Returns an operation set identifying this channel's supported operations.
 *
 * <P> SCTP channels support connecting, reading, and writing, so this
 * method returns {@code (}{@link SelectionKey#OP_CONNECT}
 * {@code |}&nbsp;{@link SelectionKey#OP_READ} {@code |}&nbsp;{@link
 * SelectionKey#OP_WRITE}{@code )}.
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return (SelectionKey.OP_READ |
            SelectionKey.OP_WRITE |
            SelectionKey.OP_CONNECT);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:SctpChannel.java


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