diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index 21eaf0353..00d8ce936 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -531,7 +531,7 @@ public class RelayService extends XWService postData( RelayService.this, token, msg ); break; case XWPDEV_ACK: - noteAck( dis.readInt() ); + noteAck( vli2un( dis ) ); break; default: DbgUtils.logf( "RelayService.gotPacket(): Unhandled cmd" ); @@ -917,7 +917,11 @@ public class RelayService extends XWService private static void noteAck( int packetID ) { synchronized( s_packetsSent ) { - s_packetsSent.remove( packetID ); + if ( s_packetsSent.contains( packetID ) ) { + s_packetsSent.remove( packetID ); + } else { + DbgUtils.logf( "Weird: got ack %d but never sent", packetID ); + } DbgUtils.logf( "RelayService.noteAck(): Got ack for %d; " + "there are %d unacked packets", packetID, s_packetsSent.size() ); diff --git a/xwords4/linux/relaycon.c b/xwords4/linux/relaycon.c index 93c5086c4..33692fcf6 100644 --- a/xwords4/linux/relaycon.c +++ b/xwords4/linux/relaycon.c @@ -50,6 +50,7 @@ static int writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd ); static bool readHeader( const XP_U8** buf, MsgHeader* header ); static size_t writeDevID( XP_U8* buf, size_t len, const XP_UCHAR* str ); static size_t writeShort( XP_U8* buf, size_t len, XP_U16 shrt ); +static size_t writeVLI( XP_U8* out, uint32_t nn ); static size_t un2vli( int nn, uint8_t* buf ); static bool vli2un( const uint8_t** inp, uint32_t* outp ); @@ -185,11 +186,7 @@ sendAckIf( RelayConStorage* storage, const MsgHeader* header ) if ( header->cmd != XWPDEV_ACK ) { XP_U8 tmpbuf[16]; int indx = writeHeader( storage, tmpbuf, XWPDEV_ACK ); - - uint8_t buf[5]; - size_t numSiz = un2vli( header->packetID, buf ); - memcpy( &tmpbuf[indx], buf, numSiz ); - indx += numSiz; + indx += writeVLI( &tmpbuf[indx], header->packetID ); sendIt( storage, tmpbuf, indx ); } } @@ -251,9 +248,12 @@ relaycon_receive( void* closure, int socket ) break; } case XWPDEV_ACK: { - XP_U32 packetID = getNetLong( &ptr ); + uint32_t packetID; + if ( !vli2un( &ptr, &packetID ) ) { + assert( 0 ); + } XP_USE( packetID ); - XP_LOGF( "got ack for packetID %ld", packetID ); + XP_LOGF( "got ack for packetID %d", packetID ); break; } case XWPDEV_ALERT: { @@ -350,6 +350,15 @@ writeShort( XP_U8* buf, size_t len, XP_U16 shrt ) return sizeof(shrt); } +static size_t +writeVLI( XP_U8* out, uint32_t nn ) +{ + uint8_t buf[5]; + size_t numSiz = un2vli( nn, buf ); + memcpy( out, buf, numSiz ); + return numSiz; +} + static XP_U16 getNetShort( const XP_U8** ptr ) { @@ -387,10 +396,9 @@ writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd ) } if ( XWPDEV_PROTO_VERSION_1 == storage->proto ) { - uint8_t buf[5]; - size_t numSiz = un2vli( packetNum, buf ); - memcpy( &dest[indx], buf, numSiz ); - indx += numSiz; + indx += writeVLI( &dest[indx], packetNum ); + } else { + assert( 0 ); } dest[indx++] = cmd; diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 54b6f1048..6a4fcfa96 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -330,7 +330,7 @@ vli2un( const unsigned char** bufpp, const unsigned char* end, uint32_t* out ) } } - bool success = in < end; + bool success = in <= end; if ( success ) { *bufpp = in; *out = result; @@ -1549,11 +1549,11 @@ static void ackPacketIf( const UDPHeader* header, const AddrInfo* addr ) { if ( UDPAckTrack::shouldAck( header->cmd ) ) { - uint32_t packetID = header->packetID; - logf( XW_LOGINFO, "%s: acking packet %d", __func__, packetID ); - packetID = htonl( packetID ); - send_via_udp( addr, NULL, XWPDEV_ACK, - &packetID, sizeof(packetID), NULL ); + logf( XW_LOGINFO, "%s: acking packet %d", __func__, header->packetID ); + + uint8_t buf[5]; + size_t siz = un2vli( header->packetID, buf ); + send_via_udp( addr, NULL, XWPDEV_ACK, buf, siz, NULL ); } }