Search in sources :

Example 51 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project Hydra by DataSecs.

the class HydraChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    // In
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    pipeline.addLast(new PacketDecoder(protocol));
    // Out
    pipeline.addLast(new LengthFieldPrepender(4));
    pipeline.addLast(new PacketEncoder(protocol));
    HydraSession session = new HydraSession(channel, protocol);
    pipeline.addLast(session);
    // Add sessions to protocol, to keep track of them
    if (isServer) {
        protocol.addSession(session);
    } else {
        protocol.setClientSession(session);
    }
    // Inform SessionListener about new session
    protocol.callSessionListener(true, session);
}
Also used : PacketEncoder(de.datasecs.hydra.shared.protocol.packets.serialization.PacketEncoder) HydraSession(de.datasecs.hydra.shared.handler.HydraSession) PacketDecoder(de.datasecs.hydra.shared.protocol.packets.serialization.PacketDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 52 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project carbondata by apache.

the class NonSecureDictionaryServer method bindToPort.

/**
 * Binds dictionary server to an available port.
 */
@Override
public void bindToPort() {
    long start = System.currentTimeMillis();
    // Configure the server.
    int i = 0;
    while (i < 10) {
        int newPort = port + i;
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
                    pipeline.addLast("NonSecureDictionaryServerHandler", nonSecureDictionaryServerHandler);
                }
            });
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
            String hostToBind = findLocalIpAddress(LOGGER);
            InetSocketAddress address = hostToBind == null ? new InetSocketAddress(newPort) : new InetSocketAddress(hostToBind, newPort);
            bootstrap.bind(address).sync();
            LOGGER.info("Dictionary Server started, Time spent " + (System.currentTimeMillis() - start) + " Listening on port " + newPort);
            this.port = newPort;
            this.host = hostToBind;
            break;
        } catch (Exception e) {
            LOGGER.error(e, "Dictionary Server Failed to bind to port:");
            if (i == 9) {
                throw new RuntimeException("Dictionary Server Could not bind to any port");
            }
        }
        i++;
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 53 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project java by wavefrontHQ.

the class PushAgent method startPickleListener.

