xwords/xwords4/common/comms.h
ehouse 4c8cf98d24 Add and set a compile-time option so comms will set a periodic timer
and use it to send and check for heartbeats over any transport.
Caller must supply a reset proc which is called when heartbeat hasn't
been received in too long.  No changes required to comms protocol, but
that means the heartbeat interval is fixed at compile time: can't be
negotiated, and the two ends had better agree.  Currently tested with
linux host and PalmOS guest, where only the first heartbeat failure is
recovered from.  So there's some debugging to be done still.
2007-11-18 23:43:27 +00:00

136 lines
4.4 KiB
C

/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2001 by Eric House (xwords@eehouse.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 _COMMS_H_
#define _COMMS_H_
#include "comtypes.h"
#include "mempool.h"
#include "xwrelay.h"
EXTERN_C_START
#define CHANNEL_NONE ((XP_PlayerAddr)0)
#define CONN_ID_NONE 0L
typedef XP_U32 MsgID; /* this is too big!!! PENDING */
typedef XP_U8 XWHostID;
typedef enum {
COMMS_CONN_UNUSED, /* I want errors on uninited case */
COMMS_CONN_IR,
COMMS_CONN_IP_NOUSE,
COMMS_CONN_RELAY,
COMMS_CONN_BT,
LAST_____FOO
} CommsConnType;
/* WHAT SHOULD THIS BE? Copied from Whiteboard.... PENDING */
#define XW_BT_UUID \
{ 0x83, 0xe0, 0x87, 0xae, 0x4e, 0x18, 0x46, 0xbe, \
0x83, 0xe0, 0x7b, 0x3d, 0xe6, 0xa1, 0xc3, 0x3b }
#define XW_BT_NAME "Crosswords"
/* on Palm BtLibDeviceAddressType is a 48-bit quantity. Linux's typeis the
same size. Goal is something all platforms support */
typedef struct XP_BtAddr { XP_U8 bits[6]; } XP_BtAddr;
typedef struct XP_BtAddrStr { XP_UCHAR chars[18]; } XP_BtAddrStr;
#ifdef COMMS_HEARTBEAT
# define IF_CH(a) a,
#else
# define IF_CH(a)
#endif
#define MAX_HOSTNAME_LEN 63
typedef struct CommsAddrRec {
CommsConnType conType;
union {
struct {
XP_UCHAR hostName_ip[MAX_HOSTNAME_LEN + 1];
XP_U32 ipAddr_ip; /* looked up from above */
XP_U16 port_ip;
} ip;
struct {
XP_UCHAR cookie[MAX_COOKIE_LEN + 1];
XP_UCHAR hostName[MAX_HOSTNAME_LEN + 1];
XP_U32 ipAddr; /* looked up from above */
XP_U16 port;
} ip_relay;
struct {
/* nothing? */
XP_UCHAR foo; /* wince doesn't like nothing here */
} ir;
struct {
/* guests can browse for the host to connect to */
XP_UCHAR hostName[MAX_HOSTNAME_LEN + 1];
XP_BtAddr btAddr;
} bt;
} u;
} CommsAddrRec;
typedef XP_S16 (*TransportSend)( const XP_U8* buf, XP_U16 len,
const CommsAddrRec* addr,
void* closure );
typedef void (*TransportReset)( void* closure );
CommsCtxt* comms_make( MPFORMAL XW_UtilCtxt* util,
XP_Bool isServer,
XP_U16 nPlayersHere, XP_U16 nPlayersTotal,
TransportSend sendproc, IF_CH(TransportReset resetproc)
void* closure );
void comms_reset( CommsCtxt* comms, XP_Bool isServer,
XP_U16 nPlayersHere, XP_U16 nPlayersTotal );
void comms_destroy( CommsCtxt* comms );
void comms_setConnID( CommsCtxt* comms, XP_U32 connID );
/* "static" method provides default when no comms present */
void comms_getInitialAddr( CommsAddrRec* addr );
void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr );
void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr );
CommsConnType comms_getConType( const CommsCtxt* comms );
XP_Bool comms_getIsServer( const CommsCtxt* comms );
CommsCtxt* comms_makeFromStream( MPFORMAL XWStreamCtxt* stream,
XW_UtilCtxt* util, TransportSend sendproc,
IF_CH(TransportReset resetproc)
void* closure );
void comms_start( CommsCtxt* comms );
void comms_writeToStream( const CommsCtxt* comms, XWStreamCtxt* stream );
XP_S16 comms_send( CommsCtxt* comms, XWStreamCtxt* stream );
XP_S16 comms_resendAll( CommsCtxt* comms );
XP_Bool comms_checkIncomingStream( CommsCtxt* comms, XWStreamCtxt* stream,
const CommsAddrRec* addr );
# ifdef DEBUG
void comms_getStats( CommsCtxt* comms, XWStreamCtxt* stream );
# endif
EXTERN_C_END
#endif /* _COMMS_H_ */