Java Code Examples for reactor.netty.Connection#from()

The following examples show how to use reactor.netty.Connection#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelOperationsHandler.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Override
final public void channelInactive(ChannelHandlerContext ctx) {
	try {
		Connection connection = Connection.from(ctx.channel());
		ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
		if (ops != null) {
			ops.onInboundClose();
		}
		else {
			listener.onStateChange(connection, ConnectionObserver.State.DISCONNECTING);
		}
	}
	catch (Throwable err) {
		exceptionCaught(ctx, err);
	}
}
 
Example 2
Source File: HttpServerTests.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
private void doTestStatus(HttpResponseStatus status) {
	EmbeddedChannel channel = new EmbeddedChannel();
	HttpServerOperations ops = new HttpServerOperations(
			Connection.from(channel),
			ConnectionObserver.emptyListener(),
			null,
			new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"),
			null,
			ServerCookieEncoder.STRICT,
			ServerCookieDecoder.STRICT);
	ops.status(status);
	HttpMessage response = ops.newFullBodyMessage(Unpooled.EMPTY_BUFFER);
	assertThat(((FullHttpResponse) response).status().reasonPhrase()).isEqualTo(status.reasonPhrase());
	// "FutureReturnValueIgnored" is suppressed deliberately
	channel.close();
}
 
Example 3
Source File: ChannelOperationsHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void channelActive(ChannelHandlerContext ctx) {
	// When AbstractNioChannel.AbstractNioUnsafe.finishConnect/fulfillConnectPromise,
	// fireChannelActive will be triggered regardless that the channel might be closed in the meantime
	if (ctx.channel().isActive()) {
		Connection c = Connection.from(ctx.channel());
		listener.onStateChange(c, ConnectionObserver.State.CONNECTED);
		ChannelOperations<?, ?> ops = opsFactory.create(c, listener, null);
		if (ops != null) {
			ops.bind();
			listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
		}
	}
}
 
Example 4
Source File: ChannelOperationsHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
final public void exceptionCaught(ChannelHandlerContext ctx, Throwable err) {
	Connection connection = Connection.from(ctx.channel());
	ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
	if (ops != null) {
		ops.onInboundError(err);
	}
	else {
		listener.onUncaughtException(connection, err);
	}
}
 
Example 5
Source File: Http2StreamBridgeServerHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
	if (secured == null) {
		secured = ctx.channel().parent().pipeline().get(SslHandler.class) != null;
	}
	if (remoteAddress == null) {
		remoteAddress =
				Optional.ofNullable(HAProxyMessageReader.resolveRemoteAddressFromProxyProtocol(ctx.channel().parent()))
				        .orElse(ctx.channel().parent().remoteAddress());
	}
	if (msg instanceof HttpRequest) {
		HttpRequest request = (HttpRequest) msg;
		HttpServerOperations ops;
		try {
			ops = new HttpServerOperations(Connection.from(ctx.channel()),
					listener,
					null,
					request,
					ConnectionInfo.from(ctx.channel().parent(),
					                    readForwardHeaders,
					                    request,
					                    secured,
					                    remoteAddress),
					cookieEncoder,
					cookieDecoder);
		}
		catch (RuntimeException e) {
			HttpServerOperations.sendDecodingFailures(ctx, e, msg);
			return;
		}
		ops.bind();
		listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
	}
	ctx.fireChannelRead(msg);
}
 
Example 6
Source File: FluxReceiveTest.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1016() throws Exception {
	EmbeddedChannel channel = new EmbeddedChannel();

	Connection connection = Connection.from(channel);
	ConnectionObserver observer = (conn, newState) -> {
		if (newState == ConnectionObserver.State.DISCONNECTING) {
			if (conn.channel().isActive() && !conn.isPersistent()) {
				conn.dispose();
			}
		}
	};
	ChannelOperations<?, ?> ops = new ChannelOperations<>(connection, observer);
	ops.bind();

	ByteBuf buffer = channel.alloc().buffer();
	buffer.writeCharSequence("testIssue1016", Charset.defaultCharset());
	ops.inbound.onInboundNext(buffer);

	CountDownLatch latch = new CountDownLatch(1);
	// There is a subscriber, but there is no request for an item
	ops.receive().subscribe(new TestSubscriber(latch));

	ops.onInboundError(new OutOfMemoryError());

	assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();

	assertThat(buffer.refCnt()).isEqualTo(0);
}