From dba86a657f8f4f0a87ee59b975478252ae657a75 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 1 Jul 2015 06:34:57 -0700 Subject: [PATCH] add and handle new INVITE message types. (This can safely be added to shipping relay.) --- xwords4/relay/xwrelay.cpp | 42 +++++++++++++++++++++++++++++++++++---- xwords4/relay/xwrelay.h | 5 +++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 1f155a2c8..8ea8c03cd 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -374,10 +374,15 @@ getHeader( const uint8_t** bufpp, const uint8_t* end, uint8_t byt; if ( getNetByte( bufpp, end, &byt ) ) { header->proto = (XWPDevProto)byt; - if ( XWPDEV_PROTO_VERSION_1 == header->proto - && vli2un( bufpp, end, &header->packetID ) - && getNetByte( bufpp, end, &byt ) - && byt < XWPDEV_N_ELEMS ) { + if ( XWPDEV_PROTO_VERSION_1 != header->proto ) { + logf( XW_LOGERROR, "%s: bad proto %d", __func__, header->proto ); + } else if ( !vli2un( bufpp, end, &header->packetID ) ) { + logf( XW_LOGERROR, "%s: can't get packet id", __func__ ); + } else if ( !getNetByte( bufpp, end, &byt ) ) { + logf( XW_LOGERROR, "%s: can't get cmd", __func__ ); + } else if ( XWPDEV_N_ELEMS <= byt ) { + logf( XW_LOGERROR, "%s: cmd %d too high", __func__, byt ); + } else { header->cmd = (XWRelayReg)byt; success = true; } @@ -838,6 +843,22 @@ post_upgrade( DevIDRelay devid ) (void)post_or_store( devid, packet, packetID, NULL, NULL ); } +void +post_invite( DevIDRelay sender, DevIDRelay invitee, const uint8_t* ptr, size_t len ) +{ + vector packet; + uint32_t packetID; + sender = htonl( sender ); + assemble_packet( packet, &packetID, XWPDEV_GOTINVITE, + &sender, sizeof(sender), + ptr, len, + NULL ); + + bool sent = post_or_store( invitee, packet, packetID, NULL, NULL ); + logf( XW_LOGINFO, "%s(): post_or_store => %s", __func__, + sent ? "sent" : "stored"); +} + /* A CONNECT message from a device gives us the hostID and socket we'll * associate with one participant in a relayed session. We'll store this * information with the cookie where other participants can find it when they @@ -1652,6 +1673,7 @@ msgToStr( XWRelayReg msg ) CASE_STR(XWPDEV_UNAVAIL); CASE_STR(XWPDEV_REG); CASE_STR(XWPDEV_REGRSP); + CASE_STR(XWPDEV_INVITE); CASE_STR(XWPDEV_KEEPALIVE); CASE_STR(XWPDEV_HAVEMSGS); CASE_STR(XWPDEV_RQSTMSGS); @@ -1756,6 +1778,18 @@ handle_udp_packet( UdpThreadClosure* utc ) } break; } + + case XWPDEV_INVITE: + DevIDRelay sender; + DevIDRelay invitee; + if ( getNetLong( &ptr, end, &sender ) + && getNetLong( &ptr, end, &invitee) ) { + logf( XW_LOGVERBOSE0, "got invite from %d for %d", + sender, invitee ); + post_invite( sender, invitee, ptr, end - ptr ); + } + break; + case XWPDEV_KEEPALIVE: case XWPDEV_RQSTMSGS: { DevID devID( ID_TYPE_RELAY ); diff --git a/xwords4/relay/xwrelay.h b/xwords4/relay/xwrelay.h index 2eef03edf..a502957c0 100644 --- a/xwords4/relay/xwrelay.h +++ b/xwords4/relay/xwrelay.h @@ -108,6 +108,11 @@ enum { XWPDEV_NONE /* 0 is an illegal value */ to check for upgrades; may eventually replace device needing a timer. */ + ,XWPDEV_INVITE /* dev->relay; format: header, sender relay + id: 4, recipient relay id: 4; nli data: + variable length. */ + ,XWPDEV_GOTINVITE /* relay->dev */ + ,XWPDEV_N_ELEMS /* MUST BE LAST */ }