Example usage for io.netty.channel ChannelOption AUTO_READ

List of usage examples for io.netty.channel ChannelOption AUTO_READ

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption AUTO_READ.

Prototype

ChannelOption AUTO_READ

To view the source code for io.netty.channel ChannelOption AUTO_READ.

Click Source Link

Usage

From source file:proxyService.ProxyFrontendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();

    // Start the connection attempt.
    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new ProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false);
    ChannelFuture f = b.connect(node.getIp(), node.getPort());
    System.out.println(this + " tunneling traffic to " + node.getIp() + ":" + node.getPort());
    outboundChannel = f.channel();//w w  w  . j a va2s .c  o m
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();
            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpClient.java

License:Apache License

/**
 * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given {@code
 * reactor} to/*from www.  java  2  s .  c o  m*/
 * send events. The number of IO threads used by the client is configured by the environment's {@code
 * reactor.tcp.ioThreadCount} property. In its absence the number of IO threads will be equal to the {@link
 * Environment#PROCESSORS number of available processors}. </p> The client will connect to the given {@code
 * connectAddress}, configuring its socket using the given {@code opts}. The given {@code codec} will be used for
 * encoding and decoding of data.
 *
 * @param env            The configuration environment
 * @param dispatcher     The dispatcher used to send events
 * @param connectAddress The address the client will connect to
 * @param options        The configuration options for the client's socket
 * @param sslOptions     The SSL configuration options for the client's socket
 * @param codec          The codec used to encode and decode data
 */
public NettyTcpClient(Environment env, Dispatcher dispatcher, InetSocketAddress connectAddress,
        final ClientSocketOptions options, final SslOptions sslOptions, Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, connectAddress, options, sslOptions, codec);
    this.connectAddress = connectAddress;

    if (options instanceof NettyClientSocketOptions) {
        this.nettyOptions = (NettyClientSocketOptions) options;
    } else {
        this.nettyOptions = null;

    }
    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = env != null
                ? env.getIntProperty("reactor.tcp.ioThreadCount", Environment.PROCESSORS)
                : Environment.PROCESSORS;
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));
    }

    this.bootstrap = new Bootstrap().group(ioGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_KEEPALIVE, options.keepAlive())
            .option(ChannelOption.SO_LINGER, options.linger())
            .option(ChannelOption.TCP_NODELAY, options.tcpNoDelay())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.AUTO_READ, sslOptions != null)
    //.remoteAddress(this.connectAddress)
    ;

    this.connectionSupplier = new Supplier<ChannelFuture>() {
        @Override
        public ChannelFuture get() {
            if (started.get()) {
                return bootstrap.connect(getConnectAddress());
            } else {
                return null;
            }
        }
    };
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpServer.java

License:Apache License

protected NettyTcpServer(Environment env, Dispatcher dispatcher, InetSocketAddress listenAddress,
        final ServerSocketOptions options, final SslOptions sslOptions, Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, listenAddress, options, sslOptions, codec);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {//from  www  .  j  av  a2s .co  m
        this.nettyOptions = null;
    }

    int selectThreadCount = getDefaultEnvironment().getIntProperty("reactor.tcp.selectThreadCount",
            Environment.PROCESSORS / 2);
    int ioThreadCount = getDefaultEnvironment().getIntProperty("reactor.tcp.ioThreadCount",
            Environment.PROCESSORS);

    this.selectorGroup = new NioEventLoopGroup(selectThreadCount,
            new NamedDaemonThreadFactory("reactor-tcp-select"));

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));
    }

    this.bootstrap = new ServerBootstrap().group(selectorGroup, ioGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, options.backlog())
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .localAddress((null == listenAddress ? new InetSocketAddress(0) : listenAddress))
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.AUTO_READ, sslOptions != null);
}

From source file:reactor.io.net.impl.netty.udp.NettyDatagramServer.java

License:Apache License

public NettyDatagramServer(@Nonnull Environment env, @Nonnull Dispatcher dispatcher,
        @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface,
        @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec) {
    super(env, dispatcher, listenAddress, multicastInterface, options, codec);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {/*from  w  ww . ja v a 2s. c  o m*/
        this.nettyOptions = null;
    }

    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        int ioThreadCount = getDefaultEnvironment().getIntProperty("reactor.udp.ioThreadCount",
                Environment.PROCESSORS);
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io"));
    }

    final InternetProtocolFamily family = toNettyFamily(options.protocolFamily());

    this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf())
            .option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr()).option(ChannelOption.AUTO_READ, false)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.timeout())
            .channelFactory(new ChannelFactory<Channel>() {
                @Override
                public Channel newChannel() {
                    return new NioDatagramChannel(family);
                }
            });

    if (null != listenAddress) {
        bootstrap.localAddress(listenAddress);
    } else {
        bootstrap.localAddress(NetUtil.LOCALHOST, 3000);
    }
    if (null != multicastInterface) {
        bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface);
    }
}

From source file:reactor.ipc.netty.options.ClientOptions.java

License:Open Source License

static void defaultClientOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000).option(ChannelOption.AUTO_READ, false)
            .option(ChannelOption.SO_RCVBUF, 1024 * 1024).option(ChannelOption.SO_SNDBUF, 1024 * 1024);
}

From source file:reactor.ipc.netty.options.ServerOptions.java

License:Open Source License

static void defaultServerOptions(ServerBootstrap bootstrap) {
    bootstrap.localAddress(LOCALHOST_AUTO_PORT).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_BACKLOG, 1000)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.SO_RCVBUF, 1024 * 1024).childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
            .childOption(ChannelOption.AUTO_READ, false).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000);
}

From source file:rereverse.Bootstrap.java

License:Open Source License

public void run() throws Exception {
    System.out.println("Starting ReReverse on local port " + localPort + "...");

    EventLoopGroup group = new NioEventLoopGroup();
    try {//from w  ww.java2s  .co  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(group).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel c) throws Exception {
                        //FIXME remove this shit. This is for testing only
                        DeliveryMapping app = new DeliveryMapping("9030", "www.bitbucket.com");
                        //TODO iterate over all ApplicationDelivery models
                        c.pipeline().addLast("rereverse", new ReReverseHandler(app));
                    }
                }).childOption(ChannelOption.AUTO_READ, false).bind(localPort).sync().channel().closeFuture()
                .sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:rereverse.ReReverseHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    final Channel inboundChannel = ctx.channel();

    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass())
            .handler(new ChannelInboundHandlerAdapter() {
                @Override//from   w  ww  .  j a va 2  s.  co  m
                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    ctx.read();
                    ctx.write(Unpooled.EMPTY_BUFFER);
                }

                @Override
                public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
                    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (future.isSuccess()) {
                                ctx.channel().read();
                            } else {
                                future.channel().close();
                            }
                        }
                    });
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    closeChannel(inboundChannel);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    cause.printStackTrace();
                    closeChannel(ctx.channel());
                }
            }).option(ChannelOption.AUTO_READ, false);

    //TODO map using url without port (?)
    ChannelFuture f = b.connect(application.remoteHost, application.remotePort);
    outbound = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                inboundChannel.read();
            } else {
                // failed
                inboundChannel.close();
            }
        }
    });
}