From d0aca01289f40b74e7002c044203fdbe4b1f1041 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 18 Aug 2013 15:48:44 -0700 Subject: [PATCH] clean up send message/alert code --- xwords4/relay/udpack.cpp | 2 +- xwords4/relay/xwrelay.cpp | 24 +++++++++++++++--------- xwords4/relay/xwrelay.h | 14 ++++++++------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/xwords4/relay/udpack.cpp b/xwords4/relay/udpack.cpp index 96e236149..7a7f3985a 100644 --- a/xwords4/relay/udpack.cpp +++ b/xwords4/relay/udpack.cpp @@ -27,7 +27,7 @@ UDPAckTrack* UDPAckTrack::s_self = NULL; /* static*/ bool UDPAckTrack::shouldAck( XWRelayReg cmd ) { - return ( XWPDEV_ACK != cmd && XWPDEV_ALERT != cmd ); + return ( XWPDEV_ACK != cmd && XWPDEV_UNAVAIL != cmd ); } /* static*/ uint32_t diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 82bcad572..ae6a822d4 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -427,7 +427,7 @@ send_via_udp_impl( int socket, const struct sockaddr* dest_addr, struct iovec vec[10]; unsigned int iocount = 0; - unsigned char header[1 + 1 + sizeof(packetNum)]; + unsigned char header[1 + sizeof(packetNum) + 1]; header[0] = XWPDEV_PROTO_VERSION; if ( NULL != packetIDP ) { *packetIDP = packetNum; @@ -640,18 +640,20 @@ post_message( DevIDRelay devid, const char* message ) short len = strlen(message); short netLen = htons(len); uint32_t packetID; - uint8_t buf[len + sizeof(netLen) + sizeof(XWPDEV_METAMSG)]; - buf[0] = XWPDEV_METAMSG; - memcpy( &buf[sizeof(XWPDEV_METAMSG)], &netLen, sizeof(netLen) ); - memcpy( &buf[sizeof(XWPDEV_METAMSG) + sizeof(netLen)], message, len ); + uint8_t buf[1 + sizeof(netLen) + len]; + XWRelayReg cmd = XWPDEV_ALERT; + buf[0] = cmd; + memcpy( &buf[1], &netLen, sizeof(netLen) ); + memcpy( &buf[1 + sizeof(netLen)], message, len ); - bool sent = send_via_udp( &addr, &packetID, XWPDEV_METAMSG, &buf[1], + bool sent = send_via_udp( &addr, &packetID, cmd, &buf[1], VSIZE(buf)-1, NULL ); if ( sent ) { MsgClosure* mc = new MsgClosure( devid, buf, VSIZE(buf) ); UDPAckTrack::setOnAck( onPostedMsgAcked, packetID, (void*)mc ); } else { - DBMgr::Get()->StoreMessage( devid, (const unsigned char*)buf, VSIZE(buf) ); + DBMgr::Get()->StoreMessage( devid, (const unsigned char*)buf, + VSIZE(buf) ); } } return success; @@ -1428,6 +1430,7 @@ msgToStr( XWRelayReg msg ) const char* str; # define CASE_STR(c) case c: str = #c; break switch( msg ) { + CASE_STR(XWPDEV_UNAVAIL); CASE_STR(XWPDEV_REG); CASE_STR(XWPDEV_REGRSP); CASE_STR(XWPDEV_KEEPALIVE); @@ -1720,9 +1723,12 @@ maint_str_loop( int udpsock, const char* str ) UDPHeader header; const unsigned char* ptr = buf; + uint32_t unavail = 0; // temp! if ( getHeader( &ptr, ptr + nRead, &header ) ) { - send_via_udp( udpsock, &saddr.u.addr, NULL, XWPDEV_ALERT, - outbuf, sizeof(outbuf), NULL ); + send_via_udp( udpsock, &saddr.u.addr, NULL, XWPDEV_UNAVAIL, + &unavail, sizeof(unavail), + outbuf, sizeof(outbuf), + NULL ); } else { logf( XW_LOGERROR, "unexpected data" ); } diff --git a/xwords4/relay/xwrelay.h b/xwords4/relay/xwrelay.h index 43266f8b2..e73ff4074 100644 --- a/xwords4/relay/xwrelay.h +++ b/xwords4/relay/xwrelay.h @@ -38,16 +38,17 @@ typedef #endif enum { XWPDEV_NONE /* 0 is an illegal value */ - /* All messages have the following six-byte header + /* All messages have the following six-byte header (if proto == 0) * proto: 1 byte * msgID: 4 byte unsigned long, 0 an illegal value * cmd: 1 byte, one of the values below. */ - ,XWPDEV_ALERT /* relay->device: provides a string message to - present to the user (with device allowed not - to present the same string more than once) - format: header, null-terminnated string: varies */ + ,XWPDEV_UNAVAIL /* relay->device: provides a string message to + present to the user (with device allowed + not to present the same string more than + once) format: header, try-again-in-seconds: 4, + length-initiated string */ ,XWPDEV_REG /* dev->relay: device registers self and self-selected (e.g. gcm) or assigned devid format: header, idType: 1, @@ -94,7 +95,8 @@ enum { XWPDEV_NONE /* 0 is an illegal value */ ,XWPDEV_DELGAME /* dev->relay: game's been deleted. format: header, relayid: 4, clientToken: 4 */ - ,XWPDEV_METAMSG /* Message to be displayed to user */ + ,XWPDEV_ALERT /* relay->dev: format: header, + length-initiated string to present to user. */ } #ifndef CANT_DO_TYPEDEF