first checked in.

This commit is contained in:
ehouse 2005-03-19 22:16:49 +00:00
parent 73104819bd
commit dc25eb9e35
4 changed files with 228 additions and 0 deletions

119
xwords4/relay/ctrl.cpp Normal file
View file

@ -0,0 +1,119 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2005 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <netdb.h> /* gethostbyname */
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <sys/select.h>
#include <stdarg.h>
#include <sys/time.h>
#include "ctrl.h"
#include "cref.h"
#include "xwrelay_priv.h"
static void
print_sock( int sock, const char* what, ... )
{
char buf[256];
va_list ap;
va_start( ap, what );
vsnprintf( buf, sizeof(buf) - 1, what, ap );
va_end(ap);
strncat( buf, "\n", sizeof(buf) );
send( sock, buf, strlen(buf), 0 );
}
static void
print_help( int socket )
{
char* help =
"Welcome to the console\n"
"Commands are:\n"
"? : prints this message\n"
"q : quits\n"
"cook : lists active cookies\n"
;
print_sock( socket, help );
} /* print_help */
static void
print_cookies( int socket )
{
print_sock( socket, "******************************" );
CookieMapIterator iter = CookieRef::GetCookieNameIterator();
const char* str;
for ( str = iter.Next(); str != NULL; str = iter.Next() ) {
print_sock( socket, str );
}
print_sock( socket, "******************************" );
}
static int
handle_command( const char* buf, int sock )
{
if ( 0 == strcmp( buf, "?" ) ) {
print_help( sock );
} else if ( 0 == strcmp( buf, "cook" ) ) {
print_cookies( sock );
} else if ( 0 == strcmp( buf, "q" ) ) {
return 0;
} else {
print_sock( sock, "unknown command: \"%s\"", buf );
print_help( sock );
}
return 1;
}
void*
ctrl_thread_main( void* arg )
{
ThreadData* localStorage = (ThreadData*)arg;
for ( ; ; ) {
char buf[512];
ssize_t nGot = recv( localStorage->socket, buf, sizeof(buf)-1, 0 );
if ( nGot <= 1 ) { /* break when just \n comes in */
break;
}
buf[nGot-2] = '\0'; /* kill \r\n stuff */
if ( !handle_command( buf, localStorage->socket ) ) {
break;
}
}
close ( localStorage->socket );
delete localStorage;
}

29
xwords4/relay/ctrl.h Normal file
View file

@ -0,0 +1,29 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2005 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _CTRL_H_
#define _CTRL_H_
void* ctrl_thread_main( void* arg );
#endif

63
xwords4/relay/xwrelay.h Normal file
View file

@ -0,0 +1,63 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2005 by Eric House (fixin@peak.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _XWRELAY_H_
#define _XWRELAY_H_
/* This file is meant to be included by both linux code that doesn't
include xptypes and from Crosswords client code.*/
/* Set if device is acting a server; cleared if as client */
#define FLAGS_SERVER_BIT 0x01
enum { XWRELAY_NONE /* 0 is an illegal value */
, XWRELAY_CONNECT
/* Sent from device to relay to establish connection to relay. Format:
flags: 1; cookieLen: 1; cookie: <cookieLen>; hostID:
2. connectionID: 2. If connectionID is not 0, host may be
attempting to reconnect to an existing game. */
, XWRELAY_CONNECTRESP
/* Sent from relay to device in response to XWRELAY_CONNECT.
Format: heartbeat_seconds: 2; connectionID: 2; */
, XWRELAY_HEARTBEAT
/* Sent in either direction. Format: cookieID: 2; srcID: 2 */
, XWRELAY_MSG_FROMRELAY
/* Sent from relay to device. Format: cookieID: 2; src_hostID: 2;
dest_hostID: 2; data <len-headerLen> */
, XWRELAY_MSG_TORELAY
/* Sent from device to relay. Format: connectionID: 2; src_hostID:
2; dest_hostID: 2 */
};
#ifndef CANT_DO_TYPEDEF
typedef unsigned char XWRELAY_Cmd;
#endif
#define HOST_ID_NONE 0
#define HOST_ID_SERVER 1
#define MAX_COOKIE_LEN 15
#define MAX_MSG_LEN 256 /* 100 is more like it */
#endif

View file

@ -0,0 +1,17 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
#ifndef _XWRELAY_PRIV_H_
#define _XWRELAY_PRIV_H_
typedef unsigned short HostID;
typedef unsigned short CookieID; /* stands in for string after connection established */
typedef struct ThreadData {
int socket;
} ThreadData;
void logf( const char* format, ... );
#endif