|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.rsocket.transport.netty; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.netty.channel.Channel; |
| 22 | +import io.netty.channel.ChannelFutureListener; |
| 23 | +import io.netty.incubator.codec.http3.DefaultHttp3DataFrame; |
| 24 | +import io.netty.incubator.codec.http3.Http3DataFrame; |
| 25 | +import io.netty.incubator.codec.http3.Http3HeadersFrame; |
| 26 | +import io.netty.incubator.codec.quic.QuicChannel; |
| 27 | +import io.netty.incubator.codec.quic.QuicStreamChannel; |
| 28 | +import io.rsocket.DuplexConnection; |
| 29 | +import io.rsocket.RSocketErrorException; |
| 30 | +import io.rsocket.frame.ErrorFrameCodec; |
| 31 | +import io.rsocket.internal.BaseDuplexConnection; |
| 32 | +import io.rsocket.internal.UnboundedProcessor; |
| 33 | +import java.nio.channels.ClosedChannelException; |
| 34 | +import java.net.SocketAddress; |
| 35 | +import java.util.Objects; |
| 36 | +import reactor.core.publisher.Flux; |
| 37 | +import reactor.core.publisher.Mono; |
| 38 | + |
| 39 | +/** An implementation of {@link DuplexConnection} that connects via HTTP/3. */ |
| 40 | +public final class Http3DuplexConnection extends BaseDuplexConnection { |
| 41 | + private final String side; |
| 42 | + private final Channel connection; |
| 43 | + private final UnboundedProcessor inbound; |
| 44 | + |
| 45 | + public Http3DuplexConnection(Channel connection) { |
| 46 | + this("unknown", connection); |
| 47 | + } |
| 48 | + |
| 49 | + public Http3DuplexConnection(String side, Channel connection) { |
| 50 | + this.connection = Objects.requireNonNull(connection, "connection must not be null"); |
| 51 | + this.side = side; |
| 52 | + this.inbound = new UnboundedProcessor(); |
| 53 | + |
| 54 | + Flux.from(sender) |
| 55 | + .concatMap( |
| 56 | + frame -> |
| 57 | + Mono.<Void>create( |
| 58 | + sink -> |
| 59 | + this.connection |
| 60 | + .writeAndFlush(new DefaultHttp3DataFrame(frame)) |
| 61 | + .addListener( |
| 62 | + future -> { |
| 63 | + if (future.isSuccess()) { |
| 64 | + sink.success(); |
| 65 | + } else { |
| 66 | + sink.error(future.cause()); |
| 67 | + } |
| 68 | + })), |
| 69 | + 1) |
| 70 | + .doOnError( |
| 71 | + throwable -> { |
| 72 | + inbound.onError(throwable); |
| 73 | + onClose.tryEmitError(throwable); |
| 74 | + this.connection.close(); |
| 75 | + }) |
| 76 | + .doFinally( |
| 77 | + __ -> { |
| 78 | + if (this.connection instanceof QuicStreamChannel) { |
| 79 | + ((QuicStreamChannel) this.connection) |
| 80 | + .shutdownOutput() |
| 81 | + .addListener(ChannelFutureListener.CLOSE); |
| 82 | + } else { |
| 83 | + this.connection.close(); |
| 84 | + } |
| 85 | + }) |
| 86 | + .subscribe(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public ByteBufAllocator alloc() { |
| 91 | + return connection.alloc(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public SocketAddress remoteAddress() { |
| 96 | + if (connection.parent() instanceof QuicChannel) { |
| 97 | + return ((QuicChannel) connection.parent()).remoteSocketAddress(); |
| 98 | + } |
| 99 | + return connection.remoteAddress(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + protected void doOnClose() { |
| 104 | + connection.close(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Mono<Void> onClose() { |
| 109 | + return super.onClose(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Flux<ByteBuf> receive() { |
| 114 | + return inbound; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void sendErrorAndClose(RSocketErrorException e) { |
| 119 | + final ByteBuf errorFrame = ErrorFrameCodec.encode(alloc(), 0, e); |
| 120 | + sender.tryEmitFinal(errorFrame); |
| 121 | + } |
| 122 | + |
| 123 | + public void handleHeaders(Http3HeadersFrame frame) { |
| 124 | + } |
| 125 | + |
| 126 | + public void handleData(Http3DataFrame frame) { |
| 127 | + ByteBuf byteBuf = frame.content().retain(); |
| 128 | + frame.release(); |
| 129 | + inbound.onNext(byteBuf); |
| 130 | + } |
| 131 | + |
| 132 | + public void handleError(Throwable cause) { |
| 133 | + inbound.onError(cause); |
| 134 | + onClose.tryEmitError(cause); |
| 135 | + } |
| 136 | + |
| 137 | + public void handleInputClosed() { |
| 138 | + inbound.onError(new ClosedChannelException()); |
| 139 | + onClose.tryEmitEmpty(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public String toString() { |
| 144 | + return "Http3DuplexConnection{" + "side='" + side + '\'' + ", connection=" + connection + '}'; |
| 145 | + } |
| 146 | +} |
0 commit comments