2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

We were never reusing the existing link

This commit is contained in:
Albert Vaca
2016-06-21 13:37:49 +02:00
parent 4fc6ca8d4f
commit 968d018f41
2 changed files with 19 additions and 14 deletions

View File

@@ -59,9 +59,11 @@ public class LanLink extends BaseLink {
private Socket socket = null; private Socket socket = null;
interface SocketClosedCallback { interface LinkDisconnectedCallback {
void socketClosed(LanLink brokenLink, boolean linkHasAnotherSocket); void linkDisconnected(LanLink brokenLink);
}; }
LinkDisconnectedCallback callback;
@Override @Override
public void disconnect() { public void disconnect() {
@@ -82,7 +84,7 @@ public class LanLink extends BaseLink {
} }
//Returns the old socket //Returns the old socket
public Socket reset(final Socket newSocket, ConnectionStarted connectionSource, final SocketClosedCallback callback) throws IOException { public Socket reset(final Socket newSocket, ConnectionStarted connectionSource) throws IOException {
Socket oldSocket = socket; Socket oldSocket = socket;
socket = newSocket; socket = newSocket;
@@ -118,8 +120,11 @@ public class LanLink extends BaseLink {
} }
} catch (Exception e) { } catch (Exception e) {
Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage()); Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage());
try { Thread.sleep(300); } catch (InterruptedException ignored) {} // Wait a bit because we might receive a new socket meanwhile
boolean thereIsaANewSocket = (newSocket != socket); boolean thereIsaANewSocket = (newSocket != socket);
callback.socketClosed(LanLink.this, thereIsaANewSocket); if (!thereIsaANewSocket) {
callback.linkDisconnected(LanLink.this);
}
} }
} }
}).start(); }).start();
@@ -129,7 +134,8 @@ public class LanLink extends BaseLink {
public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException { public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException {
super(context, deviceId, linkProvider); super(context, deviceId, linkProvider);
reset(socket, connectionSource, linkProvider); callback = linkProvider;
reset(socket, connectionSource);
} }

View File

@@ -58,7 +58,7 @@ import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener; import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
public class LanLinkProvider extends BaseLinkProvider implements LanLink.SocketClosedCallback { public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDisconnectedCallback {
public static final int MIN_VERSION_WITH_SSL_SUPPORT = 6; public static final int MIN_VERSION_WITH_SSL_SUPPORT = 6;
public static final int MIN_VERSION_WITH_NEW_PORT_SUPPORT = 7; public static final int MIN_VERSION_WITH_NEW_PORT_SUPPORT = 7;
@@ -82,13 +82,11 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.SocketC
private ArrayList<InetAddress> reverseConnectionBlackList = new ArrayList<>(); private ArrayList<InetAddress> reverseConnectionBlackList = new ArrayList<>();
@Override // SocketClosedCallback @Override // SocketClosedCallback
public void socketClosed(final LanLink brokenLink, boolean linkHasAnotherSocket) { public void linkDisconnected(LanLink brokenLink) {
if (!linkHasAnotherSocket) {
String deviceId = brokenLink.getDeviceId(); String deviceId = brokenLink.getDeviceId();
visibleComputers.remove(deviceId); visibleComputers.remove(deviceId);
connectionLost(brokenLink); connectionLost(brokenLink);
} }
}
//They received my UDP broadcast and are connecting to me. The first thing they sned should be their identity. //They received my UDP broadcast and are connecting to me. The first thing they sned should be their identity.
public void tcpPackageReceived(Socket socket) throws Exception { public void tcpPackageReceived(Socket socket) throws Exception {
@@ -253,9 +251,10 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.SocketC
if (currentLink != null) { if (currentLink != null) {
//Update old link //Update old link
Log.i("KDE/LanLinkProvider", "Reusing same link for device " + deviceId); Log.i("KDE/LanLinkProvider", "Reusing same link for device " + deviceId);
final Socket oldSocket = currentLink.reset(socket, connectionOrigin, this); final Socket oldSocket = currentLink.reset(socket, connectionOrigin);
//Log.e("KDE/LanLinkProvider", "Replacing socket. old: "+ oldSocket.hashCode() + " - new: "+ socket.hashCode()); //Log.e("KDE/LanLinkProvider", "Replacing socket. old: "+ oldSocket.hashCode() + " - new: "+ socket.hashCode());
} else { } else {
Log.i("KDE/LanLinkProvider", "Creating a new link for device " + deviceId);
//Let's create the link //Let's create the link
LanLink link = new LanLink(context, deviceId, this, socket, connectionOrigin); LanLink link = new LanLink(context, deviceId, this, socket, connectionOrigin);
visibleComputers.put(deviceId, link); visibleComputers.put(deviceId, link);