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;
|
Buffer is = mResponseStream;
|
||||||
is.mark(BUFFER_SIZE);
|
is.mark(BUFFER_SIZE);
|
||||||
@ -325,7 +331,7 @@ public class LwHttp implements HttpEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendRequest(Tile tile) throws IOException {
|
public synchronized void sendRequest(Tile tile) throws IOException {
|
||||||
|
|
||||||
if (mSocket != null) {
|
if (mSocket != null) {
|
||||||
if (--mMaxRequests < 0)
|
if (--mMaxRequests < 0)
|
||||||
@ -380,7 +386,7 @@ public class LwHttp implements HttpEngine {
|
|||||||
//mCommandStream.flush();
|
//mCommandStream.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void lwHttpConnect() throws IOException {
|
private synchronized void lwHttpConnect() throws IOException {
|
||||||
if (mSockAddr == null || mSockAddr.isUnresolved()) {
|
if (mSockAddr == null || mSockAddr.isUnresolved()) {
|
||||||
mSockAddr = new InetSocketAddress(mHost, mPort);
|
mSockAddr = new InetSocketAddress(mHost, mPort);
|
||||||
if (mSockAddr.isUnresolved())
|
if (mSockAddr.isUnresolved())
|
||||||
@ -404,44 +410,34 @@ public class LwHttp implements HttpEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
if (mSocket == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
IOUtils.closeQuietly(mSocket);
|
IOUtils.closeQuietly(mSocket);
|
||||||
|
synchronized (this) {
|
||||||
mSocket = null;
|
mSocket = null;
|
||||||
mCommandStream = null;
|
mCommandStream = null;
|
||||||
mResponseStream = null;
|
mResponseStream = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCache(OutputStream os) {
|
public synchronized void setCache(OutputStream os) {
|
||||||
if (mResponseStream == null)
|
if (mSocket == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
mResponseStream.setCache(os);
|
mResponseStream.setCache(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean requestCompleted(boolean success) {
|
public synchronized boolean requestCompleted(boolean ok) {
|
||||||
if (mResponseStream == null)
|
if (mSocket == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
mLastRequest = System.nanoTime();
|
mLastRequest = System.nanoTime();
|
||||||
mResponseStream.setCache(null);
|
mResponseStream.setCache(null);
|
||||||
|
|
||||||
if (!mResponseStream.finishedReading()) {
|
if (!ok || mMustCloseConnection || !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)
|
|
||||||
close();
|
close();
|
||||||
|
|
||||||
return success;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** write (positive) integer to byte array */
|
/** write (positive) integer to byte array */
|
||||||
|
@ -71,7 +71,7 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean success = false;
|
boolean ok = false;
|
||||||
TileWriter cacheWriter = null;
|
TileWriter cacheWriter = null;
|
||||||
try {
|
try {
|
||||||
mConn.sendRequest(tile);
|
mConn.sendRequest(tile);
|
||||||
@ -80,7 +80,7 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
cacheWriter = cache.writeTile(tile);
|
cacheWriter = cache.writeTile(tile);
|
||||||
mConn.setCache(cacheWriter.getOutputStream());
|
mConn.setCache(cacheWriter.getOutputStream());
|
||||||
}
|
}
|
||||||
success = mTileDecoder.decode(tile, sink, is);
|
ok = mTileDecoder.decode(tile, sink, is);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
log.debug("{} Socket Error: {}", tile, e.getMessage());
|
log.debug("{} Socket Error: {}", tile, e.getMessage());
|
||||||
} catch (SocketTimeoutException e) {
|
} catch (SocketTimeoutException e) {
|
||||||
@ -90,11 +90,12 @@ public class UrlTileDataSource implements ITileDataSource {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.debug("{} Network Error: {}", tile, e.getMessage());
|
log.debug("{} Network Error: {}", tile, e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
success = mConn.requestCompleted(success);
|
ok = mConn.requestCompleted(ok);
|
||||||
if (cacheWriter != null)
|
if (cacheWriter != null)
|
||||||
cacheWriter.complete(success);
|
cacheWriter.complete(ok);
|
||||||
|
|
||||||
|
sink.completed(ok ? SUCCESS : FAILED);
|
||||||
}
|
}
|
||||||
sink.completed(success ? SUCCESS : FAILED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user