Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/src/main/java/xyz/gianlu/librespot/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private void authenticate(@NotNull Authentication.LoginCredentials credentials)
* {@code true} for {@link Session#reconnect()}.
*/
private void authenticatePartial(@NotNull Authentication.LoginCredentials credentials, boolean removeLock) throws IOException, GeneralSecurityException, SpotifyAuthenticationException {
if (cipherPair == null) throw new IllegalStateException("Connection not established!");
if (conn == null || cipherPair == null) throw new IllegalStateException("Connection not established!");

Authentication.ClientResponseEncrypted clientResponseEncrypted = Authentication.ClientResponseEncrypted.newBuilder()
.setLoginCredentials(credentials)
Expand All @@ -409,7 +409,6 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden

receiver = new Receiver();


byte[] bytes0x0f = new byte[20];
random().nextBytes(bytes0x0f);
sendUnchecked(Packet.Type.Unknown_0x0f, bytes0x0f);
Expand Down Expand Up @@ -452,6 +451,8 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
public void close() throws IOException {
LOGGER.info("Closing session. {deviceId: {}}", inner.deviceId);

if (scheduledReconnect != null) scheduledReconnect.cancel(true);

closing = true;

scheduler.shutdownNow();
Expand Down Expand Up @@ -513,6 +514,9 @@ public void close() throws IOException {
}

private void sendUnchecked(Packet.Type cmd, byte[] payload) throws IOException {
if (conn == null)
throw new IOException("Cannot write to missing connection.");

cipherPair.sendEncoded(conn.out, cmd.val, payload);
}

Expand Down Expand Up @@ -692,6 +696,9 @@ public Configuration configuration() {
}

private void reconnect() {
if (closing)
return;

synchronized (reconnectionListeners) {
reconnectionListeners.forEach(ReconnectionListener::onConnectionDropped);
}
Expand All @@ -716,6 +723,9 @@ private void reconnect() {
reconnectionListeners.forEach(ReconnectionListener::onConnectionEstablished);
}
} catch (IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
if (closing)
return;

conn = null;
LOGGER.error("Failed reconnecting, retrying in 10 seconds...", ex);

Expand Down Expand Up @@ -1310,7 +1320,7 @@ public void run() {
continue;
}
} catch (IOException | GeneralSecurityException ex) {
if (running) {
if (running && !closing) {
LOGGER.error("Failed reading packet!", ex);
reconnect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.RejectedExecutionException;

/**
* @author Gianlu
Expand All @@ -65,6 +66,7 @@ public final class DeviceStateHandler implements Closeable, DealerClient.Message
private final Connect.PutStateRequest.Builder putState;
private final AsyncWorker<Connect.PutStateRequest> putStateWorker;
private volatile String connectionId = null;
private volatile boolean closing = false;

public DeviceStateHandler(@NotNull Session session, @NotNull PlayerConfiguration conf) {
this.session = session;
Expand Down Expand Up @@ -226,7 +228,11 @@ public synchronized void updateState(@NotNull Connect.PutStateReason reason, int
.setClientSideTimestamp(TimeProvider.currentTimeMillis())
.getDeviceBuilder().setDeviceInfo(deviceInfo).setPlayerState(state);

putStateWorker.submit(putState.build());
try {
putStateWorker.submit(putState.build());
} catch (RejectedExecutionException ex) {
if (!closing) LOGGER.error("Failed to submit update state task.", ex);
}
}

public synchronized int getVolume() {
Expand All @@ -244,6 +250,8 @@ public void setVolume(int val) {

@Override
public void close() {
closing = true;

session.dealer().removeMessageListener(this);
session.dealer().removeRequestListener(this);

Expand Down