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
UDPAckTrack::shouldAck( XWRelayReg cmd )
{
return ( XWPDEV_ACK != cmd && XWPDEV_ALERT != cmd );
return ( XWPDEV_ACK != cmd && XWPDEV_UNAVAIL != cmd );
}
/* static*/ uint32_t

View file

@ -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" );
}

View file

@ -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