Search in sources :

Example 6 with ChannelException

use of io.netty.channel.ChannelException in project pulsar by yahoo.

the class ConnectionPool method createConnection.

private CompletableFuture<ClientCnx> createConnection(InetSocketAddress address, int connectionKey) {
    if (log.isDebugEnabled()) {
        log.debug("Connection for {} not found in cache", address);
    }
    final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();
    // Trigger async connect to broker
    bootstrap.connect(address).addListener((ChannelFuture future) -> {
        if (!future.isSuccess()) {
            cnxFuture.completeExceptionally(new PulsarClientException(future.cause()));
            cleanupConnection(address, connectionKey, cnxFuture);
            return;
        }
        log.info("[{}] Connected to server", future.channel());
        future.channel().closeFuture().addListener(v -> {
            if (log.isDebugEnabled()) {
                log.debug("Removing closed connection from pool: {}", v);
            }
            cleanupConnection(address, connectionKey, cnxFuture);
        });
        // We are connected to broker, but need to wait until the connect/connected handshake is
        // complete
        final ClientCnx cnx = (ClientCnx) future.channel().pipeline().get("handler");
        if (!future.channel().isActive() || cnx == null) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection was already closed by the time we got notified", future.channel());
            }
            cnxFuture.completeExceptionally(new ChannelException("Connection already closed"));
            return;
        }
        cnx.connectionFuture().thenRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection handshake completed", cnx.channel());
            }
            cnxFuture.complete(cnx);
        }).exceptionally(exception -> {
            log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
            cnxFuture.completeExceptionally(exception);
            cleanupConnection(address, connectionKey, cnxFuture);
            cnx.ctx().close();
            return null;
        });
    });
    return cnxFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ChannelException(io.netty.channel.ChannelException)

Example 7 with ChannelException

use of io.netty.channel.ChannelException in project netty by netty.

the class NioEventLoop method openSelector.

private Selector openSelector() {
    try {
        unwrappedSelector = provider.openSelector();
    } catch (IOException e) {
        throw new ChannelException("failed to open a new selector", e);
    }
    if (DISABLE_KEYSET_OPTIMIZATION) {
        return unwrappedSelector;
    }
    final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
    Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                return Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoader());
            } catch (Throwable cause) {
                return cause;
            }
        }
    });
    if (!(maybeSelectorImplClass instanceof Class) || // ensure the current selector implementation is what we can instrument.
    !((Class<?>) maybeSelectorImplClass).isAssignableFrom(unwrappedSelector.getClass())) {
        if (maybeSelectorImplClass instanceof Throwable) {
            Throwable t = (Throwable) maybeSelectorImplClass;
            logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, t);
        }
        return unwrappedSelector;
    }
    final Class<?> selectorImplClass = (Class<?>) maybeSelectorImplClass;
    Object maybeException = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
                Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField);
                if (cause != null) {
                    return cause;
                }
                cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField);
                if (cause != null) {
                    return cause;
                }
                selectedKeysField.set(unwrappedSelector, selectedKeySet);
                publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
                return null;
            } catch (NoSuchFieldException e) {
                return e;
            } catch (IllegalAccessException e) {
                return e;
            }
        }
    });
    if (maybeException instanceof Exception) {
        selectedKeys = null;
        Exception e = (Exception) maybeException;
        logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, e);
        return unwrappedSelector;
    }
    selectedKeys = selectedKeySet;
    logger.trace("instrumented a special java.util.Set into: {}", unwrappedSelector);
    return new SelectedSelectionKeySetSelector(unwrappedSelector, selectedKeySet);
}
Also used : IOException(java.io.IOException) CancelledKeyException(java.nio.channels.CancelledKeyException) EventLoopException(io.netty.channel.EventLoopException) IOException(java.io.IOException) ChannelException(io.netty.channel.ChannelException) Field(java.lang.reflect.Field) ChannelException(io.netty.channel.ChannelException)

Example 8 with ChannelException

use of io.netty.channel.ChannelException in project Glowstone by GlowstoneMC.

the class GlowChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel c) {
    MessageHandler handler = new MessageHandler(connectionManager);
    CodecsHandler codecs = new CodecsHandler(ProtocolType.HANDSHAKE.getProtocol());
    FramingHandler framing = new FramingHandler();
    try {
        c.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
        // Not supported on all OSs, like Windows XP and lesser
        GlowServer.logger.warning("Your OS does not support type of service.");
    }
    c.config().setAllocator(PooledByteBufAllocator.DEFAULT);
    c.pipeline().addLast("idle_timeout", new IdleStateHandler(READ_IDLE_TIMEOUT, WRITE_IDLE_TIMEOUT, 0)).addLast("legacy_ping", new LegacyPingHandler(connectionManager)).addLast("encryption", NoopHandler.INSTANCE).addLast("framing", framing).addLast("compression", NoopHandler.INSTANCE).addLast("codecs", codecs).addLast("handler", handler);
}
Also used : LegacyPingHandler(net.glowstone.net.handler.legacyping.LegacyPingHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelException(io.netty.channel.ChannelException)

Aggregations

ChannelException (io.netty.channel.ChannelException)8 IOException (java.io.IOException)4 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 EventLoopException (io.netty.channel.EventLoopException)1 UdtMessage (io.netty.channel.udt.UdtMessage)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 Field (java.lang.reflect.Field)1 CancelledKeyException (java.nio.channels.CancelledKeyException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LegacyPingHandler (net.glowstone.net.handler.legacyping.LegacyPingHandler)1