Search in sources :

Example 26 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project baseio by generallycloud.

the class NettyClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class).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 ####################");
        long old = System.currentTimeMillis();
        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);
        for (int i = 0; i < time; i++) {
            String s = "hello Service! ---> :" + i;
            ChannelFuture f1 = channel.writeAndFlush(s);
            f1.isDone();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long spend = (System.currentTimeMillis() - old);
        System.out.println("## Execute Time:" + time);
        System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
        System.out.println("## Expend Time:" + spend);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) 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) BigDecimal(java.math.BigDecimal) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 27 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project camel by apache.

the class MultipleCodecsTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    // START SNIPPET: registry-beans
    ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
    StringDecoder stringDecoder = new StringDecoder();
    registry.bind("length-decoder", lengthDecoder);
    registry.bind("string-decoder", stringDecoder);
    LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
    StringEncoder stringEncoder = new StringEncoder();
    registry.bind("length-encoder", lengthEncoder);
    registry.bind("string-encoder", stringEncoder);
    List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
    decoders.add(lengthDecoder);
    decoders.add(stringDecoder);
    List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
    encoders.add(lengthEncoder);
    encoders.add(stringEncoder);
    registry.bind("encoders", encoders);
    registry.bind("decoders", decoders);
    // END SNIPPET: registry-beans
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) StringEncoder(io.netty.handler.codec.string.StringEncoder) ArrayList(java.util.ArrayList) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender)

Example 28 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project dubbo by alibaba.

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 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)

Example 29 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.

the class NettyClient method main.

/**
 * Netty创建全部都是实现自AbstractBootstrap。
 * 客户端的是Bootstrap,服务端的则是	ServerBootstrap。
 */
public static void main(String[] args) throws Exception {
    try {
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("decoder", new StringDecoder());
                p.addLast("encoder", new StringEncoder());
                p.addLast(new BaseClient1Handler());
                p.addLast(new BaseClient2Handler());
            }
        });
        // 连接服务端
        ChannelFuture future = b.connect(host, port).sync();
        System.out.println("客户端连接成功!");
        // 发送消息
        future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
        // 关闭
        future.channel().closeFuture().sync();
    } finally {
        // 释放资源
        group.shutdownGracefully();
    }
// start();
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 30 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.

the class NettyServer method start.

/**
 * Netty创建全部都是实现自AbstractBootstrap。
 * 客户端的是Bootstrap,服务端的则是	ServerBootstrap。
 */
public static void start() throws InterruptedException {
    // 引导辅助程序
    ServerBootstrap sb = new ServerBootstrap();
    // 通过nio方式来接收连接和处理连接
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // 通过nio方式来接收连接和处理连接
        sb.group(group);
        // 设置nio类型的channel
        sb.channel(NioServerSocketChannel.class);
        sb.childHandler(new // 有连接到达时会创建一个channel
        ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                // 字符串解码 和 编码
                p.addLast("decoder", new StringDecoder());
                p.addLast("encoder", new StringEncoder());
                // 在channel队列中添加一个handler来处理业务
                p.addLast("handler", new NettyServerHandler());
            // 以("\n")为结尾分割的 解码器
            // cp.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
            }
        });
        // 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
        ChannelFuture f = sb.bind(port).sync();
        System.out.println("服务端已启动... 端口是:" + port);
        // 应用程序会一直等待,直到channel关闭
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    // group.shutdownGracefully().sync();//关闭EventLoopGroup,释放掉所有资源包括创建的线程
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)40 StringDecoder (io.netty.handler.codec.string.StringDecoder)37 ChannelPipeline (io.netty.channel.ChannelPipeline)28 SocketChannel (io.netty.channel.socket.SocketChannel)13 ChannelFuture (io.netty.channel.ChannelFuture)12 EventLoopGroup (io.netty.channel.EventLoopGroup)9 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)9 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)9 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)8 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)7 Channel (io.netty.channel.Channel)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)7 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)7 NettyServerAndClient (org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient)5 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)5 SocketChannel (org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel)5 StringDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder)5