protected void startPickleListener(String strPort, GraphiteFormatter formatter) {
    if (prefix != null && !prefix.isEmpty()) {
        preprocessors.forPort(strPort).forReportPoint().addTransformer(new ReportPointAddPrefixTransformer(prefix));
    }
    preprocessors.forPort(strPort).forReportPoint().addFilter(new ReportPointTimestampInRangeFilter(dataBackfillCutoffHours, dataPrefillCutoffHours));
    int port = Integer.parseInt(strPort);
    // Set up a custom handler
    ChannelHandler handler = new ChannelByteArrayHandler(new PickleProtocolDecoder("unknown", customSourceTags, formatter.getMetricMangler(), port), new PointHandlerImpl(strPort, pushValidationLevel, pushBlockedSamples, getFlushTasks(strPort)), preprocessors.forPort(strPort));
    // to the decoder.
    class FrameDecoderFactoryImpl implements StreamIngester.FrameDecoderFactory {

        @Override
        public ChannelInboundHandler getDecoder() {
            return new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, 1000000, 0, 4, 0, 4, false);
        }
    }
    startAsManagedThread(new StreamIngester(new FrameDecoderFactoryImpl(), handler, port).withChildChannelOptions(childChannelOptions), "listener-binary-pickle-" + port);
}
Also used : StreamIngester(com.wavefront.ingester.StreamIngester) ReportPointAddPrefixTransformer(com.wavefront.agent.preprocessor.ReportPointAddPrefixTransformer) PickleProtocolDecoder(com.wavefront.ingester.PickleProtocolDecoder) ReportPointTimestampInRangeFilter(com.wavefront.agent.preprocessor.ReportPointTimestampInRangeFilter) QueuingChannelHandler(com.wavefront.agent.histogram.QueuingChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 54 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project web3sdk by FISCO-BCOS.

the class ChannelConnections method startConnect.

public void startConnect() {
    if (running) {
        logger.debug("服务已启动");
        return;
    }
    logger.debug("初始化connections connect");
    // 初始化netty
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    bootstrap.group(workerGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    final ChannelConnections selfService = this;
    final ThreadPoolTaskExecutor selfThreadPool = threadPool;
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    final Resource keystoreResource = resolver.getResource(getClientKeystorePath());
    final Resource caResource = resolver.getResource(getCaCertPath());
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            KeyStore ks = KeyStore.getInstance("JKS");
            InputStream ksInputStream = keystoreResource.getInputStream();
            ks.load(ksInputStream, getKeystorePassWord().toCharArray());
            /*
				 * 每次连接使用新的handler 连接信息从socketChannel中获取
				 */
            ChannelHandler handler = new ChannelHandler();
            handler.setConnections(selfService);
            handler.setIsServer(false);
            handler.setThreadPool(selfThreadPool);
            SslContext sslCtx = SslContextBuilder.forClient().trustManager(caResource.getFile()).keyManager((PrivateKey) ks.getKey("client", getClientCertPassWord().toCharArray()), (X509Certificate) ks.getCertificate("client")).build();
            ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new LengthFieldBasedFrameDecoder(1024 * 1024 * 4, 0, 4, -4, 0), new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS), handler);
        }
    });
    running = true;
    Thread loop = new Thread() {

        public void run() {
            try {
                while (true) {
                    if (!running) {
                        return;
                    }
                    // 尝试重连
                    reconnect();
                    Thread.sleep(heartBeatDelay);
                }
            } catch (InterruptedException e) {
                logger.error("系统错误", e);
            }
        }
    };
    loop.start();
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) KeyStore(java.security.KeyStore) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 55 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project pravega by pravega.

the class AdminConnectionListenerTest method testCreateEncodingStack.

@Test
public void testCreateEncodingStack() {
    @Cleanup AdminConnectionListener listener = new AdminConnectionListener(false, false, "localhost", 6622, mock(StreamSegmentStore.class), mock(TableStore.class), new PassingTokenVerifier(), null, null, SecurityConfigDefaults.TLS_PROTOCOL_VERSION);
    List<ChannelHandler> stack = listener.createEncodingStack("connection");
    // Check that the order of encoders is the right one.
    Assert.assertTrue(stack.get(0) instanceof ExceptionLoggingHandler);
    Assert.assertTrue(stack.get(1) instanceof CommandEncoder);
    Assert.assertTrue(stack.get(2) instanceof LengthFieldBasedFrameDecoder);
    Assert.assertTrue(stack.get(3) instanceof CommandDecoder);
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) PassingTokenVerifier(io.pravega.segmentstore.server.host.delegationtoken.PassingTokenVerifier) ChannelHandler(io.netty.channel.ChannelHandler) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) Cleanup(lombok.Cleanup) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) Test(org.junit.Test)

Aggregations

LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)71 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)37 SocketChannel (io.netty.channel.socket.SocketChannel)35 ChannelPipeline (io.netty.channel.ChannelPipeline)31 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)25 Bootstrap (io.netty.bootstrap.Bootstrap)20 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)20 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)19 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)18 Channel (io.netty.channel.Channel)14 ChannelFuture (io.netty.channel.ChannelFuture)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 StringEncoder (io.netty.handler.codec.string.StringEncoder)13 SslContext (io.netty.handler.ssl.SslContext)13 StringDecoder (io.netty.handler.codec.string.StringDecoder)12 IOException (java.io.IOException)11 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)9 CommandDecoder (io.pravega.shared.protocol.netty.CommandDecoder)8 CommandEncoder (io.pravega.shared.protocol.netty.CommandEncoder)8 ExceptionLoggingHandler (io.pravega.shared.protocol.netty.ExceptionLoggingHandler)7