close UrlTile conn on cancel
- synchronize connect() and close()
This commit is contained in:
parent
71715dccd9
commit
33c645e888
@ -247,7 +247,13 @@ public class LwHttp implements HttpEngine {
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream read() throws IOException {
|
||||
private void checkSocket() throws IOException {
|
||||
if (mSocket == null)
|
||||
throw new IOException("No Socket");
|
||||
}
|
||||
|
||||
public synchronized InputStream read() throws IOException {
|
||||
checkSocket();
|
||||
|
||||
Buffer is = mResponseStream;
|
||||
is.mark(BUFFER_SIZE);
|
||||
@ -325,7 +331,7 @@ public class LwHttp implements HttpEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRequest(Tile tile) throws IOException {
|
||||
public synchronized void sendRequest(Tile tile) throws IOException {
|
||||
|
||||
if (mSocket != null) {
|
||||
if (--mMaxRequests < 0)
|
||||
@ -380,7 +386,7 @@ public class LwHttp implements HttpEngine {
|
||||
//mCommandStream.flush();
|
||||
}
|
||||
|
||||
private void lwHttpConnect() throws IOException {
|
||||
private synchronized void lwHttpConnect() throws IOException {
|
||||
if (mSockAddr == null || mSockAddr.isUnresolved()) {
|
||||
mSockAddr = new InetSocketAddress(mHost, mPort);
|
||||
if (mSockAddr.isUnresolved())
|
||||
@ -404,44 +410,34 @@ public class LwHttp implements HttpEngine {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (mSocket == null)
|
||||
return;
|
||||
|
||||
IOUtils.closeQuietly(mSocket);
|
||||
mSocket = null;
|
||||
mCommandStream = null;
|
||||
mResponseStream = null;
|
||||
synchronized (this) {
|
||||
mSocket = null;
|
||||
mCommandStream = null;
|
||||
mResponseStream = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCache(OutputStream os) {
|
||||
if (mResponseStream == null)
|
||||
public synchronized void setCache(OutputStream os) {
|
||||
if (mSocket == null)
|
||||
return;
|
||||
|
||||
mResponseStream.setCache(os);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestCompleted(boolean success) {
|
||||
if (mResponseStream == null)
|
||||
public synchronized boolean requestCompleted(boolean ok) {
|
||||
if (mSocket == null)
|
||||
return false;
|
||||
|
||||
mLastRequest = System.nanoTime();
|
||||
mResponseStream.setCache(null);
|
||||
|
||||
if (!mResponseStream.finishedReading()) {
|
||||
if (dbg)
|
||||
log.debug("invalid buffer position");
|
||||
|
||||
/* hmmm, some bitmaps seems to not be decoded to the
|
||||
* end but still working */
|
||||
close();
|
||||
}
|
||||
|
||||
if (!success || mMustCloseConnection)
|
||||
if (!ok || mMustCloseConnection || !mResponseStream.finishedReading())
|
||||
close();
|
||||
|
||||
return success;
|
||||
return ok;
|
||||
}
|
||||
|
||||
/** write (positive) integer to byte array */
|
||||
|
@ -71,7 +71,7 @@ public class UrlTileDataSource implements ITileDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
boolean success = false;
|
||||
boolean ok = false;
|
||||
TileWriter cacheWriter = null;
|
||||
try {
|
||||
mConn.sendRequest(tile);
|
||||
@ -80,7 +80,7 @@ public class UrlTileDataSource implements ITileDataSource {
|
||||
cacheWriter = cache.writeTile(tile);
|
||||
mConn.setCache(cacheWriter.getOutputStream());
|
||||
}
|
||||
success = mTileDecoder.decode(tile, sink, is);
|
||||
ok = mTileDecoder.decode(tile, sink, is);
|
||||
} catch (SocketException e) {
|
||||
log.debug("{} Socket Error: {}", tile, e.getMessage());
|
||||
} catch (SocketTimeoutException e) {
|
||||
@ -90,11 +90,12 @@ public class UrlTileDataSource implements ITileDataSource {
|
||||
} catch (IOException e) {
|
||||
log.debug("{} Network Error: {}", tile, e.getMessage());
|
||||
} finally {
|
||||
success = mConn.requestCompleted(success);
|
||||
ok = mConn.requestCompleted(ok);
|
||||
if (cacheWriter != null)
|
||||
cacheWriter.complete(success);
|
||||
cacheWriter.complete(ok);
|
||||
|
||||
sink.completed(ok ? SUCCESS : FAILED);
|
||||
}
|
||||
sink.completed(success ? SUCCESS : FAILED);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user