use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture 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();
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project java-in-action by xinghalo.
the class ChatServer method start.
public ChannelFuture start(InetSocketAddress address) {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group).channel(NioServerSocketChannel.class).childHandler(createInitializer(channelGroup));
ChannelFuture future = bootstrap.bind(address);
future.syncUninterruptibly();
channel = future.channel();
return future;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project java-in-action by xinghalo.
the class EchoClient method start.
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port)).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project java-in-action by xinghalo.
the class EchoServer method start.
public void start() throws Exception {
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(serverHandler);
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project java-in-action by xinghalo.
the class TimeClient method connect.
public void connect(int port, String host) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeClientHandler());
}
});
ChannelFuture f = b.connect(host, port);
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
Aggregations