dirkk
dirkk

Reputation: 6218

Netty 4 - Outbound message at head of pipeline discarded

I am using Netty 4 RC1. I initialize my pipeline at the client side:

public class NodeClientInitializer extends ChannelInitializer<SocketChannel> {

  @Override
  protected void initChannel(SocketChannel sc) throws Exception {
    // Frame encoding and decoding
    sc.pipeline()
      .addLast("logger", new LoggingHandler(LogLevel.DEBUG))

    // Business logic
      .addLast("handler", new NodeClientHandler());
  }
}

NodeClientHandler has the following relevant code:

public class NodeClientHandler extends ChannelInboundByteHandlerAdapter {
  private void sendInitialInformation(ChannelHandlerContext c) {
    c.write(0x05);
  }

  @Override
  public void channelActive(ChannelHandlerContext c) throws Exception {
    sendInitialInformation(c);
  }
}

I connect to the server using:

  public void connect(final InetSocketAddress addr) {
    Bootstrap bootstrap = new Bootstrap();
    ChannelFuture cf = null;
    try {
      // set up the pipeline
      bootstrap.group(new NioEventLoopGroup())
        .channel(NioSocketChannel.class)
        .handler(new NodeClientInitializer());

      // connect
      bootstrap.remoteAddress(addr);
      cf = bootstrap.connect();
      cf.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture op) throws Exception {
          logger.info("Connect to {}", addr.toString());
        }
      });

      cf.channel().closeFuture().syncUninterruptibly();
    } finally {
      bootstrap.shutdown();
    }
  }

So, what I basically want to do is to send some initial information from the client to the server, after the channel is active (i.e. the connect was successful). However, when doing the c.write() I get the following warning and no package is send:

WARNING: Discarded 1 outbound message(s) that reached at the head of the pipeline. Please check your pipeline configuration.

I know there is no outbound handler in my pipeline, but I didn't think I need one (at this point) and I thought Netty would take care to transport the ByteBuffer over to the server. What am I doing wrong here in the pipeline configuration?

Upvotes: 0

Views: 2057

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

Netty only handle messages of type ByteBuf by default if you write to the Channel. So you need to wrap it in a ByteBuf. See also the Unpooled class with its static helpers to create ByteBuf instances.

Upvotes: 1

Related Questions