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 ( ; ; ) {
List<PacketData> dataList = null;
try {
PacketData outData = m_queue.take();
if ( null != outData ) {
dataList = new ArrayList<>();
dataList.add(outData);
m_queue.drainTo(dataList);
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);
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;