handle sending of messages generated by games opened only to receive

messages from relay.  And handle report that relayID is bad.
This commit is contained in:
Eric House 2013-01-19 14:37:49 -08:00
parent 63d3f70cdf
commit 1bb9f9b1bf
6 changed files with 203 additions and 81 deletions

View file

@ -208,12 +208,14 @@ loadGame( XWStreamCtxt* stream, sqlite3* pDb, sqlite3_int64 rowid )
int result = sqlite3_prepare_v2( pDb, buf, -1, &ppStmt, NULL );
XP_ASSERT( SQLITE_OK == result );
result = sqlite3_step( ppStmt );
XP_ASSERT( SQLITE_ROW == result );
XP_Bool success = SQLITE_ROW == result;
if ( success ) {
const void* ptr = sqlite3_column_blob( ppStmt, 0 );
int size = sqlite3_column_bytes( ppStmt, 0 );
stream_putBytes( stream, ptr, size );
}
sqlite3_finalize( ppStmt );
return XP_TRUE;
return success;
}
void
@ -230,7 +232,7 @@ deleteGame( sqlite3* pDb, sqlite3_int64 rowid )
}
void
store( sqlite3* pDb, const gchar* key, const gchar* value )
db_store( sqlite3* pDb, const gchar* key, const gchar* value )
{
char buf[256];
snprintf( buf, sizeof(buf),
@ -245,7 +247,7 @@ store( sqlite3* pDb, const gchar* key, const gchar* value )
}
void
fetch( sqlite3* pDb, const gchar* key, gchar* buf, gint buflen )
db_fetch( sqlite3* pDb, const gchar* key, gchar* buf, gint buflen )
{
char query[256];
snprintf( query, sizeof(query),
@ -262,6 +264,19 @@ fetch( sqlite3* pDb, const gchar* key, gchar* buf, gint buflen )
sqlite3_finalize( ppStmt );
}
void
db_remove( sqlite3* pDb, const gchar* key )
{
char query[256];
snprintf( query, sizeof(query), "DELETE FROM pairs WHERE key = '%s'", key );
sqlite3_stmt *ppStmt;
int result = sqlite3_prepare_v2( pDb, query, -1, &ppStmt, NULL );
XP_ASSERT( SQLITE_OK == result );
result = sqlite3_step( ppStmt );
XP_ASSERT( SQLITE_DONE == result );
sqlite3_finalize( ppStmt );
}
static void
getColumnText( sqlite3_stmt *ppStmt, int iCol, XP_UCHAR* buf, int len )
{

View file

@ -51,7 +51,8 @@ void deleteGame( sqlite3* pDb, sqlite3_int64 rowid );
#define KEY_RDEVID "RDEVID"
void store( sqlite3* dbp, const gchar* key, const gchar* value );
void fetch( sqlite3* dbp, const gchar* key, gchar* buf, gint buflen );
void db_store( sqlite3* dbp, const gchar* key, const gchar* value );
void db_fetch( sqlite3* dbp, const gchar* key, gchar* buf, gint buflen );
void db_remove( sqlite3* dbp, const gchar* key );
#endif

View file

@ -66,6 +66,7 @@
#include "memstream.h"
#include "filestream.h"
#include "gamesdb.h"
#include "relaycon.h"
/* static guint gtkSetupClientSocket( GtkGameGlobals* globals, int sock ); */
static void setCtrlsForTray( GtkGameGlobals* globals );
@ -392,13 +393,15 @@ relay_error_gtk( void* closure, XWREASON relayErr )
}
static XP_Bool
relay_sendNoConn_gtk( const XP_U8* XP_UNUSED(msg), XP_U16 XP_UNUSED(len),
const XP_UCHAR* XP_UNUSED(relayID), void* closure )
relay_sendNoConn_gtk( const XP_U8* msg, XP_U16 len, const XP_UCHAR* relayID,
void* closure )
{
GtkGameGlobals* globals = (GtkGameGlobals*)closure;
XP_Bool success = XP_FALSE;
if ( !!globals->cGlobals.pDb && !globals->draw ) {
XP_ASSERT( 0 ); /* implement me!!! */
XP_S16 nSent = relaycon_sendnoconn( globals->cGlobals.params, msg, len,
relayID, globals->cGlobals.selRow );
success = nSent == len;
}
return success;
} /* relay_sendNoConn_gtk */

View file

@ -30,6 +30,7 @@
static void onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid,
XP_Bool isNew );
static void updateButtons( GtkAppGlobals* apg );
static void sendRelayReg( GtkAppGlobals* apg );
static void
recordOpened( GtkAppGlobals* apg, GtkGameGlobals* globals )
@ -406,12 +407,40 @@ gtkGotBuf( void* closure, XP_U8* buf, XP_U16 len )
}
}
static gint
requestMsgs( gpointer data )
{
GtkAppGlobals* apg = (GtkAppGlobals*)data;
XP_UCHAR devIDBuf[64] = {0};
db_fetch( apg->pDb, KEY_RDEVID, devIDBuf, sizeof(devIDBuf) );
if ( '\0' != devIDBuf[0] ) {
relaycon_requestMsgs( apg->params, devIDBuf );
} else {
XP_LOGF( "%s: not requesting messages as don't have relay id", __func__ );
}
return 0; /* don't run again */
}
static void
gtkNoticeRcvd( void* closure, XP_U32 XP_UNUSED(gameToken) )
{
LOG_FUNC();
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
(void)g_idle_add( requestMsgs, apg );
}
static void
gtkDevIDChanged( void* closure, const XP_UCHAR* devID )
{
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
if ( !!devID ) {
XP_LOGF( "%s(devID=%s)", __func__, devID );
store( apg->pDb, KEY_RDEVID, devID );
db_store( apg->pDb, KEY_RDEVID, devID );
} else {
XP_LOGF( "%s: bad relayid", __func__ );
db_remove( apg->pDb, KEY_RDEVID );
sendRelayReg( apg );
}
}
void
@ -445,6 +474,27 @@ handle_sigintterm( int XP_UNUSED(sig) )
handle_destroy( NULL, g_globals_for_signal );
}
static void
sendRelayReg( GtkAppGlobals* apg )
{
LaunchParams* params = apg->params;
XP_UCHAR devIDBuf[64] = {0};
XP_UCHAR* devID;
DevIDType typ = ID_TYPE_RELAY;
if ( !!params->rDevID ) {
devID = params->rDevID;
} else {
db_fetch( apg->pDb, KEY_RDEVID, devIDBuf, sizeof(devIDBuf) );
if ( '\0' != devIDBuf[0] ) {
devID = devIDBuf;
} else {
devID = params->devID;
typ = ID_TYPE_LINUX;
}
}
relaycon_reg( params, devID, typ );
}
int
gtkmain( LaunchParams* params )
{
@ -463,28 +513,14 @@ gtkmain( LaunchParams* params )
RelayConnProcs procs = {
.msgReceived = gtkGotBuf,
.msgNoticeReceived = gtkNoticeRcvd,
.devIDChanged = gtkDevIDChanged,
};
XP_UCHAR devIDBuf[64] = {0};
XP_UCHAR* devID;
DevIDType typ = ID_TYPE_RELAY;
if ( !!params->rDevID ) {
devID = params->rDevID;
} else {
fetch( apg.pDb, KEY_RDEVID, devIDBuf, sizeof(devIDBuf) );
if ( '\0' != devIDBuf[0] ) {
devID = devIDBuf;
} else {
devID = params->devID;
typ = ID_TYPE_LINUX;
}
}
relaycon_init( params, &procs, &apg,
params->connInfo.relay.relayName,
params->connInfo.relay.defaultSendPort,
devID, typ );
params->connInfo.relay.defaultSendPort );
sendRelayReg( &apg );
(void)makeGamesWindow( &apg );
gtk_main();

