From 3d4bf643d4dad4220a90d020772d20942aed3358 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sun, 25 Mar 2018 20:32:25 +0200 Subject: [PATCH] Synchronize everything to see if this fixes a crash in 8.1 --- .../Backends/LanBackend/LanLinkProvider.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 42d3a030..dc3533e6 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -61,6 +61,9 @@ import javax.net.ssl.SSLSocket; public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDisconnectedCallback { + public static final Object mutex = new Object(); + + public static final int MIN_VERSION_WITH_SSL_SUPPORT = 6; public static final int MIN_VERSION_WITH_NEW_PORT_SUPPORT = 7; @@ -95,7 +98,10 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis NetworkPacket networkPacket; try { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); - String message = reader.readLine(); + String message; + synchronized (mutex) { + message = reader.readLine(); + } networkPacket = NetworkPacket.unserialize(message); //Log.e("TcpListener","Received TCP package: "+networkPacket.serialize()); } catch (Exception e) { @@ -143,9 +149,11 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis int tcpPort = identityPacket.getInt("tcpPort", MIN_PORT); SocketFactory socketFactory = SocketFactory.getDefault(); - Socket socket = socketFactory.createSocket(address, tcpPort); - configureSocket(socket); - + Socket socket; + synchronized (mutex) { + socket = socketFactory.createSocket(address, tcpPort); + configureSocket(socket); + } OutputStream out = socket.getOutputStream(); NetworkPacket myIdentity = NetworkPacket.createIdentityPacket(context); out.write(myIdentity.serialize().getBytes()); @@ -244,7 +252,9 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis @Override public void run() { try { - sslsocket.startHandshake(); + synchronized (mutex) { + sslsocket.startHandshake(); + } } catch (Exception e) { Log.e("KDE/LanLinkProvider","Handshake failed with " + identityPacket.getString("deviceName")); e.printStackTrace(); @@ -265,7 +275,7 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis } - private synchronized void addLink(final NetworkPacket identityPacket, Socket socket, LanLink.ConnectionStarted connectionOrigin) throws IOException { + private void addLink(final NetworkPacket identityPacket, Socket socket, LanLink.ConnectionStarted connectionOrigin) throws IOException { String deviceId = identityPacket.getString("deviceId"); LanLink currentLink = visibleComputers.get(deviceId); @@ -328,8 +338,11 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis public void run() { while (listening) { try { - Socket socket = tcpServer.accept(); - configureSocket(socket); + Socket socket; + synchronized (mutex) { + socket = tcpServer.accept(); + configureSocket(socket); + } tcpPacketReceived(socket); } catch (Exception e) { e.printStackTrace();