Search in sources :

Example 11 with GenericFutureListener

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);
}
Also used : Future(io.netty.util.concurrent.Future) SPacketDisconnect(net.minecraft.network.play.server.SPacketDisconnect) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 12 with GenericFutureListener

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);
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) ByteBuf(io.netty.buffer.ByteBuf) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 13 with GenericFutureListener

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());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteBuf(io.netty.buffer.ByteBuf) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.Test)

Example 14 with GenericFutureListener

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);
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with GenericFutureListener

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();
                }
            }
        }
    });
}
Also used : Future(io.netty.util.concurrent.Future) ConnectionState(io.github.tesla.gateway.netty.transmit.ConnectionState) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Aggregations

GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)15 Test (org.junit.Test)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Future (io.netty.util.concurrent.Future)4 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ByteBuf (io.netty.buffer.ByteBuf)2 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Map (java.util.Map)2 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)1 ChannelFutureListenerWithTracingAndMdc (com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc)1 Span (com.nike.wingtips.Span)1 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 ConnectionState (io.github.tesla.gateway.netty.transmit.ConnectionState)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1