View file

@ -27,18 +27,18 @@ typedef struct _RelayConStorage {
int socket;
RelayConnProcs procs;
void* procsClosure;
struct sockaddr_in saddr;
} RelayConStorage;
static RelayConStorage* getStorage( LaunchParams* params );
static void addressToServer( struct sockaddr_in* to, const CommsAddrRec* addr );
static XP_U32 addrForHost( const CommsAddrRec* addr );
static XP_U32 hostNameToIP( const XP_UCHAR* name );
static void relaycon_receive( void* closure, int socket );
static ssize_t sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len );
static size_t addStrWithLength( XP_U8* buf, XP_U8* end, const XP_UCHAR* str );
void
relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
void* procsClosure, const char* host, int port,
const XP_UCHAR* devID, DevIDType typ )
void* procsClosure, const char* host, int port )
{
XP_ASSERT( !params->relayConStorage );
RelayConStorage* storage = getStorage( params );
@ -49,50 +49,100 @@ relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
(*params->socketChanged)( params->socketChangedClosure, storage->socket, -1,
relaycon_receive, params );
XP_MEMSET( &storage->saddr, 0, sizeof(storage->saddr) );
storage->saddr.sin_family = PF_INET;
storage->saddr.sin_addr.s_addr = htonl( hostNameToIP(host) );
storage->saddr.sin_port = htons(port);
}
void
relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ )
{
LOG_FUNC();
XP_U8 tmpbuf[32];
int indx = 0;
RelayConStorage* storage = getStorage( params );
XP_ASSERT( !!devID );
XP_U16 idLen = XP_STRLEN( devID );
XP_U16 lenNBO = XP_HTONS( idLen );
XP_U8 tmpbuf[1 + 1 + 1 + sizeof(lenNBO) + idLen];
tmpbuf[0] = XWREG_PROTO_VERSION;
tmpbuf[1] = XWRREG_REG;
tmpbuf[2] = typ;
XP_MEMCPY( &tmpbuf[3], &lenNBO, sizeof(lenNBO) );
XP_MEMCPY( &tmpbuf[5], devID, idLen );
tmpbuf[indx++] = XWPDEV_PROTO_VERSION;
tmpbuf[indx++] = XWPDEV_REG;
tmpbuf[indx++] = typ;
XP_MEMCPY( &tmpbuf[indx], &lenNBO, sizeof(lenNBO) );
indx += sizeof(lenNBO);
XP_MEMCPY( &tmpbuf[indx], devID, idLen );
indx += idLen;
struct sockaddr_in to = {0};
to.sin_family = PF_INET;
to.sin_addr.s_addr = htonl( hostNameToIP(host) );
to.sin_port = htons(port);
(void)sendto( storage->socket, tmpbuf, sizeof(tmpbuf), 0, /* flags */
(struct sockaddr*)&to, sizeof(to) );
sendIt( storage, tmpbuf, indx );
}
XP_S16
relaycon_send( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
XP_U32 gameToken, const CommsAddrRec* addrRec )
XP_U32 gameToken, const CommsAddrRec* XP_UNUSED(addrRec) )
{
ssize_t nSent = -1;
RelayConStorage* storage = getStorage( params );
struct sockaddr_in to = {0};
addressToServer( &to, addrRec );
XP_U8 tmpbuf[1 + 1 + sizeof(gameToken) + buflen];
tmpbuf[0] = XWREG_PROTO_VERSION;
tmpbuf[1] = XWRREG_MSG;
tmpbuf[0] = XWPDEV_PROTO_VERSION;
tmpbuf[1] = XWPDEV_MSG;
XP_U32 inNBO = htonl(gameToken);
XP_MEMCPY( &tmpbuf[2], &inNBO, sizeof(inNBO) );
XP_MEMCPY( &tmpbuf[1 + 1 + sizeof(gameToken)], buf, buflen );
nSent = sendto( storage->socket, tmpbuf, sizeof(tmpbuf), 0, /* flags */
(struct sockaddr*)&to, sizeof(to) );
if ( 1 + 1 + sizeof(gameToken) < nSent ) {
nSent -= 1 + 1 + sizeof(gameToken);
nSent = sendIt( storage, tmpbuf, sizeof(tmpbuf) );
if ( nSent > buflen ) {
nSent = buflen;
}
LOG_RETURNF( "%d", nSent );
return nSent;
}
XP_S16
relaycon_sendnoconn( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
const XP_UCHAR* relayID, XP_U32 gameToken )
{
XP_LOGF( "%s(relayID=%s)", __func__, relayID );
XP_U16 indx = 0;
ssize_t nSent = -1;
RelayConStorage* storage = getStorage( params );
XP_U16 idLen = XP_STRLEN( relayID );
XP_U8 tmpbuf[1 + 1 +
1 + idLen +
sizeof(gameToken) + buflen];
tmpbuf[indx++] = XWPDEV_PROTO_VERSION;
tmpbuf[indx++] = XWPDEV_MSGNOCONN;
gameToken = htonl( gameToken );
XP_MEMCPY( &tmpbuf[indx], &gameToken, sizeof(gameToken) );
indx += sizeof(gameToken);
XP_MEMCPY( &tmpbuf[indx], relayID, idLen );
indx += idLen;
tmpbuf[indx++] = '\n';
XP_MEMCPY( &tmpbuf[indx], buf, buflen );
nSent = sendIt( storage, tmpbuf, sizeof(tmpbuf) );
if ( nSent > buflen ) {
nSent = buflen;
}
LOG_RETURNF( "%d", nSent );
return nSent;
}
void
relaycon_requestMsgs( LaunchParams* params, const XP_UCHAR* devID )
{
XP_LOGF( "%s(devID=%s)", __func__, devID );
RelayConStorage* storage = getStorage( params );
XP_U8 tmpbuf[128];
int indx = 0;
tmpbuf[indx++] = XWPDEV_PROTO_VERSION;
tmpbuf[indx++] = XWPDEV_RQSTMSGS;
indx += addStrWithLength( &tmpbuf[indx], tmpbuf + sizeof(tmpbuf), devID );
sendIt( storage, tmpbuf, indx );
}
static void
relaycon_receive( void* closure, int socket )
{
@ -111,10 +161,10 @@ relaycon_receive( void* closure, int socket )
if ( 0 <= nRead ) {
XP_U8* ptr = buf;
const XP_U8* end = buf + nRead;
XP_ASSERT( XWREG_PROTO_VERSION == *ptr++ );
XP_ASSERT( XWPDEV_PROTO_VERSION == *ptr++ );
XWRelayReg cmd = *ptr++;
switch( cmd ) {
case XWRREG_REGRSP: {
case XWPDEV_REGRSP: {
XP_U16 len;
XP_MEMCPY( &len, ptr, sizeof(len) );
len = ntohs( len );
@ -125,10 +175,20 @@ relaycon_receive( void* closure, int socket )
(*storage->procs.devIDChanged)( storage->procsClosure, devID );
}
break;
case XWRREG_MSG:
case XWPDEV_MSG:
(*storage->procs.msgReceived)( storage->procsClosure,
ptr, end - ptr );
break;
case XWPDEV_BADREG:
(*storage->procs.devIDChanged)( storage->procsClosure, NULL );
break;
case XWPDEV_HAVEMSGS: {
XP_U32 gameToken;
XP_MEMCPY( &gameToken, ptr, sizeof(gameToken) );
ptr += sizeof( gameToken );
(*storage->procs.msgNoticeReceived)( storage->procsClosure, ntohl(gameToken) );
break;
}
default:
XP_LOGF( "%s: Unexpected cmd %d", __func__, cmd );
XP_ASSERT( 0 );
@ -157,24 +217,6 @@ getStorage( LaunchParams* params )
return storage;
}
static void
addressToServer( struct sockaddr_in* to, const CommsAddrRec* addr )
{
to->sin_family = PF_INET;
to->sin_addr.s_addr = htonl( addrForHost(addr) );
to->sin_port = htons(addr->u.ip_relay.port);
}
static XP_U32
addrForHost( const CommsAddrRec* addr )
{
XP_U32 ip = addr->u.ip_relay.ipAddr;
if ( 0L == ip ) {
ip = hostNameToIP( addr->u.ip_relay.hostName );
}
return ip;
} /* addrForHost */
static XP_U32
hostNameToIP( const XP_UCHAR* name )
{
@ -191,3 +233,23 @@ hostNameToIP( const XP_UCHAR* name )
XP_LOGF( "%s found %lx for %s", __func__, ip, name );
return ip;
}
static ssize_t
sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
{
return sendto( storage->socket, msgbuf, len, 0, /* flags */
(struct sockaddr*)&storage->saddr, sizeof(storage->saddr) );
}
static size_t
addStrWithLength( XP_U8* buf, XP_U8* end, const XP_UCHAR* str )
{
XP_U16 len = XP_STRLEN( str );
if ( buf + len + sizeof(len) <= end ) {
XP_U16 lenNBO = htons( len );
XP_MEMCPY( buf, &lenNBO, sizeof(lenNBO) );
buf += sizeof(lenNBO);
XP_MEMCPY( buf, str, len );
}
return len + sizeof(len);
}

View file

@ -24,13 +24,18 @@
typedef struct _Procs {
void (*msgReceived)( void* closure, XP_U8* buf, XP_U16 len );
void (*msgNoticeReceived)( void* closure, XP_U32 gameToken );
void (*devIDChanged)( void* closure, const XP_UCHAR* devID );
} RelayConnProcs;
void relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
void* procsClosure, const char* host, int port,
const XP_UCHAR* devID, DevIDType typ );
void* procsClosure, const char* host, int port );
void relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ );
XP_S16 relaycon_send( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
XP_U32 gameID, const CommsAddrRec* addrRec );
XP_S16 relaycon_sendnoconn( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
const XP_UCHAR* relayID, XP_U32 gameToken );
void relaycon_requestMsgs( LaunchParams* params, const XP_UCHAR* devID );
void relaycon_cleanup( LaunchParams* params );
#endif