Example usage for io.netty.channel.socket SocketChannel config

List of usage examples for io.netty.channel.socket SocketChannel config

Introduction

In this page you can find the example usage for io.netty.channel.socket SocketChannel config.

Prototype

@Override
    SocketChannelConfig config();

Source Link

Usage

From source file:com.codeabovelab.dm.platform.http.async.NettyRequestFactory.java

License:Apache License

private Bootstrap getBootstrap() {
    if (this.bootstrap == null) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override/*from www. ja  va  2 s . c  o m*/
                    protected void initChannel(SocketChannel channel) throws Exception {
                        configureChannel(channel.config());
                        ChannelPipeline pipeline = channel.pipeline();
                        if (sslContext != null) {
                            pipeline.addLast(sslContext.newHandler(channel.alloc()));
                        }
                        pipeline.addLast(new HttpClientCodec());
                        //pipeline.addLast(new HttpObjectAggregator(maxResponseSize));
                        if (readTimeout > 0) {
                            pipeline.addLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS));
                        }
                    }
                });
        this.bootstrap = bootstrap;
    }
    return this.bootstrap;
}

From source file:com.seagate.kinetic.client.io.provider.nio.ssl.SslChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    if (mservice.getConfiguration().getConnectTimeoutMillis() > 0) {
        ch.config().setConnectTimeoutMillis(mservice.getConfiguration().getConnectTimeoutMillis());
    }//www  . j ava  2  s  . c om

    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = SslContextFactory.getClientContext().createSSLEngine();

    engine.setUseClientMode(true);

    /**
     * enable TLS V1.x protocols.
     */
    TlsUtil.enableSupportedProtocols(engine);

    // add ssl handler
    pipeline.addLast("ssl", new SslHandler(engine));

    // decoder
    pipeline.addLast("decoder", new KineticDecoder());
    // encoder
    pipeline.addLast("encoder", new KineticEncoder());

    pipeline.addLast("handler", new SslMessageServiceHandler(mservice));

    logger.info("ssl channel initialized ... ");
}

From source file:com.seagate.kinetic.client.io.provider.nio.tcp.NioChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    if (mservice.getConfiguration().getConnectTimeoutMillis() > 0) {
        ch.config().setConnectTimeoutMillis(mservice.getConfiguration().getConnectTimeoutMillis());
    }//from  w w w  .  j  a  va  2s  . c om

    ChannelPipeline p = ch.pipeline();

    // decoder
    p.addLast("decoder", new KineticDecoder());
    // encoder
    p.addLast("encoder", new KineticEncoder());

    p.addLast("handler", new NioMessageServiceHandler(mservice));

    logger.info("nio channel initialized ...");
}

From source file:com.tc.websocket.server.WebSocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {

    IConfig cfg = Config.getInstance();//  w  w w  . j  ava2 s  .  c o m

    //if we need to check for ByteBuf leaks.
    if (cfg.isLeakDetector()) {
        ResourceLeakDetector.setLevel(Level.ADVANCED);
    }

    //so we get enough data to build our pipeline
    ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(1024));

    ChannelPipeline pipeline = ch.pipeline();

    int incomingPort = ch.localAddress().getPort();

    //if users are coming in on a different port than the proxy port we need to redirect them.
    if (cfg.isProxy() && cfg.getPort() != incomingPort) {
        redirectBuilder.apply(pipeline);
        return;
    }

    if (cfg.isEncrypted()) {
        SslContext sslContext = factory.createSslContext(Config.getInstance());
        SSLEngine engine = sslContext.newEngine(ch.alloc());
        engine.setUseClientMode(false);
        engine.setNeedClientAuth(cfg.isCertAuth());
        ch.pipeline().addFirst("ssl", new SslHandler(engine));
    }

    if (cfg.isProxy()) {
        pipeline.channel().config().setAutoRead(false);
        pipeline.addLast(
                guicer.inject(new ProxyFrontendHandler(cfg.getProxyBackendHost(), cfg.getProxyBackendPort())));

    } else {
        websocketBuilder.apply(pipeline);
    }

}

From source file:com.vmware.dcp.common.http.netty.NettyHttpClientRequestInitializer.java

