This commit is contained in:
Redkale
2017-11-14 10:54:56 +08:00
parent ac5662114a
commit e0b2120ee5

View File

@@ -125,8 +125,8 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
/** /**
* 创建TCP协议客户端连接 * 创建TCP协议客户端连接
* *
* @param address 连接点子 * @param address 连接点子
* @param group 连接AsynchronousChannelGroup * @param group 连接AsynchronousChannelGroup
* @param readTimeoutSecond 读取超时秒数 * @param readTimeoutSecond 读取超时秒数
* @param writeTimeoutSecond 写入超时秒数 * @param writeTimeoutSecond 写入超时秒数
* *
@@ -134,43 +134,46 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
* @throws java.io.IOException 异常 * @throws java.io.IOException 异常
*/ */
public static CompletableFuture<AsyncConnection> createTCP(final AsynchronousChannelGroup group, final SocketAddress address, public static CompletableFuture<AsyncConnection> createTCP(final AsynchronousChannelGroup group, final SocketAddress address,
final int readTimeoutSecond, final int writeTimeoutSecond) throws IOException { final int readTimeoutSecond, final int writeTimeoutSecond) {
return createTCP(group, address, false, readTimeoutSecond, writeTimeoutSecond); return createTCP(group, address, false, readTimeoutSecond, writeTimeoutSecond);
} }
/** /**
* 创建TCP协议客户端连接 * 创建TCP协议客户端连接
* *
* @param address 连接点子 * @param address 连接点子
* @param group 连接AsynchronousChannelGroup * @param group 连接AsynchronousChannelGroup
* @param noDelay TcpNoDelay * @param noDelay TcpNoDelay
* @param readTimeoutSecond 读取超时秒数 * @param readTimeoutSecond 读取超时秒数
* @param writeTimeoutSecond 写入超时秒数 * @param writeTimeoutSecond 写入超时秒数
* *
* @return 连接CompletableFuture * @return 连接CompletableFuture
* @throws java.io.IOException 异常
*/ */
public static CompletableFuture<AsyncConnection> createTCP(final AsynchronousChannelGroup group, final SocketAddress address, public static CompletableFuture<AsyncConnection> createTCP(final AsynchronousChannelGroup group, final SocketAddress address,
final boolean noDelay, final int readTimeoutSecond, final int writeTimeoutSecond) throws IOException { final boolean noDelay, final int readTimeoutSecond, final int writeTimeoutSecond) {
final CompletableFuture future = new CompletableFuture(); final CompletableFuture future = new CompletableFuture();
final AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group); try {
channel.connect(address, null, new CompletionHandler<Void, Void>() { final AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group);
@Override channel.connect(address, null, new CompletionHandler<Void, Void>() {
public void completed(Void result, Void attachment) { @Override
if (noDelay) { public void completed(Void result, Void attachment) {
try { if (noDelay) {
channel.setOption(StandardSocketOptions.TCP_NODELAY, true); try {
} catch (IOException e) { channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
} catch (IOException e) {
}
} }
future.complete(create(channel, address, readTimeoutSecond, writeTimeoutSecond));
} }
future.complete(create(channel, address, readTimeoutSecond, writeTimeoutSecond));
}
@Override @Override
public void failed(Throwable exc, Void attachment) { public void failed(Throwable exc, Void attachment) {
future.completeExceptionally(exc); future.completeExceptionally(exc);
} }
}); });
} catch (IOException e) {
future.completeExceptionally(e);
}
return future; return future;
} }