snapshot toward implementing fake-sms as per-device rather than per-game

This commit is contained in:
Eric House 2013-12-11 07:54:26 -08:00
parent a4f274a1f4
commit 7cffa27a88
7 changed files with 195 additions and 7 deletions

View file

@ -2141,7 +2141,7 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
# ifdef XWFEATURE_SMS
} else if ( params->conType == COMMS_CONN_SMS ) {
addr.conType = COMMS_CONN_SMS;
XP_STRNCPY( addr.u.sms.phone, params->connInfo.sms.serverPhone,
XP_STRNCPY( addr.u.sms.phone, params->connInfo.sms.phone,
sizeof(addr.u.sms.phone) - 1 );
addr.u.sms.port = params->connInfo.sms.port;
# endif

View file

@ -532,7 +532,7 @@ createOrLoadObjects( GtkGameGlobals* globals )
#endif
#ifdef XWFEATURE_SMS
} else if ( addr.conType == COMMS_CONN_SMS ) {
XP_STRNCPY( addr.u.sms.phone, params->connInfo.sms.serverPhone,
XP_STRNCPY( addr.u.sms.phone, params->connInfo.sms.phone,
sizeof(addr.u.sms.phone) - 1 );
addr.u.sms.port = params->connInfo.sms.port;
#endif

View file

@ -26,6 +26,7 @@
#include "gtkboard.h"
#include "linuxmain.h"
#include "relaycon.h"
#include "linuxsms.h"
#include "gtkask.h"
static void onNewData( GtkAppGlobals* apg, sqlite3_int64 rowid,
@ -607,6 +608,14 @@ gtkmain( LaunchParams* params )
linux_doInitialReg( params, idIsNew );
}
#ifdef XWFEATURE_SMS
SMSProcs smsProcs = {
.socketChanged = gtkSocketChanged,
};
linux_sms2_init( params, &smsProcs, &apg );
#endif
apg.window = makeGamesWindow( &apg );
gtk_main();

View file

@ -1816,7 +1816,7 @@ initFromParams( CommonGlobals* cGlobals, LaunchParams* params )
#ifdef XWFEATURE_SMS
} else if ( params->conType == COMMS_CONN_SMS ) {
addr->conType = COMMS_CONN_SMS;
XP_STRNCPY( addr->u.sms.phone, params->connInfo.sms.serverPhone,
XP_STRNCPY( addr->u.sms.phone, params->connInfo.sms.phone,
sizeof(addr->u.sms.phone) - 1 );
addr->u.sms.port = params->connInfo.sms.port;
#endif
@ -1920,7 +1920,7 @@ main( int argc, char** argv )
CommsConnType conType = COMMS_CONN_NONE;
#ifdef XWFEATURE_SMS
char* serverPhone = NULL;
char* phone = NULL;
#endif
#ifdef XWFEATURE_BLUETOOTH
const char* btaddr = NULL;
@ -2119,7 +2119,7 @@ main( int argc, char** argv )
#ifdef XWFEATURE_SMS
case CMD_SMSNUMBER: /* SMS phone number */
XP_ASSERT( COMMS_CONN_NONE == conType );
serverPhone = optarg;
phone = optarg;
conType = COMMS_CONN_SMS;
break;
#endif
@ -2420,7 +2420,7 @@ main( int argc, char** argv )
#endif
#ifdef XWFEATURE_SMS
} else if ( conType == COMMS_CONN_SMS ) {
mainParams.connInfo.sms.serverPhone = serverPhone;
mainParams.connInfo.sms.phone = phone;
if ( !portNum ) {
portNum = "1";
}

View file

@ -264,4 +264,165 @@ linux_sms_receive( CommonGlobals* globals, int sock,
return nRead;
} /* linux_sms_receive */
typedef struct _LinSMS2Data {
int fd, wd;
XP_UCHAR myQueue[256];
XP_U16 port;
FILE* lock;
const SMSProcs* procs;
void* procClosure;
} LinSMS2Data;
static LinSMS2Data* getStorage( LaunchParams* params );
static void
lock_queue2( LinSMS2Data* data )
{
char lock[256];
snprintf( lock, sizeof(lock), "%s/%s", data->myQueue, LOCK_FILE );
FILE* fp = fopen( lock, "w" );
XP_ASSERT( NULL == data->lock );
data->lock = fp;
}
static void
unlock_queue2( LinSMS2Data* data )
{
XP_ASSERT( NULL != data->lock );
fclose( data->lock );
data->lock = NULL;
}
static XP_S16
decodeAndDelete2( LinSMS2Data* data, const gchar* name,
XP_U8* buf, XP_U16 buflen )
{
XP_S16 nRead = -1;
char path[256];
snprintf( path, sizeof(path), "%s/%s", data->myQueue, name );
gchar* contents;
gsize length;
#ifdef DEBUG
gboolean success =
#endif
g_file_get_contents( path, &contents, &length, NULL );
XP_ASSERT( success );
unlink( path );
if ( 0 == strncmp( "from: ", contents, 6 ) ) {
char phone[32];
gchar* eol = strstr( contents, "\n" );
*eol = '\0';
XP_STRNCPY( phone, &contents[6], sizeof(phone) );
XP_ASSERT( !*eol );
++eol; /* skip NULL */
*strstr(eol, "\n" ) = '\0';
XP_U16 inlen = strlen(eol); /* skip \n */
XP_U8 out[inlen];
XP_U16 outlen = sizeof(out);
XP_Bool valid = smsToBin( out, &outlen, eol, inlen );
if ( valid && outlen <= buflen ) {
XP_MEMCPY( buf, out, outlen );
nRead = outlen;
/* addr->conType = COMMS_CONN_SMS; */
/* addr->u.sms.port = data->port; */
}
}
g_free( contents );
return nRead;
} /* decodeAndDelete */
static void
parseAndDispatch( LinSMS2Data* data, uint8_t* buf, int len )
{
XP_USE( data );
XP_USE( buf );
XP_USE( len );
}
static void
sms2_receive( void* closure, int socket )
{
LaunchParams* params = (LaunchParams*)closure;
XP_ASSERT( !!params->sms2Storage );
LinSMS2Data* data = getStorage( params );
XP_S16 nRead = -1;
XP_ASSERT( socket == data->fd );
lock_queue2( data );
/* read required or we'll just get the event again. But we don't care
about the result or the buffer contents. */
XP_U8 buffer[sizeof(struct inotify_event) + 16];
if ( 0 > read( socket, buffer, sizeof(buffer) ) ) {
}
char shortest[256] = { '\0' };
GDir* dir = g_dir_open( data->myQueue, 0, NULL );
for ( ; ; ) {
const gchar* name = g_dir_read_name( dir );
if ( NULL == name ) {
break;
} else if ( 0 == strcmp( name, LOCK_FILE ) ) {
continue;
}
if ( !shortest[0] || 0 < strcmp( shortest, name ) ) {
snprintf( shortest, sizeof(shortest), "%s", name );
}
}
g_dir_close( dir );
uint8_t buf[256];
if ( !!shortest[0] ) {
nRead = decodeAndDelete2( data, shortest, buf, sizeof(buf) );
}
unlock_queue2( data );
if ( 0 < nRead ) {
parseAndDispatch( data, buf, nRead );
}
}
void
linux_sms2_init( LaunchParams* params, const SMSProcs* procs,
void* procClosure )
{
XP_ASSERT( !!params->connInfo.sms.phone );
LinSMS2Data* data = getStorage( params );
XP_ASSERT( !!data );
data->procs = procs;
data->procClosure = procClosure;
makeQueuePath( params->connInfo.sms.phone, params->connInfo.sms.port,
data->myQueue, sizeof(data->myQueue) );
data->port = params->connInfo.sms.port;
(void)g_mkdir_with_parents( data->myQueue, 0777 );
int fd = inotify_init();
data->fd = fd;
data->wd = inotify_add_watch( fd, data->myQueue, IN_MODIFY );
(*procs->socketChanged)( procClosure, fd, -1, sms2_receive, params );
} /* linux_sms2_init */
static LinSMS2Data*
getStorage( LaunchParams* params )
{
LinSMS2Data* storage = (LinSMS2Data*)params->sms2Storage;
if ( NULL == storage ) {
storage = XP_CALLOC( params->mpool, sizeof(*storage) );
params->sms2Storage = storage;
}
return storage;
}
#endif /* XWFEATURE_SMS */

View file

@ -32,5 +32,20 @@ XP_S16 linux_sms_send( CommonGlobals* globals, const XP_U8* buf,
XP_S16 linux_sms_receive( CommonGlobals* globals, int sock,
XP_U8* buf, XP_U16 buflen, CommsAddrRec* addr );
typedef struct _SMSProcs {
void (*msgReceived)( void* closure, const XP_U8* buf, XP_U16 len );
void (*msgNoticeReceived)( void* closure );
void (*devIDReceived)( void* closure, const XP_UCHAR* devID,
XP_U16 maxInterval );
void (*msgErrorMsg)( void* closure, const XP_UCHAR* msg );
void (*socketChanged)( void* closure, int newSock, int oldSock,
SockReceiver proc, void* procClosure );
} SMSProcs;
void linux_sms2_init( LaunchParams* params, const SMSProcs* procs,
void* procClosure );
#endif /* XWFEATURE_SMS */
#endif /* #ifndef _LINUXSMS_H_ */

View file

@ -63,6 +63,9 @@ typedef struct LaunchParams {
XP_U32 dbFileID;
#endif
void* relayConStorage; /* opaque outside of relaycon.c */
#ifdef XWFEATURE_SMS
void* sms2Storage;
#endif
char* pipe;
char* nbs;
char* bonusFile;
@ -139,7 +142,7 @@ typedef struct LaunchParams {
#endif
#ifdef XWFEATURE_SMS
struct {
const char* serverPhone;
const char* phone;
int port;
} sms;
#endif