From 41990e7e6a471f2ebc188d32c02d027081c88bdf Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 13 Nov 2017 19:43:01 +0800 Subject: [PATCH] =?UTF-8?q?AsyncConnection=E6=8F=90=E4=BE=9BcreateTCP?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E3=80=82=E5=BC=82=E6=AD=A5=E6=9E=84=E5=BB=BA?= =?UTF-8?q?AsyncConnection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/AsyncConnection.java | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/org/redkale/net/AsyncConnection.java b/src/org/redkale/net/AsyncConnection.java index fb998423e..6c3001119 100644 --- a/src/org/redkale/net/AsyncConnection.java +++ b/src/org/redkale/net/AsyncConnection.java @@ -158,6 +158,35 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl } } + /** + * 创建TCP协议客户端连接 + * + * @param address 连接点子 + * @param group 连接AsynchronousChannelGroup + * @param readTimeoutSecond0 读取超时秒数 + * @param writeTimeoutSecond0 写入超时秒数 + * + * @return 连接CompletableFuture + * @throws java.io.IOException 异常 + */ + public static CompletableFuture createTCP(final AsynchronousChannelGroup group, final SocketAddress address, + final int readTimeoutSecond0, final int writeTimeoutSecond0) throws IOException { + final CompletableFuture future = new CompletableFuture(); + final AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group); + channel.connect(address, null, new CompletionHandler() { + @Override + public void completed(Void result, Void attachment) { + future.complete(create(channel, address, readTimeoutSecond0, writeTimeoutSecond0)); + } + + @Override + public void failed(Throwable exc, Void attachment) { + future.completeExceptionally(exc); + } + }); + return future; + } + private static class BIOUDPAsyncConnection extends AsyncConnection { private int readTimeoutSecond;