mirror of
https://github.com/revanced/jadb.git
synced 2025-02-19 05:26:47 +01:00
Merge pull request #32 from vidstige/race-condition-in-socketserver
Race condition in socketserver
This commit is contained in:
commit
a01ce1bb17
@ -10,6 +10,8 @@ public abstract class SocketServer implements Runnable {
|
|||||||
private final int port;
|
private final int port;
|
||||||
private ServerSocket socket;
|
private ServerSocket socket;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
private boolean isStarted = false;
|
||||||
private final Object lockObject = new Object();
|
private final Object lockObject = new Object();
|
||||||
|
|
||||||
protected SocketServer(int port) {
|
protected SocketServer(int port) {
|
||||||
@ -20,9 +22,7 @@ public abstract class SocketServer implements Runnable {
|
|||||||
thread = new Thread(this, "Fake Adb Server");
|
thread = new Thread(this, "Fake Adb Server");
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
thread.start();
|
thread.start();
|
||||||
synchronized (lockObject) {
|
serverReady();
|
||||||
lockObject.wait();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
@ -35,9 +35,7 @@ public abstract class SocketServer implements Runnable {
|
|||||||
socket = new ServerSocket(port);
|
socket = new ServerSocket(port);
|
||||||
socket.setReuseAddress(true);
|
socket.setReuseAddress(true);
|
||||||
|
|
||||||
synchronized (lockObject) {
|
waitForServer();
|
||||||
lockObject.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Socket c = socket.accept();
|
Socket c = socket.accept();
|
||||||
@ -49,6 +47,21 @@ public abstract class SocketServer implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void serverReady() throws InterruptedException {
|
||||||
|
synchronized (lockObject) {
|
||||||
|
if (!isStarted) {
|
||||||
|
lockObject.wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForServer() {
|
||||||
|
synchronized (lockObject) {
|
||||||
|
lockObject.notify();
|
||||||
|
isStarted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract Runnable createResponder(Socket socket);
|
protected abstract Runnable createResponder(Socket socket);
|
||||||
|
|
||||||
public void stop() throws IOException, InterruptedException {
|
public void stop() throws IOException, InterruptedException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user