aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoushik Dutta <koushd@gmail.com>2012-10-26 00:24:33 -0700
committerKoushik Dutta <koushd@gmail.com>2012-10-26 00:26:34 -0700
commit9b546b1327edc4caaf5dd0711d48da46849fa478 (patch)
tree03db4d6da94d798a8a72c76500b0eebc74f35ce9
parentb0a9100294f6768a67b79424c4ccaf97b9ba2679 (diff)
downloadAndroidAsync-9b546b1327edc4caaf5dd0711d48da46849fa478.tar.gz
AndroidAsync-9b546b1327edc4caaf5dd0711d48da46849fa478.tar.bz2
AndroidAsync-9b546b1327edc4caaf5dd0711d48da46849fa478.zip
Do not read DatagramChannels multiple times on a read event. This will cause a read-block to occur on Android versions prior to ICS.
-rw-r--r--AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java13
1 files changed, 11 insertions, 2 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java b/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java
index 8dbc1a2..1a1912a 100644
--- a/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java
+++ b/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java
@@ -101,12 +101,17 @@ class AsyncSocketImpl implements AsyncSocket {
// so we can be quicker about allocations during the next
// time this socket reads.
int maxRead = 0;
+ int maxAlloc = 1024 * 1024;
+ // keep udp at roughly the mtu, which is 1540 or something
+ // letting it grow freaks out nio apparently.
+ if (mChannel.isChunked())
+ maxAlloc = 8192;
while (true) {
if (b == null) {
- b = ByteBuffer.allocate(Math.min(Math.max(mToAlloc, 2 << 11), 1024 * 1024));
+ b = ByteBuffer.allocate(Math.min(Math.max(mToAlloc, 2 << 11), maxAlloc));
}
else {
- b = ByteBuffer.allocate(Math.min(b.capacity() * 2, 1024 * 1024));
+ b = ByteBuffer.allocate(Math.min(b.capacity() * 2, maxAlloc));
}
int read = mChannel.read(b);
maxRead = Math.max(read, maxRead);
@@ -128,6 +133,10 @@ class AsyncSocketImpl implements AsyncSocket {
Util.emitAllData(this, list);
list = new ByteBufferList();
}
+ // attempting to read a udp channel more than once
+ // causes nio to freak out on gb.
+ if (mChannel.isChunked())
+ break;
}
mToAlloc = maxRead;