Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 79 | public Socket reset(final Socket newSocket, ConnectionStarted connectionSource) throws IOException { | |||
---|---|---|---|---|---|
84 | this.connectionSource = connectionSource; | 84 | this.connectionSource = connectionSource; | ||
85 | 85 | | |||
86 | if (oldSocket != null) { | 86 | if (oldSocket != null) { | ||
87 | oldSocket.close(); //This should cancel the readThread | 87 | oldSocket.close(); //This should cancel the readThread | ||
88 | } | 88 | } | ||
89 | 89 | | |||
90 | //Log.e("LanLink", "Start listening"); | 90 | //Log.e("LanLink", "Start listening"); | ||
91 | //Create a thread to take care of incoming data for the new socket | 91 | //Create a thread to take care of incoming data for the new socket | ||
92 | new Thread(new Runnable() { | 92 | new Thread(() -> { | ||
93 | @Override | | |||
94 | public void run() { | | |||
95 | try { | 93 | try { | ||
96 | BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8)); | 94 | BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8)); | ||
97 | while (true) { | 95 | while (true) { | ||
98 | String packet; | 96 | String packet; | ||
99 | try { | 97 | try { | ||
100 | packet = reader.readLine(); | 98 | packet = reader.readLine(); | ||
101 | } catch (SocketTimeoutException e) { | 99 | } catch (SocketTimeoutException e) { | ||
102 | continue; | 100 | continue; | ||
Show All 10 Lines | |||||
113 | } catch (Exception e) { | 111 | } catch (Exception e) { | ||
114 | Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage()); | 112 | Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage()); | ||
115 | try { Thread.sleep(300); } catch (InterruptedException ignored) {} // Wait a bit because we might receive a new socket meanwhile | 113 | try { Thread.sleep(300); } catch (InterruptedException ignored) {} // Wait a bit because we might receive a new socket meanwhile | ||
116 | boolean thereIsaANewSocket = (newSocket != socket); | 114 | boolean thereIsaANewSocket = (newSocket != socket); | ||
117 | if (!thereIsaANewSocket) { | 115 | if (!thereIsaANewSocket) { | ||
118 | callback.linkDisconnected(LanLink.this); | 116 | callback.linkDisconnected(LanLink.this); | ||
119 | } | 117 | } | ||
120 | } | 118 | } | ||
121 | } | | |||
122 | }).start(); | 119 | }).start(); | ||
123 | 120 | | |||
124 | return oldSocket; | 121 | return oldSocket; | ||
125 | } | 122 | } | ||
126 | 123 | | |||
127 | public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException { | 124 | public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket socket, ConnectionStarted connectionSource) throws IOException { | ||
128 | super(context, deviceId, linkProvider); | 125 | super(context, deviceId, linkProvider); | ||
129 | callback = linkProvider; | 126 | callback = linkProvider; | ||
▲ Show 20 Lines • Show All 172 Lines • Show Last 20 Lines |