From d1a5a9474062e68da895814c43fd4d2b92b1c99a Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 11 Dec 2017 06:42:35 -0800 Subject: [PATCH] fix crash processing multiple packets When grouping to allow multiple packets per outbound API call I forgot that some are there to mark the end-of-queue: can't be sent! Trying caused a NPE. Now if any EOQ is found in the queue that batch is dropped and the thread's exited. --- .../org/eehouse/android/xw4/RelayService.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java index 29ffd94f2..f2a378026 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java @@ -618,13 +618,18 @@ public class RelayService extends XWService for ( ; ; ) { List dataList = null; try { - PacketData outData = m_queue.take(); - if ( null != outData ) { - dataList = new ArrayList<>(); + dataList = new ArrayList<>(); + for ( PacketData outData = m_queue.take(); // blocks + null != outData; + outData = m_queue.poll() ) { // doesn't block + if ( outData.isEOQ() ) { + dataList = null; + break; + } dataList.add(outData); - m_queue.drainTo(dataList); + Log.d( TAG, "got %d packets; %d more left", dataList.size(), + m_queue.size()); } - Log.d( TAG, "got %d packets; %d more left", dataList.size(), m_queue.size()); } catch ( InterruptedException ie ) { Log.w( TAG, "write thread killed" ); break; @@ -665,6 +670,7 @@ public class RelayService extends XWService try { JSONArray dataArray = new JSONArray(); for ( PacketData packet : packets ) { + Assert.assertFalse( packet.isEOQ() ); byte[] datum = packet.assemble(); dataArray.put( Utils.base64Encode(datum) ); sentLen += datum.length; @@ -1554,6 +1560,8 @@ public class RelayService extends XWService System.currentTimeMillis() - m_created ); } + public boolean isEOQ() { return 0 == getLength(); } + public int getLength() { int result = 0;