title: Why is it necessary to call ctx.writeAndFlush when using the MessageToByteEncoder
body: <p>I am implementing as SIP server on top of Netty and everything was working fine using Netty 3.x. However I decided to upgrade to Netty 4 because of the well defined threading model but things have changed a lot and I am lost. My initial question (I got most of it working, but it doesn't quite make sense to me just yet) is regarding the <code>MessageToByteEncoder</code> and why I have to call <code>ctx.writeAndFlush</code> myself.</p>
  
  <p>The bootstrap:</p>
  
  <pre><code>private void createTCPListeningPoint() {
      this.serverBootstrap = new ServerBootstrap();
  
      this.serverBootstrap.group(this.bossGroup, this.workerGroup)
              .channel(NioServerSocketChannel.class)
              .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {
                  @Override
                  public void initChannel(final SocketChannel ch) throws Exception {
                      final ChannelPipeline pipeline = ch.pipeline();
                      pipeline.addLast("decoder", new SipFrameDecoder(Protocol.TCP));
                      pipeline.addLast("encoder", new SipMessageEncoder());
                      pipeline.addLast("handler", NettyNetworkStack.this.sipHandler);
                  }
              });
      // .option(ChannelOption.SO_BACKLOG, 128)
      // .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
      // .childOption(ChannelOption.SO_KEEPALIVE, true)
      // .childOption(ChannelOption.TCP_NODELAY, true);
  }
  </code></pre>
  
  <p>(the bind operation takes place later and is not shown here)</p>
  
  <p>Since the incoming pipeline is working (the decoder and handler is being called as they should) the only interesting part is the <code>SipMessageEncoder</code>, which is super simple and the code for it is found below. Also note that I have been playing around with the various options but as suspected, I haven't found any combination that changes this behavior.</p>
  
  <p>The encoder:</p>
  
  <pre><code>public final class SipMessageEncoder extends MessageToByteEncoder&lt;SipMessage&gt; {
  
  @Override
  protected void encode(final ChannelHandlerContext ctx, final SipMessage msg, final ByteBuf out) throws Exception {
      final Buffer b = msg.toBuffer();
      for (int i = 0; i &lt; b.getReadableBytes(); ++i) {
          out.writeByte(b.getByte(i));
      }
      out.writeByte(SipParser.CR);
      out.writeByte(SipParser.LF);
      // ctx.writeAndFlush(out);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The <code>SipMessage</code> and <code>SipParser</code> etc are taken from my other opensource project pkts.io but is not really relevant to this question. Just know that the <code>SipMessage.toBuffer()</code> essentially just spits a raw <code>byte[]</code> which I then am transferring to the <code>ByteBuff</code> as passed in by Netty.</p>
  
  <p>The above code does not work unless I do <code>ctx.writeAndFlush(out)</code>. However, I thought, as in Netty 3, that the purpose of this slightly higher-level encoder is to shield me from these details. Step debugging through the code the <code>ByteBuf</code> is written to the <code>ctx</code> but not flushed, which I guess is the reason for it not showing up on the socket. So, my questions are simply:</p>
  
  <ul>
  <li>Why do I have to call <code>ctx.writeAndFlush</code> myself when I am using the <code>SipMessageEncoder</code>. The <code>NumberEncoder</code> in the Factorial example doesn't do this and looking through the HTTP codecs I couldn't see that it did that either so obviously I am missing something obvious. </li>
  <li>Also, the <code>ByteBuf</code> is being written to the context but is never flushed so when will it be? I tried to push a lot of traffic through a single TCP connection (using sipp) to see if that would eventually force a flush but nope, nothing shows up ever.</li>
  </ul>
  
  <p>Btw, I also tried to use the <code>MessageToMessageEncoder</code> and allocate the ByteBuf through the context and add it to the <code>List</code> but the same result.</p>
  
  <p>Btw2 – I am using Netty 4.0.10.Final</p>
  
  <p>Thanks,</p>
  
  <p>/Jonas</p>