Question

In Netty, does anyone know if the ChannelBuffer passed to a handler for an upstream event (after you cast it from ChannelEvent / or MessageEvent) will be concurrently written to by the Netty framework while you process it?

The user guide and examples do not explicitly clarify whether this will happen or not.

For example, imagine the method below in a class that extends SimpleChannelHandler and is bound as the handler on a netty server:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
  ChannelBuffer buffer = (ChannelBuffer) e.getMessage();

  if (buffer.readableBytes() == 4)
  {
    // Is it possible for the number of readable bytes to increase here?
  }
}

In the above method, is it possible for the number of readable bytes to increase on the commented line (assuming the client is still sending data), or will those frames be guaranteed to be separate messages and not written into the supplied buffer?

The reason I ask is to know whether or not I should copy the buffer to a local one before doing some of my processing, or whether such a copy is superfluous.

Was it helpful?

Solution

Once Netty creates a ChannelBuffer and triggers a MessageEvent with it, it's out of Netty's hands and thus Netty does not modify it by any means. The handlers or other user code can modify the buffer, but usually, well written handlers do not interfere with other handlers. If a bad handler keeps the reference to the received buffer and modifies later, then you will see broken buffer state though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top