Search in sources :

Example 21 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project tech by ffyyhh995511.

the class HelloClientInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    /*
         * 这个地方的 必须和服务端对应上。否则无法正常解码和编码
         * 
         * 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
         * 
         * */
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 客户端的逻辑
    pipeline.addLast("handler", new HelloClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 22 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project tutorials-java by Artister.

the class ClientChannelInitializer method initChannel.

protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 客户端的handler
    // 先调用handler在ChannnelActive方法中调用fireChannelActive会激活handler1
    pipeline.addLast("handler", new HwClientHandler());
    pipeline.addLast("handler1", new BaseClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HwClientHandler(org.ko.netty.t2.handler.HwClientHandler) BaseClientHandler(org.ko.netty.t2.handler.BaseClientHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 23 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project tutorials-java by Artister.

the class ServerChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 字符长度
    pipeline.addLast(new LineBasedFrameDecoder(2048));
    // 自己的逻辑Handler
    pipeline.addLast("handler", new HelloWordServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HelloWordServerHandler(org.ko.netty.t3.handler.HelloWordServerHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 24 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project tutorials-java by Artister.

the class NettyServer method service.

public static void service() throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            // TODO Auto-generated method stub
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new TcpServerHandler());
        }
    });
    ChannelFuture f = bootstrap.bind(IP, PORT).sync();
    f.channel().closeFuture().sync();
    System.out.println("TCP服务器已启动");
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) TcpServerHandler(org.ko.netty.tn.handler.TcpServerHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 25 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder in project flink by apache.

the class NettyClientServerSslTest method testSslPinningForInvalidFingerprint.

@Test
public void testSslPinningForInvalidFingerprint() 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").replaceAll("[0-9A-Z]", "0"));
    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());
    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)

Aggregations

StringDecoder (io.netty.handler.codec.string.StringDecoder)65 StringEncoder (io.netty.handler.codec.string.StringEncoder)42 ChannelPipeline (io.netty.channel.ChannelPipeline)36 SocketChannel (io.netty.channel.socket.SocketChannel)28 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)22 ChannelFuture (io.netty.channel.ChannelFuture)18 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)17 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)16 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)15 Bootstrap (io.netty.bootstrap.Bootstrap)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)13 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)12 Channel (io.netty.channel.Channel)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 LoggingHandler (io.netty.handler.logging.LoggingHandler)9 Test (org.junit.Test)7 ChannelHandler (io.netty.channel.ChannelHandler)6 ByteBuf (io.netty.buffer.ByteBuf)5