Skip to content

Commit 3508705

Browse files
committed
Fixed #77
1 parent 3516d46 commit 3508705

2 files changed

Lines changed: 59 additions & 32 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,35 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
303303

304304
@Override
305305
public void close() throws IOException {
306-
receiver.stop();
307-
receiver = null;
306+
if (receiver != null) {
307+
receiver.stop();
308+
receiver = null;
309+
}
308310

309-
player.close();
310-
player = null;
311+
if (player != null) {
312+
player.close();
313+
player = null;
314+
}
311315

312-
audioKeyManager.close();
313-
audioKeyManager = null;
316+
if (audioKeyManager != null) {
317+
audioKeyManager.close();
318+
audioKeyManager = null;
319+
}
314320

315-
channelManager.close();
316-
channelManager = null;
321+
if (channelManager != null) {
322+
channelManager.close();
323+
channelManager = null;
324+
}
317325

318-
spirc.close();
319-
spirc = null;
326+
if (spirc != null) {
327+
spirc.close();
328+
spirc = null;
329+
}
320330

321-
mercuryClient.close();
322-
mercuryClient = null;
331+
if (mercuryClient != null) {
332+
mercuryClient.close();
333+
mercuryClient = null;
334+
}
323335

324336
executorService.shutdown();
325337
conn.socket.close();
@@ -419,6 +431,10 @@ public boolean valid() {
419431
return apWelcome != null && conn != null && !conn.socket.isClosed();
420432
}
421433

434+
public boolean isConnecting() {
435+
return cipherPair == null || authLock.get();
436+
}
437+
422438
@NotNull
423439
public String deviceId() {
424440
return inner.deviceId;

core/src/main/java/xyz/gianlu/librespot/core/ZeroconfServer.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.security.GeneralSecurityException;
2626
import java.security.MessageDigest;
2727
import java.util.*;
28+
import java.util.concurrent.ExecutorService;
29+
import java.util.concurrent.Executors;
2830

2931
/**
3032
* @author Gianlu
@@ -78,7 +80,7 @@ public class ZeroconfServer implements Closeable {
7880
private final Session.Inner inner;
7981
private final DiffieHellman keys;
8082
private final JmDNS[] instances;
81-
private Session session;
83+
private volatile Session session;
8284

8385
private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOException {
8486
this.inner = inner;
@@ -119,7 +121,7 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
119121
instances[i].registerService(serviceInfo);
120122
atLeastOne = true;
121123
} catch (SocketException ex) {
122-
LOGGER.warn("Failed creating socket for " + bound[i]);
124+
LOGGER.warn("Failed creating socket for " + bound[i], ex);
123125
}
124126
}
125127

@@ -274,6 +276,21 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
274276
aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Arrays.copyOfRange(encryptionKey, 0, 16), "AES"), new IvParameterSpec(iv));
275277
byte[] decrypted = aes.doFinal(encrypted);
276278

279+
280+
String resp = DEFAULT_SUCCESSFUL_ADD_USER.toString();
281+
out.write(httpVersion.getBytes());
282+
out.write(" 200 OK".getBytes());
283+
out.write(EOL);
284+
out.write("Content-Length: ".getBytes());
285+
out.write(String.valueOf(resp.length()).getBytes());
286+
out.write(EOL);
287+
out.flush();
288+
289+
out.write(EOL);
290+
out.write(resp.getBytes());
291+
out.flush();
292+
293+
277294
try {
278295
Authentication.LoginCredentials credentials = inner.decryptBlob(username, decrypted);
279296
if (hasValidSession()) {
@@ -289,22 +306,7 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
289306
} catch (Session.SpotifyAuthenticationException | SpotifyIrc.IrcException ex) {
290307
LOGGER.fatal("Failed handling connection! Going away.", ex);
291308
close();
292-
return;
293309
}
294-
295-
String resp = DEFAULT_SUCCESSFUL_ADD_USER.toString();
296-
297-
out.write(httpVersion.getBytes());
298-
out.write(" 200 OK".getBytes());
299-
out.write(EOL);
300-
out.write("Content-Length: ".getBytes());
301-
out.write(String.valueOf(resp.length()).getBytes());
302-
out.write(EOL);
303-
out.flush();
304-
305-
out.write(EOL);
306-
out.write(resp.getBytes());
307-
out.flush();
308310
}
309311

310312
public interface Configuration {
@@ -318,6 +320,7 @@ public interface Configuration {
318320

319321
private class HttpRunner implements Runnable, Closeable {
320322
private final ServerSocket serverSocket;
323+
private final ExecutorService executorService = Executors.newCachedThreadPool();
321324
private volatile boolean shouldStop = false;
322325

323326
HttpRunner(int port) throws IOException {
@@ -328,10 +331,18 @@ private class HttpRunner implements Runnable, Closeable {
328331
@Override
329332
public void run() {
330333
while (!shouldStop) {
331-
try (Socket socket = serverSocket.accept()) { // We don't need this to be async
332-
handle(socket);
334+
try {
335+
Socket socket = serverSocket.accept();
336+
executorService.execute(() -> {
337+
try {
338+
handle(socket);
339+
socket.close();
340+
} catch (IOException ex) {
341+
LOGGER.fatal("Failed handling request!", ex);
342+
}
343+
});
333344
} catch (IOException ex) {
334-
LOGGER.fatal("Failed handling request!", ex);
345+
LOGGER.fatal("Failed handling connection!", ex);
335346
}
336347
}
337348
}

0 commit comments

Comments
 (0)