Search in sources :

Example 41 with ChannelFuture

use of io.netty.channel.ChannelFuture in project redisson by redisson.

the class ConnectionWatchdog method reattachBlockingQueue.

private void reattachBlockingQueue(RedisConnection connection, final CommandData<?, ?> commandData) {
    if (commandData == null || !commandData.isBlockingCommand() || commandData.getPromise().isDone()) {
        return;
    }
    ChannelFuture future = connection.send(commandData);
    future.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                log.error("Can't reconnect blocking queue to new connection. {}", commandData);
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RedisException(org.redisson.client.RedisException)

Example 42 with ChannelFuture

use of io.netty.channel.ChannelFuture in project redisson by redisson.

the class RedisClient method connectPubSubAsync.

public RFuture<RedisPubSubConnection> connectPubSubAsync() {
    final RPromise<RedisPubSubConnection> f = new RedissonPromise<RedisPubSubConnection>();
    ChannelFuture channelFuture = bootstrap.connect();
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                final RedisPubSubConnection c = new RedisPubSubConnection(RedisClient.this, future.channel());
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        if (!f.trySuccess(c)) {
                            c.closeAsync();
                        }
                    }
                });
            } else {
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        f.tryFailure(future.cause());
                    }
                });
            }
        }
    });
    return f;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedissonPromise(org.redisson.misc.RedissonPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 43 with ChannelFuture

use of io.netty.channel.ChannelFuture in project vert.x by eclipse.

the class ProxyChannelProvider method connect.

@Override
public void connect(VertxInternal vertx, Bootstrap bootstrap, ProxyOptions options, String host, int port, Handler<Channel> channelInitializer, Handler<AsyncResult<Channel>> channelHandler) {
    final String proxyHost = options.getHost();
    final int proxyPort = options.getPort();
    final String proxyUsername = options.getUsername();
    final String proxyPassword = options.getPassword();
    final ProxyType proxyType = options.getType();
    vertx.resolveAddress(proxyHost, dnsRes -> {
        if (dnsRes.succeeded()) {
            InetAddress address = dnsRes.result();
            InetSocketAddress proxyAddr = new InetSocketAddress(address, proxyPort);
            ProxyHandler proxy;
            switch(proxyType) {
                default:
                case HTTP:
                    proxy = proxyUsername != null && proxyPassword != null ? new HttpProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new HttpProxyHandler(proxyAddr);
                    break;
                case SOCKS5:
                    proxy = proxyUsername != null && proxyPassword != null ? new Socks5ProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new Socks5ProxyHandler(proxyAddr);
                    break;
                case SOCKS4:
                    proxy = proxyUsername != null ? new Socks4ProxyHandler(proxyAddr, proxyUsername) : new Socks4ProxyHandler(proxyAddr);
                    break;
            }
            bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
            InetSocketAddress targetAddress = InetSocketAddress.createUnresolved(host, port);
            bootstrap.handler(new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addFirst("proxy", proxy);
                    pipeline.addLast(new ChannelInboundHandlerAdapter() {

                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            if (evt instanceof ProxyConnectionEvent) {
                                pipeline.remove(proxy);
                                pipeline.remove(this);
                                channelInitializer.handle(ch);
                                channelHandler.handle(Future.succeededFuture(ch));
                            }
                            ctx.fireUserEventTriggered(evt);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                            channelHandler.handle(Future.failedFuture(cause));
                        }
                    });
                }
            });
            ChannelFuture future = bootstrap.connect(targetAddress);
            future.addListener(res -> {
                if (!res.isSuccess()) {
                    channelHandler.handle(Future.failedFuture(res.cause()));
                }
            });
        } else {
            channelHandler.handle(Future.failedFuture(dnsRes.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ProxyHandler(io.netty.handler.proxy.ProxyHandler) HttpProxyHandler(io.netty.handler.proxy.HttpProxyHandler) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ProxyConnectionEvent(io.netty.handler.proxy.ProxyConnectionEvent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) HttpProxyHandler(io.netty.handler.proxy.HttpProxyHandler) ProxyType(io.vertx.core.net.ProxyType) InetAddress(java.net.InetAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 44 with ChannelFuture

use of io.netty.channel.ChannelFuture in project vert.x by eclipse.

the class ChannelProvider method connect.

public void connect(VertxInternal vertx, Bootstrap bootstrap, ProxyOptions options, String host, int port, Handler<Channel> channelInitializer, Handler<AsyncResult<Channel>> channelHandler) {
    bootstrap.resolver(vertx.nettyAddressResolverGroup());
    bootstrap.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channelInitializer.handle(channel);
        }
    });
    ChannelFuture fut = bootstrap.connect(host, port);
    fut.addListener(res -> {
        if (res.isSuccess()) {
            channelHandler.handle(Future.succeededFuture(fut.channel()));
        } else {
            channelHandler.handle(Future.failedFuture(res.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel)

Example 45 with ChannelFuture

use of io.netty.channel.ChannelFuture in project vert.x by eclipse.

the class Http2ClientTest method testClearText.

private void testClearText(boolean upgrade) throws Exception {
    ServerBootstrap bootstrap = createH2CServer((dec, enc) -> new Http2EventAdapter() {

        @Override
        public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
            enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise());
            ctx.flush();
        }
    }, upgrade);
    ChannelFuture s = bootstrap.bind(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT).sync();
    try {
        client.close();
        client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(upgrade));
        client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            assertEquals(HttpVersion.HTTP_2, resp.version());
            testComplete();
        }).exceptionHandler(this::fail).end();
        await();
    } finally {
        s.channel().close().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)383 Test (org.junit.Test)131 Channel (io.netty.channel.Channel)120 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)105 Bootstrap (io.netty.bootstrap.Bootstrap)98 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)94 ChannelFutureListener (io.netty.channel.ChannelFutureListener)88 ByteBuf (io.netty.buffer.ByteBuf)81 InetSocketAddress (java.net.InetSocketAddress)79 EventLoopGroup (io.netty.channel.EventLoopGroup)78 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)76 IOException (java.io.IOException)69 ChannelPipeline (io.netty.channel.ChannelPipeline)67 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 ClosedChannelException (java.nio.channels.ClosedChannelException)56 ChannelInitializer (io.netty.channel.ChannelInitializer)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 ArrayList (java.util.ArrayList)46 SslHandler (io.netty.handler.ssl.SslHandler)45 Http2Headers (io.netty.handler.codec.http2.Http2Headers)44