Skip to content

Commit 1942cc3

Browse files
author
harry
committed
修复当连接关闭后,minicap和minitouch没有正常退出的问题
1 parent 7128f35 commit 1942cc3

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

src/com/yeetor/minicap/Minicap.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class Minicap {
5454

5555
private Banner banner;
5656

57+
private Socket minicapSocket;
58+
5759

5860
public static void installMinicap(IDevice device) throws MinicapInstallException {
5961
if (device == null) {
@@ -245,6 +247,38 @@ public void reStart(final float scale, final int rotate) {
245247
start(scale, rotate);
246248
}
247249

250+
public void kill() {
251+
running = false;
252+
if (minicapThread != null) {
253+
minicapThread.stop();
254+
}
255+
256+
// 关闭socket
257+
if (minicapSocket != null && minicapSocket.isConnected()) {
258+
try {
259+
minicapSocket.close();
260+
} catch (IOException e) {
261+
}
262+
minicapSocket = null;
263+
}
264+
265+
if (dataReaderThread != null) {
266+
try {
267+
dataReaderThread.join();
268+
} catch (InterruptedException e) {
269+
e.printStackTrace();
270+
}
271+
}
272+
273+
if (imageParserThread != null) {
274+
try {
275+
imageParserThread.join();
276+
} catch (InterruptedException e) {
277+
e.printStackTrace();
278+
}
279+
}
280+
}
281+
248282
/**
249283
* 启动线程开启 minicap
250284
* @param shellCommand
@@ -286,22 +320,22 @@ public void run() {
286320
int tryTime = 20;
287321
while (true) {
288322
// 连接minicap启动的服务
289-
Socket socket = new Socket(host, port);
290-
InputStream inputStream = socket.getInputStream();
323+
minicapSocket = new Socket(host, port);
324+
InputStream inputStream = minicapSocket.getInputStream();
291325
bytes = new byte[4096];
292326
int n = inputStream.read(bytes);
293327

294328
if (n == -1) {
295329
Thread.sleep(10);
296-
socket.close();
330+
minicapSocket.close();
297331
} else {
298332
// bytes内包含有信息,需要给Dataparser处理
299333
dataQueue.add(Arrays.copyOfRange(bytes, 0, n));
300334
running = true;
301335
onStartup(true);
302336

303337
// 启动 DataReader ImageParser
304-
dataReaderThread = startDataReaderThread(socket);
338+
dataReaderThread = startDataReaderThread(minicapSocket);
305339
imageParserThread = startImageParserThread();
306340
break;
307341
}

src/com/yeetor/minitouch/Minitouch.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public void start() {
9898
minitouchInitialThread = startInitialThread("127.0.0.1", forward.getPort());
9999
}
100100

101+
public void kill() {
102+
if (minitouchThread != null) {
103+
minitouchThread.stop();
104+
}
105+
// 关闭socket
106+
if (minitouchSocket != null && minitouchSocket.isConnected()) {
107+
try {
108+
minitouchSocket.close();
109+
} catch (IOException e) {
110+
}
111+
minitouchSocket = null;
112+
}
113+
}
114+
101115
public void sendEvent(String str) {
102116
if (minitouchOutputStream == null) {
103117
return;

src/com/yeetor/p2p/Protocol.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,18 @@ public Minitouch getMinitouch() {
7575
public LocalClient getLocalClient() {
7676
return localClient;
7777
}
78+
79+
public void close() {
80+
81+
if (minicap != null) {
82+
minicap.kill();
83+
minicap = null;
84+
}
85+
86+
if (minitouch != null) {
87+
minitouch.kill();
88+
minitouch = null;
89+
}
90+
91+
}
7892
}

src/com/yeetor/p2p/WSServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ public void onDisconnect(ChannelHandlerContext ctx) {
6767
for (Protocol protocol : protocolList) {
6868
if (protocol.getBroswerSocket() != null && protocol.getBroswerSocket() == ctx) {
6969
protocol.broswerDisconnect();
70+
protocol.close();
7071
protocolList.remove(protocol);
7172
break;
7273
}
7374

7475
if (protocol.getClientSocket() != null && protocol.getClientSocket() == ctx) {
7576
protocol.clientDisconnect();
77+
protocol.close();
7678
protocolList.remove(protocol);
7779
break;
7880
}

0 commit comments

Comments
 (0)