3838import io .vertx .core .internal .net .NetServerInternal ;
3939import io .vertx .core .internal .net .SslChannelProvider ;
4040import io .vertx .core .internal .net .SslHandshakeCompletionHandler ;
41+ import io .vertx .core .internal .net .TcpServerInternal ;
4142import io .vertx .core .internal .resolver .NameResolver ;
4243import io .vertx .core .internal .tls .SslContextManager ;
4344import io .vertx .core .internal .tls .SslContextProvider ;
5960 * @author <a href="http://tfox.org">Tim Fox</a>
6061 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
6162 */
62- public class TcpServerImpl implements NetServerInternal {
63+ public class TcpServerImpl implements TcpServerInternal {
6364
6465 private static final Logger log = LoggerFactory .getLogger (TcpServerImpl .class );
6566
@@ -69,7 +70,7 @@ public class TcpServerImpl implements NetServerInternal {
6970 private final boolean fileRegionEnabled ;
7071 private final boolean registerWriteHandler ;
7172 private final String protocol ;
72- private Handler <NetSocket > handler ;
73+ private Handler <TcpSocket > handler ;
7374 private Handler <Throwable > exceptionHandler ;
7475
7576 // Per server
@@ -114,12 +115,7 @@ public SslContextProvider sslContextProvider() {
114115 }
115116
116117 @ Override
117- public synchronized Handler <NetSocket > connectHandler () {
118- return handler ;
119- }
120-
121- @ Override
122- public synchronized TcpServerImpl connectHandler (Handler <NetSocket > handler ) {
118+ public synchronized TcpServerImpl connectHandler (Handler <TcpSocket > handler ) {
123119 if (isListening ()) {
124120 throw new IllegalStateException ("Cannot set connectHandler when server is listening" );
125121 }
@@ -136,11 +132,6 @@ public synchronized TcpServerImpl exceptionHandler(Handler<Throwable> handler) {
136132 return this ;
137133 }
138134
139- public int actualPort () {
140- TcpServerImpl server = actualServer ;
141- return server != null ? server .actualPort : actualPort ;
142- }
143-
144135 @ Override
145136 public Future <Void > shutdown (Duration timeout ) {
146137 ConnectionGroup group = channelGroup ;
@@ -151,22 +142,22 @@ public Future<Void> shutdown(Duration timeout) {
151142 }
152143
153144 @ Override
154- public Future <NetServer > listen (SocketAddress localAddress ) {
145+ public Future <SocketAddress > listen (SocketAddress localAddress ) {
155146 return listen (vertx .getOrCreateContext (), localAddress );
156147 }
157148
158- public Future <NetServer > listen (ContextInternal context , SocketAddress localAddress ) {
149+ public Future <SocketAddress > listen (ContextInternal context , SocketAddress localAddress ) {
159150 if (localAddress == null ) {
160151 throw new NullPointerException ("No null bind local address" );
161152 }
162153 if (handler == null ) {
163154 throw new IllegalStateException ("Set connect handler first" );
164155 }
165- return bind (context , localAddress ). map ( this ) ;
156+ return bind (context , localAddress );
166157 }
167158
168159 @ Override
169- public Future <NetServer > listen () {
160+ public Future <SocketAddress > listen () {
170161 return listen (config .getPort (), config .getHost ());
171162 }
172163
@@ -177,11 +168,11 @@ public boolean isClosed() {
177168 private class NetSocketInitializer {
178169
179170 private final ContextInternal context ;
180- private final Handler <NetSocket > connectionHandler ;
171+ private final Handler <TcpSocket > connectionHandler ;
181172 private final Handler <Throwable > exceptionHandler ;
182173 private final GlobalTrafficShapingHandler trafficShapingHandler ;
183174
184- NetSocketInitializer (ContextInternal context , Handler <NetSocket > connectionHandler , Handler <Throwable > exceptionHandler , GlobalTrafficShapingHandler trafficShapingHandler ) {
175+ NetSocketInitializer (ContextInternal context , Handler <TcpSocket > connectionHandler , Handler <Throwable > exceptionHandler , GlobalTrafficShapingHandler trafficShapingHandler ) {
185176 this .context = context ;
186177 this .connectionHandler = connectionHandler ;
187178 this .exceptionHandler = exceptionHandler ;
@@ -320,6 +311,19 @@ public int sniEntrySize() {
320311 return sslContextManager .sniEntrySize ();
321312 }
322313
314+ @ Override
315+ public SocketAddress bindAddress () {
316+ Future <Channel > f = bindFuture ;
317+ if (f == null ) {
318+ return null ;
319+ }
320+ Channel ch = f .result ();
321+ if (ch == null ) {
322+ return null ;
323+ }
324+ return vertx .transport ().convert (ch .localAddress ());
325+ }
326+
323327 public Future <Boolean > updateSSLOptions (ServerSSLOptions options , boolean force ) {
324328 TcpServerImpl server = actualServer ;
325329 if (server != null && server != this ) {
@@ -406,16 +410,17 @@ public void updateTrafficShapingOptions(TrafficShapingOptions options, Promise<B
406410 }
407411 }
408412
409- private synchronized Future <Channel > bind (ContextInternal context , SocketAddress localAddress ) {
413+ private synchronized Future <SocketAddress > bind (ContextInternal context , SocketAddress localAddress ) {
410414 if (listening ) {
411415 throw new IllegalStateException ("Listen already called" );
412416 }
413417
414418 this .listening = true ;
415419 this .eventLoop = context .nettyEventLoop ();
416420
421+ PromiseInternal <Channel > promise = context .promise ();
417422 SocketAddress bindAddress ;
418- Map <ServerID , NetServerInternal > sharedNetServers = vertx .sharedTcpServers ();
423+ Map <ServerID , TcpServerInternal > sharedNetServers = ( Map ) vertx .sharedTcpServers ();
419424 synchronized (sharedNetServers ) {
420425 actualPort = localAddress .port ();
421426 String hostOrPath = localAddress .isInetSocket () ? localAddress .host () : localAddress .path ();
@@ -451,7 +456,6 @@ protected void handleShutdown(Duration timeout, Completable<Void> completion) {
451456 }
452457 };
453458 channelGroup = group ;
454- PromiseInternal <Channel > promise = context .promise ();
455459 if (main == null ) {
456460
457461 SslContextManager helper ;
@@ -508,8 +512,6 @@ protected void handleShutdown(Duration timeout, Completable<Void> completion) {
508512 }
509513 listening = false ;
510514 });
511-
512- return bindFuture ;
513515 } else {
514516 // Server already exists with that host/port - we will use that
515517 actualServer = main ;
@@ -523,9 +525,10 @@ protected void handleShutdown(Duration timeout, Completable<Void> completion) {
523525 };
524526 actualServer .channelBalancer .addWorker (eventLoop , worker );
525527 main .bindFuture .onComplete (promise );
526- return promise .future ();
527528 }
528529 }
530+ return promise .future ()
531+ .map (ch -> vertx .transport ().convert (ch .localAddress ()));
529532 }
530533
531534 private void bind (
@@ -535,7 +538,7 @@ private void bind(
535538 SocketAddress localAddress ,
536539 boolean shared ,
537540 Promise <Channel > promise ,
538- Map <ServerID , NetServerInternal > sharedNetServers ,
541+ Map <ServerID , TcpServerInternal > sharedNetServers ,
539542 ServerID id ) {
540543 // Socket bind
541544 channelBalancer .addWorker (eventLoop , worker );
@@ -629,7 +632,7 @@ private void handleShutdown(Completable<Void> completion) {
629632 completion .succeed ();
630633 return ;
631634 }
632- Map <ServerID , NetServerInternal > servers = vertx .sharedTcpServers ();
635+ Map <ServerID , TcpServerInternal > servers = vertx .sharedTcpServers ();
633636 boolean hasHandlers ;
634637 synchronized (servers ) {
635638 ServerChannelLoadBalancer balancer = actualServer .channelBalancer ;
0 commit comments