clean up send message/alert code

This commit is contained in:
Eric House 2013-08-18 15:48:44 -07:00
parent 47203437cc
commit d0aca01289
3 changed files with 24 additions and 16 deletions

View file

@ -27,7 +27,7 @@ UDPAckTrack* UDPAckTrack::s_self = NULL;
/* static*/ bool /* static*/ bool
UDPAckTrack::shouldAck( XWRelayReg cmd ) UDPAckTrack::shouldAck( XWRelayReg cmd )
{ {
return ( XWPDEV_ACK != cmd && XWPDEV_ALERT != cmd ); return ( XWPDEV_ACK != cmd && XWPDEV_UNAVAIL != cmd );
} }
/* static*/ uint32_t /* static*/ uint32_t

View file

@ -427,7 +427,7 @@ send_via_udp_impl( int socket, const struct sockaddr* dest_addr,
struct iovec vec[10]; struct iovec vec[10];
unsigned int iocount = 0; unsigned int iocount = 0;
unsigned char header[1 + 1 + sizeof(packetNum)]; unsigned char header[1 + sizeof(packetNum) + 1];
header[0] = XWPDEV_PROTO_VERSION; header[0] = XWPDEV_PROTO_VERSION;
if ( NULL != packetIDP ) { if ( NULL != packetIDP ) {
*packetIDP = packetNum; *packetIDP = packetNum;
@ -640,18 +640,20 @@ post_message( DevIDRelay devid, const char* message )
short len = strlen(message); short len = strlen(message);
short netLen = htons(len); short netLen = htons(len);
uint32_t packetID; uint32_t packetID;
uint8_t buf[len + sizeof(netLen) + sizeof(XWPDEV_METAMSG)]; uint8_t buf[1 + sizeof(netLen) + len];
buf[0] = XWPDEV_METAMSG; XWRelayReg cmd = XWPDEV_ALERT;
memcpy( &buf[sizeof(XWPDEV_METAMSG)], &netLen, sizeof(netLen) ); buf[0] = cmd;
memcpy( &buf[sizeof(XWPDEV_METAMSG) + sizeof(netLen)], message, len ); 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 ); VSIZE(buf)-1, NULL );
if ( sent ) { if ( sent ) {
MsgClosure* mc = new MsgClosure( devid, buf, VSIZE(buf) ); MsgClosure* mc = new MsgClosure( devid, buf, VSIZE(buf) );
UDPAckTrack::setOnAck( onPostedMsgAcked, packetID, (void*)mc ); UDPAckTrack::setOnAck( onPostedMsgAcked, packetID, (void*)mc );
} else { } else {
DBMgr::Get()->StoreMessage( devid, (const unsigned char*)buf, VSIZE(buf) ); DBMgr::Get()->StoreMessage( devid, (const unsigned char*)buf,
VSIZE(buf) );
} }
} }
return success; return success;
@ -1428,6 +1430,7 @@ msgToStr( XWRelayReg msg )
const char* str; const char* str;
# define CASE_STR(c) case c: str = #c; break # define CASE_STR(c) case c: str = #c; break
switch( msg ) { switch( msg ) {
CASE_STR(XWPDEV_UNAVAIL);
CASE_STR(XWPDEV_REG); CASE_STR(XWPDEV_REG);
CASE_STR(XWPDEV_REGRSP); CASE_STR(XWPDEV_REGRSP);
CASE_STR(XWPDEV_KEEPALIVE); CASE_STR(XWPDEV_KEEPALIVE);
@ -1720,9 +1723,12 @@ maint_str_loop( int udpsock, const char* str )
UDPHeader header; UDPHeader header;
const unsigned char* ptr = buf; const unsigned char* ptr = buf;
uint32_t unavail = 0; // temp!
if ( getHeader( &ptr, ptr + nRead, &header ) ) { if ( getHeader( &ptr, ptr + nRead, &header ) ) {
send_via_udp( udpsock, &saddr.u.addr, NULL, XWPDEV_ALERT, send_via_udp( udpsock, &saddr.u.addr, NULL, XWPDEV_UNAVAIL,
outbuf, sizeof(outbuf), NULL ); &unavail, sizeof(unavail),
outbuf, sizeof(outbuf),
NULL );
} else { } else {
logf( XW_LOGERROR, "unexpected data" ); logf( XW_LOGERROR, "unexpected data" );
} }

View file

@ -38,16 +38,17 @@
typedef typedef
#endif #endif
enum { XWPDEV_NONE /* 0 is an illegal value */ 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 * proto: 1 byte
* msgID: 4 byte unsigned long, 0 an illegal value * msgID: 4 byte unsigned long, 0 an illegal value
* cmd: 1 byte, one of the values below. * cmd: 1 byte, one of the values below.
*/ */
,XWPDEV_ALERT /* relay->device: provides a string message to ,XWPDEV_UNAVAIL /* relay->device: provides a string message to
present to the user (with device allowed not present to the user (with device allowed
to present the same string more than once) not to present the same string more than
format: header, null-terminnated string: varies */ once) format: header, try-again-in-seconds: 4,
length-initiated string */
,XWPDEV_REG /* dev->relay: device registers self and ,XWPDEV_REG /* dev->relay: device registers self and
self-selected (e.g. gcm) or assigned devid self-selected (e.g. gcm) or assigned devid
format: header, idType: 1, 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: ,XWPDEV_DELGAME /* dev->relay: game's been deleted. format:
header, relayid: 4, clientToken: 4 */ 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 #ifndef CANT_DO_TYPEDEF