2015-07-22 06:16:29 -07:00
|
|
|
/* -*- compile-command: "cd ../linux && make MEMDEBUG=TRUE -j3"; -*- */
|
2003-11-01 05:35:29 +00:00
|
|
|
/*
|
2014-09-25 20:05:37 -07:00
|
|
|
* Copyright 2001 - 2014 by Eric House (xwords@eehouse.org). All rights
|
2009-02-01 15:50:58 +00:00
|
|
|
* reserved.
|
2003-11-01 05:35:29 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2005-03-19 21:46:51 +00:00
|
|
|
#include "xwrelay.h"
|
2008-01-07 01:10:43 +00:00
|
|
|
#include "server.h"
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2005-01-31 03:31:50 +00:00
|
|
|
EXTERN_C_START
|
|
|
|
|
2003-11-01 05:35:29 +00:00
|
|
|
#define CHANNEL_NONE ((XP_PlayerAddr)0)
|
|
|
|
#define CONN_ID_NONE 0L
|
|
|
|
|
2005-10-02 15:39:38 +00:00
|
|
|
typedef XP_U32 MsgID; /* this is too big!!! PENDING */
|
2017-11-10 21:34:02 -08:00
|
|
|
typedef XP_U8 XWHostID;
|
2003-11-01 05:35:29 +00:00
|
|
|
|
|
|
|
typedef enum {
|
2007-12-09 01:59:15 +00:00
|
|
|
COMMS_CONN_NONE /* I want errors on uninited case */
|
|
|
|
,COMMS_CONN_IR
|
|
|
|
,COMMS_CONN_IP_DIRECT
|
|
|
|
,COMMS_CONN_RELAY
|
|
|
|
,COMMS_CONN_BT
|
2008-12-29 01:35:29 +00:00
|
|
|
,COMMS_CONN_SMS
|
2016-11-14 08:06:53 -08:00
|
|
|
,COMMS_CONN_P2P /* a.k.a. Wifi direct */
|
2019-11-23 10:53:51 -08:00
|
|
|
,COMMS_CONN_NFC
|
2009-02-28 19:27:45 +00:00
|
|
|
|
|
|
|
,COMMS_CONN_NTYPES
|
2003-11-01 05:35:29 +00:00
|
|
|
} CommsConnType;
|
|
|
|
|
2014-10-15 07:26:18 -07:00
|
|
|
typedef XP_U8 CommsConnTypes;
|
|
|
|
|
2009-09-12 21:39:13 +00:00
|
|
|
typedef enum {
|
|
|
|
COMMS_RELAYSTATE_UNCONNECTED
|
2009-12-04 08:14:03 +00:00
|
|
|
, COMMS_RELAYSTATE_DENIED /* terminal; new game or reset required to
|
|
|
|
fix */
|
2009-09-12 21:39:13 +00:00
|
|
|
, COMMS_RELAYSTATE_CONNECT_PENDING
|
|
|
|
, COMMS_RELAYSTATE_CONNECTED
|
2009-11-08 21:30:13 +00:00
|
|
|
, COMMS_RELAYSTATE_RECONNECTED
|
2009-09-12 21:39:13 +00:00
|
|
|
, COMMS_RELAYSTATE_ALLCONNECTED
|
2017-11-10 21:34:02 -08:00
|
|
|
#ifdef RELAY_VIA_HTTP
|
|
|
|
, COMMS_RELAYSTATE_USING_HTTP /* connection state doesn't matter */
|
|
|
|
#endif
|
2009-09-12 21:39:13 +00:00
|
|
|
} CommsRelayState;
|
|
|
|
|
2012-02-27 20:30:48 -08:00
|
|
|
#ifdef XWFEATURE_BLUETOOTH
|
2019-03-08 21:32:52 -08:00
|
|
|
# define XW_BT_NAME "CrossWords"
|
2012-02-27 20:30:48 -08:00
|
|
|
#endif
|
2007-11-03 22:04:06 +00:00
|
|
|
|
2006-08-26 21:12:10 +00:00
|
|
|
/* on Palm BtLibDeviceAddressType is a 48-bit quantity. Linux's typeis the
|
|
|
|
same size. Goal is something all platforms support */
|
2006-09-14 01:25:40 +00:00
|
|
|
typedef struct XP_BtAddr { XP_U8 bits[6]; } XP_BtAddr;
|
|
|
|
typedef struct XP_BtAddrStr { XP_UCHAR chars[18]; } XP_BtAddrStr;
|
2006-08-26 21:12:10 +00:00
|
|
|
|
2007-11-18 23:43:27 +00:00
|
|
|
#ifdef COMMS_HEARTBEAT
|
|
|
|
# define IF_CH(a) a,
|
|
|
|
#else
|
|
|
|
# define IF_CH(a)
|
|
|
|
#endif
|
|
|
|
|
2005-01-31 03:31:50 +00:00
|
|
|
#define MAX_HOSTNAME_LEN 63
|
2008-12-29 01:35:29 +00:00
|
|
|
#define MAX_PHONE_LEN 31
|
2016-11-14 08:06:53 -08:00
|
|
|
#define MAX_P2P_MAC_LEN 17
|
2008-12-29 01:35:29 +00:00
|
|
|
|
2012-02-06 06:27:53 -08:00
|
|
|
typedef struct _CommsAddrRec {
|
2014-09-25 20:05:37 -07:00
|
|
|
XP_U16 _conTypes;
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2014-09-25 20:05:37 -07:00
|
|
|
struct {
|
2003-11-01 05:35:29 +00:00
|
|
|
struct {
|
2005-03-19 21:46:51 +00:00
|
|
|
XP_UCHAR hostName_ip[MAX_HOSTNAME_LEN + 1];
|
|
|
|
XP_U32 ipAddr_ip; /* looked up from above */
|
|
|
|
XP_U16 port_ip;
|
|
|
|
} ip;
|
|
|
|
struct {
|
2017-11-10 21:34:02 -08:00
|
|
|
XP_UCHAR invite[MAX_INVITE_LEN + 1]; /* room!!!! */
|
2005-01-31 03:31:50 +00:00
|
|
|
XP_UCHAR hostName[MAX_HOSTNAME_LEN + 1];
|
|
|
|
XP_U32 ipAddr; /* looked up from above */
|
|
|
|
XP_U16 port;
|
2010-09-03 06:38:46 -07:00
|
|
|
XP_Bool seeksPublicRoom;
|
|
|
|
XP_Bool advertiseRoom;
|
2005-03-19 21:46:51 +00:00
|
|
|
} ip_relay;
|
2003-11-01 05:35:29 +00:00
|
|
|
struct {
|
|
|
|
/* nothing? */
|
|
|
|
XP_UCHAR foo; /* wince doesn't like nothing here */
|
|
|
|
} ir;
|
2005-03-19 21:46:51 +00:00
|
|
|
struct {
|
2006-08-26 21:12:10 +00:00
|
|
|
/* guests can browse for the host to connect to */
|
|
|
|
XP_UCHAR hostName[MAX_HOSTNAME_LEN + 1];
|
2012-01-31 18:43:26 -08:00
|
|
|
XP_BtAddrStr btAddr;
|
2005-03-19 21:46:51 +00:00
|
|
|
} bt;
|
2008-12-29 01:35:29 +00:00
|
|
|
struct {
|
|
|
|
XP_UCHAR phone[MAX_PHONE_LEN + 1];
|
|
|
|
XP_U16 port;
|
|
|
|
} sms;
|
2016-11-14 08:06:53 -08:00
|
|
|
struct {
|
|
|
|
XP_UCHAR mac_addr[MAX_P2P_MAC_LEN + 1];
|
|
|
|
} p2p;
|
2003-11-01 05:35:29 +00:00
|
|
|
} u;
|
|
|
|
} CommsAddrRec;
|
|
|
|
|
2006-08-23 04:44:55 +00:00
|
|
|
typedef XP_S16 (*TransportSend)( const XP_U8* buf, XP_U16 len,
|
2015-09-30 06:50:04 -07:00
|
|
|
const XP_UCHAR* msgNo,
|
2005-07-30 01:48:17 +00:00
|
|
|
const CommsAddrRec* addr,
|
2014-10-15 07:26:18 -07:00
|
|
|
CommsConnType conType,
|
2012-01-25 18:27:37 -08:00
|
|
|
XP_U32 gameID, void* closure );
|
2009-09-12 21:39:13 +00:00
|
|
|
#ifdef COMMS_HEARTBEAT
|
2007-11-18 23:43:27 +00:00
|
|
|
typedef void (*TransportReset)( void* closure );
|
2009-09-12 21:39:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XWFEATURE_RELAY
|
|
|
|
typedef void (*RelayStatusProc)( void* closure, CommsRelayState newState );
|
2010-10-28 18:45:55 -07:00
|
|
|
typedef void (*RelayConndProc)( void* closure, XP_UCHAR* const room,
|
2011-05-19 05:51:00 -07:00
|
|
|
XP_Bool reconnect,
|
2010-10-29 06:31:33 -07:00
|
|
|
XP_U16 devOrder, /* 1 means created room, etc. */
|
2011-05-19 05:51:00 -07:00
|
|
|
XP_Bool allHere, XP_U16 nMissing );
|
2009-12-04 08:14:03 +00:00
|
|
|
typedef void (*RelayErrorProc)( void* closure, XWREASON relayErr );
|
2015-09-30 06:50:04 -07:00
|
|
|
typedef XP_Bool (*RelayNoConnProc)( const XP_U8* buf, XP_U16 len,
|
|
|
|
const XP_UCHAR* msgNo,
|
2011-08-10 18:28:34 -07:00
|
|
|
const XP_UCHAR* relayID, void* closure );
|
2017-11-10 21:34:02 -08:00
|
|
|
# ifdef RELAY_VIA_HTTP
|
|
|
|
typedef void (*RelayRequestJoinProc)( void* closure, const XP_UCHAR* devID,
|
|
|
|
const XP_UCHAR* room, XP_U16 nPlayersHere,
|
|
|
|
XP_U16 nPlayersTotal, XP_U16 seed,
|
|
|
|
XP_U16 lang );
|
|
|
|
# endif
|
2009-09-12 21:39:13 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-10 14:42:54 -08:00
|
|
|
typedef void (*MsgCountChange)( void* closure, XP_U16 msgCount );
|
|
|
|
|
2011-08-16 19:36:23 -07:00
|
|
|
typedef enum {
|
|
|
|
COMMS_XPORT_FLAGS_NONE = 0
|
|
|
|
,COMMS_XPORT_FLAGS_HASNOCONN = 1
|
|
|
|
} CommsTransportFlags;
|
|
|
|
|
|
|
|
#ifdef COMMS_XPORT_FLAGSPROC
|
|
|
|
typedef XP_U32 (*FlagsProc)( void* closure );
|
|
|
|
#endif
|
|
|
|
|
2009-09-12 21:39:13 +00:00
|
|
|
typedef struct _TransportProcs {
|
2011-08-16 19:36:23 -07:00
|
|
|
# ifdef COMMS_XPORT_FLAGSPROC
|
|
|
|
FlagsProc getFlags;
|
|
|
|
#else
|
|
|
|
XP_U32 flags;
|
|
|
|
#endif
|
2009-09-12 21:39:13 +00:00
|
|
|
TransportSend send;
|
|
|
|
#ifdef COMMS_HEARTBEAT
|
|
|
|
TransportReset reset;
|
|
|
|
#endif
|
|
|
|
#ifdef XWFEATURE_RELAY
|
|
|
|
RelayStatusProc rstatus;
|
2009-12-14 04:03:17 +00:00
|
|
|
RelayConndProc rconnd;
|
2009-12-04 08:14:03 +00:00
|
|
|
RelayErrorProc rerror;
|
2011-08-10 18:28:34 -07:00
|
|
|
RelayNoConnProc sendNoConn;
|
2017-11-10 21:34:02 -08:00
|
|
|
# ifdef RELAY_VIA_HTTP
|
|
|
|
RelayRequestJoinProc requestJoin;
|
|
|
|
# endif
|
2019-12-10 14:42:54 -08:00
|
|
|
MsgCountChange countChanged;
|
2009-09-12 21:39:13 +00:00
|
|
|
#endif
|
|
|
|
void* closure;
|
|
|
|
} TransportProcs;
|
2003-11-01 05:35:29 +00:00
|
|
|
|
|
|
|
CommsCtxt* comms_make( MPFORMAL XW_UtilCtxt* util,
|
2005-10-02 15:39:38 +00:00
|
|
|
XP_Bool isServer,
|
|
|
|
XP_U16 nPlayersHere, XP_U16 nPlayersTotal,
|
2014-12-21 17:40:00 -08:00
|
|
|
const TransportProcs* procs, XP_U16 forceChannel
|
2011-07-31 22:23:46 -07:00
|
|
|
#ifdef SET_GAMESEED
|
|
|
|
,XP_U16 gameSeed
|
|
|
|
#endif
|
|
|
|
);
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2005-10-02 15:39:38 +00:00
|
|
|
void comms_reset( CommsCtxt* comms, XP_Bool isServer,
|
|
|
|
XP_U16 nPlayersHere, XP_U16 nPlayersTotal );
|
2010-06-05 03:39:16 +00:00
|
|
|
void comms_resetSame( CommsCtxt* comms );
|
2014-11-20 07:12:25 -08:00
|
|
|
void comms_transportFailed( CommsCtxt* comms, CommsConnType failed );
|
2009-02-01 15:50:58 +00:00
|
|
|
|
2003-11-01 05:35:29 +00:00
|
|
|
void comms_destroy( CommsCtxt* comms );
|
|
|
|
|
|
|
|
void comms_setConnID( CommsCtxt* comms, XP_U32 connID );
|
|
|
|
|
2008-01-07 01:10:43 +00:00
|
|
|
/* "static" methods work when no comms present */
|
2010-03-21 03:12:58 +00:00
|
|
|
void comms_getInitialAddr( CommsAddrRec* addr
|
|
|
|
#ifdef XWFEATURE_RELAY
|
|
|
|
, const XP_UCHAR* relayName
|
2010-03-25 04:50:19 +00:00
|
|
|
, XP_U16 relayPort
|
2010-03-21 03:12:58 +00:00
|
|
|
#endif
|
|
|
|
);
|
2008-01-07 01:10:43 +00:00
|
|
|
XP_Bool comms_checkAddr( DeviceRole role, const CommsAddrRec* addr,
|
|
|
|
XW_UtilCtxt* util );
|
|
|
|
|
2006-10-15 13:53:17 +00:00
|
|
|
void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr );
|
2020-02-24 21:10:49 +01:00
|
|
|
void comms_augmentHostAddr( CommsCtxt* comms, const CommsAddrRec* addr );
|
2012-04-18 23:23:36 -07:00
|
|
|
void comms_getAddrs( const CommsCtxt* comms, CommsAddrRec addr[],
|
|
|
|
XP_U16* nRecs );
|
2015-07-06 20:28:16 -07:00
|
|
|
XP_Bool comms_formatRelayID( const CommsCtxt* comms, XP_U16 indx,
|
|
|
|
XP_UCHAR* buf, XP_U16* lenp );
|
|
|
|
|
2014-02-28 18:59:12 -08:00
|
|
|
XP_U16 comms_countPendingPackets( const CommsCtxt* comms );
|
|
|
|
|
2005-03-19 21:46:51 +00:00
|
|
|
|
2010-08-22 12:16:57 -07:00
|
|
|
#ifdef XWFEATURE_RELAY
|
2010-10-11 06:20:30 -07:00
|
|
|
XP_Bool comms_getRelayID( const CommsCtxt* comms, XP_UCHAR* buf, XP_U16* len );
|
2010-08-22 12:16:57 -07:00
|
|
|
#endif
|
|
|
|
|
2014-10-15 07:26:18 -07:00
|
|
|
CommsConnTypes comms_getConTypes( const CommsCtxt* comms );
|
2020-03-14 18:52:28 -07:00
|
|
|
void comms_dropHostAddr( CommsCtxt* comms, CommsConnType typ );
|
2006-10-15 13:53:17 +00:00
|
|
|
XP_Bool comms_getIsServer( const CommsCtxt* comms );
|
2005-06-27 05:45:28 +00:00
|
|
|
|
2003-11-01 05:35:29 +00:00
|
|
|
CommsCtxt* comms_makeFromStream( MPFORMAL XWStreamCtxt* stream,
|
2011-07-31 22:23:46 -07:00
|
|
|
XW_UtilCtxt* util,
|
2014-12-29 07:39:28 -08:00
|
|
|
const TransportProcs* procs,
|
|
|
|
XP_U16 forceChannel );
|
2005-07-06 00:58:20 +00:00
|
|
|
void comms_start( CommsCtxt* comms );
|
2013-01-16 06:31:09 -08:00
|
|
|
void comms_stop( CommsCtxt* comms );
|
2012-09-10 07:31:45 -07:00
|
|
|
void comms_writeToStream( CommsCtxt* comms, XWStreamCtxt* stream,
|
|
|
|
XP_U16 saveToken );
|
|
|
|
void comms_saveSucceeded( CommsCtxt* comms, XP_U16 saveToken );
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2014-11-02 17:24:37 -08:00
|
|
|
void addrFromStream( CommsAddrRec* addr, XWStreamCtxt* stream );
|
|
|
|
void addrToStream( XWStreamCtxt* stream, const CommsAddrRec* addr );
|
|
|
|
|
2005-03-19 21:46:51 +00:00
|
|
|
XP_S16 comms_send( CommsCtxt* comms, XWStreamCtxt* stream );
|
2016-02-02 07:01:48 -08:00
|
|
|
XP_S16 comms_resendAll( CommsCtxt* comms, CommsConnType filter, XP_Bool force );
|
2019-11-18 09:26:46 -08:00
|
|
|
|
|
|
|
typedef void (*PendingMsgProc)( void* closure, XP_U8* msg, XP_U16 len, MsgID msgID );
|
|
|
|
void comms_getPending( CommsCtxt* comms, PendingMsgProc proc, void* closure );
|
2010-11-08 17:22:38 -08:00
|
|
|
XP_U16 comms_getChannelSeed( CommsCtxt* comms );
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2012-04-13 20:21:59 -07:00
|
|
|
#ifdef XWFEATURE_COMMSACK
|
|
|
|
void comms_ackAny( CommsCtxt* comms );
|
|
|
|
#endif
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2015-02-07 13:24:09 -08:00
|
|
|
typedef struct _CommsMsgState {
|
|
|
|
struct AddressRecord* rec;
|
2019-11-03 08:35:56 +00:00
|
|
|
XP_U32 msgID;
|
|
|
|
XP_PlayerAddr channelNo;
|
2015-02-08 10:37:54 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
const CommsCtxt* comms;
|
|
|
|
#endif
|
2015-02-07 13:24:09 -08:00
|
|
|
} CommsMsgState;
|
2003-11-01 05:35:29 +00:00
|
|
|
|
2015-02-07 13:24:09 -08:00
|
|
|
XP_Bool comms_checkIncomingStream( CommsCtxt* comms, XWStreamCtxt* stream,
|
|
|
|
const CommsAddrRec* addr,
|
|
|
|
CommsMsgState* state );
|
|
|
|
void comms_msgProcessed( CommsCtxt* comms, CommsMsgState* state,
|
|
|
|
XP_Bool rejected );
|
2010-10-21 19:14:10 -07:00
|
|
|
XP_Bool comms_checkComplete( const CommsAddrRec* const addr );
|
|
|
|
|
|
|
|
XP_Bool comms_canChat( const CommsCtxt* comms );
|
2010-10-27 20:50:11 -07:00
|
|
|
XP_Bool comms_isConnected( const CommsCtxt* const comms );
|
2009-09-05 13:08:46 +00:00
|
|
|
|
2017-11-10 21:34:02 -08:00
|
|
|
#ifdef RELAY_VIA_HTTP
|
|
|
|
void comms_gameJoined( CommsCtxt* comms, const XP_UCHAR* connname, XWHostID hid );
|
|
|
|
#endif
|
|
|
|
|
2014-09-25 20:05:37 -07:00
|
|
|
CommsConnType addr_getType( const CommsAddrRec* addr );
|
|
|
|
void addr_setType( CommsAddrRec* addr, CommsConnType type );
|
2014-10-15 07:26:18 -07:00
|
|
|
void addr_addType( CommsAddrRec* addr, CommsConnType type );
|
2015-07-01 07:10:45 -07:00
|
|
|
void types_addType( XP_U16* conTypes, CommsConnType type );
|
2014-10-28 08:03:19 -07:00
|
|
|
void addr_rmType( CommsAddrRec* addr, CommsConnType type );
|
2014-10-15 07:26:18 -07:00
|
|
|
XP_Bool addr_hasType( const CommsAddrRec* addr, CommsConnType type );
|
2015-07-01 07:10:45 -07:00
|
|
|
XP_Bool types_hasType( XP_U16 conTypes, CommsConnType type );
|
2014-10-15 07:26:18 -07:00
|
|
|
XP_Bool addr_iter( const CommsAddrRec* addr, CommsConnType* typp,
|
|
|
|
XP_U32* state );
|
2015-07-01 07:10:45 -07:00
|
|
|
XP_Bool types_iter( XP_U32 conTypes, CommsConnType* typp, XP_U32* state );
|
2014-09-25 20:05:37 -07:00
|
|
|
|
2003-11-01 05:35:29 +00:00
|
|
|
# ifdef DEBUG
|
|
|
|
void comms_getStats( CommsCtxt* comms, XWStreamCtxt* stream );
|
2009-03-06 13:43:03 +00:00
|
|
|
const char* ConnType2Str( CommsConnType typ );
|
2009-09-12 21:39:13 +00:00
|
|
|
const char* CommsRelayState2Str( CommsRelayState state );
|
2010-11-18 22:27:06 -08:00
|
|
|
const char* XWREASON2Str( XWREASON reason );
|
2014-11-06 06:35:28 -08:00
|
|
|
|
|
|
|
void comms_setAddrDisabled( CommsCtxt* comms, CommsConnType typ,
|
|
|
|
XP_Bool send, XP_Bool enabled );
|
|
|
|
XP_Bool comms_getAddrDisabled( const CommsCtxt* comms, CommsConnType typ,
|
|
|
|
XP_Bool send );
|
|
|
|
# else
|
2015-02-07 12:54:46 -08:00
|
|
|
# define comms_setAddrDisabled( comms, typ, send, enabled )
|
2014-11-06 06:35:28 -08:00
|
|
|
# define comms_getAddrDisabled( comms, typ, send ) XP_FALSE
|
2003-11-01 05:35:29 +00:00
|
|
|
# endif
|
|
|
|
|
2005-01-31 03:31:50 +00:00
|
|
|
EXTERN_C_END
|
|
|
|
|
2003-11-01 05:35:29 +00:00
|
|
|
#endif /* _COMMS_H_ */
|