下面列出了怎么用io.netty.channel.socket.DuplexChannel的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
void closeChannelInbound(final Channel channel) {
if (!has(state, IN_CLOSED)) {
LOGGER.debug("{} Half-Closing INBOUND (reset)", channel);
setSocketResetOnClose(channel);
((DuplexChannel) channel).shutdownInput().addListener((ChannelFutureListener) this::onHalfClosed);
}
}
@Override
void closeChannelOutbound(final Channel channel) {
if (!has(state, OUT_CLOSED)) {
LOGGER.debug("{} Half-Closing OUTBOUND (reset)", channel);
setSocketResetOnClose(channel);
((DuplexChannel) channel).shutdownOutput().addListener((ChannelFutureListener) this::onHalfClosed);
}
}
private void clientHalfCloseOutbound(final Channel channel) {
assert isClient;
if (!has(state, OUT_CLOSED) && channel instanceof DuplexChannel) {
LOGGER.debug("{} Half-Closing OUTBOUND", channel);
state = unset(state, WRITE);
((DuplexChannel) channel).shutdownOutput().addListener((ChannelFutureListener) this::onHalfClosed);
}
}
private void onHalfClosed(ChannelFuture future) {
DuplexChannel dplxChannel = (DuplexChannel) future.channel();
if (dplxChannel.isInputShutdown() && dplxChannel.isOutputShutdown()) {
LOGGER.debug("{} Fully closing socket channel, both input and output shutdown", dplxChannel);
closeChannel(dplxChannel, null);
}
}
private DuplexChannel connect() {
try {
return connect(bootstrap);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException {
DockerClientConfig dockerClientConfig = getDockerClientConfig();
String path = dockerClientConfig.getDockerHost().getPath();
return (DuplexChannel) bootstrap.connect(new DomainSocketAddress(path)).sync().channel();
}
@Override
public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException {
DockerClientConfig dockerClientConfig = getDockerClientConfig();
String host = dockerClientConfig.getDockerHost().getHost();
int port = dockerClientConfig.getDockerHost().getPort();
if (port == -1) {
throw new RuntimeException("no port configured for " + host);
}
final DuplexChannel channel = (DuplexChannel) bootstrap.connect(host, port).sync().channel();
final SslHandler ssl = initSsl(dockerClientConfig);
if (ssl != null) {
channel.pipeline().addFirst(ssl);
// https://tools.ietf.org/html/rfc5246#section-7.2.1
// TLS has its own special message about connection termination. Because TLS is a
// session-level protocol, it can be covered by any transport-level protocol like
// TCP, UTP and so on. But we know exactly that data being transferred over TCP and
// that other side will never send any byte into this TCP connection, so this
// channel should be closed.
// RFC says that we must notify opposite side about closing. This could be done only
// in sun.security.ssl.SSLEngineImpl and unfortunately it does not send this
// message. On the other hand RFC does not enforce the opposite side to wait for
// such message.
ssl.sslCloseFuture().addListener(future -> channel.eventLoop().execute(channel::close));
}
return channel;
}
@Override
public void close() throws IOException {
((DuplexChannel) context.channel()).shutdownOutput();
}
@Override
public void close() throws IOException {
((DuplexChannel) context.channel()).shutdownOutput();
}
private DuplexChannel getChannel() {
return channelProvider.getChannel();
}
@Override
public DuplexChannel getChannel() {
DuplexChannel channel = connect();
channel.pipeline().addLast(new LoggingHandler(getClass()));
return channel;
}
private DuplexChannel connect(final Bootstrap bootstrap) throws InterruptedException {
return nettyInitializer.connect(bootstrap);
}
DuplexChannel getChannel();
DuplexChannel connect(final Bootstrap bootstrap) throws InterruptedException;