@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc(), FactorialClient.HOST, FactorialClient.PORT)); } // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); // Add the number codec first, pipeline.addLast(new BigIntegerDecoder()); pipeline.addLast(new NumberEncoder()); // and then business logic. pipeline.addLast(new FactorialClientHandler()); }
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); } // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); // Add the number codec first, pipeline.addLast(new BigIntegerDecoder()); pipeline.addLast(new NumberEncoder()); // and then business logic. // Please note we create a handler for every new channel // because it has stateful properties. pipeline.addLast(new FactorialServerHandler()); }
@Override protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception { if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) { return new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } if ("deflate".equalsIgnoreCase(contentEncoding) || "x-deflate".equalsIgnoreCase(contentEncoding)) { ZlibWrapper wrapper; if (strict) { wrapper = ZlibWrapper.ZLIB; } else { wrapper = ZlibWrapper.ZLIB_OR_NONE; } // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(wrapper)); } // 'identity' or unsupported return null; }
@Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); // use the IdleStateHandler to get notified if you haven't received or sent data for dozens of seconds. // If this is the case, a heartbeat will be written to the remote peer, and if this fails the connection is closed. pipeline.addLast(this.executorGroup, "idleStateHandler", new IdleStateHandler(0, 0, Constants.HEARTBEAT_PERIOD, TimeUnit.SECONDS)); pipeline.addLast(this.executorGroup, "heartbeatHandler", heartbeatHandler); if (this.compression) { // Enable stream compression pipeline.addLast(this.executorGroup, "deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(this.executorGroup, "inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } // NUL (0x00) is a message delimiter pipeline.addLast(this.executorGroup, "framer", new DelimiterBasedFrameDecoder(8192, Delimiters.nulDelimiter())); // string encoder / decoder are responsible for encoding / decoding an UTF-8 string pipeline.addLast(this.executorGroup, "encoder", utf8Encoder); pipeline.addLast(this.executorGroup, "decoder", utf8Decoder); // client hander is responsible for as a remoting call stub pipeline.addLast(this.executorGroup, "clientHandler", clientHandler); }
@Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (this.compression) { // Enable stream compression pipeline.addLast(this.executorGroup, "deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(this.executorGroup, "inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } // NUL (0x00) is a message delimiter pipeline.addLast(this.executorGroup, "framer", new DelimiterBasedFrameDecoder(8192, Delimiters.nulDelimiter())); // string encoder / decoder are responsible for encoding / decoding an UTF-8 string pipeline.addLast(this.executorGroup, "encoder", utf8Encoder); pipeline.addLast(this.executorGroup, "decoder", utf8Decoder); // server hander is responsible for as a remoting call skeleton pipeline.addLast(this.executorGroup, "serverHandler", serverHandler); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); // Add the number codec first, pipeline.addLast("decoder", new BigIntegerDecoder()); pipeline.addLast("encoder", new NumberEncoder()); // and then business logic. // Please note we create a handler for every new channel // because it has stateful properties. pipeline.addLast("handler", new FactorialServerHandler()); }
private void enableGzip(final ChannelHandlerContext ctx) { final ChannelPipeline p = ctx.pipeline(); try { // p.addAfter("connmgr", "gzipdeflater", new JZlibEncoder(ZlibWrapper.GZIP){ // // TODO // }); p.addAfter("gzipdetector", "gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); p.remove(this); } catch (Exception ex) { log.error("Failed to add gzip handlers", ex); } }
@Override protected void initChannel(final NioSocketChannel ch) throws Exception { final ChannelPipeline p = ch.pipeline(); if(compressionEnabled) p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); p.addLast("stringEncoder", STR_ENCODER); p.addLast("metricEncoder", METRIC_ENCODER); p.addLast("stringDecoder", new StringDecoder(UTF8)); p.addLast("responseHandler", RESPONSE_HANDLER); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // Use the first five bytes to detect gzip if (in.readableBytes() < 5) { return; } if (SslHandler.isEncrypted(in)) { // If the channel is encrypted, close the channel as SSL must be // disabled and only unencrypted connections are supported. LOGGER.warn( "Connection is encrypted when SSL is disabled, closing"); in.clear(); ctx.close(); return; } final ChannelPipeline p = ctx.pipeline(); final int magic1 = in.getUnsignedByte(in.readerIndex()); final int magic2 = in.getUnsignedByte(in.readerIndex() + 1); if (isGzip(magic1, magic2)) { LOGGER.debug( "Channel is gzipped, replacing gzip detector with inflater"); p.replace("gzipDetector", "inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } else { LOGGER.debug("Channel is not gzipped, removing gzip detector"); p.remove(this); } }
private void enableGzip(ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline(); p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); p.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); p.addLast("unificationB", new PortUnificationServerHandler(sslCtx, detectSsl, false)); p.remove(this); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) if (compress) { pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } /** * length (4 bytes). * * Note: max message size is 64 Mb = 67108864 bytes this defines a * framer with a max of 64 Mb message, 4 bytes are the length, and strip * 4 bytes */ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4)); // decoder must be first pipeline.addLast("protobufDecoder", new ProtobufDecoder(CommandMessage.getDefaultInstance())); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("protobufEncoder", new ProtobufEncoder()); // our server processor (new instance for each connection) pipeline.addLast("handler", new CommHandler()); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) if (compress) { pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } /** * length (4 bytes). * * Note: max message size is 64 Mb = 67108864 bytes this defines a * framer with a max of 64 Mb message, 4 bytes are the length, and strip * 4 bytes */ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4)); // decoder must be first pipeline.addLast("protobufDecoder", new ProtobufDecoder(WorkMessage.getDefaultInstance())); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("protobufEncoder", new ProtobufEncoder()); // our server processor (new instance for each connection) pipeline.addLast("handler", new WorkChannelHandler (state)); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) if (compress) { pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } /** * length (4 bytes). * * Note: max message size is 64 Mb = 67108864 bytes this defines a * framer with a max of 64 Mb message, 4 bytes are the length, and strip * 4 bytes */ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4)); // decoder must be first pipeline.addLast("protobufDecoder", new ProtobufDecoder(CommandMessage.getDefaultInstance())); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("protobufEncoder", new ProtobufEncoder()); // our server processor (new instance for each connection) pipeline.addLast("handler", new CommandChannelHandler (conf, cmdMessageHandler)); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) if (compress) { pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } /** * length (4 bytes). * * Note: max message size is 64 Mb = 67108864 bytes this defines a * framer with a max of 64 Mb message, 4 bytes are the length, and strip * 4 bytes */ pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4)); // decoder must be first pipeline.addLast("protobufDecoder", new ProtobufDecoder(ClusterMonitor.getDefaultInstance())); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("protobufEncoder", new ProtobufEncoder()); // our server processor (new instance for each connection) pipeline.addLast("handler", new MonitorHandler()); }
@Override protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception { String contentEncoding = headers.headers().get(HttpHeaders.Names.CONTENT_ENCODING); if (contentEncoding != null && !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) { return null; } ZlibWrapper wrapper = determineWrapper(acceptEncoding); if (wrapper == null) { return null; } String targetContentEncoding; switch (wrapper) { case GZIP: targetContentEncoding = "gzip"; break; case ZLIB: targetContentEncoding = "deflate"; break; default: throw new Error(); } return new Result( targetContentEncoding, new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel))); }
private void enableGzip(ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline(); p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); p.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); p.addLast("2ndPhaseSwitch", new ProtocolSwitch(false)); p.remove(this); log.info("enabled gzip: [{}]", ctx.channel().id()); }
private void enableGzip(ChannelHandlerContext ctx) { ChannelPipeline pipeline = ctx.pipeline(); pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); pipeline.addLast("unificationB", new PortUnificationServerHandler(false)); pipeline.remove(this); }
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); pipeline.addLast(new NumberEncoder()); pipeline.addLast(new FactorialClientHandler()); }
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); pipeline.addLast(new BigIntegerDecoder()); pipeline.addLast(new FactorialServerHandler()); }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Enable stream compression (you can remove these two if unnecessary) pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); // Add the number codec first, pipeline.addLast("decoder", new BigIntegerDecoder()); pipeline.addLast("encoder", new NumberEncoder()); // and then business logic. pipeline.addLast("handler", new FactorialClientHandler(count)); }
private void enableGzip(ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline(); p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); p.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); p.addLast("unificationB", new PortUnificationServerHandler(detectSsl, false)); p.remove(this); }
@Override protected Result beginEncode(HttpResponse headers, CharSequence acceptEncoding) throws Exception { String contentEncoding = headers.headers().get(HttpHeaders.Names.CONTENT_ENCODING); if (contentEncoding != null && !HttpHeaders.equalsIgnoreCase(HttpHeaders.Values.IDENTITY, contentEncoding)) { return null; } ZlibWrapper wrapper = determineWrapper(acceptEncoding); if (wrapper == null) { return null; } String targetContentEncoding; switch (wrapper) { case GZIP: targetContentEncoding = "gzip"; break; case ZLIB: targetContentEncoding = "deflate"; break; default: throw new Error(); } return new Result( targetContentEncoding, new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel))); }
public void start() throws Exception { EventLoopGroup groupAcceptor = new NioEventLoopGroup(); EventLoopGroup groupWorker = new NioEventLoopGroup(); try { ServerBootstrap server = new ServerBootstrap(); server.group(groupAcceptor, groupWorker). channel(NioServerSocketChannel.class). childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP)); ch.pipeline().addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEncoder(), new ServerHandler() ); } }). option(ChannelOption.SO_BACKLOG, 500). option(ChannelOption.SO_KEEPALIVE, true). option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Configure.SERVER_NETWORK_CONNECTION_TIMEOUT_MILLIS); ChannelFuture future = server.bind(Configure.SERVER_NETWORK_HOST, Configure.SERVER_NETWORK_PORT).sync(); future.channel().closeFuture().sync(); } finally { groupAcceptor.shutdownGracefully(); groupWorker.shutdownGracefully(); } }
ZlibStreamDecoder(ZlibWrapper zlibWrapper) { decoder = new EmbeddedChannel(false, ZlibCodecFactory.newZlibDecoder(zlibWrapper)); }