reconnect without loosing commands on broken idle connection (#16299)

Signed-off-by: Christian Wicke <github@c.fg9.eu>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Christian Wicke 2024-01-20 14:28:52 +01:00 committed by Ciprian Pascu
parent d49804aeab
commit 57dd845eab

View File

@ -54,6 +54,7 @@ public class MPDConnectionThread extends Thread {
private final List<MPDCommand> pendingCommands = new ArrayList<>();
private AtomicBoolean isInIdle = new AtomicBoolean(false);
private AtomicBoolean wakingUpFromIdle = new AtomicBoolean(false);
private AtomicBoolean disposed = new AtomicBoolean(false);
public MPDConnectionThread(MPDResponseListener listener, String address, Integer port, String password) {
@ -70,7 +71,6 @@ public class MPDConnectionThread extends Thread {
while (!disposed.get()) {
try {
synchronized (pendingCommands) {
pendingCommands.clear();
pendingCommands.add(new MPDCommand("status"));
pendingCommands.add(new MPDCommand("currentsong"));
}
@ -92,7 +92,16 @@ public class MPDConnectionThread extends Thread {
closeSocket();
if (!disposed.get()) {
sleep(RECONNECTION_TIMEOUT_SEC * 1000);
if (wakingUpFromIdle.compareAndSet(true, false)) {
logger.debug("reconnecting immediately and keeping pending commands");
} else {
logger.debug("reconnecting in {} seconds and clearing pending commands...",
RECONNECTION_TIMEOUT_SEC);
sleep(RECONNECTION_TIMEOUT_SEC * 1000);
synchronized (pendingCommands) {
pendingCommands.clear();
}
}
}
}
} catch (InterruptedException ignore) {
@ -246,6 +255,7 @@ public class MPDConnectionThread extends Thread {
private void sendCommand(MPDCommand command) throws IOException {
logger.trace("send command '{}'", command);
wakingUpFromIdle.set("noidle".equals(command.getCommand()));
final Socket socket = this.socket;
if (socket != null) {
String line = command.asLine();