Пример #1
0
  @Override
  public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
      UnsafeByteArrayOutputStream output = new UnsafeByteArrayOutputStream(1024); // 不需要关闭

      if (!(context.getMessage() instanceof Response)) {
        downstreamCodec.encode(channel, output, context.getMessage());
      } else {
        upstreamCodec.encode(channel, output, context.getMessage());
      }

      GrizzlyChannel.removeChannelIfDisconnectd(connection);
      byte[] bytes = output.toByteArray();
      Buffer buffer = connection.getTransport().getMemoryManager().allocate(bytes.length);
      buffer.put(bytes);
      buffer.flip();
      buffer.allowBufferDispose(true);
      context.setMessage(buffer);
    } finally {
      GrizzlyChannel.removeChannelIfDisconnectd(connection);
    }
    return context.getInvokeAction();
  }
 @Override
 protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
   UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(1024);
   com.antrou.dubbo.remoting.netty4.NettyChannel channel =
       com.antrou.dubbo.remoting.netty4.NettyChannel.getOrAddChannel(
           ctx.channel(), url, handler);
   try {
     codec.encode(channel, os, msg);
   } finally {
     com.antrou.dubbo.remoting.netty4.NettyChannel.removeChannelIfDisconnected(ctx.channel());
   }
   out.writeBytes(os.toByteBuffer());
 }