本文整理汇总了Java中java.nio.channels.SocketChannel.finishConnect方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.finishConnect方法的具体用法?Java SocketChannel.finishConnect怎么用?Java SocketChannel.finishConnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.finishConnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start0
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void start0() throws IOException {
while (!closed) {
processQueues();
selector.select();
if (selector.selectedKeys().isEmpty()) {
processQueues();
}
for (Iterator<SelectionKey> i = selector.selectedKeys().iterator(); i.hasNext(); ) {
SelectionKey key = i.next();
i.remove();
if (key.isConnectable()) {
SocketChannel socketChannel = (SocketChannel) key.channel();
socketChannel.finishConnect();
LOG.info("SocketChannel connected.");
}
if (key.isReadable()) {
this.socketChannelReader.readFromKey(key);
}
}
}
}
示例2: openSocketChannel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public synchronized SocketChannel openSocketChannel(Address address) throws IOException {
if (this.clientSocketChannel != null && this.clientSocketChannel.isOpen()) {
return this.clientSocketChannel;
}
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
this.registerPendingMap.put(socketChannel, OP_CONNECT | OP_READ);
this.selector.wakeup();
LOG.info("Connecting to " + address + "...");
socketChannel.connect(new InetSocketAddress(address.getHost(), address.getPort()));
while (!socketChannel.finishConnect()) {
}
LOG.info("Connected.");
this.clientSocketChannel = socketChannel;
return this.clientSocketChannel;
}
示例3: testConnectCallback
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void testConnectCallback() throws IOException {
eventLoop.registerAccept(acceptSocket, serverHandler);
SocketChannel clientSocket = SocketChannel.open();
clientSocket.configureBlocking(false);
boolean connected = clientSocket.connect(
new InetSocketAddress(InetAddress.getLocalHost(), serverPort));
assertFalse(connected);
assertTrue(clientSocket.isConnectionPending());
eventLoop.registerConnect(clientSocket, serverHandler);
// registering it for reading as well is not permitted: causes problems on Linux
try {
eventLoop.registerRead(clientSocket, serverHandler);
fail("expected exception");
} catch (AssertionError e) {}
// The event loop will trigger the accept callback
assertNull(serverHandler.client);
eventLoop.runOnce();
assertNotNull(serverHandler.client);
assertTrue(clientSocket.isConnectionPending());
// The event loop will also have triggered the connect callback
assertTrue(serverHandler.connected);
connected = clientSocket.finishConnect();
assertTrue(connected);
assertTrue(clientSocket.isConnected());
// Registering some other handler in response to the connect event should work
eventLoop.registerRead(clientSocket, new TestHandler());
clientSocket.close();
}
示例4: disconnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void disconnect(SelectionKey sk){
SocketChannel sc=(SocketChannel)sk.channel();
try {
sc.finishConnect();
}catch (IOException e){
}
}
示例5: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void connect(SelectionKey key) {
SocketChannel sc = (SocketChannel) key.channel();
try {
while (!sc.finishConnect()) {
logger.info("connecting to server ....");
}
key.interestOps(SelectionKey.OP_READ);
parser.getWaitFor().add(Protocol.handshake);
logger.info("connected to server success");
} catch (IOException e) {
logger.error("connecting to server error", e);
}
}
示例6: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void connect(SelectionKey key) {
logger.info("connect to server");
SocketChannel sc = (SocketChannel) key.channel();
try {
while (!sc.finishConnect()) {
}
key.interestOps(SelectionKey.OP_READ);
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: openConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static PeerWireConnection openConnection(InetSocketAddress addr) throws IOException {
SocketChannel channel = SocketChannel.open(addr);
channel.finishConnect();
// System.out.println("Are we blocking: " + channel.isBlocking());
// System.out.println("Are we connected: " + channel.isConnected());
return new PeerWireConnection(channel, true);
}
示例8: finishConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected boolean finishConnect(SocketChannel handle) throws Exception {
if (handle.finishConnect()) {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
return true;
}
return false;
}
示例9: finishConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(AbstractConnection c, SocketChannel channel)
throws IOException {
if (channel.isConnectionPending()) {
// 阻塞或非阻塞
channel.finishConnect();
c.setLocalPort(channel.socket().getLocalPort());
return true;
} else {
return false;
}
}
示例10: doTransport
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@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();
}
示例11: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void connect(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
// 首先判断是否连接已经建立,如果没有,则调用finishConnect()完成连接
if (channel.isConnectionPending()) {
channel.finishConnect();
}
channel.configureBlocking(false);
//建立连接后,向Channel写入数据,并同时注册读事件为感兴趣的事件
channel.write(ByteBuffer.wrap(new String("hello server!\r\n").getBytes()));
channel.register(this.selector, SelectionKey.OP_READ);
}
示例12: finishConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(AbstractConnection c, SocketChannel channel)
throws IOException {
if (channel.isConnectionPending()) {
channel.finishConnect();
c.setLocalPort(channel.socket().getLocalPort());
return true;
} else {
return false;
}
}
示例13: finishConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private boolean finishConnect(Connection c, SocketChannel channel) throws IOException {
if (channel.isConnectionPending()) {
channel.finishConnect();
c.setLocalPort(channel.socket().getLocalPort());
return true;
} else {
return false;
}
}
示例14: doTransport
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@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();
}
示例15: handleClients
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
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);
}
}
}
}
}