Search in sources :

Example 16 with StringDecoder

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

the class NettyClientServerSslTest method testSslHandshakeError.

/**
	 * Verify SSL handshake error when untrusted server certificate is used
	 *
	 */
@Test
public void testSslHandshakeError() throws Exception {
    NettyProtocol protocol = new NettyProtocol() {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            return new ChannelHandler[0];
        }

        @Override
        public ChannelHandler[] getClientChannelHandlers() {
            return new ChannelHandler[0];
        }
    };
    Configuration config = createSslConfig();
    // Use a server certificate which is not present in the truststore
    config.setString(ConfigConstants.SECURITY_SSL_KEYSTORE, "src/test/resources/untrusted.keystore");
    NettyConfig nettyConfig = new NettyConfig(InetAddress.getLoopbackAddress(), NetUtils.getAvailablePort(), NettyTestUtil.DEFAULT_SEGMENT_SIZE, 1, config);
    NettyTestUtil.NettyServerAndClient serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    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(io.netty.handler.codec.string.StringEncoder) Configuration(org.apache.flink.configuration.Configuration) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 17 with StringDecoder

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

the class TcpStringServer method addDecoders.

@Override
protected void addDecoders(SocketChannel channel) {
    // Add delimiter and string decoders to do the work
    channel.pipeline().addLast(new DelimiterBasedFrameDecoder(maxFrameLength, stripDelimiter, Unpooled.wrappedBuffer(delimiter.getBytes())));
    channel.pipeline().addLast(new StringDecoder());
}
Also used : DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder)

Example 18 with StringDecoder

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

the class HelloServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 以("\n")为结尾分割的 解码器
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 自己的逻辑Handler
    pipeline.addLast("handler", new HelloServerHandler());
}
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 19 with StringDecoder

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

the class MyNettyServer 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 {
            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) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 20 with StringDecoder

use of io.netty.handler.codec.string.StringDecoder 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)

Aggregations

StringDecoder (io.netty.handler.codec.string.StringDecoder)20 StringEncoder (io.netty.handler.codec.string.StringEncoder)19 ChannelPipeline (io.netty.channel.ChannelPipeline)11 Channel (io.netty.channel.Channel)7 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)7 ChannelFuture (io.netty.channel.ChannelFuture)6 SocketChannel (io.netty.channel.socket.SocketChannel)5 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)5 Bootstrap (io.netty.bootstrap.Bootstrap)4 ChannelHandler (io.netty.channel.ChannelHandler)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)4 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 IOException (java.io.IOException)3 JndiRegistry (org.apache.camel.impl.JndiRegistry)3 LoggingHandler (io.netty.handler.logging.LoggingHandler)2