mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
Unify lookup by address and by channelNo; add method to check consistency of BT address.
This commit is contained in:
parent
b333476d48
commit
3374849111
2 changed files with 40 additions and 29 deletions
|
@ -163,13 +163,12 @@ static AddressRecord* rememberChannelAddress( CommsCtxt* comms,
|
||||||
static void updateChannelAddress( AddressRecord* rec, const CommsAddrRec* addr );
|
static void updateChannelAddress( AddressRecord* rec, const CommsAddrRec* addr );
|
||||||
static XP_Bool channelToAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
static XP_Bool channelToAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
||||||
const CommsAddrRec** addr );
|
const CommsAddrRec** addr );
|
||||||
static AddressRecord* getRecordFor( CommsCtxt* comms,
|
static AddressRecord* getRecordFor( CommsCtxt* comms, const CommsAddrRec* addr,
|
||||||
XP_PlayerAddr channelNo );
|
XP_PlayerAddr channelNo );
|
||||||
static XP_S16 sendMsg( CommsCtxt* comms, MsgQueueElem* elem );
|
static XP_S16 sendMsg( CommsCtxt* comms, MsgQueueElem* elem );
|
||||||
static void addToQueue( CommsCtxt* comms, MsgQueueElem* newMsgElem );
|
static void addToQueue( CommsCtxt* comms, MsgQueueElem* newMsgElem );
|
||||||
static XP_U16 countAddrRecs( const CommsCtxt* comms );
|
static XP_U16 countAddrRecs( const CommsCtxt* comms );
|
||||||
static void sendConnect( CommsCtxt* comms );
|
static void sendConnect( CommsCtxt* comms );
|
||||||
static AddressRecord* addrToRecord( CommsCtxt* comms, const CommsAddrRec* adr );
|
|
||||||
|
|
||||||
#ifdef XWFEATURE_RELAY
|
#ifdef XWFEATURE_RELAY
|
||||||
static void relayConnect( CommsCtxt* comms );
|
static void relayConnect( CommsCtxt* comms );
|
||||||
|
@ -611,6 +610,25 @@ comms_getInitialAddr( CommsAddrRec* addr )
|
||||||
#endif
|
#endif
|
||||||
} /* comms_getInitialAddr */
|
} /* comms_getInitialAddr */
|
||||||
|
|
||||||
|
XP_Bool
|
||||||
|
comms_checkAddr( DeviceRole role, const CommsAddrRec* addr, XW_UtilCtxt* util )
|
||||||
|
{
|
||||||
|
XP_Bool ok = XP_TRUE;
|
||||||
|
/* make sure the user's given us enough information to make a connection */
|
||||||
|
if ( role == SERVER_ISCLIENT ) {
|
||||||
|
if ( addr->conType == COMMS_CONN_BT ) {
|
||||||
|
XP_U32 empty = 0L; /* check four bytes to save some code */
|
||||||
|
if ( !XP_MEMCMP( &empty, &addr->u.bt.btAddr, sizeof(empty) ) ) {
|
||||||
|
ok = XP_FALSE;
|
||||||
|
if ( !!util ) {
|
||||||
|
util_userError( util, STR_NEED_BT_HOST_ADDR );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
} /* comms_checkAddr */
|
||||||
|
|
||||||
CommsConnType
|
CommsConnType
|
||||||
comms_getConType( const CommsCtxt* comms )
|
comms_getConType( const CommsCtxt* comms )
|
||||||
{
|
{
|
||||||
|
@ -684,7 +702,7 @@ XP_S16
|
||||||
comms_send( CommsCtxt* comms, XWStreamCtxt* stream )
|
comms_send( CommsCtxt* comms, XWStreamCtxt* stream )
|
||||||
{
|
{
|
||||||
XP_PlayerAddr channelNo = stream_getAddress( stream );
|
XP_PlayerAddr channelNo = stream_getAddress( stream );
|
||||||
AddressRecord* rec = getRecordFor( comms, channelNo );
|
AddressRecord* rec = getRecordFor( comms, NULL, channelNo );
|
||||||
MsgID msgID = (!!rec)? ++rec->nextMsgID : 0;
|
MsgID msgID = (!!rec)? ++rec->nextMsgID : 0;
|
||||||
MsgQueueElem* elem;
|
MsgQueueElem* elem;
|
||||||
XP_S16 result = -1;
|
XP_S16 result = -1;
|
||||||
|
@ -758,7 +776,7 @@ removeFromQueue( CommsCtxt* comms, XP_PlayerAddr channelNo, MsgID msgID )
|
||||||
XP_STATUSF( "%s: remove msgs <= " XP_LD " for channel %d (queueLen: %d)",
|
XP_STATUSF( "%s: remove msgs <= " XP_LD " for channel %d (queueLen: %d)",
|
||||||
__func__, msgID, channelNo, comms->queueLen );
|
__func__, msgID, channelNo, comms->queueLen );
|
||||||
|
|
||||||
if ( (channelNo == 0) || !!getRecordFor(comms, channelNo) ) {
|
if ( (channelNo == 0) || !!getRecordFor(comms, NULL, channelNo) ) {
|
||||||
MsgQueueElem dummy;
|
MsgQueueElem dummy;
|
||||||
MsgQueueElem* keep = &dummy;
|
MsgQueueElem* keep = &dummy;
|
||||||
MsgQueueElem* elem;
|
MsgQueueElem* elem;
|
||||||
|
@ -1012,17 +1030,18 @@ preProcess( CommsCtxt* comms, XWStreamCtxt* stream,
|
||||||
} /* preProcess */
|
} /* preProcess */
|
||||||
|
|
||||||
static AddressRecord*
|
static AddressRecord*
|
||||||
addrToRecord( CommsCtxt* comms, const CommsAddrRec* addr )
|
getRecordFor( CommsCtxt* comms, const CommsAddrRec* addr,
|
||||||
|
XP_PlayerAddr channelNo )
|
||||||
{
|
{
|
||||||
CommsConnType conType;
|
CommsConnType conType;
|
||||||
AddressRecord* rec;
|
AddressRecord* rec;
|
||||||
XP_Bool matched = XP_FALSE;
|
XP_Bool matched = XP_FALSE;
|
||||||
|
|
||||||
XP_ASSERT( !!addr );
|
/* Use addr if we have it. Otherwise use channelNo if non-0 */
|
||||||
|
conType = !!addr? addr->conType : COMMS_CONN_NONE;
|
||||||
|
|
||||||
conType = addr->conType;
|
|
||||||
for ( rec = comms->recs; !!rec; rec = rec->next ) {
|
for ( rec = comms->recs; !!rec; rec = rec->next ) {
|
||||||
XP_ASSERT( conType == rec->addr.conType );
|
XP_ASSERT( !addr || (conType == rec->addr.conType) );
|
||||||
switch( conType ) {
|
switch( conType ) {
|
||||||
case COMMS_CONN_RELAY:
|
case COMMS_CONN_RELAY:
|
||||||
if ( (addr->u.ip_relay.ipAddr == rec->addr.u.ip_relay.ipAddr)
|
if ( (addr->u.ip_relay.ipAddr == rec->addr.u.ip_relay.ipAddr)
|
||||||
|
@ -1044,6 +1063,9 @@ addrToRecord( CommsCtxt* comms, const CommsAddrRec* addr )
|
||||||
break;
|
break;
|
||||||
case COMMS_CONN_IR: /* no way to test */
|
case COMMS_CONN_IR: /* no way to test */
|
||||||
break;
|
break;
|
||||||
|
case COMMS_CONN_NONE:
|
||||||
|
matched = channelNo == rec->channelNo;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
XP_ASSERT(0);
|
XP_ASSERT(0);
|
||||||
break;
|
break;
|
||||||
|
@ -1079,7 +1101,7 @@ validateInitialMessage( CommsCtxt* comms, XP_Bool hasPayload,
|
||||||
{
|
{
|
||||||
#ifdef COMMS_HEARTBEAT
|
#ifdef COMMS_HEARTBEAT
|
||||||
XP_Bool addRec = XP_FALSE;
|
XP_Bool addRec = XP_FALSE;
|
||||||
AddressRecord* rec = addrToRecord( comms, addr );
|
AddressRecord* rec = getRecordFor( comms, addr, *channelNo );
|
||||||
LOG_FUNC();
|
LOG_FUNC();
|
||||||
|
|
||||||
if ( hasPayload ) {
|
if ( hasPayload ) {
|
||||||
|
@ -1112,7 +1134,7 @@ validateInitialMessage( CommsCtxt* comms, XP_Bool hasPayload,
|
||||||
LOG_RETURNF( "%lx", rec );
|
LOG_RETURNF( "%lx", rec );
|
||||||
return rec;
|
return rec;
|
||||||
#else
|
#else
|
||||||
AddressRecord* rec = addrToRecord( comms, addr );
|
AddressRecord* rec = getRecordFor( comms, addr, *channelNo );
|
||||||
if ( !!rec ) {
|
if ( !!rec ) {
|
||||||
rec = NULL; /* reject: we've already seen init message on channel */
|
rec = NULL; /* reject: we've already seen init message on channel */
|
||||||
} else {
|
} else {
|
||||||
|
@ -1140,7 +1162,7 @@ validateChannelMessage( CommsCtxt* comms, const CommsAddrRec* addr,
|
||||||
AddressRecord* rec;
|
AddressRecord* rec;
|
||||||
LOG_FUNC();
|
LOG_FUNC();
|
||||||
|
|
||||||
rec = getRecordFor( comms, channelNo );
|
rec = getRecordFor( comms, NULL, channelNo );
|
||||||
if ( !!rec ) {
|
if ( !!rec ) {
|
||||||
removeFromQueue( comms, channelNo, lastMsgRcd );
|
removeFromQueue( comms, channelNo, lastMsgRcd );
|
||||||
if ( msgID == rec->lastMsgRcd + 1 ) {
|
if ( msgID == rec->lastMsgRcd + 1 ) {
|
||||||
|
@ -1393,7 +1415,7 @@ rememberChannelAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
||||||
XWHostID hostID, const CommsAddrRec* addr )
|
XWHostID hostID, const CommsAddrRec* addr )
|
||||||
{
|
{
|
||||||
AddressRecord* recs = NULL;
|
AddressRecord* recs = NULL;
|
||||||
recs = getRecordFor( comms, channelNo );
|
recs = getRecordFor( comms, NULL, channelNo );
|
||||||
if ( !recs ) {
|
if ( !recs ) {
|
||||||
/* not found; add a new entry */
|
/* not found; add a new entry */
|
||||||
recs = (AddressRecord*)XP_MALLOC( comms->mpool, sizeof(*recs) );
|
recs = (AddressRecord*)XP_MALLOC( comms->mpool, sizeof(*recs) );
|
||||||
|
@ -1430,27 +1452,12 @@ static XP_Bool
|
||||||
channelToAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
channelToAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
|
||||||
const CommsAddrRec** addr )
|
const CommsAddrRec** addr )
|
||||||
{
|
{
|
||||||
AddressRecord* recs = getRecordFor( comms, channelNo );
|
AddressRecord* recs = getRecordFor( comms, NULL, channelNo );
|
||||||
XP_Bool found = !!recs;
|
XP_Bool found = !!recs;
|
||||||
*addr = found? &recs->addr : NULL;
|
*addr = found? &recs->addr : NULL;
|
||||||
return found;
|
return found;
|
||||||
} /* channelToAddress */
|
} /* channelToAddress */
|
||||||
|
|
||||||
static AddressRecord*
|
|
||||||
getRecordFor( CommsCtxt* comms, XP_PlayerAddr channelNo )
|
|
||||||
{
|
|
||||||
AddressRecord* recs = NULL;
|
|
||||||
|
|
||||||
if ( channelNo != CHANNEL_NONE ) {
|
|
||||||
for ( recs = comms->recs; !!recs; recs = recs->next ) {
|
|
||||||
if ( recs->channelNo == channelNo ) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return recs;
|
|
||||||
} /* getRecordFor */
|
|
||||||
|
|
||||||
static XP_U16
|
static XP_U16
|
||||||
countAddrRecs( const CommsCtxt* comms )
|
countAddrRecs( const CommsCtxt* comms )
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "comtypes.h"
|
#include "comtypes.h"
|
||||||
#include "mempool.h"
|
#include "mempool.h"
|
||||||
#include "xwrelay.h"
|
#include "xwrelay.h"
|
||||||
|
#include "server.h"
|
||||||
|
|
||||||
EXTERN_C_START
|
EXTERN_C_START
|
||||||
|
|
||||||
|
@ -103,8 +104,11 @@ void comms_destroy( CommsCtxt* comms );
|
||||||
|
|
||||||
void comms_setConnID( CommsCtxt* comms, XP_U32 connID );
|
void comms_setConnID( CommsCtxt* comms, XP_U32 connID );
|
||||||
|
|
||||||
/* "static" method provides default when no comms present */
|
/* "static" methods work when no comms present */
|
||||||
void comms_getInitialAddr( CommsAddrRec* addr );
|
void comms_getInitialAddr( CommsAddrRec* addr );
|
||||||
|
XP_Bool comms_checkAddr( DeviceRole role, const CommsAddrRec* addr,
|
||||||
|
XW_UtilCtxt* util );
|
||||||
|
|
||||||
void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr );
|
void comms_getAddr( const CommsCtxt* comms, CommsAddrRec* addr );
|
||||||
void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr );
|
void comms_setAddr( CommsCtxt* comms, const CommsAddrRec* addr );
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue