List of usage examples for io.netty.handler.codec LengthFieldBasedFrameDecoder LengthFieldBasedFrameDecoder
public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip)
From source file:org.apache.hadoop.hbase.security.NettyHBaseSaslRpcClient.java
License:Apache License
public void setupSaslHandler(ChannelPipeline p) { String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP); if (LOG.isDebugEnabled()) { LOG.debug("SASL client context established. Negotiated QoP: " + qop); }/*ww w. j a v a2 s . co m*/ if (qop == null || "auth".equalsIgnoreCase(qop)) { return; } // add wrap and unwrap handlers to pipeline. p.addFirst(new SaslWrapHandler(saslClient), new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4), new SaslUnwrapHandler(saslClient)); }
From source file:org.apache.helix.ipc.netty.NettyHelixIPCService.java
License:Apache License
/** * Starts message handling server, creates client bootstrap, and bootstraps partition routing * table./*w w w . j a v a 2 s . com*/ */ public void start() throws Exception { if (isShutdown.getAndSet(false)) { eventLoopGroup = new NioEventLoopGroup(); statTxMsg = metricRegistry.meter(MetricRegistry.name(NettyHelixIPCService.class, "txMsg")); statRxMsg = metricRegistry.meter(MetricRegistry.name(NettyHelixIPCService.class, "rxMsg")); statTxBytes = metricRegistry.meter(MetricRegistry.name(NettyHelixIPCService.class, "txBytes")); statRxBytes = metricRegistry.meter(MetricRegistry.name(NettyHelixIPCService.class, "rxBytes")); statChannelOpen = metricRegistry .counter(MetricRegistry.name(NettyHelixIPCService.class, "channelOpen")); statError = metricRegistry.counter(MetricRegistry.name(NettyHelixIPCService.class, "error")); // Report metrics via JMX jmxReporter = JmxReporter.forRegistry(metricRegistry).build(); jmxReporter.start(); LOG.info("Registered JMX metrics reporter"); new ServerBootstrap().group(eventLoopGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline() .addLast(new LengthFieldBasedFrameDecoder(config.getMaxFrameLength(), LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP)); socketChannel.pipeline().addLast(new NettyHelixIPCCallbackHandler( config.getInstanceName(), callbacks, statRxMsg, statRxBytes)); } }).bind(new InetSocketAddress(config.getPort())); LOG.info("Listening on port " + config.getPort()); clientBootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new LengthFieldPrepender(LENGTH_FIELD_LENGTH, true)); socketChannel.pipeline().addLast(new NettyHelixIPCBackPressureHandler()); } }); } }
From source file:org.apache.pulsar.client.impl.PulsarChannelInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { if (sslCtx != null) { ch.pipeline().addLast(TLS_HANDLER, sslCtx.newHandler(ch.alloc())); ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.COPYING_ENCODER); } else {/*from w w w . j a va 2 s .c o m*/ ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.ENCODER); } ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(PulsarDecoder.MaxFrameSize, 0, 4, 0, 4)); ch.pipeline().addLast("handler", clientCnxSupplier.get()); }
From source file:org.apache.pulsar.proxy.server.DirectProxyHandler.java
License:Apache License
public DirectProxyHandler(ProxyService service, ProxyConnection proxyConnection, String targetBrokerUrl, int protocolVersion, SslContext sslCtx) { this.authentication = proxyConnection.getClientAuthentication(); this.inboundChannel = proxyConnection.ctx().channel(); this.originalPrincipal = proxyConnection.clientAuthRole; this.clientAuthData = proxyConnection.clientAuthData; this.clientAuthMethod = proxyConnection.clientAuthMethod; this.protocolVersion = protocolVersion; this.sslCtx = sslCtx; ProxyConfiguration config = service.getConfiguration(); // Start the connection attempt. Bootstrap b = new Bootstrap(); // Tie the backend connection on the same thread to avoid context // switches when passing data between the 2 // connections b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.group(inboundChannel.eventLoop()).channel(inboundChannel.getClass()).option(ChannelOption.AUTO_READ, false);//from w w w. j a v a 2 s . c o m b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (sslCtx != null) { ch.pipeline().addLast(TLS_HANDLER, sslCtx.newHandler(ch.alloc())); } ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(PulsarDecoder.MaxFrameSize, 0, 4, 0, 4)); ch.pipeline().addLast("proxyOutboundHandler", new ProxyBackendHandler(config, protocolVersion)); } }); URI targetBroker; try { // targetBrokerUrl is coming in the "hostname:6650" form, so we need // to extract host and port targetBroker = new URI("pulsar://" + targetBrokerUrl); } catch (URISyntaxException e) { log.warn("[{}] Failed to parse broker url '{}'", inboundChannel, targetBrokerUrl, e); inboundChannel.close(); return; } ChannelFuture f = b.connect(targetBroker.getHost(), targetBroker.getPort()); outboundChannel = f.channel(); f.addListener(future -> { if (!future.isSuccess()) { // Close the connection if the connection attempt has failed. inboundChannel.close(); return; } final ProxyBackendHandler cnx = (ProxyBackendHandler) outboundChannel.pipeline() .get("proxyOutboundHandler"); cnx.setRemoteHostName(targetBroker.getHost()); }); }
From source file:org.apache.pulsar.proxy.server.ServiceChannelInitializer.java
License:Apache License
@Override protected void initChannel(SocketChannel ch) throws Exception { if (serverSslCtxRefresher != null && this.enableTls) { SslContext sslContext = serverSslCtxRefresher.get(); if (sslContext != null) { ch.pipeline().addLast(TLS_HANDLER, sslContext.newHandler(ch.alloc())); }// w ww .ja v a2 s . c o m } ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(PulsarDecoder.MaxFrameSize, 0, 4, 0, 4)); ch.pipeline().addLast("handler", new ProxyConnection(proxyService, clientSslCtxRefresher == null ? null : clientSslCtxRefresher.get())); }
From source file:org.apache.reef.wake.remote.transport.netty.NettyChannelInitializer.java
License:Apache License
@Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MAXFRAMELENGTH, 0, 4, 0, 4)) .addLast("bytesDecoder", new ByteArrayDecoder()) .addLast("frameEncoder", new LengthFieldPrepender(4)) .addLast("bytesEncoder", new ByteArrayEncoder()).addLast("chunker", new ChunkedReadWriteHandler()) .addLast("handler", handlerFactory.createChannelInboundHandler()); }
From source file:org.didinem.client.RpcClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO/*from w w w.j a v a2s . c o m*/ EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 4, 0, 4)); ch.pipeline().addLast(new LengthFieldPrepender(4, false)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new RpcClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); channel = f.channel(); } finally { // NIO group.shutdownGracefully(); } }
From source file:org.gameoss.gridcast.p2p.node.NodeClient.java
License:Apache License
public void initialize(final String host, int port, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener) throws InterruptedException { // prepare connection Bootstrap boot = new Bootstrap(); boot.group(workerGroup);/*www .ja v a 2s . c o m*/ boot.channel(NioSocketChannel.class); boot.option(ChannelOption.SO_KEEPALIVE, true); boot.option(ChannelOption.TCP_NODELAY, true); boot.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // encoders p.addLast(new LengthFieldPrepender(4)); p.addLast(new ProtostuffEncoder(messageRegistry)); // decoders p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4)); p.addLast(new ProtostuffDecoder(messageRegistry)); p.addLast(channelListener); } }); // connect channel = boot.connect(host, port).sync().channel(); }
From source file:org.gameoss.gridcast.p2p.node.NodeServer.java
License:Apache License
public void initialize(final String host, final int port, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener) { ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workerGroup);/* w w w.j a v a 2s . c om*/ boot.channel(NioServerSocketChannel.class); boot.option(ChannelOption.SO_BACKLOG, 32); boot.childOption(ChannelOption.SO_KEEPALIVE, true); boot.childOption(ChannelOption.TCP_NODELAY, true); boot.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // encoders p.addLast(new LengthFieldPrepender(4)); p.addLast(new ProtostuffEncoder(messageRegistry)); // decoders p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4)); p.addLast(new ProtostuffDecoder(messageRegistry)); p.addLast(channelListener); } }); // start accepting connection try { if (host == null) { acceptChannel = boot.bind(port).sync().channel(); } else { acceptChannel = boot.bind(host, port).sync().channel(); } } catch (InterruptedException e) { logger.error("Binding to port {} failed", port, e); } }
From source file:org.kaazing.messaging.driver.transport.netty.tcp.NettyTransportContext.java
License:Apache License
public NettyTransportContext() { super();// ww w.j a v a 2s .c o m if (USE_SSL) { SelfSignedCertificate ssc = null; try { ssc = new SelfSignedCertificate(); serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); clientSslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); } catch (CertificateException e) { LOGGER.error("CertificateException", e); throw new IllegalArgumentException("Error creating transport context", e); } catch (SSLException e) { LOGGER.error("SSLException", e); throw new IllegalArgumentException("Error creating transport context", e); } } else { serverSslCtx = null; clientSslCtx = null; } // Configure the server. serverBossGroup = new NioEventLoopGroup(1); serverWorkerGroup = new NioEventLoopGroup(); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { final ChannelPipeline p = ch.pipeline(); if (serverSslCtx != null) { p.addLast(serverSslCtx.newHandler(ch.alloc())); } p.addLast(new LengthFieldBasedFrameDecoder(1000000, 0, 4, 0, 4)); serverReceivingTransportsLock.readLock().lock(); try { serverReceivingTransports.forEach((nettyReceivingTransport) -> { if (ch.localAddress().equals(nettyReceivingTransport.getInetSocketAddress()) || nettyReceivingTransport.isInAddrAny() && ch.localAddress().getPort() == nettyReceivingTransport .getInetSocketAddress().getPort()) { p.addLast(nettyReceivingTransport.getNettyChannelHandler()); } }); } finally { serverReceivingTransportsLock.readLock().unlock(); } } }); bootstrap = new Bootstrap(); group = new NioEventLoopGroup(); bootstrap.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(); if (clientSslCtx != null) { p.addLast(clientSslCtx.newHandler(ch.alloc())); } } }); }