DWORD WINAPI CGMP::sndHandler(LPVOID s) #endif { CGMP* self = (CGMP*)s; while (!self->m_bClosed) { #ifndef WIN32 timespec timeout; timeval now; gettimeofday(&now, 0); timeout.tv_sec = now.tv_sec + 1; timeout.tv_nsec = now.tv_usec * 1000; pthread_mutex_lock(&self->m_SndQueueLock); pthread_cond_timedwait(&self->m_SndQueueCond, &self->m_SndQueueLock, &timeout); pthread_mutex_unlock(&self->m_SndQueueLock); #else WaitForSingleObject(self->m_SndQueueCond, 1000); #endif vector<CMsgRecord*> udtsend; udtsend.clear(); CGuard::enterCS(self->m_SndQueueLock); int64_t ts = CTimer::getTime(); for (list<CMsgRecord*>::iterator i = self->m_lSndQueue.begin(); i != self->m_lSndQueue.end();) { int64_t diff = ts - (*i)->m_llTimeStamp; if (diff > 10 * 1000000) { // timeout, send with UDT... list<CMsgRecord*>::iterator j = i; i ++; udtsend.push_back(*j); self->m_lSndQueue.erase(j); continue; } else if (diff > 1000000) self->UDPsend((*i)->m_strIP.c_str(), (*i)->m_iPort, (*i)->m_pMsg); // check next msg ++ i; } CGuard::leaveCS(self->m_SndQueueLock); for (vector<CMsgRecord*>::iterator i = udtsend.begin(); i != udtsend.end(); ++ i) { self->UDTsend((*i)->m_strIP.c_str(), (*i)->m_iPort, (*i)->m_pMsg); delete (*i)->m_pMsg; delete (*i); } udtsend.clear(); } return NULL; }
DWORD WINAPI CGMP::sndHandler(LPVOID s) #endif { CGMP* self = (CGMP*)s; while (!self->m_bClosed) { #ifndef WIN32 timespec timeout; timeval now; gettimeofday(&now, 0); timeout.tv_sec = now.tv_sec + 1; timeout.tv_nsec = now.tv_usec * 1000; pthread_mutex_lock(&self->m_SndQueueLock); pthread_cond_timedwait(&self->m_SndQueueCond, &self->m_SndQueueLock, &timeout); pthread_mutex_unlock(&self->m_SndQueueLock); #else WaitForSingleObject(self->m_SndQueueCond, 1000); #endif list<list<CMsgRecord*>::iterator> udtsend; udtsend.clear(); CGuard::enterCS(self->m_SndQueueLock); int64_t ts = CTimer::getTime(); for (list<CMsgRecord*>::iterator i = self->m_lSndQueue.begin(); i != self->m_lSndQueue.end(); ++i) { if ((*i)->m_pMsg->m_iLength > m_iMaxUDPMsgSize) { // Send large message using UDT. udtsend.push_back(i); continue; } int64_t diff = ts - (*i)->m_llTimeStamp; if ((diff > 10 * 1000000) && ((*i)->m_pMsg->m_piHeader[0] == 0)) { // timeout, send with UDT... udtsend.push_back(i); //TODO: should probably drop this msg instead of send using UDT continue; } else if (diff > 1000000) { // Don't send out UDP packets too often. self->UDPsend((*i)->m_strIP.c_str(), (*i)->m_iPort, (*i)->m_pMsg); } } CGuard::leaveCS(self->m_SndQueueLock); //Use UDT to send large & undelivered messages. for (list<list<CMsgRecord*>::iterator>::iterator i = udtsend.begin(); i != udtsend.end(); ++ i) { // TODO: erase this msg if send failure caused by connection problem. if (self->UDTsend((**i)->m_strIP.c_str(), (**i)->m_iPort, (**i)->m_pMsg) >= 0) { CGuard::enterCS(self->m_SndQueueLock); delete (**i)->m_pMsg; delete (**i); self->m_lSndQueue.erase(*i); CGuard::leaveCS(self->m_SndQueueLock); } } } return NULL; }