2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 05:37:43 +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;
interface SocketClosedCallback {
void socketClosed(LanLink brokenLink, boolean linkHasAnotherSocket);
};
interface LinkDisconnectedCallback {
void linkDisconnected(LanLink brokenLink);
}
LinkDisconnectedCallback callback;
@Override
public void disconnect() {
@ -82,7 +84,7 @@ public class LanLink extends BaseLink {
}
//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 = newSocket;
@ -118,8 +120,11 @@ public class LanLink extends BaseLink {
}
} catch (Exception e) {
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);
callback.socketClosed(LanLink.this, thereIsaANewSocket);
if (!thereIsaANewSocket) {
callback.linkDisconnected(LanLink.this);
}
}
}
}).start();
@ -129,7 +134,8 @@ public class LanLink extends BaseLink {
public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException {
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.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_NEW_PORT_SUPPORT = 7;
@ -82,12 +82,10 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.SocketC
private ArrayList<InetAddress> reverseConnectionBlackList = new ArrayList<>();
@Override // SocketClosedCallback
public void socketClosed(final LanLink brokenLink, boolean linkHasAnotherSocket) {
if (!linkHasAnotherSocket) {
String deviceId = brokenLink.getDeviceId();
visibleComputers.remove(deviceId);
connectionLost(brokenLink);
}
public void linkDisconnected(LanLink brokenLink) {
String deviceId = brokenLink.getDeviceId();
visibleComputers.remove(deviceId);
connectionLost(brokenLink);
}
//They received my UDP broadcast and are connecting to me. The first thing they sned should be their identity.
@ -253,9 +251,10 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.SocketC
if (currentLink != null) {
//Update old link
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());
} else {
Log.i("KDE/LanLinkProvider", "Creating a new link for device " + deviceId);
//Let's create the link
LanLink link = new LanLink(context, deviceId, this, socket, connectionOrigin);
visibleComputers.put(deviceId, link);