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.ogcs.okra.example.benchmark.BenchmarkClient.java
License:Apache License
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override//from w w w . j ava2s . c om protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); // Any other useful handler cp.addLast("strDecoder", STRING_DECODER); cp.addLast("strEncoder", STRING_ENCODER); cp.addLast("handler", new SimpleChannelInboundHandler<String>() { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { COUNT.getAndIncrement(); // ChannelPromise voidPromise = ctx.voidPromise(); // if (ctx.channel().isWritable()) { // ctx.writeAndFlush(msg, voidPromise); // } else { // ctx.channel().eventLoop().schedule(() -> { // ctx.writeAndFlush(msg, voidPromise); // }, 1L, TimeUnit.SECONDS); // } } }); } }; }
From source file:org.ogcs.okra.example.benchmark.BenchmarkServer.java
License:Apache License
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override// w w w . jav a2 s . c o m protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); // Any other useful handler cp.addLast("strDecoder", STRING_DECODER); cp.addLast("strEncoder", STRING_ENCODER); cp.addLast("handler", HANDLER); } }; }
From source file:org.ogcs.okra.example.Client.java
License:Apache License
public static void main(String[] args) { TcpProtocolClient client = new TcpProtocolClient("127.0.0.1", 9008) { @Override//from w w w. ja va 2 s .c o m protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); cp.addLast("decoder", GPB_DECODER_HANDLER); cp.addLast("encoder", GPB_ENCODER_HANDLER); // handler cp.addLast("handler", new SimpleChannelInboundHandler<Response>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Response msg) throws Exception { System.out.println("msg:" + msg.getId() + ", "); } }); // cp.addLast("handler", new ServerHandler()); } }; } }; client.start(); AtomicInteger ID = new AtomicInteger(0); Channel client1 = client.client(); client1.writeAndFlush(Request.newBuilder().setId(ID.incrementAndGet()).setApi(1).build()); }
From source file:org.ogcs.okra.example.game.impl.arpg.ArpgLogicServer.java
License:Apache License
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override/*from w w w .ja v a 2 s . c o m*/ protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); cp.addLast("decoder", STRING_DECODER); cp.addLast("encoder", STRING_ENCODER); cp.addLast("handler", new SimpleChannelInboundHandler<String>() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); sets.add(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); sets.remove(ctx); } @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // JSONObject jsonObject = JSON.parseObject(msg); System.out.println(msg); for (ChannelHandlerContext set : sets) { set.writeAndFlush(msg); } } }); } }; }
From source file:org.ogcs.okra.example.socket.TcpServer.java
License:Apache License
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override/*from www . j a va 2 s . c o m*/ protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); // Any other useful handler cp.addLast("handler", new ExampleSocketHandler()); } }; }
From source file:org.pumpkindb.Client.java
License:Mozilla Public License
public void connect() { workerGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(workerGroup);//from w ww . ja v a 2 s .com b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addFirst(new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast(new LengthFieldPrepender(4)); ch.pipeline().addLast(new FrameEncoder()); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; messageHandler.accept(buf); } }); } }); ChannelFuture channelFuture = b.connect(host, port).syncUninterruptibly(); channel = channelFuture.channel(); }
From source file:org.robotninjas.protobuf.netty.client.ClientInitializer.java
License:Open Source License
@Override protected void initChannel(T ch) throws Exception { ChannelPipeline p = ch.pipeline();/*from w ww . ja va 2 s .c o m*/ p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); p.addLast("protobufDecoder", new ProtobufDecoder(NettyRpcProto.RpcContainer.getDefaultInstance())); p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("protobufEncoder", new ProtobufEncoder()); ConcurrentHashMap<Integer, RpcCall> callMap = new ConcurrentHashMap<Integer, RpcCall>(); InboundHandler handler = new InboundHandler(callMap); if (eventExecutor.isPresent()) { p.addLast(eventExecutor.get(), "inboundHandler", handler); } else { p.addLast("inboundHandler", handler); } p.addLast("outboundHandler", new OutboundHandler(callMap)); }
From source file:org.skfiy.typhon.net.Netty4Connector.java
License:Apache License
@Override protected void startInternal() throws LifecycleException { setState(LifecycleState.STARTING);/*from w w w . j a v a2 s .co m*/ fireLifecycleListener(START_EVENT); System.setProperty("io.netty.noJdkZlibDecoder", "false"); final byte[] delimiters = new byte[] { '\n' }; final String compressionMode = Typhons.getProperty("typhon.spi.net.compressionMode"); final Netty4EndpointHandler handler = new Netty4EndpointHandler(); final Netty4ConnectionLimitHandler limitHandler = new Netty4ConnectionLimitHandler(); // ???? final LengthFieldPrepender lengthFieldPrepender; final DelimiterBasedFrameEncoder delimiterBasedFrameEncoder; if ("zlib".equals(compressionMode)) { lengthFieldPrepender = new LengthFieldPrepender(4); delimiterBasedFrameEncoder = null; } else { lengthFieldPrepender = null; delimiterBasedFrameEncoder = new DelimiterBasedFrameEncoder(delimiters); } // final LoggingHandler loggingHandler; if (isLogEnabled()) { loggingHandler = new LoggingHandler(LogLevel.DEBUG); } else { loggingHandler = null; } serverBootstrap = new ServerBootstrap(); serverBootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel c) throws Exception { ChannelPipeline pipeline = c.pipeline(); if ("zlib".equals(compressionMode)) { pipeline.addLast("lengthFieldPrepender", lengthFieldPrepender); pipeline.addLast("lengthFieldBasedFrameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB)); } else { pipeline.addLast("delimiterBasedFrameDecoder", new DelimiterBasedFrameDecoder(65535, new ByteBuf[] { Unpooled.wrappedBuffer(delimiters) })); pipeline.addLast("delimiterBasedFrameEncoder", delimiterBasedFrameEncoder); } if (isLogEnabled()) { pipeline.addLast(loggingHandler); } pipeline.addLast(new IdleStateHandler(60 * 10, 60 * 10, 0)); pipeline.addLast(limitHandler, handler); } }); channel = serverBootstrap.bind(host, port).channel(); CLOG.debug("Netty4Connector started on port {}", port); MBeanServer mbs = MBeanUtils.REGISTRY.getMBeanServer(); Object obj = null; try { obj = mbs.invoke(Container.OBJECT_NAME, "getInstance", new Object[] { ProtocolHandler.class }, new String[] { Class.class.getName() }); } catch (Exception ex) { CLOG.error("ProtocolHandler", ex); throw new TyphonException(ex); } handler.setProtocolHandler((ProtocolHandler) obj); }
From source file:org.traccar.protocol.AnytrekProtocol.java
License:Apache License
public AnytrekProtocol() { addServer(new TrackerServer(false, getName()) { @Override/* w w w .jav a2s. c o m*/ protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 2, 2, 2, 0)); pipeline.addLast(new AnytrekProtocolDecoder(AnytrekProtocol.this)); } }); }
From source file:org.traccar.protocol.AstraProtocol.java
License:Apache License
public AstraProtocol() { addServer(new TrackerServer(false, getName()) { @Override// w ww . j a v a 2 s . co m protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 1, 2, -3, 0)); pipeline.addLast(new AstraProtocolDecoder(AstraProtocol.this)); } }); addServer(new TrackerServer(true, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new AstraProtocolDecoder(AstraProtocol.this)); } }); }