diff --git a/xwords4/relay/udpack.cpp b/xwords4/relay/udpack.cpp index 297032c7c..75762b829 100644 --- a/xwords4/relay/udpack.cpp +++ b/xwords4/relay/udpack.cpp @@ -23,13 +23,17 @@ UDPAckTrack* UDPAckTrack::s_self = NULL; +/* static*/ bool +UDPAckTrack::shouldAck( XWRelayReg cmd ) +{ + return ( XWPDEV_ACK != cmd && XWPDEV_ALERT != cmd ); +} + /* static*/ uint32_t UDPAckTrack::nextPacketID( XWRelayReg cmd ) { - uint32_t result; - if ( XWPDEV_ACK == cmd || XWPDEV_ALERT == cmd ) { - result = 0; - } else { + uint32_t result = 0; + if ( shouldAck( cmd ) ) { result = get()->nextPacketIDImpl(); } return result; @@ -115,9 +119,10 @@ UDPAckTrack::threadProc() } string_printf( leaked, ", " ); } - logf( XW_LOGERROR, "these packets leaked: %s", leaked.c_str() ); + logf( XW_LOGERROR, "%s: these packets leaked: %s", __func__, + leaked.c_str() ); } else { - logf( XW_LOGINFO, "no packets leaked" ); + logf( XW_LOGINFO, "%s: no packets leaked", __func__ ); } } return NULL; diff --git a/xwords4/relay/udpack.h b/xwords4/relay/udpack.h index 5df604154..ee94a046b 100644 --- a/xwords4/relay/udpack.h +++ b/xwords4/relay/udpack.h @@ -33,6 +33,7 @@ class UDPAckTrack { public: static uint32_t nextPacketID( XWRelayReg cmd ); static void recordAck( uint32_t packetID ); + static bool shouldAck( XWRelayReg cmd ); private: static UDPAckTrack* get(); diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index e281cf1da..52a8f04e1 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -1293,6 +1293,18 @@ msgToStr( XWRelayReg msg ) } +static void +ackPacketIf( const UDPHeader* header, const AddrInfo* addr ) +{ + if ( UDPAckTrack::shouldAck( header->cmd ) ) { + uint32_t packetID = header->packetID; + logf( XW_LOGINFO, "acking packet %d", packetID ); + packetID = htonl( packetID ); + send_via_udp( addr->socket(), addr->sockaddr(), XWPDEV_ACK, + &packetID, sizeof(packetID), NULL ); + } +} + static void udp_thread_proc( UdpThreadClosure* utc ) { @@ -1302,6 +1314,7 @@ udp_thread_proc( UdpThreadClosure* utc ) UDPHeader header; if ( getHeader( &ptr, end, &header ) ) { logf( XW_LOGINFO, "%s(msg=%s)", __func__, msgToStr( header.cmd ) ); + ackPacketIf( &header, utc->addr() ); switch( header.cmd ) { case XWPDEV_REG: { DevIDType typ = (DevIDType)*ptr++; @@ -1375,6 +1388,7 @@ udp_thread_proc( UdpThreadClosure* utc ) case XWPDEV_ACK: { uint32_t packetID; if ( getNetLong( &ptr, end, &packetID ) ) { + logf( XW_LOGINFO, "ack for packet %d", packetID ); UDPAckTrack::recordAck( packetID ); } break;