2005-03-19 23:14:27 +01:00
|
|
|
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
|
|
|
|
2005-03-25 03:59:44 +01:00
|
|
|
/*
|
2009-07-30 14:54:17 +02:00
|
|
|
* Copyright 2005-2009 by Eric House (xwords@eehouse.org). All rights
|
|
|
|
* reserved.
|
2005-03-25 03:59:44 +01: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.
|
|
|
|
*/
|
|
|
|
|
2005-03-19 23:14:27 +01:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <assert.h>
|
2005-03-25 03:59:44 +01:00
|
|
|
#include <pthread.h>
|
2005-07-06 00:05:37 +02:00
|
|
|
#include <netinet/in.h>
|
2005-10-30 06:20:31 +01:00
|
|
|
#include <stdarg.h>
|
2006-03-21 05:05:33 +01:00
|
|
|
#include <stdio.h>
|
2008-12-30 06:13:30 +01:00
|
|
|
#include <string.h>
|
2009-07-28 07:15:26 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2005-03-19 23:14:27 +01:00
|
|
|
|
|
|
|
#include "cref.h"
|
|
|
|
#include "xwrelay.h"
|
2005-03-25 03:59:44 +01:00
|
|
|
#include "mlock.h"
|
2005-04-20 14:13:20 +02:00
|
|
|
#include "tpool.h"
|
2005-07-06 00:05:37 +02:00
|
|
|
#include "states.h"
|
2005-09-02 08:56:34 +02:00
|
|
|
#include "timermgr.h"
|
|
|
|
#include "configs.h"
|
|
|
|
#include "crefmgr.h"
|
2009-09-26 16:37:49 +02:00
|
|
|
#include "permid.h"
|
2005-03-19 23:14:27 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2005-04-20 14:13:20 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* SocketsIterator class
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
SocketsIterator::SocketsIterator( SocketMap::iterator iter,
|
|
|
|
SocketMap::iterator end,
|
2009-07-31 14:43:04 +02:00
|
|
|
pthread_mutex_t* mutex )
|
2005-04-20 14:13:20 +02:00
|
|
|
: m_iter( iter )
|
2005-09-02 08:56:34 +02:00
|
|
|
, m_end( end )
|
2009-07-31 14:43:04 +02:00
|
|
|
, m_mutex( mutex )
|
2005-09-02 08:56:34 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketsIterator::~SocketsIterator()
|
2005-04-20 14:13:20 +02:00
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
pthread_mutex_unlock( m_mutex );
|
2005-04-20 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SocketsIterator::Next()
|
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
int socket = 0;
|
|
|
|
if ( m_iter != m_end ) {
|
|
|
|
socket = m_iter->first;
|
|
|
|
++m_iter;
|
|
|
|
}
|
2005-04-20 14:13:20 +02:00
|
|
|
return socket;
|
2005-03-19 23:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* CookieRef class
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
#define ASSERT_LOCKED() \
|
|
|
|
assert( m_locking_thread == pthread_self() )
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::ReInit( const char* cookie, const char* connName, CookieID id )
|
2005-03-19 23:14:27 +01:00
|
|
|
{
|
2009-07-28 07:15:26 +02:00
|
|
|
m_cookie = cookie==NULL?"":cookie;
|
2009-08-21 14:00:09 +02:00
|
|
|
m_connName = connName==NULL?"":connName;
|
2009-07-28 07:15:26 +02:00
|
|
|
m_cookieID = id;
|
|
|
|
m_totalSent = 0;
|
|
|
|
m_curState = XWS_INITED;
|
|
|
|
m_nextHostID = HOST_ID_SERVER;
|
2009-08-13 14:59:55 +02:00
|
|
|
m_nPlayersSought = 0;
|
2009-07-28 07:15:26 +02:00
|
|
|
m_nPlayersHere = 0;
|
2009-07-31 14:43:04 +02:00
|
|
|
m_locking_thread = 0;
|
2009-07-28 07:41:15 +02:00
|
|
|
m_starttime = uptime();
|
2009-09-14 03:55:03 +02:00
|
|
|
m_gameFull = false;
|
2009-11-08 22:35:39 +01:00
|
|
|
m_nHostMsgs = 0;
|
2009-11-09 06:30:42 +01:00
|
|
|
m_in_handleEvents = false;
|
2009-07-28 07:15:26 +02:00
|
|
|
|
2009-03-02 02:50:14 +01:00
|
|
|
RelayConfigs::GetConfigs()->GetValueFor( "HEARTBEAT", &m_heatbeat );
|
2009-07-28 07:15:26 +02:00
|
|
|
logf( XW_LOGINFO, "initing cref for cookie %s, connName %s",
|
2005-10-30 06:20:31 +01:00
|
|
|
m_cookie.c_str(), m_connName.c_str() );
|
2005-03-19 23:14:27 +01:00
|
|
|
}
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
|
|
|
|
CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id )
|
|
|
|
{
|
|
|
|
pthread_mutex_init( &m_mutex, NULL );
|
|
|
|
ReInit( cookie, connName, id );
|
|
|
|
}
|
|
|
|
|
2005-03-19 23:14:27 +01:00
|
|
|
CookieRef::~CookieRef()
|
|
|
|
{
|
2005-09-02 08:56:34 +02:00
|
|
|
cancelAllConnectedTimer();
|
|
|
|
|
2005-04-20 14:13:20 +02:00
|
|
|
/* get rid of any sockets still contained */
|
|
|
|
XWThreadPool* tPool = XWThreadPool::GetTPool();
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
int socket = iter->m_socket;
|
2005-04-20 14:13:20 +02:00
|
|
|
tPool->CloseSocket( socket );
|
2005-09-05 17:50:49 +02:00
|
|
|
m_sockets.erase( iter );
|
2005-04-20 14:13:20 +02:00
|
|
|
}
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
logf( XW_LOGINFO,
|
|
|
|
"CookieRef for %d being deleted; sent %d bytes over lifetime",
|
2005-10-01 18:33:45 +02:00
|
|
|
m_cookieID, m_totalSent );
|
2005-09-02 08:56:34 +02:00
|
|
|
} /* ~CookieRef */
|
2005-03-19 23:14:27 +01:00
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
void
|
|
|
|
CookieRef::Clear(void)
|
|
|
|
{
|
|
|
|
m_cookie = "";
|
|
|
|
m_connName = "";
|
|
|
|
m_cookieID = 0;
|
|
|
|
m_eventQueue.clear();
|
2009-11-08 22:35:39 +01:00
|
|
|
|
|
|
|
if ( 0 < m_nHostMsgs ) {
|
|
|
|
unsigned int ii;
|
|
|
|
for ( ii = 0; ii < sizeof(m_hostMsgQueues)/sizeof(m_hostMsgQueues[0]);++ii){
|
|
|
|
m_hostMsgQueues[ii].clear();
|
|
|
|
}
|
|
|
|
/* m_nHostMsgs will get cleared in ReInit */
|
|
|
|
}
|
|
|
|
} /* Clear */
|
2009-07-28 07:15:26 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
CookieRef::Lock( void )
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
pthread_mutex_lock( &m_mutex );
|
|
|
|
|
|
|
|
/* We get here possibly after having been blocked on the mutex for a
|
|
|
|
while. This cref may no longer be live. If it's not, unlock and
|
|
|
|
return. */
|
|
|
|
|
|
|
|
assert( m_locking_thread == 0 );
|
|
|
|
m_locking_thread = pthread_self();
|
|
|
|
|
|
|
|
if ( notInUse() ) {
|
2009-07-31 14:43:04 +02:00
|
|
|
logf( XW_LOGINFO, "%s: not locking %p because not in use", __func__,
|
|
|
|
this );
|
2009-07-28 07:15:26 +02:00
|
|
|
success = false;
|
2009-07-31 14:43:04 +02:00
|
|
|
m_locking_thread = 0;
|
2009-07-28 07:15:26 +02:00
|
|
|
pthread_mutex_unlock( &m_mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
} /* CookieRef::Lock */
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::Unlock() {
|
|
|
|
assert( m_locking_thread == pthread_self() );
|
2009-07-31 14:43:04 +02:00
|
|
|
m_locking_thread = 0;
|
2009-07-28 07:15:26 +02:00
|
|
|
pthread_mutex_unlock( &m_mutex );
|
|
|
|
}
|
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
bool
|
2009-11-01 02:38:03 +01:00
|
|
|
CookieRef::_Connect( int socket, HostID hid, int nPlayersH, int nPlayersS,
|
2009-09-26 16:37:49 +02:00
|
|
|
int seed )
|
2005-03-19 23:14:27 +01:00
|
|
|
{
|
2009-12-04 09:03:27 +01:00
|
|
|
bool connected = false;
|
2009-02-01 17:00:20 +01:00
|
|
|
if ( CRefMgr::Get()->Associate( socket, this ) ) {
|
2009-09-14 03:55:03 +02:00
|
|
|
if ( hid == HOST_ID_NONE ) {
|
2009-09-17 05:42:12 +02:00
|
|
|
logf( XW_LOGINFO, "%s: Waiting to assign host id", __func__ );
|
2009-09-14 03:55:03 +02:00
|
|
|
} else {
|
|
|
|
logf( XW_LOGINFO, "NOT assigned host id; why?" );
|
|
|
|
}
|
2009-11-01 02:38:03 +01:00
|
|
|
pushConnectEvent( socket, hid, nPlayersH, nPlayersS, seed );
|
2009-02-01 17:00:20 +01:00
|
|
|
handleEvents();
|
2009-12-04 09:03:27 +01:00
|
|
|
connected = HasSocket_locked( socket );
|
2009-02-01 17:00:20 +01:00
|
|
|
} else {
|
|
|
|
logf( XW_LOGINFO, "dropping connect event; already connected" );
|
2005-10-01 18:33:45 +02:00
|
|
|
}
|
2009-12-04 09:03:27 +01:00
|
|
|
return connected;
|
2005-03-19 23:14:27 +01:00
|
|
|
}
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
2009-11-01 02:38:03 +01:00
|
|
|
CookieRef::_Reconnect( int socket, HostID hid, int nPlayersH, int nPlayersS,
|
2009-09-26 16:37:49 +02:00
|
|
|
int seed )
|
2005-09-02 08:56:34 +02:00
|
|
|
{
|
2009-02-01 17:00:20 +01:00
|
|
|
(void)CRefMgr::Get()->Associate( socket, this );
|
2009-11-01 02:38:03 +01:00
|
|
|
pushReconnectEvent( socket, hid, nPlayersH, nPlayersS, seed );
|
2005-09-02 08:56:34 +02:00
|
|
|
handleEvents();
|
|
|
|
}
|
|
|
|
|
2005-09-05 17:50:49 +02:00
|
|
|
void
|
|
|
|
CookieRef::_Disconnect( int socket, HostID hostID )
|
|
|
|
{
|
2009-02-01 17:00:20 +01:00
|
|
|
logf( XW_LOGINFO, "%s(socket=%d, hostID=%d)", __func__, socket, hostID );
|
2005-09-05 17:50:49 +02:00
|
|
|
CRefMgr::Get()->Disassociate( socket, this );
|
|
|
|
|
|
|
|
CRefEvent evt;
|
2009-12-04 09:03:27 +01:00
|
|
|
evt.type = XWE_DISCONN;
|
2005-09-05 17:50:49 +02:00
|
|
|
evt.u.discon.socket = socket;
|
|
|
|
evt.u.discon.srcID = hostID;
|
|
|
|
m_eventQueue.push_back( evt );
|
|
|
|
|
|
|
|
handleEvents();
|
|
|
|
}
|
|
|
|
|
2005-10-30 06:20:31 +01:00
|
|
|
void
|
|
|
|
CookieRef::_Shutdown()
|
|
|
|
{
|
|
|
|
CRefEvent evt;
|
|
|
|
evt.type = XWE_SHUTDOWN;
|
|
|
|
m_eventQueue.push_back( evt );
|
|
|
|
|
|
|
|
handleEvents();
|
|
|
|
} /* _Shutdown */
|
|
|
|
|
2005-03-19 23:14:27 +01:00
|
|
|
int
|
|
|
|
CookieRef::SocketForHost( HostID dest )
|
|
|
|
{
|
2009-08-21 14:00:09 +02:00
|
|
|
int socket = -1;
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::const_iterator iter;
|
2009-09-26 16:37:49 +02:00
|
|
|
assert( dest != 0 ); /* don't use as lookup before assigned */
|
2009-08-21 14:00:09 +02:00
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_hostID == dest ) {
|
|
|
|
socket = iter->m_socket;
|
|
|
|
break;
|
|
|
|
}
|
2005-03-19 23:14:27 +01:00
|
|
|
}
|
2009-08-21 14:00:09 +02:00
|
|
|
|
2005-10-30 06:20:31 +01:00
|
|
|
logf( XW_LOGVERBOSE0, "returning socket=%d for hostid=%x", socket, dest );
|
2005-03-19 23:14:27 +01:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2005-09-05 17:50:49 +02:00
|
|
|
/* The idea here is: have we never seen the XW_ST_ALLCONNECTED state. This
|
|
|
|
needs to include any states reachable from XW_ST_ALLCONNECTED from which
|
|
|
|
recovery back to XW_ST_ALLCONNECTED is possible. This is used to decide
|
2009-09-26 16:37:49 +02:00
|
|
|
whether to admit a connection based on its cookie -- whether that coookie
|
2005-09-05 17:50:49 +02:00
|
|
|
should join an existing cref or get a new one? */
|
2008-12-30 06:13:30 +01:00
|
|
|
bool
|
2005-09-05 17:50:49 +02:00
|
|
|
CookieRef::NeverFullyConnected()
|
|
|
|
{
|
2009-07-29 06:25:21 +02:00
|
|
|
return m_curState != XWS_ALLCONND
|
2005-10-02 18:08:42 +02:00
|
|
|
&& m_curState != XWS_MISSING;
|
2005-09-05 17:50:49 +02:00
|
|
|
}
|
|
|
|
|
2008-12-30 06:13:30 +01:00
|
|
|
bool
|
2009-12-04 09:03:27 +01:00
|
|
|
CookieRef::GameOpen( const char* cookie, bool isNew,
|
2009-09-26 16:37:49 +02:00
|
|
|
bool* alreadyHere )
|
2005-10-02 17:39:38 +02:00
|
|
|
{
|
2008-12-30 06:13:30 +01:00
|
|
|
bool accept = false;
|
2009-09-26 16:37:49 +02:00
|
|
|
*alreadyHere = false;
|
2005-10-02 17:39:38 +02:00
|
|
|
/* First, do we have room. Second, are we missing this guy? */
|
|
|
|
|
2009-09-14 03:55:03 +02:00
|
|
|
if ( isNew && m_gameFull ) {
|
|
|
|
/* do nothing; reject */
|
|
|
|
logf( XW_LOGINFO, "reject: game for %s is full", cookie );
|
|
|
|
} else if ( m_curState != XWS_INITED
|
2009-12-04 09:03:27 +01:00
|
|
|
&& m_curState != XWS_WAITGUESTS
|
2005-10-02 18:08:42 +02:00
|
|
|
&& m_curState != XWS_MISSING ) {
|
2005-10-02 17:39:38 +02:00
|
|
|
/* do nothing; reject */
|
2005-10-02 18:08:42 +02:00
|
|
|
logf( XW_LOGINFO, "reject: bad state %s", stateString(m_curState) );
|
2005-10-02 17:39:38 +02:00
|
|
|
} else {
|
2009-11-02 02:01:47 +01:00
|
|
|
accept = true;
|
2005-10-02 17:39:38 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:00:09 +02:00
|
|
|
/* Error to connect if cookie doesn't match. */
|
|
|
|
if ( accept && !!cookie && 0 != strcmp( cookie, Cookie() ) ) {
|
|
|
|
logf( XW_LOGERROR, "%s: not accepting b/c cookie mismatch: %s vs %s",
|
|
|
|
__func__, cookie, Cookie() );
|
|
|
|
accept = false;
|
|
|
|
}
|
|
|
|
|
2005-10-02 17:39:38 +02:00
|
|
|
return accept;
|
2009-09-14 03:55:03 +02:00
|
|
|
} /* GameOpen */
|
2005-10-01 18:33:45 +02:00
|
|
|
|
2005-09-03 08:55:08 +02:00
|
|
|
void
|
|
|
|
CookieRef::notifyDisconn( const CRefEvent* evt )
|
|
|
|
{
|
|
|
|
int socket = evt->u.disnote.socket;
|
2005-09-03 17:41:17 +02:00
|
|
|
unsigned char buf[] = {
|
|
|
|
XWRELAY_DISCONNECT_YOU,
|
|
|
|
evt->u.disnote.why
|
|
|
|
};
|
2005-09-03 08:55:08 +02:00
|
|
|
|
2009-07-06 03:50:51 +02:00
|
|
|
send_with_length( socket, buf, sizeof(buf), true );
|
2005-09-03 08:55:08 +02:00
|
|
|
} /* notifyDisconn */
|
|
|
|
|
2005-03-19 23:14:27 +01:00
|
|
|
void
|
2005-09-05 17:50:49 +02:00
|
|
|
CookieRef::removeSocket( int socket )
|
2005-03-19 23:14:27 +01:00
|
|
|
{
|
2009-07-06 03:50:51 +02:00
|
|
|
logf( XW_LOGINFO, "%s(socket=%d)", __func__, socket );
|
2005-04-20 14:13:20 +02:00
|
|
|
int count;
|
|
|
|
{
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2005-04-20 14:13:20 +02:00
|
|
|
|
2005-09-05 17:50:49 +02:00
|
|
|
count = m_sockets.size();
|
2009-09-26 16:37:49 +02:00
|
|
|
if ( count > 0 ) {
|
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_socket == socket ) {
|
|
|
|
m_sockets.erase(iter);
|
|
|
|
--count;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-20 14:13:20 +02:00
|
|
|
}
|
2009-09-26 16:37:49 +02:00
|
|
|
} else {
|
|
|
|
logf( XW_LOGERROR, "%s: no socket %d to remove", __func__, socket );
|
2005-03-19 23:14:27 +01:00
|
|
|
}
|
|
|
|
}
|
2005-03-25 03:59:44 +01:00
|
|
|
|
|
|
|
if ( count == 0 ) {
|
2005-09-02 08:56:34 +02:00
|
|
|
pushLastSocketGoneEvent();
|
2005-03-25 03:59:44 +01:00
|
|
|
}
|
2005-10-30 06:20:31 +01:00
|
|
|
} /* removeSocket */
|
2005-09-02 08:56:34 +02:00
|
|
|
|
2008-12-30 06:13:30 +01:00
|
|
|
bool
|
2005-09-02 08:56:34 +02:00
|
|
|
CookieRef::HasSocket( int socket )
|
2009-07-28 07:15:26 +02:00
|
|
|
{
|
|
|
|
bool result = Lock();
|
|
|
|
if ( result ) {
|
|
|
|
result = HasSocket_locked( socket );
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CookieRef::HasSocket_locked( int socket )
|
2005-09-02 08:56:34 +02:00
|
|
|
{
|
2008-12-30 06:13:30 +01:00
|
|
|
bool found = false;
|
2005-09-02 08:56:34 +02:00
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_socket == socket ) {
|
2008-12-30 06:13:30 +01:00
|
|
|
found = true;
|
2005-09-02 08:56:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-28 07:15:26 +02:00
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
return found;
|
2009-07-28 07:15:26 +02:00
|
|
|
} /* HasSocket_locked */
|
2005-03-19 23:14:27 +01:00
|
|
|
|
2007-11-19 00:38:56 +01:00
|
|
|
#ifdef RELAY_HEARTBEAT
|
2005-06-23 06:26:44 +02:00
|
|
|
void
|
2005-09-02 08:56:34 +02:00
|
|
|
CookieRef::_HandleHeartbeat( HostID id, int socket )
|
2005-06-23 06:26:44 +02:00
|
|
|
{
|
2005-07-06 00:05:37 +02:00
|
|
|
pushHeartbeatEvent( id, socket );
|
|
|
|
handleEvents();
|
|
|
|
} /* HandleHeartbeat */
|
|
|
|
|
|
|
|
void
|
2005-09-03 08:55:08 +02:00
|
|
|
CookieRef::_CheckHeartbeats( time_t now )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
2009-08-21 14:00:09 +02:00
|
|
|
logf( XW_LOGINFO, "%s", __func__ );
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
time_t last = iter->m_lastHeartbeat;
|
2005-10-30 06:20:31 +01:00
|
|
|
if ( (now - last) > GetHeartbeat() ) {
|
2009-08-21 14:00:09 +02:00
|
|
|
pushHeartFailedEvent( iter->m_socket );
|
2005-09-03 08:55:08 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-30 06:20:31 +01:00
|
|
|
|
2005-07-06 00:05:37 +02:00
|
|
|
handleEvents();
|
|
|
|
} /* CheckHeartbeats */
|
2007-11-19 00:38:56 +01:00
|
|
|
#endif
|
2005-07-06 00:05:37 +02:00
|
|
|
|
|
|
|
void
|
2005-09-02 08:56:34 +02:00
|
|
|
CookieRef::_Forward( HostID src, HostID dest, unsigned char* buf, int buflen )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
|
|
|
pushForwardEvent( src, dest, buf, buflen );
|
|
|
|
handleEvents();
|
|
|
|
} /* Forward */
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
|
|
|
CookieRef::_Remove( int socket )
|
|
|
|
{
|
|
|
|
pushRemoveSocketEvent( socket );
|
|
|
|
handleEvents();
|
|
|
|
} /* Forward */
|
|
|
|
|
2005-07-06 00:05:37 +02:00
|
|
|
void
|
2009-08-21 14:00:09 +02:00
|
|
|
CookieRef::pushConnectEvent( int socket, HostID srcID,
|
2009-11-01 02:38:03 +01:00
|
|
|
int nPlayersH, int nPlayersS,
|
2009-09-26 16:37:49 +02:00
|
|
|
int seed )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2009-12-04 09:03:27 +01:00
|
|
|
evt.type = nPlayersS == 0? XWE_GUESTCONNECT : XWE_HOSTCONNECT;
|
2005-07-06 00:05:37 +02:00
|
|
|
evt.u.con.socket = socket;
|
|
|
|
evt.u.con.srcID = srcID;
|
2005-10-02 17:39:38 +02:00
|
|
|
evt.u.con.nPlayersH = nPlayersH;
|
2009-11-01 02:38:03 +01:00
|
|
|
evt.u.con.nPlayersS = nPlayersS;
|
2009-09-26 16:37:49 +02:00
|
|
|
evt.u.con.seed = seed;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
2005-07-06 00:05:37 +02:00
|
|
|
} /* pushConnectEvent */
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
2009-09-26 16:37:49 +02:00
|
|
|
CookieRef::pushReconnectEvent( int socket, HostID srcID, int nPlayersH,
|
2009-11-01 02:38:03 +01:00
|
|
|
int nPlayersS, int seed )
|
2005-09-02 08:56:34 +02:00
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2009-12-04 09:03:27 +01:00
|
|
|
evt.type = XWE_RECONNECT;
|
2005-09-03 20:35:34 +02:00
|
|
|
evt.u.con.socket = socket;
|
|
|
|
evt.u.con.srcID = srcID;
|
2005-10-02 17:39:38 +02:00
|
|
|
evt.u.con.nPlayersH = nPlayersH;
|
2009-11-01 02:38:03 +01:00
|
|
|
evt.u.con.nPlayersS = nPlayersS;
|
2009-09-26 16:37:49 +02:00
|
|
|
evt.u.con.seed = seed;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
2005-09-03 20:35:34 +02:00
|
|
|
} /* pushReconnectEvent */
|
2005-09-02 08:56:34 +02:00
|
|
|
|
2007-11-19 00:38:56 +01:00
|
|
|
#ifdef RELAY_HEARTBEAT
|
2005-07-06 00:05:37 +02:00
|
|
|
void
|
|
|
|
CookieRef::pushHeartbeatEvent( HostID id, int socket )
|
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_HEARTRCVD;
|
2005-07-06 00:05:37 +02:00
|
|
|
evt.u.heart.id = id;
|
|
|
|
evt.u.heart.socket = socket;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::pushHeartFailedEvent( int socket )
|
|
|
|
{
|
2009-08-21 14:00:09 +02:00
|
|
|
logf( XW_LOGINFO, "%s", __func__ );
|
2005-09-03 08:55:08 +02:00
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_HEARTFAILED;
|
2005-09-03 08:55:08 +02:00
|
|
|
evt.u.heart.socket = socket;
|
|
|
|
m_eventQueue.push_back( evt );
|
2005-07-06 00:05:37 +02:00
|
|
|
}
|
2007-11-19 00:38:56 +01:00
|
|
|
#endif
|
2005-07-06 00:05:37 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::pushForwardEvent( HostID src, HostID dest,
|
|
|
|
unsigned char* buf, int buflen )
|
|
|
|
{
|
2005-10-30 06:20:31 +01:00
|
|
|
logf( XW_LOGVERBOSE1, "pushForwardEvent: %d -> %d", src, dest );
|
2005-07-06 00:05:37 +02:00
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_FORWARDMSG;
|
2005-07-06 00:05:37 +02:00
|
|
|
evt.u.fwd.src = src;
|
|
|
|
evt.u.fwd.dest = dest;
|
|
|
|
evt.u.fwd.buf = buf;
|
|
|
|
evt.u.fwd.buflen = buflen;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
2005-07-06 00:05:37 +02:00
|
|
|
}
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
|
|
|
CookieRef::pushRemoveSocketEvent( int socket )
|
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_REMOVESOCKET;
|
2005-09-02 08:56:34 +02:00
|
|
|
evt.u.rmsock.socket = socket;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::pushNotifyDisconEvent( int socket, XWREASON why )
|
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_NOTIFYDISCON;
|
2005-09-03 08:55:08 +02:00
|
|
|
evt.u.disnote.socket = socket;
|
|
|
|
evt.u.disnote.why = why;
|
|
|
|
m_eventQueue.push_back( evt );
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::pushLastSocketGoneEvent()
|
|
|
|
{
|
|
|
|
CRefEvent evt;
|
2005-10-02 18:08:42 +02:00
|
|
|
evt.type = XWE_NOMORESOCKETS;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( evt );
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
|
|
|
|
2009-12-14 05:10:23 +01:00
|
|
|
void
|
|
|
|
CookieRef::checkHaveRoom( const CRefEvent* evt )
|
|
|
|
{
|
|
|
|
CRefEvent newEvt;
|
|
|
|
|
|
|
|
assert ( m_nPlayersSought > 0 );
|
|
|
|
if ( m_nPlayersSought < m_nPlayersHere + evt->u.con.nPlayersH ) {
|
|
|
|
newEvt.type = XWE_TOO_MANY;
|
|
|
|
newEvt.u.rmsock.socket = evt->u.con.socket;
|
|
|
|
} else {
|
|
|
|
newEvt = *evt;
|
|
|
|
newEvt.type = XWE_HAVE_ROOM;
|
|
|
|
}
|
|
|
|
m_eventQueue.push_back( newEvt );
|
|
|
|
}
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
|
|
|
CookieRef::handleEvents()
|
|
|
|
{
|
2009-11-09 06:30:42 +01:00
|
|
|
assert( !m_in_handleEvents );
|
|
|
|
m_in_handleEvents = true;
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
/* Assumption: has mutex!!!! */
|
|
|
|
while ( m_eventQueue.size () > 0 ) {
|
2009-11-01 02:38:03 +01:00
|
|
|
XW_RELAY_STATE nextState;
|
2005-07-06 00:05:37 +02:00
|
|
|
CRefEvent evt = m_eventQueue.front();
|
|
|
|
m_eventQueue.pop_front();
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
XW_RELAY_ACTION takeAction;
|
2009-11-01 02:38:03 +01:00
|
|
|
if ( getFromTable( m_curState, evt.type, &takeAction, &nextState ) ) {
|
2005-07-06 00:05:37 +02:00
|
|
|
|
2007-02-06 06:52:22 +01:00
|
|
|
logf( XW_LOGINFO, "%s: %s -> %s on evt %s, act=%s", __func__,
|
2009-11-01 02:38:03 +01:00
|
|
|
stateString(m_curState), stateString(nextState),
|
2005-10-30 06:20:31 +01:00
|
|
|
eventString(evt.type), actString(takeAction) );
|
2005-07-06 00:05:37 +02:00
|
|
|
|
|
|
|
switch( takeAction ) {
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-12-14 05:10:23 +01:00
|
|
|
case XWA_CHECK_HAVE_ROOM:
|
|
|
|
checkHaveRoom( &evt );
|
|
|
|
break;
|
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
case XWA_SEND_HOST_RSP:
|
|
|
|
case XWA_SEND_GUEST_RSP:
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_SEND_1ST_RERSP:
|
2009-12-04 09:03:27 +01:00
|
|
|
if ( increasePlayerCounts( &evt, false ) ) {
|
|
|
|
setAllConnectedTimer();
|
|
|
|
sendResponse( &evt, takeAction != XWA_SEND_1ST_RERSP );
|
|
|
|
}
|
2005-09-03 20:35:34 +02:00
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_SEND_RERSP:
|
2009-12-04 09:03:27 +01:00
|
|
|
if ( increasePlayerCounts( &evt, true ) ) {
|
|
|
|
sendResponse( &evt, false );
|
|
|
|
sendAnyStored( &evt );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XWA_SEND_NO_ROOM:
|
|
|
|
send_denied( &evt, XWRELAY_ERROR_NO_ROOM );
|
|
|
|
removeSocket( evt.u.rmsock.socket );
|
|
|
|
break;
|
|
|
|
case XWA_SEND_DUP_ROOM:
|
|
|
|
send_denied( &evt, XWRELAY_ERROR_DUP_ROOM );
|
|
|
|
removeSocket( evt.u.rmsock.socket );
|
2005-07-06 00:05:37 +02:00
|
|
|
break;
|
2009-12-14 05:10:23 +01:00
|
|
|
case XWA_SEND_TOO_MANY:
|
|
|
|
send_denied( &evt, XWRELAY_ERROR_TOO_MANY );
|
|
|
|
removeSocket( evt.u.rmsock.socket );
|
|
|
|
break;
|
2005-07-06 00:05:37 +02:00
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_FWD:
|
2009-11-08 22:35:39 +01:00
|
|
|
forward_or_store( &evt );
|
2005-07-06 00:05:37 +02:00
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_TIMERDISCONN:
|
2005-09-03 08:55:08 +02:00
|
|
|
disconnectSockets( 0, XWRELAY_ERROR_TIMEOUT );
|
|
|
|
break;
|
2005-10-19 05:44:55 +02:00
|
|
|
|
2005-10-30 06:20:31 +01:00
|
|
|
case XWA_SHUTDOWN:
|
|
|
|
disconnectSockets( 0, XWRELAY_ERROR_SHUTDOWN );
|
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_HEARTDISCONN:
|
2005-09-03 20:35:34 +02:00
|
|
|
notifyOthers( evt.u.heart.socket, XWRELAY_DISCONNECT_OTHER,
|
|
|
|
XWRELAY_ERROR_HEART_OTHER );
|
2005-10-19 05:44:55 +02:00
|
|
|
setAllConnectedTimer();
|
|
|
|
reducePlayerCounts( evt.u.discon.socket );
|
2005-09-03 17:41:17 +02:00
|
|
|
disconnectSockets( evt.u.heart.socket,
|
|
|
|
XWRELAY_ERROR_HEART_YOU );
|
2005-07-06 00:05:37 +02:00
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_DISCONNECT:
|
2005-10-19 05:44:55 +02:00
|
|
|
setAllConnectedTimer();
|
2005-10-02 17:39:38 +02:00
|
|
|
reducePlayerCounts( evt.u.discon.socket );
|
2009-09-20 20:47:47 +02:00
|
|
|
notifyOthers( evt.u.discon.socket, XWRELAY_DISCONNECT_OTHER,
|
|
|
|
XWRELAY_ERROR_OTHER_DISCON );
|
2005-09-05 17:50:49 +02:00
|
|
|
removeSocket( evt.u.discon.socket );
|
|
|
|
/* Don't notify. This is a normal part of a game ending. */
|
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_NOTEHEART:
|
2005-07-06 00:05:37 +02:00
|
|
|
noteHeartbeat( &evt );
|
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_NOTIFYDISCON:
|
2005-09-03 08:55:08 +02:00
|
|
|
notifyDisconn( &evt );
|
|
|
|
break;
|
|
|
|
|
2009-12-14 05:10:23 +01:00
|
|
|
case XWA_REMOVESOCK_2:
|
2005-10-19 05:44:55 +02:00
|
|
|
setAllConnectedTimer();
|
2009-12-14 05:10:23 +01:00
|
|
|
/* fallthru */
|
|
|
|
case XWA_REMOVESOCK_1:
|
2005-10-02 17:39:38 +02:00
|
|
|
reducePlayerCounts( evt.u.rmsock.socket );
|
2009-12-14 05:10:23 +01:00
|
|
|
if ( XWA_REMOVESOCK_2 == takeAction ) {
|
|
|
|
notifyOthers( evt.u.rmsock.socket, XWRELAY_DISCONNECT_OTHER,
|
|
|
|
XWRELAY_ERROR_LOST_OTHER );
|
|
|
|
}
|
2005-09-05 17:50:49 +02:00
|
|
|
removeSocket( evt.u.rmsock.socket );
|
2005-09-02 08:56:34 +02:00
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_SENDALLHERE:
|
2005-10-15 18:35:18 +02:00
|
|
|
cancelAllConnectedTimer();
|
2009-08-21 14:00:09 +02:00
|
|
|
assignHostIds();
|
2009-09-26 16:37:49 +02:00
|
|
|
assignConnName();
|
2009-12-14 05:10:23 +01:00
|
|
|
sendAllHere( true );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XWA_SNDALLHERE_2:
|
|
|
|
sendAllHere( false );
|
2009-11-02 02:01:47 +01:00
|
|
|
break;
|
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
case XWA_NOTE_EMPTY:
|
2009-11-09 06:30:42 +01:00
|
|
|
cancelAllConnectedTimer();
|
2009-11-08 22:35:39 +01:00
|
|
|
if ( 0 == count_msgs_stored() ) {
|
|
|
|
CRefEvent evt;
|
|
|
|
evt.type = XWE_NOMOREMSGS;
|
|
|
|
m_eventQueue.push_back( evt );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-10-02 18:08:42 +02:00
|
|
|
case XWA_NONE:
|
2005-08-02 06:57:13 +02:00
|
|
|
/* nothing to do for these */
|
2005-07-06 00:05:37 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-01 02:38:03 +01:00
|
|
|
m_curState = nextState;
|
2005-07-06 00:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
2009-11-09 06:30:42 +01:00
|
|
|
m_in_handleEvents = false;
|
2005-07-06 00:05:37 +02:00
|
|
|
} /* handleEvents */
|
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
bool
|
2009-07-06 03:50:51 +02:00
|
|
|
CookieRef::send_with_length( int socket, unsigned char* buf, int bufLen,
|
|
|
|
bool cascade )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
2009-07-06 03:50:51 +02:00
|
|
|
bool failed = false;
|
2009-07-28 07:15:26 +02:00
|
|
|
if ( send_with_length_unsafe( socket, buf, bufLen ) ) {
|
|
|
|
RecordSent( bufLen, socket );
|
|
|
|
} else {
|
|
|
|
failed = true;
|
2005-07-06 00:05:37 +02:00
|
|
|
}
|
2009-07-28 07:15:26 +02:00
|
|
|
|
2009-07-06 03:50:51 +02:00
|
|
|
if ( failed && cascade ) {
|
2009-11-09 06:30:42 +01:00
|
|
|
pushRemoveSocketEvent( socket );
|
2009-07-06 03:50:51 +02:00
|
|
|
XWThreadPool::GetTPool()->CloseSocket( socket );
|
|
|
|
}
|
2009-11-08 22:35:39 +01:00
|
|
|
return !failed;
|
2009-07-28 07:15:26 +02:00
|
|
|
} /* send_with_length */
|
2005-07-06 00:05:37 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
putNetShort( unsigned char** bufpp, unsigned short s )
|
|
|
|
{
|
|
|
|
s = htons( s );
|
|
|
|
memcpy( *bufpp, &s, sizeof(s) );
|
|
|
|
*bufpp += sizeof(s);
|
|
|
|
}
|
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
void
|
|
|
|
CookieRef::store_message( HostID dest, const unsigned char* buf,
|
|
|
|
unsigned int len )
|
|
|
|
{
|
|
|
|
logf( XW_LOGVERBOSE0, "%s: storing msg size %d for dest %d", __func__,
|
|
|
|
len, dest );
|
|
|
|
assert( dest > 0 );
|
|
|
|
--dest; // 1-based
|
|
|
|
MsgBuffer* entry = new MsgBuffer( buf, buf+len );
|
|
|
|
assert( dest < sizeof(m_hostMsgQueues)/sizeof(m_hostMsgQueues[0]) );
|
|
|
|
m_hostMsgQueues[dest].push_back( entry );
|
|
|
|
++m_nHostMsgs;
|
|
|
|
}
|
2009-11-02 02:01:47 +01:00
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
void
|
|
|
|
CookieRef::send_stored_messages( HostID dest, int socket )
|
|
|
|
{
|
|
|
|
assert( dest > 0 );
|
|
|
|
logf( XW_LOGVERBOSE0, "%s(dest=%d)", __func__, dest );
|
|
|
|
--dest; // 0 is invalid value
|
|
|
|
assert( dest < sizeof(m_hostMsgQueues)/sizeof(m_hostMsgQueues[0]) );
|
|
|
|
assert( -1 != socket );
|
|
|
|
|
|
|
|
MsgBufQueue& mqueue = m_hostMsgQueues[dest];
|
|
|
|
while ( mqueue.size() > 0 ) {
|
|
|
|
assert( m_nHostMsgs > 0 );
|
|
|
|
// send_with_length will call _Remove if it fails to send. So
|
|
|
|
// need to check on presence of socket each time through! No,
|
|
|
|
// the break below takes care of that.
|
|
|
|
|
|
|
|
MsgBufQueue::iterator iter = mqueue.begin();
|
|
|
|
|
|
|
|
logf( XW_LOGVERBOSE0, "%s: sending stored msg (len=%d)",
|
|
|
|
__func__, (*iter)->size() );
|
2009-12-04 09:03:27 +01:00
|
|
|
if ( ! send_with_length( socket, &((**iter)[0]), (*iter)->size(),
|
|
|
|
true ) ) {
|
2009-11-08 22:35:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mqueue.erase( iter );
|
|
|
|
delete *iter;
|
|
|
|
--m_nHostMsgs;
|
|
|
|
}
|
|
|
|
} /* send_stored_messages */
|
2009-11-02 02:01:47 +01:00
|
|
|
|
|
|
|
bool
|
2009-12-04 09:03:27 +01:00
|
|
|
CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
|
2005-10-02 17:39:38 +02:00
|
|
|
{
|
|
|
|
int nPlayersH = evt->u.con.nPlayersH;
|
2009-11-01 02:38:03 +01:00
|
|
|
int nPlayersS = evt->u.con.nPlayersS;
|
2009-11-02 02:01:47 +01:00
|
|
|
int socket = evt->u.con.socket;
|
2005-10-02 17:39:38 +02:00
|
|
|
HostID hid = evt->u.con.srcID;
|
2009-11-02 02:01:47 +01:00
|
|
|
int seed = evt->u.con.seed;
|
2009-12-04 09:03:27 +01:00
|
|
|
CRefEvent newevt;
|
|
|
|
bool addHost = true;
|
|
|
|
|
2009-09-26 16:37:49 +02:00
|
|
|
assert( hid <= 4 );
|
2009-12-04 09:03:27 +01:00
|
|
|
|
|
|
|
newevt.type = XWE_NONE;
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-11-02 02:01:47 +01:00
|
|
|
logf( XW_LOGINFO, "%s: hid=%d, nPlayersH=%d, "
|
|
|
|
"nPlayersS=%d", __func__, hid, nPlayersH, nPlayersS );
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-11-02 02:01:47 +01:00
|
|
|
ASSERT_LOCKED();
|
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
/* Add the players provided by this [re]connect event to the cref after
|
|
|
|
performing sanity checks. If this is an initial connect, then the host
|
|
|
|
should be added first. If it's a recon, any order is possible. In no
|
|
|
|
circumstances should the number of players present exceed the number
|
|
|
|
sought (if known.) Currently some of this stuff is asserted. Instead
|
|
|
|
when bad values are seen the sender should be notified then
|
|
|
|
disconnencted. On the host side the error message should probably
|
|
|
|
recommend a new game as things must be pretty f*cked up. Or somebody's
|
|
|
|
mucking with me. */
|
|
|
|
|
|
|
|
if ( reconn ) {
|
|
|
|
if ( nPlayersS > 0 ) {
|
|
|
|
assert( 0 == m_nPlayersSought );
|
|
|
|
m_nPlayersSought = nPlayersS;
|
|
|
|
}
|
|
|
|
m_nPlayersHere += nPlayersH;
|
|
|
|
assert( 0 == m_nPlayersSought || m_nPlayersHere <= m_nPlayersSought );
|
|
|
|
if ( m_nPlayersHere == m_nPlayersSought ) {
|
|
|
|
newevt.type = XWE_ALLHERE;
|
|
|
|
} else {
|
|
|
|
newevt.type = XWE_SOMEMISSING;
|
|
|
|
}
|
|
|
|
} else if ( nPlayersS > 0 ) { /* a host; init values */
|
|
|
|
assert( m_nPlayersSought == 0 );
|
|
|
|
assert( m_nPlayersHere == 0 );
|
|
|
|
m_nPlayersHere = nPlayersH;
|
|
|
|
m_nPlayersSought = nPlayersS;
|
|
|
|
} else { /* a guest */
|
|
|
|
assert( reconn || m_nPlayersSought != 0 ); /* host better be here */
|
|
|
|
|
|
|
|
if ( m_nPlayersSought < m_nPlayersHere + nPlayersH ) { /* too many;
|
|
|
|
reject */
|
|
|
|
newevt.type = XWE_TOO_MANY;
|
|
|
|
addHost = false;
|
|
|
|
} else {
|
|
|
|
m_nPlayersHere += nPlayersH;
|
|
|
|
if ( m_nPlayersHere == m_nPlayersSought ) { /* complete! */
|
|
|
|
newevt.type = XWE_ALLHERE;
|
|
|
|
m_gameFull = true;
|
|
|
|
} else {
|
|
|
|
newevt.type = XWE_SOMEMISSING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 02:01:47 +01:00
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
if ( newevt.type != XWE_NONE ) {
|
|
|
|
m_eventQueue.push_back( newevt );
|
2005-10-02 17:39:38 +02:00
|
|
|
}
|
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
if ( addHost ) {
|
|
|
|
/* first add the rec here, whether it'll stay for not */
|
|
|
|
logf( XW_LOGINFO, "%s: remembering pair: hostid=%x, "
|
|
|
|
"socket=%d (size=%d)",
|
|
|
|
__func__, hid, socket, m_sockets.size());
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
HostRec hr( hid, socket, nPlayersH, nPlayersS, seed );
|
|
|
|
m_sockets.push_back( hr );
|
|
|
|
|
|
|
|
logf( XW_LOGVERBOSE1, "%s: here=%d; total=%d", __func__,
|
|
|
|
m_nPlayersHere, m_nPlayersSought );
|
2009-09-14 03:55:03 +02:00
|
|
|
}
|
2009-12-04 09:03:27 +01:00
|
|
|
return addHost;
|
2005-10-02 17:39:38 +02:00
|
|
|
} /* increasePlayerCounts */
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::reducePlayerCounts( int socket )
|
|
|
|
{
|
2005-10-30 06:20:31 +01:00
|
|
|
logf( XW_LOGVERBOSE1, "reducePlayerCounts on socket %d", socket );
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_socket == socket ) {
|
|
|
|
if ( iter->m_hostID == HOST_ID_SERVER ) {
|
2009-11-02 02:01:47 +01:00
|
|
|
m_nPlayersSought -= iter->m_nPlayersS;
|
2005-10-02 17:39:38 +02:00
|
|
|
} else {
|
2009-11-01 02:38:03 +01:00
|
|
|
assert( iter->m_nPlayersS == 0 );
|
2005-10-02 17:39:38 +02:00
|
|
|
}
|
2009-08-21 14:00:09 +02:00
|
|
|
m_nPlayersHere -= iter->m_nPlayersH;
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-08-13 14:59:55 +02:00
|
|
|
logf( XW_LOGVERBOSE1, "reducePlayerCounts: m_nPlayersHere=%d; m_nPlayersSought=%d",
|
|
|
|
m_nPlayersHere, m_nPlayersSought );
|
2005-10-02 17:39:38 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* reducePlayerCounts */
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
|
|
|
CookieRef::setAllConnectedTimer()
|
|
|
|
{
|
|
|
|
time_t inHowLong;
|
2009-12-27 19:17:24 +01:00
|
|
|
if ( RelayConfigs::GetConfigs()->GetValueFor( "ALLCONN", &inHowLong ) ) {
|
|
|
|
TimerMgr::GetTimerMgr()->SetTimer( inHowLong,
|
|
|
|
s_checkAllConnected, this, 0 );
|
|
|
|
}
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::cancelAllConnectedTimer()
|
|
|
|
{
|
2005-10-15 17:49:22 +02:00
|
|
|
TimerMgr::GetTimerMgr()->ClearTimer( s_checkAllConnected, this );
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
|
|
|
|
2005-07-06 00:05:37 +02:00
|
|
|
void
|
2008-12-30 06:13:30 +01:00
|
|
|
CookieRef::sendResponse( const CRefEvent* evt, bool initial )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
|
|
|
int socket = evt->u.con.socket;
|
|
|
|
|
|
|
|
/* Now send the response */
|
2009-11-02 02:01:47 +01:00
|
|
|
unsigned char buf[1 /* cmd */
|
|
|
|
+ sizeof(short) /* heartbeat */
|
|
|
|
+ sizeof(short) /* total here */
|
|
|
|
+ sizeof(short) /* total expected */
|
2005-10-02 17:39:38 +02:00
|
|
|
];
|
|
|
|
|
2005-07-06 00:05:37 +02:00
|
|
|
unsigned char* bufp = buf;
|
|
|
|
|
2005-10-01 18:33:45 +02:00
|
|
|
*bufp++ = initial ? XWRELAY_CONNECT_RESP : XWRELAY_RECONNECT_RESP;
|
2005-07-06 00:05:37 +02:00
|
|
|
putNetShort( &bufp, GetHeartbeat() );
|
2009-11-02 02:01:47 +01:00
|
|
|
*bufp++ = GetPlayersSought();
|
2009-12-14 05:10:23 +01:00
|
|
|
*bufp++ = GetPlayersHere();
|
2005-07-06 00:05:37 +02:00
|
|
|
|
2009-07-06 03:50:51 +02:00
|
|
|
send_with_length( socket, buf, bufp - buf, true );
|
2009-08-21 14:00:09 +02:00
|
|
|
logf( XW_LOGVERBOSE0, "sent %s", cmdToStr( XWRELAY_Cmd(buf[0]) ) );
|
2005-07-06 00:05:37 +02:00
|
|
|
} /* sendResponse */
|
|
|
|
|
|
|
|
void
|
2009-11-08 22:35:39 +01:00
|
|
|
CookieRef::sendAnyStored( const CRefEvent* evt )
|
|
|
|
{
|
|
|
|
HostID dest = evt->u.con.srcID;
|
|
|
|
if ( HOST_ID_NONE != dest ) {
|
|
|
|
send_stored_messages( dest, evt->u.con.socket );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::forward_or_store( const CRefEvent* evt )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
|
|
|
unsigned char* buf = evt->u.fwd.buf;
|
|
|
|
int buflen = evt->u.fwd.buflen;
|
|
|
|
HostID dest = evt->u.fwd.dest;
|
|
|
|
|
|
|
|
int destSocket = SocketForHost( dest );
|
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
/* This is an ugly hack!!!! */
|
|
|
|
*buf = XWRELAY_MSG_FROMRELAY;
|
|
|
|
|
|
|
|
if ( (destSocket == -1)
|
|
|
|
|| !send_with_length( destSocket, buf, buflen, true ) ) {
|
|
|
|
store_message( dest, buf, buflen );
|
|
|
|
}
|
2005-07-06 00:05:37 +02:00
|
|
|
|
2009-11-08 22:35:39 +01:00
|
|
|
/* also note that we've heard from src recently */
|
2007-11-19 00:38:56 +01:00
|
|
|
#ifdef RELAY_HEARTBEAT
|
2009-11-08 22:35:39 +01:00
|
|
|
HostID src = evt->u.fwd.src;
|
|
|
|
pushHeartbeatEvent( src, SocketForHost(src) );
|
2007-11-19 00:38:56 +01:00
|
|
|
#endif
|
2009-11-08 22:35:39 +01:00
|
|
|
} /* forward_or_store */
|
2005-07-06 00:05:37 +02:00
|
|
|
|
2009-12-04 09:03:27 +01:00
|
|
|
void
|
|
|
|
CookieRef::send_denied( const CRefEvent* evt, XWREASON why )
|
|
|
|
{
|
|
|
|
denyConnection( evt->u.con.socket, why );
|
|
|
|
}
|
|
|
|
|
2005-09-03 17:41:17 +02:00
|
|
|
void
|
2009-07-06 03:50:51 +02:00
|
|
|
CookieRef::send_msg( int socket, HostID id, XWRelayMsg msg, XWREASON why,
|
|
|
|
bool cascade )
|
2005-09-03 20:35:34 +02:00
|
|
|
{
|
|
|
|
unsigned char buf[10];
|
|
|
|
short tmp;
|
2009-07-06 03:50:51 +02:00
|
|
|
unsigned int len = 0;
|
2005-09-03 20:35:34 +02:00
|
|
|
buf[len++] = msg;
|
|
|
|
|
|
|
|
switch ( msg ) {
|
|
|
|
case XWRELAY_DISCONNECT_OTHER:
|
|
|
|
buf[len++] = why;
|
|
|
|
tmp = htons( id );
|
|
|
|
memcpy( &buf[len], &tmp, 2 );
|
|
|
|
len += 2;
|
|
|
|
break;
|
|
|
|
default:
|
2005-10-02 18:08:42 +02:00
|
|
|
logf( XW_LOGINFO, "not handling message %d", msg );
|
2005-09-03 20:35:34 +02:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2009-04-05 20:47:45 +02:00
|
|
|
assert( len <= sizeof(buf) );
|
2009-07-06 03:50:51 +02:00
|
|
|
send_with_length( socket, buf, len, cascade );
|
2005-09-03 20:35:34 +02:00
|
|
|
} /* send_msg */
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::notifyOthers( int socket, XWRelayMsg msg, XWREASON why )
|
2005-09-03 17:41:17 +02:00
|
|
|
{
|
|
|
|
assert( socket != 0 );
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
int other = iter->m_socket;
|
2005-09-03 17:41:17 +02:00
|
|
|
if ( other != socket ) {
|
2009-08-21 14:00:09 +02:00
|
|
|
send_msg( other, iter->m_hostID, msg, why, false );
|
2005-09-03 17:41:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* notifyOthers */
|
|
|
|
|
2009-11-02 02:01:47 +01:00
|
|
|
void
|
|
|
|
CookieRef::moveSockets( void )
|
|
|
|
{
|
|
|
|
ASSERT_LOCKED();
|
|
|
|
|
|
|
|
vector<int> sockets;
|
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
sockets.push_back( iter->m_socket );
|
|
|
|
}
|
|
|
|
|
|
|
|
CRefMgr::Get()->MoveSockets( sockets, this );
|
|
|
|
}
|
|
|
|
|
2005-10-02 17:39:38 +02:00
|
|
|
void
|
2009-12-14 05:10:23 +01:00
|
|
|
CookieRef::sendAllHere( bool initial )
|
2005-10-02 17:39:38 +02:00
|
|
|
{
|
2009-11-02 02:01:47 +01:00
|
|
|
unsigned char buf[1 + 1 /* hostID */
|
|
|
|
+ sizeof(CookieID)
|
|
|
|
+ 1 + MAX_CONNNAME_LEN];
|
|
|
|
|
2005-10-02 17:39:38 +02:00
|
|
|
unsigned char* bufp = buf;
|
2009-08-21 14:00:09 +02:00
|
|
|
unsigned char* idLoc;
|
2005-10-02 17:39:38 +02:00
|
|
|
|
2009-12-14 05:10:23 +01:00
|
|
|
*bufp++ = initial? XWRELAY_ALLHERE : XWRELAY_ALLBACK;
|
2009-08-21 14:00:09 +02:00
|
|
|
idLoc = bufp++; /* space for hostId, remembering address */
|
|
|
|
|
2009-11-02 02:01:47 +01:00
|
|
|
putNetShort( &bufp, GetCookieID() );
|
|
|
|
|
2009-09-26 16:37:49 +02:00
|
|
|
const char* connName = ConnName();
|
|
|
|
assert( !!connName && connName[0] );
|
|
|
|
int len = strlen( connName );
|
|
|
|
assert( len < MAX_CONNNAME_LEN );
|
|
|
|
*bufp++ = (char)len;
|
|
|
|
memcpy( bufp, connName, len );
|
|
|
|
bufp += len;
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter = m_sockets.begin();
|
2005-10-02 17:39:38 +02:00
|
|
|
while ( iter != m_sockets.end() ) {
|
2009-08-21 14:00:09 +02:00
|
|
|
logf( XW_LOGINFO, "%s: sending to hostid %d", __func__,
|
|
|
|
iter->m_hostID );
|
|
|
|
*idLoc = iter->m_hostID; /* write in this target's hostId */
|
2009-09-14 03:55:03 +02:00
|
|
|
send_with_length( iter->m_socket, buf, bufp-buf, true );
|
2005-10-02 17:39:38 +02:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
} /* sendAllHere */
|
|
|
|
|
2009-08-21 14:00:09 +02:00
|
|
|
void
|
|
|
|
CookieRef::assignHostIds( void )
|
|
|
|
{
|
|
|
|
ASSERT_LOCKED();
|
|
|
|
HostID nextId = HOST_ID_SERVER;
|
|
|
|
|
|
|
|
unsigned int bits = 0;
|
|
|
|
|
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_hostID != HOST_ID_NONE ) {
|
|
|
|
bits |= 1 << iter->m_hostID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( (bits & (1 << HOST_ID_SERVER)) != 0 );
|
|
|
|
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_hostID == HOST_ID_NONE ) {
|
|
|
|
while ( ((1 << nextId) & bits) != 0 ) {
|
|
|
|
++nextId;
|
|
|
|
}
|
|
|
|
iter->m_hostID = nextId++; /* ++: don't reuse */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-26 16:37:49 +02:00
|
|
|
#define CONNNAME_DELIM ' ' /* ' ' so will wrap in browser */
|
|
|
|
/* Does my seed belong as part of existing connName */
|
|
|
|
bool
|
|
|
|
CookieRef::SeedBelongs( int gameSeed )
|
|
|
|
{
|
|
|
|
bool belongs = false;
|
|
|
|
const char* ptr = ConnName();
|
|
|
|
const char* end = ptr + strlen(ptr);
|
|
|
|
assert( '\0' != ptr[0] );
|
|
|
|
char buf[5];
|
|
|
|
snprintf( buf, sizeof(buf), "%.4X", gameSeed );
|
|
|
|
|
|
|
|
for ( ; *ptr != CONNNAME_DELIM && ptr < end; ptr += 4 ) {
|
|
|
|
if ( 0 == strncmp( ptr, buf, 4 ) ) {
|
|
|
|
belongs = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return belongs;
|
|
|
|
} /* SeedBelongs */
|
|
|
|
|
|
|
|
/* does my connName provide a home for seeds already in this connName-less
|
|
|
|
ref? */
|
|
|
|
bool
|
|
|
|
CookieRef::SeedsBelong( const char* connName )
|
|
|
|
{
|
|
|
|
bool found = true;
|
|
|
|
assert( !m_connName[0] );
|
|
|
|
const char* delim = strchr( connName, CONNNAME_DELIM );
|
|
|
|
assert( !!delim );
|
|
|
|
|
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
char buf[5];
|
|
|
|
snprintf( buf, sizeof(buf), "%.4X", iter->m_seed );
|
|
|
|
const char* match = strstr( connName, buf );
|
|
|
|
if ( !match || match > delim ) {
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
} /* SeedsBelong */
|
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::assignConnName( void )
|
|
|
|
{
|
|
|
|
if ( '\0' == ConnName()[0] ) {
|
|
|
|
|
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
char buf[5];
|
|
|
|
snprintf( buf, sizeof(buf), "%.4X", iter->m_seed );
|
|
|
|
m_connName += buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connName += CONNNAME_DELIM + PermID::GetNextUniqueID();
|
|
|
|
|
|
|
|
logf( XW_LOGINFO, "%s: assigning name: %s", __func__, ConnName() );
|
|
|
|
} else {
|
|
|
|
logf( XW_LOGINFO, "%s: already named: %s", __func__, ConnName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-06 00:05:37 +02:00
|
|
|
void
|
2005-09-03 08:55:08 +02:00
|
|
|
CookieRef::disconnectSockets( int socket, XWREASON why )
|
2005-07-06 00:05:37 +02:00
|
|
|
{
|
2005-09-03 08:55:08 +02:00
|
|
|
if ( socket == 0 ) {
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
assert( iter->m_socket != 0 );
|
|
|
|
disconnectSockets( iter->m_socket, why );
|
2005-09-03 08:55:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pushNotifyDisconEvent( socket, why );
|
|
|
|
pushRemoveSocketEvent( socket );
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
2005-09-03 08:55:08 +02:00
|
|
|
} /* disconnectSockets */
|
2005-07-06 00:05:37 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::noteHeartbeat( const CRefEvent* evt )
|
|
|
|
{
|
|
|
|
int socket = evt->u.heart.socket;
|
|
|
|
HostID id = evt->u.heart.id;
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
if ( iter->m_hostID == id ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-05 17:50:49 +02:00
|
|
|
if ( iter == m_sockets.end() ) {
|
2005-10-02 18:08:42 +02:00
|
|
|
logf( XW_LOGERROR, "no socket for HostID %x", id );
|
2005-09-02 08:56:34 +02:00
|
|
|
} else {
|
2005-06-23 06:26:44 +02:00
|
|
|
|
2009-08-21 14:00:09 +02:00
|
|
|
int second_socket = iter->m_socket;
|
|
|
|
if ( second_socket == socket ) {
|
|
|
|
logf( XW_LOGVERBOSE1, "upping m_lastHeartbeat from %d to %d",
|
|
|
|
iter->m_lastHeartbeat, uptime() );
|
|
|
|
iter->m_lastHeartbeat = uptime();
|
|
|
|
} else {
|
|
|
|
/* PENDING If the message came on an unexpected socket, kill the
|
|
|
|
connection. An attack is the most likely explanation. But:
|
|
|
|
now it's happening after a crash and clients reconnect. */
|
2009-12-04 09:03:27 +01:00
|
|
|
logf( XW_LOGERROR, "wrong socket record for HostID %x; wanted %d, "
|
|
|
|
"found %d", id, socket, second_socket );
|
2009-08-21 14:00:09 +02:00
|
|
|
}
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
2005-07-06 00:05:37 +02:00
|
|
|
} /* noteHeartbeat */
|
2005-06-23 06:26:44 +02:00
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
/* timer callback */
|
|
|
|
/* static */ void
|
|
|
|
CookieRef::s_checkAllConnected( void* closure )
|
|
|
|
{
|
|
|
|
/* Need to ensure */
|
|
|
|
CookieRef* self = (CookieRef*)closure;
|
|
|
|
SafeCref scr(self);
|
2005-10-01 18:33:45 +02:00
|
|
|
scr.CheckAllConnected();
|
2005-09-02 08:56:34 +02:00
|
|
|
}
|
|
|
|
|
2005-04-20 14:13:20 +02:00
|
|
|
void
|
2005-09-02 08:56:34 +02:00
|
|
|
CookieRef::_CheckAllConnected()
|
|
|
|
{
|
2009-08-21 14:00:09 +02:00
|
|
|
logf( XW_LOGVERBOSE0, "%s", __func__ );
|
2005-09-14 07:15:27 +02:00
|
|
|
/* MutexLock ml( &m_EventsMutex ); */
|
2005-09-02 08:56:34 +02:00
|
|
|
CRefEvent newEvt;
|
2005-10-02 18:08:42 +02:00
|
|
|
newEvt.type = XWE_CONNTIMER;
|
2005-09-03 08:55:08 +02:00
|
|
|
m_eventQueue.push_back( newEvt );
|
2005-09-02 08:56:34 +02:00
|
|
|
handleEvents();
|
|
|
|
}
|
|
|
|
|
2005-10-30 06:20:31 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
CookieRef::logf( XW_LogLevel level, const char* format, ... )
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int len;
|
|
|
|
|
2009-02-28 20:35:32 +01:00
|
|
|
len = snprintf( buf, sizeof(buf), "cid:%d ", m_cookieID );
|
2005-10-30 06:20:31 +01:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, format );
|
|
|
|
vsnprintf( buf + len, sizeof(buf) - len, format, ap );
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
::logf( level, buf );
|
|
|
|
}
|
|
|
|
|
2005-09-02 08:56:34 +02:00
|
|
|
void
|
|
|
|
CookieRef::_PrintCookieInfo( string& out )
|
2005-04-20 14:13:20 +02:00
|
|
|
{
|
2005-10-16 03:19:25 +02:00
|
|
|
out += "Cookie=";
|
|
|
|
out += Cookie();
|
2005-04-20 14:13:20 +02:00
|
|
|
out += "\n";
|
2005-10-16 03:19:25 +02:00
|
|
|
out += "connName=";
|
2009-09-14 05:11:11 +02:00
|
|
|
char buf[MAX_CONNNAME_LEN+MAX_INVITE_LEN];
|
2005-10-01 18:33:45 +02:00
|
|
|
|
2005-10-16 03:19:25 +02:00
|
|
|
snprintf( buf, sizeof(buf), "%s\n", ConnName() );
|
2005-10-01 18:33:45 +02:00
|
|
|
out += buf;
|
2005-09-04 22:37:42 +02:00
|
|
|
|
2005-10-16 03:19:25 +02:00
|
|
|
snprintf( buf, sizeof(buf), "id=%d\n", GetCookieID() );
|
2005-04-20 14:13:20 +02:00
|
|
|
out += buf;
|
|
|
|
|
2005-10-16 03:19:25 +02:00
|
|
|
snprintf( buf, sizeof(buf), "Bytes sent=%d\n", m_totalSent );
|
|
|
|
out += buf;
|
|
|
|
|
2009-08-13 14:59:55 +02:00
|
|
|
snprintf( buf, sizeof(buf), "Total players=%d\n", m_nPlayersSought );
|
2005-10-16 03:19:25 +02:00
|
|
|
out += buf;
|
|
|
|
snprintf( buf, sizeof(buf), "Players here=%d\n", m_nPlayersHere );
|
2005-04-20 14:13:20 +02:00
|
|
|
out += buf;
|
|
|
|
|
2005-10-19 05:44:55 +02:00
|
|
|
snprintf( buf, sizeof(buf), "State=%s\n", stateString( m_curState ) );
|
|
|
|
out += buf;
|
|
|
|
|
|
|
|
/* Timer state: how long since last heartbeat; how long til disconn timer
|
|
|
|
fires. */
|
|
|
|
|
2005-04-20 14:13:20 +02:00
|
|
|
/* n messages */
|
|
|
|
/* open since when */
|
2005-10-19 05:44:55 +02:00
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2005-10-19 05:44:55 +02:00
|
|
|
snprintf( buf, sizeof(buf), "Hosts connected=%d; cur time = %ld\n",
|
2009-03-10 13:52:17 +01:00
|
|
|
m_sockets.size(), uptime() );
|
2005-10-19 05:44:55 +02:00
|
|
|
out += buf;
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
2005-10-19 05:44:55 +02:00
|
|
|
snprintf( buf, sizeof(buf), " HostID=%d; socket=%d;last hbeat=%ld\n",
|
2009-08-21 14:00:09 +02:00
|
|
|
iter->m_hostID, iter->m_socket,
|
|
|
|
iter->m_lastHeartbeat );
|
2005-10-19 05:44:55 +02:00
|
|
|
out += buf;
|
|
|
|
}
|
2005-04-20 14:13:20 +02:00
|
|
|
|
|
|
|
} /* PrintCookieInfo */
|
2009-02-28 20:35:32 +01:00
|
|
|
|
|
|
|
void
|
2009-09-26 16:37:49 +02:00
|
|
|
CookieRef::_FormatHostInfo( string* hostIds, string* seeds, string* addrs )
|
2009-02-28 20:35:32 +01:00
|
|
|
{
|
2009-07-28 07:15:26 +02:00
|
|
|
ASSERT_LOCKED();
|
2009-08-21 14:00:09 +02:00
|
|
|
vector<HostRec>::iterator iter;
|
2009-07-28 07:15:26 +02:00
|
|
|
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
|
|
|
|
|
|
|
if ( !!hostIds ) {
|
|
|
|
char buf[8];
|
2009-08-21 14:00:09 +02:00
|
|
|
snprintf( buf, sizeof(buf), "%d ", iter->m_hostID );
|
2009-07-28 07:15:26 +02:00
|
|
|
*hostIds += buf;
|
|
|
|
}
|
|
|
|
|
2009-09-26 16:37:49 +02:00
|
|
|
if ( !!seeds ) {
|
|
|
|
char buf[6];
|
|
|
|
snprintf( buf, sizeof(buf), "%.4X ", iter->m_seed );
|
|
|
|
*seeds += buf;
|
|
|
|
}
|
|
|
|
|
2009-07-28 07:15:26 +02:00
|
|
|
if ( !!addrs ) {
|
2009-08-21 14:00:09 +02:00
|
|
|
int s = iter->m_socket;
|
2009-07-28 07:15:26 +02:00
|
|
|
struct sockaddr_in name;
|
|
|
|
socklen_t siz = sizeof(name);
|
|
|
|
if ( 0 == getpeername( s, (struct sockaddr*)&name, &siz) ) {
|
|
|
|
char buf[32] = {0};
|
|
|
|
snprintf( buf, sizeof(buf), "%s ", inet_ntoa(name.sin_addr) );
|
|
|
|
*addrs += buf;
|
|
|
|
}
|
|
|
|
}
|
2009-02-28 20:35:32 +01:00
|
|
|
}
|
|
|
|
}
|