本文整理汇总了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);
}
}
示例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: 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();
}
示例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);
}
示例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();
}
示例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);
}
示例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();
}
}
});
}
}
示例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;
}
示例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;
}
示例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);
}
}
}
}
}
示例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> {@link SelectionKey#OP_READ} <tt>|</tt> {@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);
}
示例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 |} {@link SelectionKey#OP_READ} {@code |} {@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);
}