Search in sources :

Example 16 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project baseio by generallycloud.

the class NettyClientThread method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = NettyUtil.newEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
                pipeline.addLast("handler", new HelloClient());
            }
        });
        System.out.println("################## Test start ####################");
        ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
        System.out.println(f.isSuccess());
        Channel channel = f.channel();
        System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
        int len = 1024 * 64;
        StringBuilder s = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            s.append(len % 10);
        }
        final String msg = s.toString();
        Util.exec(() -> {
            int i = 0;
            for (; ; ) {
                // String s = "hello Service! ---> :" + i;
                ChannelFuture f1 = channel.writeAndFlush(msg);
                Util.sleep(1);
                System.out.println(f1.isDone() + "--------" + i);
                i++;
            }
        });
        Util.sleep(Integer.MAX_VALUE);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 17 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project benchmark by seelunzi.

the class ClientIniter method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast("stringD", new StringDecoder());
    pipeline.addLast("stringC", new StringEncoder());
    pipeline.addLast("http", new HttpClientCodec());
    pipeline.addLast("chat", new ChatClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 18 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project benchmark by seelunzi.

the class ServerIniterHandler method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast("docode", new StringDecoder());
    pipeline.addLast("encode", new StringEncoder());
    pipeline.addLast("chat", new ChatServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 19 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.

the class NettyServerFilter method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline ph = ch.pipeline();
    // 以("\n")为结尾分割的 解码器
    // ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // 解码和编码,应和客户端一致
    ph.addLast("decoder", new StringDecoder());
    ph.addLast("encoder", new StringEncoder());
    // 服务端业务逻辑
    ph.addLast("handler", new NettyServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 20 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.

the class NettyClientFilter method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline ph = ch.pipeline();
    /*
         * 解码和编码,应和服务端一致
         * */
    // ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    ph.addLast("decoder", new StringDecoder());
    ph.addLast("encoder", new StringEncoder());
    // 客户端的逻辑
    ph.addLast("handler", new NettyClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) 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