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 ); int result = sqlite3_prepare_v2( pDb, buf, -1, &ppStmt, NULL );
XP_ASSERT( SQLITE_OK == result ); XP_ASSERT( SQLITE_OK == result );
result = sqlite3_step( ppStmt ); 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 ); const void* ptr = sqlite3_column_blob( ppStmt, 0 );
int size = sqlite3_column_bytes( ppStmt, 0 ); int size = sqlite3_column_bytes( ppStmt, 0 );
stream_putBytes( stream, ptr, size ); stream_putBytes( stream, ptr, size );
}
sqlite3_finalize( ppStmt ); sqlite3_finalize( ppStmt );
return XP_TRUE; return success;
} }
void void
@ -230,7 +232,7 @@ deleteGame( sqlite3* pDb, sqlite3_int64 rowid )
} }
void void
store( sqlite3* pDb, const gchar* key, const gchar* value ) db_store( sqlite3* pDb, const gchar* key, const gchar* value )
{ {
char buf[256]; char buf[256];
snprintf( buf, sizeof(buf), snprintf( buf, sizeof(buf),
@ -245,7 +247,7 @@ store( sqlite3* pDb, const gchar* key, const gchar* value )
} }
void 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]; char query[256];
snprintf( query, sizeof(query), snprintf( query, sizeof(query),
@ -262,6 +264,19 @@ fetch( sqlite3* pDb, const gchar* key, gchar* buf, gint buflen )
sqlite3_finalize( ppStmt ); 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 static void
getColumnText( sqlite3_stmt *ppStmt, int iCol, XP_UCHAR* buf, int len ) 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" #define KEY_RDEVID "RDEVID"
void store( sqlite3* dbp, const gchar* key, const gchar* value ); void db_store( sqlite3* dbp, const gchar* key, const gchar* value );
void fetch( sqlite3* dbp, const gchar* key, gchar* buf, gint buflen ); void db_fetch( sqlite3* dbp, const gchar* key, gchar* buf, gint buflen );
void db_remove( sqlite3* dbp, const gchar* key );
#endif #endif

View file

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

View file

@ -30,6 +30,7 @@
static void onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid, static void onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid,
XP_Bool isNew ); XP_Bool isNew );
static void updateButtons( GtkAppGlobals* apg ); static void updateButtons( GtkAppGlobals* apg );
static void sendRelayReg( GtkAppGlobals* apg );
static void static void
recordOpened( GtkAppGlobals* apg, GtkGameGlobals* globals ) 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 static void
gtkDevIDChanged( void* closure, const XP_UCHAR* devID ) gtkDevIDChanged( void* closure, const XP_UCHAR* devID )
{ {
GtkAppGlobals* apg = (GtkAppGlobals*)closure; GtkAppGlobals* apg = (GtkAppGlobals*)closure;
if ( !!devID ) {
XP_LOGF( "%s(devID=%s)", __func__, 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 void
@ -445,6 +474,27 @@ handle_sigintterm( int XP_UNUSED(sig) )
handle_destroy( NULL, g_globals_for_signal ); 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 int
gtkmain( LaunchParams* params ) gtkmain( LaunchParams* params )
{ {
@ -463,28 +513,14 @@ gtkmain( LaunchParams* params )
RelayConnProcs procs = { RelayConnProcs procs = {
.msgReceived = gtkGotBuf, .msgReceived = gtkGotBuf,
.msgNoticeReceived = gtkNoticeRcvd,
.devIDChanged = gtkDevIDChanged, .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, relaycon_init( params, &procs, &apg,
params->connInfo.relay.relayName, params->connInfo.relay.relayName,
params->connInfo.relay.defaultSendPort, params->connInfo.relay.defaultSendPort );
devID, typ ); sendRelayReg( &apg );
(void)makeGamesWindow( &apg ); (void)makeGamesWindow( &apg );
gtk_main(); gtk_main();

View file

@ -27,18 +27,18 @@ typedef struct _RelayConStorage {
int socket; int socket;
RelayConnProcs procs; RelayConnProcs procs;
void* procsClosure; void* procsClosure;
struct sockaddr_in saddr;
} RelayConStorage; } RelayConStorage;
static RelayConStorage* getStorage( LaunchParams* params ); 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 XP_U32 hostNameToIP( const XP_UCHAR* name );
static void relaycon_receive( void* closure, int socket ); 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 void
relaycon_init( LaunchParams* params, const RelayConnProcs* procs, relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
void* procsClosure, const char* host, int port, void* procsClosure, const char* host, int port )
const XP_UCHAR* devID, DevIDType typ )
{ {
XP_ASSERT( !params->relayConStorage ); XP_ASSERT( !params->relayConStorage );
RelayConStorage* storage = getStorage( params ); RelayConStorage* storage = getStorage( params );
@ -49,50 +49,100 @@ relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
(*params->socketChanged)( params->socketChangedClosure, storage->socket, -1, (*params->socketChanged)( params->socketChangedClosure, storage->socket, -1,
relaycon_receive, params ); 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_ASSERT( !!devID );
XP_U16 idLen = XP_STRLEN( devID ); XP_U16 idLen = XP_STRLEN( devID );
XP_U16 lenNBO = XP_HTONS( idLen ); XP_U16 lenNBO = XP_HTONS( idLen );
XP_U8 tmpbuf[1 + 1 + 1 + sizeof(lenNBO) + idLen]; tmpbuf[indx++] = XWPDEV_PROTO_VERSION;
tmpbuf[0] = XWREG_PROTO_VERSION; tmpbuf[indx++] = XWPDEV_REG;
tmpbuf[1] = XWRREG_REG; tmpbuf[indx++] = typ;
tmpbuf[2] = typ; XP_MEMCPY( &tmpbuf[indx], &lenNBO, sizeof(lenNBO) );
XP_MEMCPY( &tmpbuf[3], &lenNBO, sizeof(lenNBO) ); indx += sizeof(lenNBO);
XP_MEMCPY( &tmpbuf[5], devID, idLen ); XP_MEMCPY( &tmpbuf[indx], devID, idLen );
indx += idLen;
struct sockaddr_in to = {0}; sendIt( storage, tmpbuf, indx );
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) );
} }
XP_S16 XP_S16
relaycon_send( LaunchParams* params, const XP_U8* buf, XP_U16 buflen, 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; ssize_t nSent = -1;
RelayConStorage* storage = getStorage( params ); RelayConStorage* storage = getStorage( params );
struct sockaddr_in to = {0};
addressToServer( &to, addrRec );
XP_U8 tmpbuf[1 + 1 + sizeof(gameToken) + buflen]; XP_U8 tmpbuf[1 + 1 + sizeof(gameToken) + buflen];
tmpbuf[0] = XWREG_PROTO_VERSION; tmpbuf[0] = XWPDEV_PROTO_VERSION;
tmpbuf[1] = XWRREG_MSG; tmpbuf[1] = XWPDEV_MSG;
XP_U32 inNBO = htonl(gameToken); XP_U32 inNBO = htonl(gameToken);
XP_MEMCPY( &tmpbuf[2], &inNBO, sizeof(inNBO) ); XP_MEMCPY( &tmpbuf[2], &inNBO, sizeof(inNBO) );
XP_MEMCPY( &tmpbuf[1 + 1 + sizeof(gameToken)], buf, buflen ); XP_MEMCPY( &tmpbuf[1 + 1 + sizeof(gameToken)], buf, buflen );
nSent = sendto( storage->socket, tmpbuf, sizeof(tmpbuf), 0, /* flags */ nSent = sendIt( storage, tmpbuf, sizeof(tmpbuf) );
(struct sockaddr*)&to, sizeof(to) ); if ( nSent > buflen ) {
if ( 1 + 1 + sizeof(gameToken) < nSent ) { nSent = buflen;
nSent -= 1 + 1 + sizeof(gameToken);
} }
LOG_RETURNF( "%d", nSent ); LOG_RETURNF( "%d", nSent );
return 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 static void
relaycon_receive( void* closure, int socket ) relaycon_receive( void* closure, int socket )
{ {
@ -111,10 +161,10 @@ relaycon_receive( void* closure, int socket )
if ( 0 <= nRead ) { if ( 0 <= nRead ) {
XP_U8* ptr = buf; XP_U8* ptr = buf;
const XP_U8* end = buf + nRead; const XP_U8* end = buf + nRead;
XP_ASSERT( XWREG_PROTO_VERSION == *ptr++ ); XP_ASSERT( XWPDEV_PROTO_VERSION == *ptr++ );
XWRelayReg cmd = *ptr++; XWRelayReg cmd = *ptr++;
switch( cmd ) { switch( cmd ) {
case XWRREG_REGRSP: { case XWPDEV_REGRSP: {
XP_U16 len; XP_U16 len;
XP_MEMCPY( &len, ptr, sizeof(len) ); XP_MEMCPY( &len, ptr, sizeof(len) );
len = ntohs( len ); len = ntohs( len );
@ -125,10 +175,20 @@ relaycon_receive( void* closure, int socket )
(*storage->procs.devIDChanged)( storage->procsClosure, devID ); (*storage->procs.devIDChanged)( storage->procsClosure, devID );
} }
break; break;
case XWRREG_MSG: case XWPDEV_MSG:
(*storage->procs.msgReceived)( storage->procsClosure, (*storage->procs.msgReceived)( storage->procsClosure,
ptr, end - ptr ); ptr, end - ptr );
break; 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: default:
XP_LOGF( "%s: Unexpected cmd %d", __func__, cmd ); XP_LOGF( "%s: Unexpected cmd %d", __func__, cmd );
XP_ASSERT( 0 ); XP_ASSERT( 0 );
@ -157,24 +217,6 @@ getStorage( LaunchParams* params )
return storage; 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 static XP_U32
hostNameToIP( const XP_UCHAR* name ) hostNameToIP( const XP_UCHAR* name )
{ {
@ -191,3 +233,23 @@ hostNameToIP( const XP_UCHAR* name )
XP_LOGF( "%s found %lx for %s", __func__, ip, name ); XP_LOGF( "%s found %lx for %s", __func__, ip, name );
return ip; 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 { typedef struct _Procs {
void (*msgReceived)( void* closure, XP_U8* buf, XP_U16 len ); 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 ); void (*devIDChanged)( void* closure, const XP_UCHAR* devID );
} RelayConnProcs; } RelayConnProcs;
void relaycon_init( LaunchParams* params, const RelayConnProcs* procs, void relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
void* procsClosure, const char* host, int port, void* procsClosure, const char* host, int port );
const XP_UCHAR* devID, DevIDType typ ); 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_S16 relaycon_send( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
XP_U32 gameID, const CommsAddrRec* addrRec ); 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 ); void relaycon_cleanup( LaunchParams* params );
#endif #endif