Skip to content

Commit fb48896

Browse files
committed
Close connection if needed (closes #62)
1 parent 777e57e commit fb48896

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

common/src/main/java/xyz/gianlu/librespot/common/NetUtils.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ private NetUtils() {
1616
}
1717

1818
@NotNull
19-
public static StatusLine parseStatusLine(@NotNull String line) {
20-
int index = line.indexOf(' ');
21-
String httpVersion = line.substring(0, index);
22-
line = line.substring(index + 1);
23-
index = line.indexOf(' ');
24-
String statusCode = line.substring(0, index);
25-
String statusPhrase = line.substring(index + 1);
26-
return new StatusLine(httpVersion, Integer.parseInt(statusCode), statusPhrase);
19+
public static StatusLine parseStatusLine(@NotNull String line) throws IOException {
20+
try {
21+
int index = line.indexOf(' ');
22+
String httpVersion = line.substring(0, index);
23+
line = line.substring(index + 1);
24+
index = line.indexOf(' ');
25+
String statusCode = line.substring(0, index);
26+
String statusPhrase = line.substring(index + 1);
27+
return new StatusLine(httpVersion, Integer.parseInt(statusCode), statusPhrase);
28+
} catch (Exception ex) {
29+
throw new IOException(line, ex);
30+
}
2731
}
2832

2933
@NotNull

core/src/main/java/xyz/gianlu/librespot/cdn/CdnManager.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.ByteBuffer;
1717
import java.sql.SQLException;
1818
import java.util.Map;
19+
import java.util.Objects;
1920
import java.util.concurrent.ExecutorService;
2021
import java.util.concurrent.Executors;
2122

@@ -232,13 +233,18 @@ private void requestChunk(int index) {
232233
}
233234

234235
private static class CdnLoader {
235-
private final Socket socket;
236-
private final DataInputStream in;
237-
private final OutputStream out;
238236
private final AudioUrl moreAudio;
237+
private Socket socket;
238+
private DataInputStream in;
239+
private OutputStream out;
239240

240241
CdnLoader(@NotNull AudioUrl moreAudio) throws IOException {
241242
this.moreAudio = moreAudio;
243+
244+
populateSocket();
245+
}
246+
247+
private synchronized void populateSocket() throws IOException {
242248
this.socket = moreAudio.createSocket();
243249
this.in = new DataInputStream(socket.getInputStream());
244250
this.out = socket.getOutputStream();
@@ -251,11 +257,18 @@ public synchronized Response request(int chunk) throws IOException, CdnException
251257

252258
@NotNull
253259
public synchronized Response request(int rangeStart, int rangeEnd) throws IOException, CdnException {
260+
if (socket == null || socket.isClosed())
261+
populateSocket();
262+
254263
moreAudio.sendRequest(out, rangeStart, rangeEnd);
255264

256265
NetUtils.StatusLine sl = NetUtils.parseStatusLine(Utils.readLine(in));
257-
if (sl.statusCode != 206)
266+
if (sl.statusCode == 408) {
267+
socket.close();
268+
return request(rangeStart, rangeEnd);
269+
} else if (sl.statusCode != 206) {
258270
throw new IOException(sl.statusCode + ": " + sl.statusPhrase);
271+
}
259272

260273
Map<String, String> headers = NetUtils.parseHeaders(in);
261274
String contentLengthStr = headers.get("Content-Length");
@@ -266,6 +279,10 @@ public synchronized Response request(int rangeStart, int rangeEnd) throws IOExce
266279
byte[] buffer = new byte[contentLength];
267280
in.readFully(buffer);
268281

282+
String connectionStr = headers.get("Connection");
283+
if (Objects.equals(connectionStr, "close"))
284+
socket.close();
285+
269286
return new Response(buffer, headers);
270287
}
271288

0 commit comments

Comments
 (0)