when one device is disconnected for heartbeat, tell others it's going

away.
This commit is contained in:
ehouse 2005-09-03 15:41:17 +00:00
parent ca8877a5de
commit a4033a44c3
3 changed files with 43 additions and 10 deletions

View file

@ -152,10 +152,10 @@ void
CookieRef::notifyDisconn( const CRefEvent* evt ) CookieRef::notifyDisconn( const CRefEvent* evt )
{ {
int socket = evt->u.disnote.socket; int socket = evt->u.disnote.socket;
unsigned char buf[2]; unsigned char buf[] = {
XWRELAY_DISCONNECT_YOU,
buf[0] = XWRELAY_DISCONNECT; evt->u.disnote.why
buf[1] = evt->u.disnote.why; };
send_with_length( socket, buf, sizeof(buf) ); send_with_length( socket, buf, sizeof(buf) );
} /* notifyDisconn */ } /* notifyDisconn */
@ -225,7 +225,7 @@ CookieRef::_CheckHeartbeats( time_t now )
map<HostID,HostRec>::iterator iter = m_hostSockets.begin(); map<HostID,HostRec>::iterator iter = m_hostSockets.begin();
while ( iter != m_hostSockets.end() ) { while ( iter != m_hostSockets.end() ) {
time_t last = iter->second.m_lastHeartbeat; time_t last = iter->second.m_lastHeartbeat;
if ( (now - last) > GetHeartbeat() * 2 ) { if ( (now - last) > GetHeartbeat() ) {
pushHeartFailedEvent( iter->second.m_socket ); pushHeartFailedEvent( iter->second.m_socket );
} }
++iter; ++iter;
@ -403,7 +403,9 @@ CookieRef::handleEvents()
disconnectSockets( 0, XWRELAY_ERROR_TIMEOUT ); disconnectSockets( 0, XWRELAY_ERROR_TIMEOUT );
break; break;
case XW_ACTION_HEARTDISCONNECT: case XW_ACTION_HEARTDISCONNECT:
disconnectSockets( evt.u.heart.socket, XWRELAY_ERROR_HEART ); notifyOthers( evt.u.heart.socket, XWRELAY_ERROR_HEART_OTHER );
disconnectSockets( evt.u.heart.socket,
XWRELAY_ERROR_HEART_YOU );
break; break;
case XW_ACTION_NOTEHEART: case XW_ACTION_NOTEHEART:
@ -548,6 +550,31 @@ CookieRef::checkFromServer( const CRefEvent* evt )
} }
} }
void
CookieRef::notifyOthers( int socket, XWREASON why )
{
assert( socket != 0 );
RWReadLock ml( &m_sockets_rwlock );
map<HostID,HostRec>::iterator iter = m_hostSockets.begin();
while ( iter != m_hostSockets.end() ) {
int other = iter->second.m_socket;
if ( other != socket ) {
unsigned char buf[4];
buf[0] = XWRELAY_DISCONNECT_OTHER;
buf[1] = why;
HostID id = iter->first;
short tmp = htons( id );
memcpy( &buf[2], &tmp, 2 );
send_with_length( other, buf, sizeof(buf) );
}
++iter;
}
} /* notifyOthers */
void void
CookieRef::disconnectSockets( int socket, XWREASON why ) CookieRef::disconnectSockets( int socket, XWREASON why )
{ {

View file

@ -157,6 +157,7 @@ class CookieRef {
void forward( const CRefEvent* evt ); void forward( const CRefEvent* evt );
void checkDest( const CRefEvent* evt ); void checkDest( const CRefEvent* evt );
void checkFromServer( const CRefEvent* evt ); void checkFromServer( const CRefEvent* evt );
void notifyOthers( int socket, XWREASON why );
void disconnectSockets( int socket, XWREASON why ); void disconnectSockets( int socket, XWREASON why );
void noteHeartbeat(const CRefEvent* evt); void noteHeartbeat(const CRefEvent* evt);

View file

@ -43,9 +43,13 @@ enum { XWRELAY_NONE /* 0 is an illegal value */
XWRELAY_RECONNECT. Format: heartbeat_seconds: 2; connectionID: XWRELAY_RECONNECT. Format: heartbeat_seconds: 2; connectionID:
2; */ 2; */
, XWRELAY_DISCONNECT , XWRELAY_DISCONNECT_YOU
/* Sent from relay when existing connection is terminated. Includes /* Sent from relay when existing connection is terminated.
reason code */ Format: errorCode: 1 */
, XWRELAY_DISCONNECT_OTHER
/* Another device has left the game.
Format: errorCode: 1; lostHostId: 2 */
, XWRELAY_CONNECTDENIED , XWRELAY_CONNECTDENIED
/* The relay says go away. Format: reason code: 1 */ /* The relay says go away. Format: reason code: 1 */
@ -81,7 +85,8 @@ typedef enum {
,XWRELAY_ERROR_RELAYBUSY ,XWRELAY_ERROR_RELAYBUSY
,XWRELAY_ERROR_SHUTDOWN /* relay's going down */ ,XWRELAY_ERROR_SHUTDOWN /* relay's going down */
,XWRELAY_ERROR_TIMEOUT /* Other players didn't show */ ,XWRELAY_ERROR_TIMEOUT /* Other players didn't show */
,XWRELAY_ERROR_HEART /* Haven't heard from you in too long */ ,XWRELAY_ERROR_HEART_YOU /* Haven't heard from somebody in too long */
,XWRELAY_ERROR_HEART_OTHER
,XWRELAY_ERROR_LASTERR ,XWRELAY_ERROR_LASTERR
} XWREASON; } XWREASON;