From 40e1d29243c1035bc5f4be15bb041e55f8926fbf Mon Sep 17 00:00:00 2001 From: Andy2 Date: Thu, 23 Sep 2010 06:32:57 -0700 Subject: [PATCH] use new methods to store undeliverable messages in a db rather than in memory. Confirmed that devices can hookup without ever being connected at the same time and that the messages survive a relay reboot. No further testing yet. --- xwords4/relay/cref.cpp | 50 +++++++++++++----------------------------- xwords4/relay/cref.h | 3 --- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index edc54fd00..c5d29cd8d 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -93,7 +93,6 @@ CookieRef::ReInit( const char* cookie, const char* connName, CookieID id, m_locking_thread = 0; m_starttime = uptime(); m_gameFull = false; - m_nHostMsgs = 0; m_in_handleEvents = false; m_langCode = langCode; m_nPendingAcks = 0; @@ -146,14 +145,6 @@ CookieRef::Clear(void) m_connName = ""; m_cookieID = 0; m_eventQueue.clear(); - - 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 */ bool @@ -686,7 +677,7 @@ CookieRef::handleEvents() case XWA_NOTE_EMPTY: //cancelAllConnectedTimer(); - if ( 0 == count_msgs_stored() ) { + if ( 0 == DBMgr::Get()->CountStoredMessages( ConnName() ) ) { CRefEvent evt( XWE_NOMOREMSGS ); m_eventQueue.push_back( evt ); } @@ -746,41 +737,30 @@ CookieRef::store_message( HostID dest, const unsigned char* buf, { 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; + + DBMgr::Get()->StoreMessage( ConnName(), dest, buf, len ); } 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( dest > 0 && dest <= 4 ); 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() ); - if ( ! send_with_length( socket, &((**iter)[0]), (*iter)->size(), - true ) ) { + for ( ; ; ) { + unsigned char buf[MAX_MSG_LEN]; + size_t buflen = sizeof(buf); + int msgID; + if ( !DBMgr::Get()->GetStoredMessage( ConnName(), dest, + buf, &buflen, &msgID ) ) { break; } - mqueue.erase( iter ); - delete *iter; - --m_nHostMsgs; + if ( ! send_with_length( socket, buf, buflen, true ) ) { + break; + } + DBMgr::Get()->RemoveStoredMessage( msgID ); } } /* send_stored_messages */ diff --git a/xwords4/relay/cref.h b/xwords4/relay/cref.h index da1ec947b..f826f74bc 100644 --- a/xwords4/relay/cref.h +++ b/xwords4/relay/cref.h @@ -242,7 +242,6 @@ class CookieRef { void store_message( HostID dest, const unsigned char* buf, unsigned int len ); void send_stored_messages( HostID dest, int socket ); - unsigned int count_msgs_stored( void ) { return m_nHostMsgs; } void printSeeds( const char* caller ); @@ -250,8 +249,6 @@ class CookieRef { static void s_checkAllConnected( void* closure ); static void s_checkAck( void* closure ); - unsigned int m_nHostMsgs; - MsgBufQueue m_hostMsgQueues[4]; vector m_sockets; bool m_gameFull; /* once we've filled up, no more *new* connections ever */