Search in sources :

Example 1 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class VertxHandler method channelWritabilityChanged.

@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    C conn = getConnection();
    if (conn != null) {
        ContextImpl context = getContext(conn);
        context.executeFromIO(conn::handleInterestedOpsChanged);
    }
}
Also used : ContextImpl(io.vertx.core.impl.ContextImpl)

Example 2 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class VertxHandler method channelReadComplete.

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    C conn = getConnection();
    if (conn != null) {
        ContextImpl context = getContext(conn);
        context.executeFromIO(conn::endReadAndFlush);
    }
}
Also used : ContextImpl(io.vertx.core.impl.ContextImpl)

Example 3 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class NetClientImpl method connect.

private void connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler, int remainingAttempts) {
    Objects.requireNonNull(host, "No null host accepted");
    Objects.requireNonNull(connectHandler, "No null connectHandler accepted");
    ContextImpl context = vertx.getOrCreateContext();
    sslHelper.validate(vertx);
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(context.nettyEventLoop());
    bootstrap.channel(NioSocketChannel.class);
    applyConnectionOptions(bootstrap);
    ChannelProvider channelProvider;
    if (options.getProxyOptions() == null) {
        channelProvider = ChannelProvider.INSTANCE;
    } else {
        channelProvider = ProxyChannelProvider.INSTANCE;
    }
    Handler<Channel> channelInitializer = ch -> {
        if (sslHelper.isSSL()) {
            SslHandler sslHandler = sslHelper.createSslHandler(vertx, host, port);
            ch.pipeline().addLast("ssl", sslHandler);
        }
        ChannelPipeline pipeline = ch.pipeline();
        if (logEnabled) {
            pipeline.addLast("logging", new LoggingHandler());
        }
        if (sslHelper.isSSL()) {
            pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
        }
        if (options.getIdleTimeout() > 0) {
            pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout()));
        }
        pipeline.addLast("handler", new VertxNetHandler<NetSocketImpl>(ch, socketMap) {

            @Override
            protected void handleMsgReceived(Object msg) {
                ByteBuf buf = (ByteBuf) msg;
                conn.handleDataReceived(Buffer.buffer(buf));
            }
        });
    };
    Handler<AsyncResult<Channel>> channelHandler = res -> {
        if (res.succeeded()) {
            Channel ch = res.result();
            if (sslHelper.isSSL()) {
                SslHandler sslHandler = (SslHandler) ch.pipeline().get("ssl");
                io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture();
                fut.addListener(future2 -> {
                    if (future2.isSuccess()) {
                        connected(context, ch, connectHandler, host, port);
                    } else {
                        failed(context, ch, future2.cause(), connectHandler);
                    }
                });
            } else {
                connected(context, ch, connectHandler, host, port);
            }
        } else {
            if (remainingAttempts > 0 || remainingAttempts == -1) {
                context.executeFromIO(() -> {
                    log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds");
                    vertx.setTimer(options.getReconnectInterval(), tid -> connect(port, host, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1));
                });
            } else {
                failed(context, null, res.cause(), connectHandler);
            }
        }
    };
    channelProvider.connect(vertx, bootstrap, options.getProxyOptions(), host, port, channelInitializer, channelHandler);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelOption(io.netty.channel.ChannelOption) LoggingHandler(io.netty.handler.logging.LoggingHandler) ContextImpl(io.vertx.core.impl.ContextImpl) TCPMetrics(io.vertx.core.spi.metrics.TCPMetrics) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) LoggerFactory(io.vertx.core.logging.LoggerFactory) ByteBuf(io.netty.buffer.ByteBuf) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.logging.Logger) NetClient(io.vertx.core.net.NetClient) Metrics(io.vertx.core.spi.metrics.Metrics) Closeable(io.vertx.core.Closeable) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricsProvider(io.vertx.core.spi.metrics.MetricsProvider) ChannelPipeline(io.netty.channel.ChannelPipeline) Future(io.vertx.core.Future) NetClientOptions(io.vertx.core.net.NetClientOptions) Objects(java.util.Objects) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Buffer(io.vertx.core.buffer.Buffer) SslHandler(io.netty.handler.ssl.SslHandler) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ContextImpl(io.vertx.core.impl.ContextImpl) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult)

Example 4 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class NetServerBase method actualClose.

private void actualClose(ContextImpl closeContext, Handler<AsyncResult<Void>> done) {
    if (id != null) {
        vertx.sharedNetServers().remove(id);
    }
    ContextImpl currCon = vertx.getContext();
    for (C sock : socketMap.values()) {
        sock.close();
    }
    // Sanity check
    if (vertx.getContext() != currCon) {
        throw new IllegalStateException("Context was changed");
    }
    ChannelGroupFuture fut = serverChannelGroup.close();
    fut.addListener(cg -> {
        if (metrics != null) {
            metrics.close();
        }
        executeCloseDone(closeContext, done, fut.cause());
    });
}
Also used : ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture) ContextImpl(io.vertx.core.impl.ContextImpl)

Example 5 with ContextImpl

use of io.vertx.core.impl.ContextImpl in project vert.x by eclipse.

the class NetServerBase method close.

@Override
public synchronized void close(Handler<AsyncResult<Void>> done) {
    ContextImpl context = vertx.getOrCreateContext();
    if (!listening) {
        if (done != null) {
            executeCloseDone(context, done, null);
        }
        return;
    }
    listening = false;
    synchronized (vertx.sharedNetServers()) {
        if (actualServer != null) {
            actualServer.handlerManager.removeHandler(registeredHandler, listenContext);
            if (actualServer.handlerManager.hasHandlers()) {
                // The actual server still has handlers so we don't actually close it
                if (done != null) {
                    executeCloseDone(context, done, null);
                }
            } else {
                // No Handlers left so close the actual server
                // The done handler needs to be executed on the context that calls close, NOT the context
                // of the actual server
                actualServer.actualClose(context, done);
            }
        }
    }
    if (creatingContext != null) {
        creatingContext.removeCloseHook(this);
    }
}
Also used : ContextImpl(io.vertx.core.impl.ContextImpl)

Aggregations

ContextImpl (io.vertx.core.impl.ContextImpl)13 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ChannelGroupFuture (io.netty.channel.group.ChannelGroupFuture)2 AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 VertxInternal (io.vertx.core.impl.VertxInternal)2 Logger (io.vertx.core.logging.Logger)2 LoggerFactory (io.vertx.core.logging.LoggerFactory)2 IOException (java.io.IOException)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Unpooled (io.netty.buffer.Unpooled)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelOption (io.netty.channel.ChannelOption)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1