Search in sources :

Example 26 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.

the class ServerHandlersInit method initChannel.

protected void initChannel(SocketChannel socketChannel) throws Exception {
    SslHandler sslHandler = SSLHandlerProvider.getSSLHandler();
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast(sslHandler);
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new SecureChatServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) SslHandler(io.netty.handler.ssl.SslHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 27 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project remote-integration-example by thingsboard.

the class CustomIntegration method init.

@Override
public void init(TbIntegrationInitParams params) throws Exception {
    super.init(params);
    JsonNode configuration = mapper.readTree(params.getConfiguration().getConfiguration().get("configuration").asText());
    try {
        bossGroup = new NioEventLoopGroup();
        workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel socketChannel) {
                socketChannel.pipeline().addLast(new StringEncoder(), new StringDecoder(), new LineBasedFrameDecoder(1024));
                socketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                        log.debug("Server received the message: {}", msg);
                        if (msg.startsWith("Hello to ThingsBoard!")) {
                            deviceName = msg.substring(msg.indexOf("[") + 1, msg.indexOf("]"));
                            ctx.writeAndFlush("Hello from ThingsBoard!");
                            initialized = true;
                        } else {
                            if (initialized) {
                                CustomResponse response = new CustomResponse();
                                process(new CustomIntegrationMsg(msg, response));
                                ctx.writeAndFlush(response.getResult());
                            } else {
                                log.warn("The flaw was not started correctly!");
                            }
                        }
                    }
                });
            }
        });
        int port = getBindPort(configuration);
        serverChannel = bootstrap.bind(port).sync().channel();
        client = new CustomClient(port, getMsgGeneratorIntervalMs(configuration));
    } catch (Exception e) {
        log.error("Failed to init TCP server!", e);
        throw new RuntimeException();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) JsonNode(com.fasterxml.jackson.databind.JsonNode) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CustomIntegrationMsg(org.thingsboard.integration.custom.message.CustomIntegrationMsg) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) StringEncoder(io.netty.handler.codec.string.StringEncoder) CustomClient(org.thingsboard.integration.custom.client.CustomClient) CustomResponse(org.thingsboard.integration.custom.message.CustomResponse) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 28 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project learn-simple by muggle0.

the class MyClientInitializer method initChannel.

protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    // jia 一堆处理器 策略模式
    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 MyClientHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 29 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink-mirror by flink-ci.

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 30 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink-mirror by flink-ci.

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)

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