use of lee.study.down.handle.HttpDownInitializer in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startChunkDown.
protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
// 重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
setChannel(chunkInfo, future.channel());
}
synchronized (requestInfo) {
LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
if (httpDownInfo.getTaskInfo().isSupportRange()) {
requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
} else {
requestInfo.headers().remove(HttpHeaderNames.RANGE);
}
future.channel().writeAndFlush(httpDownInfo.getRequest());
}
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
future.channel().writeAndFlush(content);
}
} else {
LOGGER.debug("下载连接失败:" + chunkInfo);
chunkInfo.setStatus(HttpDownStatus.FAIL);
future.channel().close();
}
});
}
Aggregations