public static ByteBuf safeBuffer(ByteBufHolder holder, ByteBufAllocator allocator) { return safeBuffer(holder.content(), allocator); }
private void connected(ContextInternal context, Channel ch, Handler<AsyncResult<NetSocket>> connectHandler, SocketAddress remoteAddress) { initChannel(ch.pipeline()); VertxHandler<NetSocketImpl> handler = VertxHandler.create(context, ctx -> new NetSocketImpl(vertx, ctx, remoteAddress, context, sslHelper, metrics)); handler.addHandler(sock -> { socketMap.put(ch, sock); context.executeFromIO(v -> { if (metrics != null) { sock.metric(metrics.connected(sock.remoteAddress(), sock.remoteName())); } sock.registerEventBusHandler(); connectHandler.handle(Future.succeededFuture(sock)); }); }); handler.removeHandler(conn -> { socketMap.remove(ch); }); ch.pipeline().addLast("handler", handler); }
public static <C extends ConnectionBase> VertxHandler<C> create(ContextInternal context, Function<ChannelHandlerContext, C> connectionFactory) { return new VertxHandler<>(context, connectionFactory); }
private void connected(HandlerHolder<Handlers> handler, Channel ch) { NetServerImpl.this.initChannel(ch.pipeline()); VertxHandler<NetSocketImpl> nh = VertxHandler.<NetSocketImpl>create(handler.context, ctx -> new NetSocketImpl(vertx, ctx, handler.context, sslHelper, metrics)); nh.addHandler(conn -> socketMap.put(ch, conn)); nh.removeHandler(conn -> socketMap.remove(ch)); ch.pipeline().addLast("handler", nh); NetSocketImpl sock = nh.getConnection(); handler.context.executeFromIO(v -> { if (metrics != null) { sock.metric(metrics.connected(sock.remoteAddress(), sock.remoteName())); } sock.registerEventBusHandler(); handler.handler.connectionHandler.handle(sock); }); }
public static <C extends ConnectionBase> VertxHandler<C> create(C connection) { return create(connection.context, ctx -> connection); }
@Override public void exceptionCaught(ChannelHandlerContext chctx, final Throwable t) throws Exception { Channel ch = chctx.channel(); // Don't remove the connection at this point, or the handleClosed won't be called when channelInactive is called! C connection = getConnection(); if (connection != null) { context.executeFromIO(v -> { try { if (ch.isOpen()) { ch.close(); } } catch (Throwable ignore) { } connection.handleException(t); }); } else { ch.close(); } }
/** * Fail the connection, the {@code error} will be sent to the pipeline and the connection will * stop processing any further message. * * @param error the {@code Throwable} to propagate */ public void fail(Throwable error) { handler().fail(error); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { setConnection(connectionFactory.apply(ctx)); }
private void connected(HandlerHolder<Handlers> handler, Channel ch) { NetServerImpl.this.initChannel(ch.pipeline()); VertxHandler<NetSocketImpl> nh = VertxHandler.<NetSocketImpl>create(handler.context, ctx -> new NetSocketImpl(vertx, ctx, handler.context, sslHelper, metrics)); nh.addHandler(conn -> socketMap.put(ch, conn)); nh.removeHandler(conn -> socketMap.remove(ch)); ch.pipeline().addLast("handler", nh); NetSocketImpl sock = nh.getConnection(); handler.context.executeFromIO(v -> { if (metrics != null) { sock.metric(metrics.connected(sock.remoteAddress(), sock.remoteName())); } sock.registerEventBusHandler(); handler.handler.connectionHandler.handle(sock); }); }
private void init() { channel.pipeline().addLast("handler", VertxHandler.create(context, this::createConnection)); }
@Override public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { C conn = getConnection(); context.executeFromIO(v -> conn.handleInterestedOpsChanged()); }
/** * Fail the connection, the {@code error} will be sent to the pipeline and the connection will * stop processing any further message. * * @param error the {@code Throwable} to propagate */ public void fail(Throwable error) { handler().fail(error); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { setConnection(connectionFactory.apply(ctx)); }
@Override public void handle(Object event) { if (event instanceof ByteBuf) { ByteBuf byteBuf = (ByteBuf) event; byteBuf = VertxHandler.safeBuffer(byteBuf, allocator); Buffer data = Buffer.buffer(byteBuf); reportBytesRead(data.length()); dataHandler.handle(data); } } }
Future<ConnectResult<HttpClientConnection>> future) { boolean upgrade = version == HttpVersion.HTTP_2 && options.isHttp2ClearTextUpgrade(); VertxHandler<Http1xClientConnection> clientHandler = VertxHandler.create(context, chctx -> { Http1xClientConnection conn = new Http1xClientConnection(listener, upgrade ? HttpVersion.HTTP_1_1 : version, client, endpointMetric, chctx, ssl, host, port, context, metrics); if (metrics != null) { clientHandler.addHandler(conn -> { if (upgrade) { future.complete(new ConnectResult<>(new Http2UpgradedClientConnection(client, conn), 1, http2Weight)); clientHandler.removeHandler(conn -> { listener.onEvict(); });
chctx.pipeline().replace("handler", "handler", VertxHandler.create(socket));
@Override public void exceptionCaught(ChannelHandlerContext chctx, final Throwable t) throws Exception { Channel ch = chctx.channel(); // Don't remove the connection at this point, or the handleClosed won't be called when channelInactive is called! C connection = getConnection(); if (connection != null) { context.executeFromIO(v -> { try { if (ch.isOpen()) { ch.close(); } } catch (Throwable ignore) { } connection.handleException(t); }); } else { ch.close(); } }
public static <C extends ConnectionBase> VertxHandler<C> create(ContextInternal context, Function<ChannelHandlerContext, C> connectionFactory) { return new VertxHandler<>(context, connectionFactory); }
WebSocketFrameInternal decodeFrame(WebSocketFrame msg) { ByteBuf payload = safeBuffer(msg, chctx.alloc()); boolean isFinal = msg.isFinalFragment(); FrameType frameType; if (msg instanceof BinaryWebSocketFrame) { frameType = FrameType.BINARY; } else if (msg instanceof CloseWebSocketFrame) { frameType = FrameType.CLOSE; } else if (msg instanceof PingWebSocketFrame) { frameType = FrameType.PING; } else if (msg instanceof PongWebSocketFrame) { frameType = FrameType.PONG; } else if (msg instanceof TextWebSocketFrame) { frameType = FrameType.TEXT; } else if (msg instanceof ContinuationWebSocketFrame) { frameType = FrameType.CONTINUATION; } else { throw new IllegalStateException("Unsupported websocket msg " + msg); } return new WebSocketFrameImpl(frameType, payload, isFinal); }
VertxHandler<Http1xServerConnection> handler = VertxHandler.create(holder2.context, chctx -> { Http1xServerConnection conn = new Http1xServerConnection(holder2.context.owner(), sslHelper, handler.addHandler(conn -> { connectionMap.put(pipeline.channel(), conn); }); handler.removeHandler(conn -> { connectionMap.remove(pipeline.channel()); });