License:Open Source License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();/*w w  w. jav a 2  s.c  o  m*/
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);
    if (this.pool.getSSLContext() != null) {
        SSLEngine engine = this.pool.getSSLContext().createSSLEngine();
        engine.setUseClientMode(true);
        p.addLast(SSL_HANDLER, new SslHandler(engine));
    }
    p.addLast(ENCODER_HANDLER, new HttpRequestEncoder());
    p.addLast(DECODER_HANDLER, new HttpResponseDecoder());
    p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(SocketContext.MAX_REQUEST_SIZE));
    p.addLast(DCP_HANDLER, new NettyHttpServerResponseHandler(this.pool));
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpServerInitializer.java

License:Open Source License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();/*from w  ww . j  av  a 2  s  .  c o  m*/
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);
    if (this.sslContext != null) {
        SslHandler handler = this.sslContext.newHandler(ch.alloc());
        SslClientAuthMode mode = this.host.getState().sslClientAuthMode;
        if (mode != null) {
            switch (mode) {
            case NEED:
                handler.engine().setNeedClientAuth(true);
                break;
            case WANT:
                handler.engine().setWantClientAuth(true);
                break;
            default:
                break;
            }
        }
        p.addLast(SSL_HANDLER, handler);
    }

    p.addLast(DECODER_HANDLER,
            new HttpRequestDecoder(MAX_INITIAL_LINE_LENGTH, MAX_HEADER_SIZE, MAX_CHUNK_SIZE));
    p.addLast(ENCODER_HANDLER, new HttpResponseEncoder());
    p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(NettyChannelContext.MAX_REQUEST_SIZE));
    p.addLast(DCP_WEBSOCKET_HANDLER, new NettyWebSocketRequestHandler(this.host,
            ServiceUriPaths.CORE_WEB_SOCKET_ENDPOINT, ServiceUriPaths.WEB_SOCKET_SERVICE_PREFIX));
    p.addLast(DCP_HANDLER, new NettyHttpClientRequestHandler(this.host));
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpClientRequestInitializer.java

License:Open Source License

/**
 * initChannel is called by Netty when a channel is first used.
 *//* w w  w .j  a  v a  2  s.c  o  m*/
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);
    if (this.pool.getSSLContext() != null) {
        if (this.isHttp2Only) {
            throw new IllegalArgumentException("HTTP/2 with SSL is not supported");
        }
        SSLEngine engine = this.pool.getSSLContext().createSSLEngine();
        engine.setUseClientMode(true);
        p.addLast(SSL_HANDLER, new SslHandler(engine));
    }

    HttpClientCodec http1_codec = new HttpClientCodec(NettyChannelContext.MAX_INITIAL_LINE_LENGTH,
            NettyChannelContext.MAX_HEADER_SIZE, NettyChannelContext.MAX_CHUNK_SIZE, false, false);

    // The HttpClientCodec combines the HttpRequestEncoder and the HttpResponseDecoder, and it
    // also provides a method for upgrading the protocol, which we use to support HTTP/2.
    p.addLast(HTTP1_CODEC, http1_codec);

    if (this.isHttp2Only) {
        try {
            NettyHttpToHttp2Handler connectionHandler = makeHttp2ConnectionHandler();
            Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec(connectionHandler);
            HttpClientUpgradeHandler upgradeHandler = new HttpClientUpgradeHandler(http1_codec, upgradeCodec,
                    this.requestPayloadSizeLimit);

            p.addLast(UPGRADE_HANDLER, upgradeHandler);
            p.addLast(UPGRADE_REQUEST, new UpgradeRequestHandler());

            // This promise will be triggered when we negotiate the settings.
            // That's important because we can't send user data until the negotiation is done
            ChannelPromise settingsPromise = ch.newPromise();
            p.addLast("settings-handler", new Http2SettingsHandler(settingsPromise));
            ch.attr(NettyChannelContext.SETTINGS_PROMISE_KEY).set(settingsPromise);
            p.addLast(EVENT_LOGGER, new NettyHttp2UserEventLogger(this.debugLogging));
        } catch (Throwable ex) {
            Utils.log(NettyHttpClientRequestInitializer.class,
                    NettyHttpClientRequestInitializer.class.getSimpleName(), Level.WARNING,
                    "Channel Initializer exception: %s", ex);
            throw ex;
        }
    } else {
        // The HttpObjectAggregator is not needed for HTTP/2. For HTTP/1.1 it
        // aggregates the HttpMessage and HttpContent into the FullHttpResponse
        p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(this.requestPayloadSizeLimit));
    }
    p.addLast(XENON_HANDLER, new NettyHttpServerResponseHandler(this.pool));
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpServerInitializer.java

