Commit ff7258d5 authored by akwizgran's avatar akwizgran

Removed read buffer, increased wake lock coverage.

parent 6de4678d
......@@ -37,41 +37,46 @@ class NetworkClient extends Thread {
@Override
public void run() {
byte[] buf = new byte[4096];
while (!isInterrupted()) {
Socket s = new Socket();
try {
log.log("Connecting to " + host + ":" + port);
s.setSoTimeout(SOCKET_TIMEOUT_MS + keepaliveIntervalMs);
s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MS);
log.log("Requesting keepalives every " + (keepaliveIntervalMs / 1000) + " seconds");
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeShort(keepaliveIntervalMs / 1000);
out.flush();
InputStream in = s.getInputStream();
for (int read = 0; read != -1 && !isInterrupted(); read = in.read(buf)) {
wakeLock.acquire();
log.log("Read " + read + " bytes");
wakeLock.release();
wakeLock.acquire();
try {
while (!isInterrupted()) {
Socket s = new Socket();
try {
log.log("Connecting to " + host + ":" + port);
s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MS);
s.setSoTimeout(SOCKET_TIMEOUT_MS + keepaliveIntervalMs);
log.log("Requesting keepalives every "
+ (keepaliveIntervalMs / 1000) + " seconds");
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeShort(keepaliveIntervalMs / 1000);
out.flush();
InputStream in = s.getInputStream();
int read = 0;
while (read != -1 && !isInterrupted()) {
wakeLock.release();
read = in.read();
wakeLock.acquire();
if (read == -1) log.log("End of stream");
else log.log("Read keepalive");
}
if (isInterrupted()) {
log.log("Network thread interrupted");
return;
}
} catch (IOException e) {
log.log("Network error: " + e);
} finally {
tryToClose(s);
}
if (isInterrupted()) {
try {
sleep(RECONNECT_DELAY_MS);
} catch (InterruptedException e) {
log.log("Network thread interrupted");
tryToClose(s);
return;
} else {
log.log("End of stream");
tryToClose(s);
}
} catch (IOException e) {
log.log("Network error: " + e);
tryToClose(s);
}
try {
sleep(RECONNECT_DELAY_MS);
} catch (InterruptedException e) {
log.log("Network thread interrupted");
return;
}
} finally {
if (wakeLock.isHeld()) wakeLock.release();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment