Try to bind IPv4 and IPv6 sockets whenever connectivity changes.

This commit is contained in:
akwizgran
2020-08-14 10:44:33 +01:00
parent 0e2d905486
commit e06eee2358
2 changed files with 9 additions and 3 deletions

View File

@@ -277,11 +277,11 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
// make outgoing connections on API 21+ if another network
// has internet access
socketFactory = SocketFactory.getDefault();
if (s == INACTIVE) bind();
bind();
} else {
LOG.info("Connected to wifi");
socketFactory = getSocketFactory();
if (s == INACTIVE) bind();
bind();
}
});
}

View File

@@ -149,15 +149,21 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener {
protected void bind() {
bindExecutor.execute(() -> {
if (getState() != INACTIVE) return;
State s = getState();
if (s != ACTIVE && s != INACTIVE) return;
bind(true);
bind(false);
});
}
private void bind(boolean ipv4) {
ServerSocket old = state.getServerSocket(ipv4);
ServerSocket ss = null;
for (InetSocketAddress addr : getLocalSocketAddresses(ipv4)) {
if (old != null && addr.equals(old.getLocalSocketAddress())) {
LOG.info("Server socket already bound");
return;
}
try {
ss = new ServerSocket();
ss.bind(addr);