@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))); }
@Test public void testGetTargetContentEncoding() throws Exception { HttpContentCompressor compressor = new HttpContentCompressor(); String[] tests = { // Accept-Encoding -> Content-Encoding "", null, "*", "gzip", "*;q=0.0", null, "gzip", "gzip", "compress, gzip;q=0.5", "gzip", "gzip; q=0.5, identity", "gzip", "gzip ; q=0.1", "gzip", "gzip; q=0, deflate", "deflate", " deflate ; q=0 , *;q=0.5", "gzip", }; for (int i = 0; i < tests.length; i += 2) { String acceptEncoding = tests[i]; String contentEncoding = tests[i + 1]; ZlibWrapper targetWrapper = compressor.determineWrapper(acceptEncoding); String targetEncoding = null; if (targetWrapper != null) { switch (targetWrapper) { case GZIP: targetEncoding = "gzip"; break; case ZLIB: targetEncoding = "deflate"; break; default: fail(); } } assertEquals(contentEncoding, targetEncoding); } }
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); }
protected ZlibWrapper determineWrapper(String acceptEncoding) { float starQ = -1.0f; float gzipQ = -1.0f; float deflateQ = -1.0f; for (String encoding: StringUtil.split(acceptEncoding, ',')) { float q = 1.0f; int equalsPos = encoding.indexOf('='); if (equalsPos != -1) { try { q = Float.valueOf(encoding.substring(equalsPos + 1)); } catch (NumberFormatException e) { // Ignore encoding q = 0.0f; } } if (encoding.contains("*")) { starQ = q; } else if (encoding.contains("gzip") && q > gzipQ) { gzipQ = q; } else if (encoding.contains("deflate") && q > deflateQ) { deflateQ = q; } } if (gzipQ > 0.0f || deflateQ > 0.0f) { if (gzipQ >= deflateQ) { return ZlibWrapper.GZIP; } else { return ZlibWrapper.ZLIB; } } if (starQ > 0.0f) { if (gzipQ == -1.0f) { return ZlibWrapper.GZIP; } if (deflateQ == -1.0f) { return ZlibWrapper.ZLIB; } } return null; }
@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))); }
protected ZlibWrapper determineWrapper(CharSequence acceptEncoding) { float starQ = -1.0f; float gzipQ = -1.0f; float deflateQ = -1.0f; for (String encoding: StringUtil.split(acceptEncoding.toString(), ',')) { float q = 1.0f; int equalsPos = encoding.indexOf('='); if (equalsPos != -1) { try { q = Float.valueOf(encoding.substring(equalsPos + 1)); } catch (NumberFormatException e) { // Ignore encoding q = 0.0f; } } if (encoding.contains("*")) { starQ = q; } else if (encoding.contains("gzip") && q > gzipQ) { gzipQ = q; } else if (encoding.contains("deflate") && q > deflateQ) { deflateQ = q; } } if (gzipQ > 0.0f || deflateQ > 0.0f) { if (gzipQ >= deflateQ) { return ZlibWrapper.GZIP; } else { return ZlibWrapper.ZLIB; } } if (starQ > 0.0f) { if (gzipQ == -1.0f) { return ZlibWrapper.GZIP; } if (deflateQ == -1.0f) { return ZlibWrapper.ZLIB; } } return null; }
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(); } }