本文整理了Java中java.nio.channels.SocketChannel.finishConnect()
方法的一些代码示例,展示了SocketChannel.finishConnect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketChannel.finishConnect()
方法的具体详情如下:
包路径:java.nio.channels.SocketChannel
类名称:SocketChannel
方法名:finishConnect
[英]Completes the connection process initiated by a call of connect(SocketAddress).
This method returns true if the connection is finished already and returns false if the channel is non-blocking and the connection is not finished yet.
If this channel is in blocking mode, this method will suspend and return true when the connection is finished. It closes this channel and throws an exception if the connection fails.
This method can be called at any moment and it can block other read and write operations while connecting.
[中]完成通过调用connect(SocketAddress)启动的连接过程。
如果连接已经完成,则此方法返回true;如果通道未阻塞且连接尚未完成,则此方法返回false。
如果此通道处于阻塞模式,则此方法将挂起,并在连接完成时返回true。如果连接失败,它会关闭此通道并引发异常。
此方法可以随时调用,并且在连接时可以阻止其他读写操作。
代码示例来源:origin: TooTallNate/Java-WebSocket
public boolean finishConnect() throws IOException {
return socketChannel.finishConnect();
}
代码示例来源:origin: apache/kafka
@Override
public boolean finishConnect() throws IOException {
boolean connected = socketChannel.finishConnect();
if (connected)
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
return connected;
}
代码示例来源:origin: apache/kafka
/**
* does socketChannel.finishConnect()
*/
@Override
public boolean finishConnect() throws IOException {
boolean connected = socketChannel.finishConnect();
if (connected)
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
return connected;
}
代码示例来源:origin: netty/netty
@Override
protected void doFinishConnect() throws Exception {
if (!javaChannel().finishConnect()) {
throw new Error();
}
}
代码示例来源:origin: redisson/redisson
@Override
protected void doFinishConnect() throws Exception {
if (!javaChannel().finishConnect()) {
throw new Error();
}
}
代码示例来源:origin: alibaba/cobar
public boolean finishConnect() throws IOException {
if (channel.isConnectionPending()) {
channel.finishConnect();
localPort = channel.socket().getLocalPort();
isFinishConnect = true;
return true;
} else {
return false;
}
}
代码示例来源:origin: wildfly/wildfly
@Override
protected void doFinishConnect() throws Exception {
if (!javaChannel().finishConnect()) {
throw new Error();
}
}
代码示例来源:origin: apache/nifi
/**
* This will connect the channel; if it is in a pending state then this will finish
* establishing the connection. Finally the socket handler is registered with this
* channel.
*
* @throws IOException if anything goes wrong during the connection establishment
* or registering of the handler
*/
private void connect(SelectionKey selectionKey) throws IOException {
SocketChannel clientChannel = (SocketChannel) selectionKey.channel();
if (clientChannel.isConnectionPending()) {
clientChannel.finishConnect();
}
clientChannel.register(AbstractSocketHandler.this.selector, SelectionKey.OP_READ);
}
代码示例来源:origin: apache/incubator-gobblin
private void connect() throws IOException {
if (this.proxy.isOpen()) {
if (this.proxy.finishConnect()) {
this.proxy.register(this.selector, SelectionKey.OP_WRITE, this);
SelectionKey clientKey = this.client.keyFor(this.selector);
if (clientKey != null) {
clientKey.cancel();
}
this.state = HandlerState.WRITING;
} else if (this.connectStartTime + Config.PROXY_CONNECT_TIMEOUT_MS < System.currentTimeMillis()) {
LOG.warn("Proxy connect timed out for client {}", this.client);
closeChannels();
}
}
}
代码示例来源:origin: ltsopensource/light-task-scheduler
@Override
public void connect(SelectionKey key) {
try {
channel.socketChannel().finishConnect();
key.attach(channel);
} catch (IOException e) {
eventHandler().exceptionCaught(channel, e);
key.cancel();
return;
}
key.interestOps(SelectionKey.OP_READ);
}
代码示例来源:origin: ltsopensource/light-task-scheduler
@Override
public void connect(SelectionKey key) {
try {
channel.socketChannel().finishConnect();
key.attach(channel);
} catch (IOException e) {
eventHandler().exceptionCaught(channel, e);
key.cancel();
return;
}
key.interestOps(SelectionKey.OP_READ);
}
代码示例来源:origin: voldemort/voldemort
@Override
protected void connect(SelectionKey selectionKey) throws IOException {
if(!checkTimeout()) {
return;
}
if(socketChannel.finishConnect() == false) {
return;
}
if(logger.isDebugEnabled()) {
// check buffer sizes you often don't get out what you put in!
if(socketChannel.socket().getReceiveBufferSize() != this.socketBufferSize) {
logger.debug("Requested socket receive buffer size was " + this.socketBufferSize
+ " bytes but actual size is "
+ socketChannel.socket().getReceiveBufferSize() + " bytes.");
}
if(socketChannel.socket().getSendBufferSize() != this.socketBufferSize) {
logger.debug("Requested socket send buffer size was " + this.socketBufferSize
+ " bytes but actual size is "
+ socketChannel.socket().getSendBufferSize() + " bytes.");
}
}
addClientRequest(clientRequest, timeoutMs, 0);
}
代码示例来源:origin: alibaba/nacos
@Override
public void run() {
if (key != null && key.isValid()) {
SocketChannel channel = (SocketChannel) key.channel();
Beat beat = (Beat) key.attachment();
if (channel.isConnected()) {
return;
}
try {
channel.finishConnect();
} catch (Exception ignore) {
}
try {
beat.finishCheck(false, false, beat.getTask().getCheckRTNormalized() * 2, "tcp:timeout");
key.cancel();
key.channel().close();
} catch (Exception ignore) {
}
}
}
}
代码示例来源:origin: io.netty/netty
private static void connect(SelectionKey k) throws IOException {
NioClientSocketChannel ch = (NioClientSocketChannel) k.attachment();
try {
if (ch.channel.finishConnect()) {
k.cancel();
if (ch.timoutTimer != null) {
ch.timoutTimer.cancel();
}
ch.worker.register(ch, ch.connectFuture);
}
} catch (ConnectException e) {
ConnectException newE = new ConnectException(e.getMessage() + ": " + ch.requestedRemoteAddress);
newE.setStackTrace(e.getStackTrace());
throw newE;
}
}
代码示例来源:origin: alibaba/nacos
channel.finishConnect();
beat.finishCheck(true, false, System.currentTimeMillis() - beat.getTask().getStartTime(), "tcp:ok+");
代码示例来源:origin: rapidoid/rapidoid
@Override
protected void connectOP(SelectionKey key) throws IOException {
U.must(key.isConnectable());
SocketChannel socketChannel = (SocketChannel) key.channel();
if (!socketChannel.isConnectionPending()) {
// not ready to retrieve the connection status
return;
}
ConnectionTarget target = (ConnectionTarget) key.attachment();
boolean ready;
try {
ready = socketChannel.finishConnect();
U.must(ready, "Expected an established connection!");
Log.info("Connected", "address", target.addr);
connected.add(new RapidoidChannel(socketChannel, true, target.protocol, target.holder,
target.reconnecting, target.state));
} catch (ConnectException e) {
retryConnecting(target);
}
}
代码示例来源:origin: lealone/Lealone
private void connectionEstablished(SelectionKey key, Object att) throws Exception {
SocketChannel channel = (SocketChannel) key.channel();
if (!channel.isConnectionPending())
return;
Attachment attachment = (Attachment) att;
AsyncConnection conn;
try {
channel.finishConnect();
nioEventLoopAdapter.addSocketChannel(channel);
NioWritableChannel writableChannel = new NioWritableChannel(channel, this);
if (attachment.connectionManager != null) {
conn = attachment.connectionManager.createConnection(writableChannel, false);
} else {
conn = new TcpClientConnection(writableChannel, this);
}
conn.setInetSocketAddress(attachment.inetSocketAddress);
addConnection(attachment.inetSocketAddress, conn);
channel.register(nioEventLoopAdapter.getSelector(), SelectionKey.OP_READ, conn);
} finally {
attachment.latch.countDown();
}
}
代码示例来源:origin: apache/zookeeper
SocketChannel sc = ((SocketChannel) k.channel());
if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
if (sc.finishConnect()) {
updateLastSendAndHeard();
updateSocketAddresses();
代码示例来源:origin: wildfly/wildfly
protected void connect(Address dest, boolean send_local_addr) throws Exception {
SocketAddress destAddr=new InetSocketAddress(((IpAddress)dest).getIpAddress(), ((IpAddress)dest).getPort());
try {
if(!server.deferClientBinding())
this.channel.bind(new InetSocketAddress(server.clientBindAddress(), server.clientBindPort()));
this.key=server.register(channel, SelectionKey.OP_CONNECT | SelectionKey.OP_READ, this);
if(Util.connect(channel, destAddr) && channel.finishConnect()) {
clearSelectionKey(SelectionKey.OP_CONNECT);
this.connected=channel.isConnected();
}
if(this.channel.getLocalAddress() != null && this.channel.getLocalAddress().equals(destAddr))
throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr);
if(send_local_addr)
sendLocalAddress(server.localAddress());
}
catch(Exception t) {
close();
throw t;
}
}
代码示例来源:origin: wildfly/wildfly
void handleReady(final int ops) {
final SocketChannel channel = getChannel();
boolean ok = false;
try {
if (channel.finishConnect()) {
selectorLog.tracef("handleReady connect finished");
suspend(SelectionKey.OP_CONNECT);
getSelectionKey().attach(connection.getConduit());
if (futureResult.setResult(connection)) {
ok = true;
ChannelListeners.invokeChannelListener(connection, openListener);
}
}
} catch (IOException e) {
selectorLog.tracef("ConnectHandle.handleReady Exception, " + e);
futureResult.setException(e);
} finally {
if (!ok) {
selectorLog.tracef("!OK, closing connection");
safeClose(connection);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!