From b80051cc738a72f09931d1a36f84092338ff0e9d Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 28 Aug 2013 21:36:12 -0700 Subject: [PATCH] send dev-targeted messages pulled from db by rewriting their headers with a new packetID. --- xwords4/relay/xwrelay.cpp | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 676db31a5..49c1a8e0b 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -523,6 +523,22 @@ assemble_packet( vector& packet, uint32_t* packetIDP, XWRelayReg cmd, va_end( ap ); } +// make a new packet out of an old, stealing its cmd field +static void +assemble_packet( vector& newPacket, uint32_t* packetID, + const vector& oldPacket ) +{ + UDPHeader header; + const uint8_t* ptr = oldPacket.data(); + size_t len = oldPacket.size(); + if ( !getHeader( &ptr, ptr + len, &header ) ) { + assert( 0 ); + } + assert( XWPDEV_PROTO_VERSION_1 == header.proto ); + len -= ptr - oldPacket.data(); // subtract off header length + assemble_packet( newPacket, packetID, header.cmd, ptr, len, NULL ); +} + static bool get_addr_info_if( const AddrInfo* addr, int* sockp, const struct sockaddr** dest_addr ) @@ -1507,10 +1523,24 @@ retrieveMessages( DevID& devID, const AddrInfo* addr ) vector::const_iterator iter; for ( iter = msgs.begin(); iter != msgs.end(); ++iter ) { - DBMgr::MsgInfo msg = *iter; + const DBMgr::MsgInfo& msg = *iter; uint32_t packetID; - if ( !send_msg_via_udp( addr, msg.token, msg.msg.data(), - msg.msg.size(), &packetID ) ) { + bool success = false; + if ( AddrInfo::NULL_TOKEN == msg.token ) { + int socket; + const struct sockaddr* dest_addr; + if ( get_addr_info_if( addr, &socket, &dest_addr ) ) { + vector newPacket; + assemble_packet( newPacket, &packetID, msg.msg ); + success = 0 < send_packet_via_udp_impl( newPacket, socket, + dest_addr ); + } + } else { + success = send_msg_via_udp( addr, msg.token, msg.msg.data(), + msg.msg.size(), &packetID ); + } + + if ( !success ) { logf( XW_LOGERROR, "%s: unable to send to devID %d", __func__, devID.asRelayID() ); break;