1313import io .netty .bootstrap .Bootstrap ;
1414import io .netty .channel .Channel ;
1515import io .netty .channel .ChannelFuture ;
16+ import io .netty .channel .ChannelFutureListener ;
1617import io .netty .channel .ChannelHandler ;
1718import io .netty .handler .codec .quic .BoringSSLKeylog ;
1819import io .netty .handler .codec .quic .FlushStrategy ;
1920import io .netty .handler .codec .quic .QuicCodecBuilder ;
2021import io .netty .util .internal .PlatformDependent ;
22+ import io .vertx .core .Closeable ;
2123import io .vertx .core .Completable ;
2224import io .vertx .core .Future ;
2325import io .vertx .core .internal .ContextInternal ;
4042import java .net .InetSocketAddress ;
4143import java .time .Duration ;
4244import java .util .EnumMap ;
45+ import java .util .Objects ;
4346import java .util .concurrent .TimeUnit ;
4447
4548/**
4649 * @author <a href="mailto:[email protected] ">Julien Viet</a> 4750 */
48- public abstract class QuicEndpointImpl implements QuicEndpointInternal , MetricsProvider {
51+ public abstract class QuicEndpointImpl implements QuicEndpointInternal , MetricsProvider , Closeable {
4952
5053 private static final EnumMap <QuicCongestionControlAlgorithm , io .netty .handler .codec .quic .QuicCongestionControlAlgorithm > CC_MAP = new EnumMap <>(QuicCongestionControlAlgorithm .class );
5154
@@ -62,6 +65,7 @@ public abstract class QuicEndpointImpl implements QuicEndpointInternal, MetricsP
6265 private Channel channel ;
6366 protected ConnectionGroup connectionGroup ;
6467 private FlushStrategy flushStrategy ;
68+ private ContextInternal context ;
6569
6670 public QuicEndpointImpl (VertxInternal vertx , QuicEndpointOptions options ) {
6771
@@ -95,7 +99,7 @@ public QuicEndpointImpl(VertxInternal vertx, QuicEndpointOptions options) {
9599 }
96100
97101 this .options = options ;
98- this .vertx = vertx ;
102+ this .vertx = Objects . requireNonNull ( vertx ) ;
99103 this .manager = new SslContextManager (new SSLEngineOptions () {
100104 @ Override
101105 public SSLEngineOptions copy () {
@@ -183,15 +187,28 @@ protected void handleBind(Channel channel, QuicEndpointMetrics<?, ?> metrics) {
183187 protected void handleClose (Completable <Void > completion ) {
184188 PromiseInternal <Void > promise = (PromiseInternal <Void >) completion ;
185189 Channel ch = channel ;
186- ch .close ().addListener (promise );
190+ ch .close ().addListener ((ChannelFutureListener ) future -> {
191+ ContextInternal ctx ;
192+ synchronized (QuicEndpointImpl .this ) {
193+ ctx = context ;
194+ context = null ;
195+ }
196+ ctx .removeCloseHook (QuicEndpointImpl .this );
197+ }).addListener (promise );
187198 }
188199 };
189200 }
190201
191202 @ Override
192203 public Future <Integer > bind (SocketAddress address ) {
193- ContextInternal context = vertx .getOrCreateContext ();
194- Future <SslContextProvider > f1 = manager .resolveSslContextProvider (options .getSslOptions (), context );
204+ ContextInternal current = vertx .getOrCreateContext ();
205+ synchronized (this ) {
206+ if (context != null ) {
207+ return current .failedFuture ("Already bound" );
208+ }
209+ context = current ;
210+ }
211+ Future <SslContextProvider > f1 = manager .resolveSslContextProvider (options .getSslOptions (), current );
195212 return f1 .compose (sslContextProvider -> {
196213 VertxMetrics metricsFactory = vertx .metrics ();
197214 QuicEndpointMetrics <?, ?> metrics ;
@@ -200,9 +217,10 @@ public Future<Integer> bind(SocketAddress address) {
200217 } else {
201218 metrics = null ;
202219 }
203- return bind (context , address , metrics )
220+ return bind (current , address , metrics )
204221 .map (ch -> {
205222 handleBind (ch , metrics );
223+ context .addCloseHook (this );
206224 return ((InetSocketAddress )ch .localAddress ()).getPort ();
207225 });
208226 });
@@ -228,4 +246,9 @@ public QuicEndpointInternal flushStrategy(FlushStrategy flushStrategy) {
228246 this .flushStrategy = flushStrategy ;
229247 return this ;
230248 }
249+
250+ @ Override
251+ public void close (Completable <Void > completion ) {
252+ close ().onComplete (completion );
253+ }
231254}
0 commit comments