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:io.reactivex.netty.RemoteRxServer.java
License:Apache License
@SuppressWarnings("rawtypes") public RemoteRxServer(Builder builder) { port = builder.getPort();//from ww w . jav a 2 s. c om // setup configuration state for server Map<String, RemoteObservableConfiguration> configuredObservables = new HashMap<String, RemoteObservableConfiguration>(); // add configs for (RemoteObservableConfiguration config : builder.getObservablesConfigured()) { String observableName = config.getName(); logger.debug("RemoteRxServer configured with remote observable: " + observableName); configuredObservables.put(observableName, config); } metrics = new ServerMetrics(); blockUntilCompleted = new CountDownLatch(1); // create server RxServer<RemoteRxEvent, RemoteRxEvent> server = RxNetty.createTcpServer(port, new PipelineConfiguratorComposite<RemoteRxEvent, RemoteRxEvent>( new PipelineConfigurator<RemoteRxEvent, RemoteRxEvent>() { @Override public void configureNewPipeline(ChannelPipeline pipeline) { // pipeline.addFirst(new LoggingHandler(LogLevel.ERROR)); // uncomment to enable debug logging pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); // 4 bytes to encode length pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(524288, 0, 4, 0, 4)); // max frame = half MB } }, new RxEventPipelineConfigurator()), new RemoteObservableConnectionHandler(configuredObservables, builder.getIngressPolicy(), blockUntilCompleted, metrics)); this.server = server; logger.info("RemoteRxServer started on port: " + port); }
From source file:io.soliton.protobuf.ChannelInitializers.java
License:Apache License
/** * Returns a new channel initializer suited to encode and decode a protocol * buffer message.//from w ww. j a va2 s .c o m * <p/> * <p>Message sizes over 10 MB are not supported.</p> * <p/> * <p>The handler will be executed on the I/O thread. Blocking operations * should be executed in their own thread.</p> * * @param defaultInstance an instance of the message to handle * @param handler the handler implementing the application logic * @param <M> the type of the support protocol buffer message */ public static final <M extends Message> ChannelInitializer<Channel> protoBuf(final M defaultInstance, final SimpleChannelInboundHandler<M> handler) { return new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { channel.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(10 * 1024 * 1024, 0, 4, 0, 4)); channel.pipeline().addLast("protobufDecoder", new ProtobufDecoder(defaultInstance)); channel.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4)); channel.pipeline().addLast("protobufEncoder", new ProtobufEncoder()); channel.pipeline().addLast("applicationHandler", handler); } }; }
From source file:io.viewserver.network.netty.NettyPipelineInitialiser.java
License:Apache License
@Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (pipeline.get("frameDecoder") == null) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1 << 30, 0, 4, 0, 4)); }/*from w w w . jav a 2 s . c o m*/ pipeline.addLast(new IncomingMessageHandler(networkMessageWheel)); if (pipeline.get("frameEncoder") == null) { pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); } pipeline.addLast(new OutgoingMessageHandler(networkMessageWheel)); }
From source file:lunarion.cluster.coordinator.server.CoordinatorServerChannelInitializer.java
License:Open Source License
protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, LunarDBServerChannelInitializer.MESSAGE_LENGTH, 0, LunarDBServerChannelInitializer.MESSAGE_LENGTH)); pipeline.addLast(new CoordinatorServerHandler(co, logger)); }
From source file:lunarion.node.LunarDBServerChannelInitializer.java
License:Open Source License
protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, LunarDBServerChannelInitializer.MESSAGE_LENGTH, 0, LunarDBServerChannelInitializer.MESSAGE_LENGTH)); pipeline.addLast(new LunarDBServerHandler(this.node_tc)); }
From source file:lunarion.node.requester.ClientChannelInitializer.java
License:Open Source License
@Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, ClientChannelInitializer.MESSAGE_LENGTH, 0, ClientChannelInitializer.MESSAGE_LENGTH)); pipeline.addLast(this.client_handler); }
From source file:majordodo.network.netty.NettyChannelAcceptor.java
License:Apache License
public void start() throws Exception { boolean useOpenSSL = NetworkUtils.isOpenSslAvailable(); if (ssl) {//from ww w . j ava 2 s .c o m if (sslCertFile == null) { LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate, useOpenSSL:" + useOpenSSL); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } SelfSignedCertificate ssc = new SelfSignedCertificate(); try { sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).ciphers(sslCiphers) .build(); } finally { ssc.delete(); } } else { LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath() + " , useOpenSSL:" + useOpenSSL); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword) .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).ciphers(sslCiphers) .build(); } } if (NetworkUtils.isEnableEpollNative()) { bossGroup = new EpollEventLoopGroup(workerThreads); workerGroup = new EpollEventLoopGroup(workerThreads); LOGGER.log(Level.INFO, "Using netty-native-epoll network type"); } else { bossGroup = new NioEventLoopGroup(workerThreads); workerGroup = new NioEventLoopGroup(workerThreads); } ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel( NetworkUtils.isEnableEpollNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { NettyChannel session = new NettyChannel("client", ch, callbackExecutor, null); if (acceptor != null) { acceptor.createConnection(session); } // ch.pipeline().addLast(new LoggingHandler()); // Add SSL handler first to encrypt and decrypt everything. if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc())); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messageencoder", new DodoMessageEncoder()); ch.pipeline().addLast("messagedecoder", new DodoMessageDecoder()); ch.pipeline().addLast(new InboundMessageHandler(session)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(host, port).sync(); // (7) this.channel = f.channel(); }
From source file:majordodo.network.netty.NettyConnector.java
License:Apache License
public NettyChannel connect() throws Exception { boolean useOpenSSL = NetworkUtils.isOpenSslAvailable(); if (ssl) {/*from ww w . j a va 2s . c o m*/ if (sslUnsecure) { this.sslCtx = SslContextBuilder.forClient() .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK) .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { this.sslCtx = SslContextBuilder.forClient() .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).build(); } } if (NetworkUtils.isEnableEpollNative()) { group = new EpollEventLoopGroup(); } else { group = new NioEventLoopGroup(); } LOG.log(Level.INFO, "Trying to connect to broker at " + host + ":" + port + " ssl:" + ssl + ", sslUnsecure:" + sslUnsecure + " openSsl:" + useOpenSSL); Bootstrap b = new Bootstrap(); b.group(group) .channel(NetworkUtils.isEnableEpollNative() ? EpollSocketChannel.class : NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { channel = new NettyChannel(host + ":" + port, ch, callbackExecutor, NettyConnector.this); channel.setMessagesReceiver(receiver); channel.setRemoteHost(host); if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port)); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messageencoder", new DodoMessageEncoder()); ch.pipeline().addLast("messagedecoder", new DodoMessageDecoder()); ch.pipeline().addLast(new InboundMessageHandler(channel)); } }); ChannelFuture f = b.connect(host, port).sync(); socketchannel = f.channel(); return channel; }
From source file:me.ferrybig.p2pnetwork.Main.java
private void startIncomingConnectionThread(int port) { ServerBootstrap server = new ServerBootstrap(); server.group(group);/*w w w . j av a 2 s . c o m*/ server.channel(NioServerSocketChannel.class); server.option(ChannelOption.SO_BACKLOG, 128); server.childOption(ChannelOption.SO_KEEPALIVE, true); server.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (blocked.containsKey(((InetSocketAddress) ch.remoteAddress()).getAddress())) { LOG.info(ch + "Rejected at socket level"); ch.close(); return; } ch.pipeline().addLast(new LengthFieldPrepender(4)); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new PacketEncoder()); ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new ServerBootstrapConnector(addr, incomingListener)); clientsIn.add(ch); ch.closeFuture().addListener(e1 -> { clientsIn.remove(ch); }); } }); ChannelFuture f = server.bind(port); f.addListener(e -> { this.servers.add((ServerChannel) f.channel()); f.channel().closeFuture().addListener(e1 -> { this.servers.remove((ServerChannel) f.channel()); }); }); }
From source file:me.ferrybig.p2pnetwork.Main.java
private void startOutgomingConnectionThread(InetAddress address, int port) { Bootstrap client = new Bootstrap(); client.group(group);//from w ww . j a v a 2 s . c om client.channel(NioSocketChannel.class); client.option(ChannelOption.SO_KEEPALIVE, true); client.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldPrepender(4)); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new PacketEncoder()); ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new ServerBootstrapConnector(addr, outgoingListener)); } }); ChannelFuture f = client.connect(address, port); f.addListener(e -> { this.clientsOut.add(f.channel()); f.channel().closeFuture().addListener(e1 -> { this.clientsOut.remove(f.channel()); }); }); }