Skip to content

Commit 6e1ddc4

Browse files
committed
Auto-retry downloading chunk
1 parent fb48896 commit 6e1ddc4

1 file changed

Lines changed: 38 additions & 29 deletions

File tree

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private Streamer(@NotNull ByteString fileId, byte[] key, @NotNull AudioUrl moreA
158158
try {
159159
byte[] sizeHeader;
160160
if (cacheHandler == null || (sizeHeader = cacheHandler.getHeader(AudioFileFetch.HEADER_SIZE)) == null) {
161-
CdnLoader.Response resp = loader.request(0, CHUNK_SIZE - 1);
161+
CdnLoader.Response resp = loader.request(0, CHUNK_SIZE - 1, false);
162162
String contentRange = resp.headers.get("Content-Range");
163163
if (contentRange == null)
164164
throw new CdnException("Missing Content-Range header!");
@@ -252,38 +252,47 @@ private synchronized void populateSocket() throws IOException {
252252

253253
@NotNull
254254
public synchronized Response request(int chunk) throws IOException, CdnException {
255-
return request(CHUNK_SIZE * chunk, (chunk + 1) * CHUNK_SIZE - 1);
255+
return request(CHUNK_SIZE * chunk, (chunk + 1) * CHUNK_SIZE - 1, false);
256256
}
257257

258258
@NotNull
259-
public synchronized Response request(int rangeStart, int rangeEnd) throws IOException, CdnException {
260-
if (socket == null || socket.isClosed())
261-
populateSocket();
262-
263-
moreAudio.sendRequest(out, rangeStart, rangeEnd);
264-
265-
NetUtils.StatusLine sl = NetUtils.parseStatusLine(Utils.readLine(in));
266-
if (sl.statusCode == 408) {
267-
socket.close();
268-
return request(rangeStart, rangeEnd);
269-
} else if (sl.statusCode != 206) {
270-
throw new IOException(sl.statusCode + ": " + sl.statusPhrase);
259+
public synchronized Response request(int rangeStart, int rangeEnd, boolean retried) throws IOException, CdnException {
260+
try {
261+
if (socket == null || socket.isClosed())
262+
populateSocket();
263+
264+
moreAudio.sendRequest(out, rangeStart, rangeEnd);
265+
266+
NetUtils.StatusLine sl = NetUtils.parseStatusLine(Utils.readLine(in));
267+
if (sl.statusCode == 408) {
268+
socket.close();
269+
return request(rangeStart, rangeEnd, false);
270+
} else if (sl.statusCode != 206) {
271+
throw new IOException(sl.statusCode + ": " + sl.statusPhrase);
272+
}
273+
274+
Map<String, String> headers = NetUtils.parseHeaders(in);
275+
String contentLengthStr = headers.get("Content-Length");
276+
if (contentLengthStr == null)
277+
throw new CdnException("Missing Content-Length header!");
278+
279+
int contentLength = Integer.parseInt(contentLengthStr);
280+
byte[] buffer = new byte[contentLength];
281+
in.readFully(buffer);
282+
283+
String connectionStr = headers.get("Connection");
284+
if (Objects.equals(connectionStr, "close"))
285+
socket.close();
286+
287+
return new Response(buffer, headers);
288+
} catch (IOException ex) {
289+
if (!retried) {
290+
if (socket != null) socket.close();
291+
return request(rangeStart, rangeEnd, true);
292+
} else {
293+
throw ex;
294+
}
271295
}
272-
273-
Map<String, String> headers = NetUtils.parseHeaders(in);
274-
String contentLengthStr = headers.get("Content-Length");
275-
if (contentLengthStr == null)
276-
throw new CdnException("Missing Content-Length header!");
277-
278-
int contentLength = Integer.parseInt(contentLengthStr);
279-
byte[] buffer = new byte[contentLength];
280-
in.readFully(buffer);
281-
282-
String connectionStr = headers.get("Connection");
283-
if (Objects.equals(connectionStr, "close"))
284-
socket.close();
285-
286-
return new Response(buffer, headers);
287296
}
288297

289298
private static class Response {

0 commit comments

Comments
 (0)