License:Open Source License

/**
 * initChannel is called by Netty when a channel is first used.
 *//*from w  w  w.  j av a 2  s  .com*/
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);

    SslHandler sslHandler = null;
    if (this.sslContext != null) {
        sslHandler = this.sslContext.newHandler(ch.alloc());
        SslClientAuthMode mode = this.host.getState().sslClientAuthMode;
        if (mode != null) {
            switch (mode) {
            case NEED:
                sslHandler.engine().setNeedClientAuth(true);
                break;
            case WANT:
                sslHandler.engine().setWantClientAuth(true);
                break;
            default:
                break;
            }
        }
        p.addLast(SSL_HANDLER, sslHandler);
    }

    // The HttpServerCodec combines the HttpRequestDecoder and the HttpResponseEncoder, and it
    // also provides a method for upgrading the protocol, which we use to support HTTP/2. It
    // also supports a couple other minor features (support for HEAD and CONNECT), which
    // probably don't matter to us.
    HttpServerCodec http1_codec = new HttpServerCodec(NettyChannelContext.MAX_INITIAL_LINE_LENGTH,
            NettyChannelContext.MAX_HEADER_SIZE, NettyChannelContext.MAX_CHUNK_SIZE, false);
    p.addLast(HTTP1_CODEC, http1_codec);
    if (this.sslContext == null) {
        // Today we only use HTTP/2 when SSL is disabled
        final HttpToHttp2ConnectionHandler connectionHandler = makeHttp2ConnectionHandler();
        UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {
            @Override
            public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
                if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                    return new Http2ServerUpgradeCodec(connectionHandler);
                } else {
                    return null;
                }
            }
        };
        // On upgrade, the upgradeHandler will remove the http1_codec and replace it
        // with the connectionHandler. Ideally we'd remove the aggregator (chunked transfer
        // isn't allowed in HTTP/2) and the WebSocket handler (we don't support it over HTTP/2 yet)
        // but we're not doing that yet.

        HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(http1_codec,
                upgradeCodecFactory);

        p.addLast(HTTP2_UPGRADE_HANDLER, upgradeHandler);
    }

    p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(this.responsePayloadSizeLimit));
    p.addLast(WEBSOCKET_HANDLER, new NettyWebSocketRequestHandler(this.host,
            ServiceUriPaths.CORE_WEB_SOCKET_ENDPOINT, ServiceUriPaths.WEB_SOCKET_SERVICE_PREFIX));
    p.addLast(HTTP_REQUEST_HANDLER,
            new NettyHttpClientRequestHandler(this.host, sslHandler, this.responsePayloadSizeLimit));
}

From source file:com.whizzosoftware.hobson.api.plugin.channel.AbstractChannelObjectPlugin.java

License:Open Source License

private Bootstrap configureBootstrap(Bootstrap b) {
    b.group(connectionEventLoopGroup);/*from ww  w.  j  ava  2  s  . co  m*/
    b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    b.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

    // configure for either network or serial channel
    if (isNetworkAddress()) {
        b.channel(NioSocketChannel.class);
        b.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                channel.config().setConnectTimeoutMillis(5000);
                configureChannel(channel.config());
                configurePipeline(channel.pipeline());
            }
        });
    } else {
        b.channel(RxtxChannel.class);
        b.handler(new ChannelInitializer<RxtxChannel>() {
            @Override
            public void initChannel(RxtxChannel channel) throws Exception {
                configureChannel(channel.config());
                configurePipeline(channel.pipeline());
            }
        });
    }

    return b;
}

From source file:net.tridentsdk.server.netty.ClientChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel channel) throws Exception {
    //channel.config().setOption(ChannelOption.IP_TOS, 24);
    channel.config().setOption(ChannelOption.TCP_NODELAY, true);

    this.connection = ClientConnection.registerConnection(channel);

    //Decode:/*from  w  w w .  j a  va  2  s.  c o  m*/
    channel.pipeline().addLast(new PacketDecrypter());
    channel.pipeline().addLast(new PacketDecoder());
    channel.pipeline().addLast(new PacketHandler());

    //Encode:
    channel.pipeline().addLast(new PacketEncrypter());
    channel.pipeline().addLast(new PacketEncoder());
}