use of io.netty.util.concurrent.GenericFutureListener in project MinecraftForge by MinecraftForge.
the class NetworkDispatcher method kickWithMessage.
private void kickWithMessage(String message) {
FMLLog.log(Level.ERROR, "Network Disconnect: %s", message);
final TextComponentString TextComponentString = new TextComponentString(message);
if (side == Side.CLIENT) {
manager.closeChannel(TextComponentString);
} else {
manager.sendPacket(new SPacketDisconnect(TextComponentString), new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> result) {
manager.closeChannel(TextComponentString);
}
}, (GenericFutureListener<? extends Future<? super Void>>[]) null);
}
manager.channel().config().setAutoRead(false);
}
use of io.netty.util.concurrent.GenericFutureListener in project ratpack by ratpack.
the class DefaultResponseTransmitter method transmitter.
@Override
public Subscriber<ByteBuf> transmitter(HttpResponseStatus responseStatus) {
return new Subscriber<ByteBuf>() {
private Subscription subscription;
private final AtomicBoolean done = new AtomicBoolean();
private final ChannelFutureListener cancelOnFailure = future -> {
if (!future.isSuccess()) {
cancel();
}
};
private final GenericFutureListener<Future<? super Void>> cancelOnCloseListener = c -> cancel();
private void cancel() {
channel.closeFuture().removeListener(cancelOnCloseListener);
if (done.compareAndSet(false, true)) {
subscription.cancel();
post(responseStatus);
}
}
@Override
public void onSubscribe(Subscription subscription) {
if (subscription == null) {
throw new NullPointerException("'subscription' is null");
}
if (this.subscription != null) {
subscription.cancel();
return;
}
this.subscription = subscription;
onWritabilityChanged = () -> {
if (channel.isWritable() && !done.get()) {
this.subscription.request(1);
}
};
channel.closeFuture().addListener(cancelOnCloseListener);
ChannelFuture channelFuture = pre(responseStatus);
if (channelFuture == null) {
subscription.cancel();
notifyListeners(responseStatus, channel.close());
} else {
channelFuture.addListener(cancelOnFailure);
if (channel.isWritable()) {
this.subscription.request(1);
}
}
}
@Override
public void onNext(ByteBuf o) {
o.touch();
if (channel.isOpen()) {
channel.writeAndFlush(new DefaultHttpContent(o)).addListener(cancelOnFailure);
if (channel.isWritable()) {
subscription.request(1);
}
} else {
o.release();
cancel();
}
}
@Override
public void onError(Throwable t) {
if (t == null) {
throw new NullPointerException("error is null");
}
LOGGER.warn("Exception thrown transmitting stream", t);
if (done.compareAndSet(false, true)) {
channel.closeFuture().removeListener(cancelOnCloseListener);
post(responseStatus);
}
}
@Override
public void onComplete() {
if (done.compareAndSet(false, true)) {
channel.closeFuture().removeListener(cancelOnCloseListener);
post(responseStatus);
}
}
};
}
use of io.netty.util.concurrent.GenericFutureListener in project netty by netty.
the class Http2ConnectionHandlerTest method canSendGoAwayFrame.
@SuppressWarnings("unchecked")
@Test
public void canSendGoAwayFrame() throws Exception {
ByteBuf data = dummyData();
long errorCode = Http2Error.INTERNAL_ERROR.code();
when(future.isDone()).thenReturn(true);
when(future.isSuccess()).thenReturn(true);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((GenericFutureListener) invocation.getArgument(0)).operationComplete(future);
return null;
}
}).when(future).addListener(any(GenericFutureListener.class));
handler = newHandler();
handler.goAway(ctx, STREAM_ID, errorCode, data, promise);
verify(connection).goAwaySent(eq(STREAM_ID), eq(errorCode), eq(data));
verify(frameWriter).writeGoAway(eq(ctx), eq(STREAM_ID), eq(errorCode), eq(data), eq(promise));
verify(ctx).close();
assertEquals(0, data.refCnt());
}
use of io.netty.util.concurrent.GenericFutureListener in project intellij-community by JetBrains.
the class ChannelRegistrar method close.
private void close(boolean shutdownEventLoopGroup) {
ServerChannel serverChannel = this.serverChannel.get();
if (serverChannel == null) {
LOG.assertTrue(clientChannels.isEmpty());
return;
} else if (!this.serverChannel.compareAndSet(serverChannel, null)) {
return;
}
EventLoopGroup eventLoopGroup = shutdownEventLoopGroup ? serverChannel.eventLoop().parent() : null;
try {
long start = System.currentTimeMillis();
Channel[] clientChannels = this.clientChannels.toArray(new Channel[] {});
this.clientChannels.clear();
final CountDownLatch countDown = new CountDownLatch(clientChannels.length + 1);
GenericFutureListener<ChannelFuture> listener = new GenericFutureListener<ChannelFuture>() {
@Override
public void operationComplete(@NotNull ChannelFuture future) throws Exception {
try {
Throwable cause = future.cause();
if (cause != null) {
LOG.warn(cause);
}
} finally {
countDown.countDown();
}
}
};
serverChannel.close().addListener(listener);
for (Channel channel : clientChannels) {
channel.close().addListener(listener);
}
try {
countDown.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.warn("Cannot close all channels for 10 seconds, channels: " + Arrays.toString(clientChannels));
}
long duration = System.currentTimeMillis() - start;
if (duration > 1000) {
LOG.info("Close all channels took " + duration + " ms: " + (duration / 60000) + " min " + ((duration % 60000) / 1000) + "sec");
}
} finally {
if (eventLoopGroup != null) {
eventLoopGroup.shutdownGracefully(1, 2, TimeUnit.NANOSECONDS);
}
}
}
use of io.netty.util.concurrent.GenericFutureListener in project tesla by linking12.
the class ConnectionFlow method fail.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void fail(final Throwable cause) {
final ConnectionState lastStateBeforeFailure = serverConnection.getCurrentState();
serverConnection.disconnect().addListener(new GenericFutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
synchronized (connectLock) {
if (!clientConnection.serverConnectionFailed(serverConnection, lastStateBeforeFailure, cause)) {
serverConnection.become(ConnectionState.DISCONNECTED);
notifyThreadsWaitingForConnection();
}
}
}
});
}
Aggregations