Netty FileRegion transfer fails

I am developing a Netty HTTP server that should just send a local file as quickly and efficiently as possible over HTTP.

Right now, transmission stops early, with error messages that look like this:

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90831821 bytes remaining to read

$ curl http://192.168.56.101:32324/dl/%2Fstorage%2Femulated%2F0%2FMovies%2Fstartrek-tlr3-german_h720p.mov > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (18) transfer closed with 90833209 bytes remaining to read

      

For this purpose, I have included Netty as follows:

build.gradle

// Netty & Friends.
compile group: 'io.netty', name: 'netty-all', version: '4.1.0.Beta4'
compile group: 'tv.cntt', name: 'jauter', version: '1.7'
compile 'javax.activation:activation:1.1.1'
compile 'org.javassist:javassist:3.19.0-GA'

      

MediaLinkServerInitializer.java

final class MediaLinkServerInitializer extends ChannelInitializer<SocketChannel> {

    private final Handler handler;

    public MediaLinkServerInitializer(Context context) {
        final Router router = new Router()
                .GET("/dl/:path", StaticFileHandler.class)
                .HEAD("/dl/:path", StaticFileHandler.class);

        handler = new Handler(router);
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(handler.name(), handler);
    }
}

      

This project uses the netty-router extension to connect HTTP paths to handler classes.

Bootstrap call:

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
      .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
      .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
      .channel(NioServerSocketChannel.class)
      .handler(new LoggingHandler(LogLevel.INFO))
      .childHandler(new MediaLinkServerInitializer(context));

channel = b.bind(host, port).sync().channel();

log.log(Level.CONFIG, "Started instance on {0}:{1,number,#}", new Object[]{host, port});

channel.closeFuture().addListener(new ChannelFutureListener() {
  @Override
  public void operationComplete(ChannelFuture future) throws Exception {
      log.finest("Channel closed.");
      channel = null;
  }
});

channel.closeFuture().sync();

      

StaticFileHandler.java

public class StaticFileHandler extends SimpleChannelInboundHandler<Routed> {
    @Override
    public void channelRead0(final ChannelHandlerContext ctx, final Routed request) throws Exception {
        File file = ... // get from URL, check etc.
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException ignore) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        setKeepAlive(response, isKeepAlive(request.request()));

        // Write the initial line and the header.
        ctx.write(response);

        ChannelProgressiveFuture future = (ChannelProgressiveFuture) ctx.writeAndFlush(
            new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());

        future.addListener(new ChannelProgressiveFutureListener() {
            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {

            }

            @Override
            public void operationComplete(ChannelProgressiveFuture future) throws Exception {
                log.log(Level.FINEST, "Transfer complete: {0}", future.channel());
            }
        });
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
}

      

Accordingly, presentation FileRegion can only be used if the platform supports sending a null copy via the 'sendfile' syscall. I'm sure my target platform is (Android 4.4, Kernel 3.4+).

Once I use io.netty.handler.stream.ChunkedWriteHandler

like this, then the transfer will work:

ChannelProgressiveFuture future = (ChannelProgressiveFuture) ctx.writeAndFlush(
    new ChunkedNioFile(raf.getChannel(), 0, fileLength, 1024 * 96), ctx.newProgressivePromise());

future.addListener(new ChannelProgressiveFutureListener() {
    @Override
    public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {

    }

    @Override
    public void operationComplete(ChannelProgressiveFuture future) throws Exception {
        log.log(Level.FINEST, "Transfer complete: {0}", future.channel());
    }
});

      

What could be the problem for early failed transfers?

+3


source to share





All Articles