title: Netty 4 proxy: howto stop reading after the first PDU until the next read() call?
body: <p><strong>The situation:</strong>
  I re-used the <a href="https://github.com/netty/netty/tree/4.0/example/src/main/java/io/netty/example/proxy" rel="nofollow">proxy from the Netty 4 examples</a> to create my own.
  The key difference between the example and my implementation is that the proxy only connects to its remote peer after the first protocol data unit is processed.</p>
  
  <p>The relevant parts of my front-end handler:</p>
  
  <pre><code>@Override
      public void channelActive(ChannelHandlerContext ctx) throws Exception {
          ctx.channel().read();// read the first message to trigger "channelRead0(...)"
      }
  
  @Override
  protected void channelRead0(final ChannelHandlerContext ctx, UCPPacket ucpPacket) throws Exception {
          if(!this.authenticated) {// authenticate the client then forward this packet
              this.authenticateAndForwardPacket(ctx, ucpPacket);
          } else {// forward the packet
              this.forwardPacket(ctx, ucpPacket);
          }
      }
  
      private void forwardPacket(final ChannelHandlerContext ctx, UCPPacket ucpPacket) {
          if (outboundChannel.isActive()) {
              outboundChannel.writeAndFlush(ucpPacket).addListener(new ChannelFutureListener() {
                  @Override
                  public void operationComplete(ChannelFuture future) throws Exception {
                      if (future.isSuccess()) {// forwarding the packet succeeded so read the next one
                          ctx.channel().read();
                      } else {
                          future.channel().close();
                      }
                  }
              });
          } else { // this (often) happens when you don't set setSingleDecode(true) on the DelimiterBasedFrameDecoder
              LOGGER.error("FIXME: avoid else");//FIXME: ...
          }
      }
  </code></pre>
  
  <p>The pipeline:
  <code>DelimiterBasedFrameDecoder</code> ==> <code>UcpDecoder</code> ==> <code>FrontendHandler</code></p>
  
  <p><strong>The problem:</strong>
  The first <code>read()</code> on the <code>Channel</code> will often read the bytes of multiple protocol data units which means that even with AUTO_READ set to false, 2 or more <code>UCPPacket</code>s will often have to be processed. Can I somehow tell Netty that I'm done with the <code>ByteBuf</code> after the first <code>UCPPacket</code> is decoded (until I call <code>read()</code> again)? Or what else can I do? Blocking the subsequent <code>UCPPackets</code> in the <code>channelRead0</code> method of the front-end handler until <code>this.authenticated == true</code> is obviously a no go (as this would block an IO thread).</p>
  
  <p><strong>What I tried:</strong>
  I tried <code>setSingleDecode(true)</code> on <code>DelimiterBasedFrameDecoder</code> but that didn't work well. The first frame gets decoded correctly but even after the proxy has forwarded that PDU and has called read() again, the <code>DelimiterBasedFrameDecoder</code> doesn't do anything. I can only assume that this is because the read() call judged that it would be "pointless" to call the handlers on the pipeline when no new inbound bytes where read. The thing is... Netty has already read the bytes for the second (and last) <code>UCPPacket</code>, so it has those bytes stored somewhere. Note: when I kill the client process the proxy does process those bytes, which proves what I said: it does have the unhandled bytes, it just doesn't trigger the handlers. I guess that there's a <code>decodeLast</code> or something that gets called when the channel goes inactive.</p>