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.
This commit is contained in:
Eric House 2017-12-11 06:42:35 -08:00
parent 701e6968de
commit d1a5a94740

View file

@ -618,13 +618,18 @@ public class RelayService extends XWService
for ( ; ; ) { for ( ; ; ) {
List<PacketData> dataList = null; List<PacketData> dataList = null;
try { try {
PacketData outData = m_queue.take();
if ( null != outData ) {
dataList = new ArrayList<>(); dataList = new ArrayList<>();
dataList.add(outData); for ( PacketData outData = m_queue.take(); // blocks
m_queue.drainTo(dataList); null != outData;
outData = m_queue.poll() ) { // doesn't block
if ( outData.isEOQ() ) {
dataList = null;
break;
}
dataList.add(outData);
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 ) { } catch ( InterruptedException ie ) {
Log.w( TAG, "write thread killed" ); Log.w( TAG, "write thread killed" );
break; break;
@ -665,6 +670,7 @@ public class RelayService extends XWService
try { try {
JSONArray dataArray = new JSONArray(); JSONArray dataArray = new JSONArray();
for ( PacketData packet : packets ) { for ( PacketData packet : packets ) {
Assert.assertFalse( packet.isEOQ() );
byte[] datum = packet.assemble(); byte[] datum = packet.assemble();
dataArray.put( Utils.base64Encode(datum) ); dataArray.put( Utils.base64Encode(datum) );
sentLen += datum.length; sentLen += datum.length;
@ -1554,6 +1560,8 @@ public class RelayService extends XWService
System.currentTimeMillis() - m_created ); System.currentTimeMillis() - m_created );
} }
public boolean isEOQ() { return 0 == getLength(); }
public int getLength() public int getLength()
{ {
int result = 0; int result = 0;