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);
}
}
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);
}
}
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);
}
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());
});
}
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);
}
}
Aggregations