Search in sources :

Example 41 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project spring-boot-demo by fengwenyi.

the class NettyClientInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    socketChannel.pipeline().addLast("decoder", new StringDecoder());
    socketChannel.pipeline().addLast("encoder", new StringEncoder());
    socketChannel.pipeline().addLast(new NettyClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NettyClientHandler(com.fengwenyi.demospringbootnettyclient.handler.NettyClientHandler)

Example 42 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project chunjun by DTStack.

the class DtSocketClient method start.

public void start() {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ch.pipeline().addLast("decoder", new StringDecoder(Charset.forName(encoding)));
            ch.pipeline().addLast("encoder", new StringEncoder(Charset.forName(encoding)));
            ch.pipeline().addLast(new DtClientHandler(queue, codeC, encoding));
        }
    });
    channel = bootstrap.connect(host, port).addListener(future -> {
        if (future.isSuccess()) {
            LOG.info("connect [{}:{}] success", host, port);
        } else {
            String error = String.format("connect [%s:%d] failed", host, port);
            try {
                queue.put(GenericRowData.of(KEY_EXIT0 + error));
            } catch (InterruptedException ex) {
                LOG.error(ExceptionUtil.getErrorMessage(ex));
            }
        }
    }).channel();
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) StringDecoder(io.netty.handler.codec.string.StringDecoder)

Example 43 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink by splunk.

the class NettyClientServerSslTest method testSslPinningForValidFingerprint.

@Test
public void testSslPinningForValidFingerprint() throws Exception {
    NettyProtocol protocol = new NoOpProtocol();
    Configuration config = createSslConfig();
    // pin the certificate based on internal cert
    config.setString(SecurityOptions.SSL_INTERNAL_CERT_FINGERPRINT, SSLUtilsTest.getCertificateFingerprint(config, "flink.test"));
    NettyTestUtil.NettyServerAndClient serverAndClient;
    try (NetUtils.Port port = NetUtils.getAvailablePort()) {
        NettyConfig nettyConfig = createNettyConfig(config, port);
        serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    }
    Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
    Channel ch = NettyTestUtil.connect(serverAndClient);
    ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
    assertTrue(ch.writeAndFlush("test").await().isSuccess());
    NettyTestUtil.shutdown(serverAndClient);
}
Also used : StringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder) NetUtils(org.apache.flink.util.NetUtils) Configuration(org.apache.flink.configuration.Configuration) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) StringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 44 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink by splunk.

the class NettyClientServerSslTest method testSslHandshakeError.

/**
 * Verify SSL handshake error when untrusted server certificate is used.
 */
@Test
public void testSslHandshakeError() throws Exception {
    NettyProtocol protocol = new NoOpProtocol();
    Configuration config = createSslConfig();
    // Use a server certificate which is not present in the truststore
    config.setString(SecurityOptions.SSL_INTERNAL_KEYSTORE, "src/test/resources/untrusted.keystore");
    NettyTestUtil.NettyServerAndClient serverAndClient;
    try (NetUtils.Port port = NetUtils.getAvailablePort()) {
        NettyConfig nettyConfig = createNettyConfig(config, port);
        serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    }
    Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
    Channel ch = NettyTestUtil.connect(serverAndClient);
    ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
    // Attempting to write data over ssl should fail
    assertFalse(ch.writeAndFlush("test").await().isSuccess());
    NettyTestUtil.shutdown(serverAndClient);
}
Also used : StringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder) NetUtils(org.apache.flink.util.NetUtils) Configuration(org.apache.flink.configuration.Configuration) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) StringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 45 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project dubbo by apache.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetIdleEventHandler());
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)119 StringDecoder (io.netty.handler.codec.string.StringDecoder)109 ChannelPipeline (io.netty.channel.ChannelPipeline)73 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)40 SocketChannel (io.netty.channel.socket.SocketChannel)40 ChannelFuture (io.netty.channel.ChannelFuture)31 Bootstrap (io.netty.bootstrap.Bootstrap)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)28 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)27 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)25 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)24 EventLoopGroup (io.netty.channel.EventLoopGroup)23 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)23 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)17 Channel (io.netty.channel.Channel)16 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)16 Test (org.junit.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)15 NettyServerAndClient (org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient)15 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)15