Skip to content

Commit

Permalink
Announce bound instead of configured server port
Browse files Browse the repository at this point in the history
If a port number of 0 is chosen then bind() will open a random port in
an ephemeral range instead of the provided port number
and this port can then be retrieved using getLocalPort().

This enables starting servers on arbitrary ports instead of
preconfigured ones.

Since only direct connections need to know the port number
connections using the registry or LAN can use an ephemeral port number
and the connection information tab can display the port direct
connections should use.

This requires a router with functioning UPnP instead of port forwarding
though so is not univerally useful.
  • Loading branch information
fishface60 committed Dec 27, 2024
1 parent 15cc9f3 commit 0177503
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public SocketServer(int port) {
@Override
public void start() throws IOException {
var serverSocket = new ServerSocket(port);
if (serverSocket.getLocalPort() == -1) {
throw new AssertionError("Socket not bound yet");
}
// If the above throws, it will be as though we never started.

socket = serverSocket;
Expand Down Expand Up @@ -70,6 +73,16 @@ public String getError() {
return null;
}

/** Get the port of the socket the server is running on or -1. */
public int getPort() {
// NOTE: We do not use this.port because the socket's bound port can be different
if (socket == null || socket.isClosed()) {
return -1;
}

return socket.getLocalPort();
}

////
// Threads
private static class ListeningThread extends Thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void onLoginError() {
});
}

@Nonnull
public Server createServer(@Nullable ServerConfig config) {
if (config == null) {
return new NilServer();
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/net/rptools/maptool/server/MapToolServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import net.rptools.clientserver.simple.DisconnectHandler;
import net.rptools.clientserver.simple.MessageHandler;
import net.rptools.clientserver.simple.connection.Connection;
import net.rptools.clientserver.simple.server.NilServer;
import net.rptools.clientserver.simple.server.Router;
import net.rptools.clientserver.simple.server.Server;
import net.rptools.clientserver.simple.server.ServerObserver;
import net.rptools.clientserver.simple.server.SocketServer;
import net.rptools.clientserver.simple.server.WebRTCServer;
import net.rptools.maptool.client.AppConstants;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.MapToolRegistry;
Expand Down Expand Up @@ -69,7 +72,7 @@ public enum State {
}

@Nonnull private final String serviceIdentifier;
private final Server server;
@Nonnull private final Server server;
private final MessageHandler messageHandler;
private final Router router;
private final ServerConfig config;
Expand Down Expand Up @@ -199,7 +202,11 @@ public String getName() {
}

public int getPort() {
return config == null ? -1 : config.getPort();
return switch (server) {
case NilServer s -> -1;
case SocketServer s -> s.getPort();
case WebRTCServer s -> -1;
};
}

private void connectionAdded(Connection conn) {
Expand Down Expand Up @@ -350,8 +357,8 @@ public void stop() {
}

// Close UPnP port mapping if used
if (useUPnP && config != null) {
int port = config.getPort();
int port;
if (useUPnP && (port = getPort()) != -1) {
UPnPUtil.closePort(port);
}

Expand Down Expand Up @@ -388,12 +395,13 @@ public void start() throws IOException {
}

// Use UPnP to open port in router
if (useUPnP && config != null) {
int port = getPort();
if (useUPnP && port != -1) {
MapTool.getFrame()
.showFilledGlassPane(
new StaticMessageDialog(I18N.getText("msg.info.server.upnp.discovering")));
try {
UPnPUtil.openPort(config.getPort());
UPnPUtil.openPort(port);
} finally {
MapTool.getFrame().hideGlassPane();
}
Expand All @@ -404,11 +412,11 @@ public void start() throws IOException {
try {
MapToolRegistry.RegisterResponse result =
MapToolRegistry.getInstance()
.registerInstance(config.getServerName(), config.getPort(), config.getUseWebRTC());
.registerInstance(config.getServerName(), port, config.getUseWebRTC());
if (result == MapToolRegistry.RegisterResponse.NAME_EXISTS) {
MapTool.showError("msg.error.alreadyRegistered");
} else {
heartbeatThread = new HeartbeatThread(config.getPort());
heartbeatThread = new HeartbeatThread(port);
heartbeatThread.start();
}
// TODO: I don't like this
Expand All @@ -417,8 +425,8 @@ public void start() throws IOException {
}
}

if (serviceIdentifier != null && config != null) {
announcer = new ServiceAnnouncer(serviceIdentifier, config.getPort(), AppConstants.SERVICE_GROUP);
if (serviceIdentifier != null && port != -1) {
announcer = new ServiceAnnouncer(serviceIdentifier, port, AppConstants.SERVICE_GROUP);
announcer.start();
}

Expand Down

0 comments on commit 0177503

Please sign in to comment.