From 18ef6358ffbf73021a958baefb6c4be4054e0042 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 14 Jul 2014 13:46:12 +0000 Subject: - debug prints uncommented to log packet codes etc. - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@9793 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/IcqOscarJ/src/icq_rates.cpp | 151 +++++++++++----------------------- 1 file changed, 48 insertions(+), 103 deletions(-) (limited to 'protocols/IcqOscarJ/src/icq_rates.cpp') diff --git a/protocols/IcqOscarJ/src/icq_rates.cpp b/protocols/IcqOscarJ/src/icq_rates.cpp index fb8962f4e0..a77f49c90b 100644 --- a/protocols/IcqOscarJ/src/icq_rates.cpp +++ b/protocols/IcqOscarJ/src/icq_rates.cpp @@ -44,8 +44,8 @@ rates::rates(CIcqProto *ppro, BYTE *pBuffer, WORD wLen) unpackWord(&pBuffer, &wCount); wLen -= 2; - if (wCount > MAX_RATES_GROUP_COUNT) - { // just sanity check + if (wCount > MAX_RATES_GROUP_COUNT) { + // just sanity check ppro->debugLogA("Rates: Error: Data packet contains too many rate groups!"); wCount = MAX_RATES_GROUP_COUNT; } @@ -53,12 +53,10 @@ rates::rates(CIcqProto *ppro, BYTE *pBuffer, WORD wLen) nGroups = wCount; // Parse Group details int i; - for (i=0; i= 35) - { + if (wLen >= 35) { pBuffer += 2; // Group ID unpackDWord(&pBuffer, &pGroup->dwWindowSize); unpackDWord(&pBuffer, &pGroup->dwClearLevel); @@ -69,41 +67,35 @@ rates::rates(CIcqProto *ppro, BYTE *pBuffer, WORD wLen) pBuffer += 5; wLen -= 35; } - else - { // packet broken, put some basic defaults + else { // packet broken, put some basic defaults pGroup->dwWindowSize = 10; pGroup->dwMaxLevel = 5000; } pGroup->rCurrentLevel = pGroup->dwMaxLevel; } // Parse Group associated pairs - for (i=0; inPairs = wNum; - pGroup->pPairs = (WORD*)SAFE_MALLOC(wNum*4); - for (int n=0; npPairs = (WORD*)SAFE_MALLOC(wNum * 4); + for (int n = 0; n < wNum * 2; n++) { WORD wItem; unpackWord(&pBuffer, &wItem); pGroup->pPairs[n] = wItem; } -#ifdef _DEBUG - ppro->debugLogA("Rates: %d# %d pairs.", i+1, wNum); -#endif - wLen -= wNum*4; + + wLen -= wNum * 4; } } - rates::~rates() { for (int i = 0; i < nGroups; i++) @@ -112,19 +104,14 @@ rates::~rates() nGroups = 0; } - WORD rates::getGroupFromSNAC(WORD wFamily, WORD wCommand) { - if (this) - { - for (int i = 0; i < nGroups; i++) - { + if (this) { + for (int i = 0; i < nGroups; i++) { rates_group* pGroup = &groups[i]; - for (int j = 0; j < 2 * pGroup->nPairs; j += 2) - { - if (pGroup->pPairs[j] == wFamily && pGroup->pPairs[j + 1] == wCommand) - { // we found the group + for (int j = 0; j < 2 * pGroup->nPairs; j += 2) { + if (pGroup->pPairs[j] == wFamily && pGroup->pPairs[j + 1] == wCommand) { // we found the group return (WORD)(i + 1); } } @@ -135,13 +122,10 @@ WORD rates::getGroupFromSNAC(WORD wFamily, WORD wCommand) return 0; // Failure } - WORD rates::getGroupFromPacket(icq_packet *pPacket) { - if (this) - { - if (pPacket->nChannel == ICQ_DATA_CHAN && pPacket->wLen >= 0x10) - { + if (this) { + if (pPacket->nChannel == ICQ_DATA_CHAN && pPacket->wLen >= 0x10) { WORD wFamily, wCommand; BYTE *pBuf = pPacket->pData + 6; @@ -154,7 +138,6 @@ WORD rates::getGroupFromPacket(icq_packet *pPacket) return 0; } - rates_group* rates::getGroup(WORD wGroup) { if (this && wGroup && wGroup <= nGroups) @@ -163,21 +146,18 @@ rates_group* rates::getGroup(WORD wGroup) return NULL; } - int rates::getNextRateLevel(WORD wGroup) { rates_group *pGroup = getGroup(wGroup); - if (pGroup) - { - int nLevel = pGroup->rCurrentLevel*(pGroup->dwWindowSize-1)/pGroup->dwWindowSize + (GetTickCount() - pGroup->tCurrentLevel)/pGroup->dwWindowSize; + if (pGroup) { + int nLevel = pGroup->rCurrentLevel*(pGroup->dwWindowSize - 1) / pGroup->dwWindowSize + (GetTickCount() - pGroup->tCurrentLevel) / pGroup->dwWindowSize; return nLevel < (int)pGroup->dwMaxLevel ? nLevel : pGroup->dwMaxLevel; } return -1; // Failure } - int rates::getDelayToLimitLevel(WORD wGroup, int nLevel) { rates_group *pGroup = getGroup(wGroup); @@ -188,11 +168,9 @@ int rates::getDelayToLimitLevel(WORD wGroup, int nLevel) return 0; // Failure } - void rates::packetSent(icq_packet *pPacket) { - if (this) - { + if (this) { WORD wGroup = getGroupFromPacket(pPacket); if (wGroup) @@ -200,30 +178,24 @@ void rates::packetSent(icq_packet *pPacket) } } - void rates::updateLevel(WORD wGroup, int nLevel) { rates_group *pGroup = getGroup(wGroup); - if (pGroup) - { + if (pGroup) { pGroup->rCurrentLevel = nLevel; pGroup->tCurrentLevel = GetTickCount(); -#ifdef _DEBUG + ppro->debugLogA("Rates: New level %d for #%d", nLevel, wGroup); -#endif } } - int rates::getLimitLevel(WORD wGroup, int nLevel) { rates_group *pGroup = getGroup(wGroup); - if (pGroup) - { - switch(nLevel) - { + if (pGroup) { + switch (nLevel) { case RML_CLEAR: return pGroup->dwClearLevel; @@ -234,37 +206,33 @@ int rates::getLimitLevel(WORD wGroup, int nLevel) return pGroup->dwLimitLevel; case RML_IDLE_10: - return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel)/10); + return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10); case RML_IDLE_30: - return pGroup->dwClearLevel + (3*(pGroup->dwMaxLevel - pGroup->dwClearLevel)/10); + return pGroup->dwClearLevel + (3 * (pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10); case RML_IDLE_50: - return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel)/2); + return pGroup->dwClearLevel + ((pGroup->dwMaxLevel - pGroup->dwClearLevel) / 2); case RML_IDLE_70: - return pGroup->dwClearLevel + (7*(pGroup->dwMaxLevel - pGroup->dwClearLevel)/10); + return pGroup->dwClearLevel + (7 * (pGroup->dwMaxLevel - pGroup->dwClearLevel) / 10); } } return 9999; // some high number - without rates we allow anything } - void rates::initAckPacket(icq_packet *pPacket) { - serverPacketInit(pPacket, 10 + nGroups * (int)sizeof(WORD)); + serverPacketInit(pPacket, 10 + nGroups * (int)sizeof(WORD)); packFNACHeader(pPacket, ICQ_SERVICE_FAMILY, ICQ_CLIENT_RATE_ACK); for (WORD wGroup = 1; wGroup <= nGroups; wGroup++) packWord(pPacket, wGroup); } - - // // Rate Level 2 Management ///////////////////////////// - rates_queue_item::rates_queue_item(CIcqProto *ppro, WORD wGroup) : bCreated(FALSE), dwUin(0), szUid(NULL) { this->ppro = ppro; @@ -273,20 +241,18 @@ rates_queue_item::rates_queue_item(CIcqProto *ppro, WORD wGroup) : bCreated(FALS rates_queue_item::~rates_queue_item() { - if (bCreated) - { + if (bCreated) { SAFE_FREE(&szUid); bCreated = FALSE; } } - BOOL rates_queue_item::isEqual(rates_queue_item *pItem) -{ // the same event (equal address of _vftable) for the same contact +{ + // the same event (equal address of _vftable) for the same contact return (pItem->hContact == this->hContact) && (*(void**)pItem == *(void**)this); } - rates_queue_item* rates_queue_item::copyItem(rates_queue_item *pDest) { if (!pDest) @@ -300,15 +266,11 @@ rates_queue_item* rates_queue_item::copyItem(rates_queue_item *pDest) return pDest; } - void rates_queue_item::execute() { -#ifdef _DEBUG ppro->debugLogA("Rates: Error executing abstract event."); -#endif } - BOOL rates_queue_item::isOverRate(int nLevel) { icq_lock l(ppro->m_ratesMutex); @@ -319,7 +281,6 @@ BOOL rates_queue_item::isOverRate(int nLevel) return FALSE; } - rates_queue::rates_queue(CIcqProto *ppro, const char *szDescr, int nLimitLevel, int nWaitLevel, int nDuplicates) { this->listsMutex = new icq_critical_section(); @@ -330,7 +291,6 @@ rates_queue::rates_queue(CIcqProto *ppro, const char *szDescr, int nLimitLevel, duplicates = nDuplicates; } - rates_queue::~rates_queue() { cleanup(); @@ -356,9 +316,7 @@ void __cdecl CIcqProto::rateDelayThread(rate_delay_args *pArgs) void rates_queue::initDelay(int nDelay, IcqRateFunc delaycode) { -#ifdef _DEBUG ppro->debugLogA("Rates: Delay %dms", nDelay); -#endif rate_delay_args *pArgs = (rate_delay_args*)SAFE_MALLOC(sizeof(rate_delay_args)); // This will be freed in the new thread pArgs->queue = this; @@ -376,7 +334,7 @@ void rates_queue::cleanup() if (pendingListSize) ppro->debugLogA("Rates: Purging %d %s(s).", pendingListSize, szDescr); - for (int i=0; i < pendingListSize; i++) + for (int i = 0; i < pendingListSize; i++) delete pendingList[i]; SAFE_FREE((void**)&pendingList); pendingListSize = 0; @@ -388,8 +346,7 @@ void rates_queue::processQueue() if (!pendingList) return; - if (!ppro->icqOnline()) - { + if (!ppro->icqOnline()) { cleanup(); return; } @@ -398,8 +355,7 @@ void rates_queue::processQueue() ppro->m_ratesMutex->Enter(); listsMutex->Enter(); - if (item->isOverRate(limitLevel)) - { // the rate is higher, keep sleeping + if (item->isOverRate(limitLevel)) { // the rate is higher, keep sleeping int nDelay = ppro->m_rates->getDelayToLimitLevel(item->wGroup, ppro->m_rates->getLimitLevel(item->wGroup, waitLevel)); listsMutex->Leave(); @@ -410,8 +366,7 @@ void rates_queue::processQueue() } ppro->m_ratesMutex->Leave(); - if (pendingListSize > 1) - { // we need to keep order + if (pendingListSize > 1) { // we need to keep order memmove(&pendingList[0], &pendingList[1], (pendingListSize - 1) * sizeof(rates_queue_item*)); } else @@ -421,16 +376,13 @@ void rates_queue::processQueue() listsMutex->Leave(); - if (ppro->icqOnline()) - { + if (ppro->icqOnline()) { ppro->debugLogA("Rates: Resuming %s.", szDescr); item->execute(); } - else - ppro->debugLogA("Rates: Discarding %s.", szDescr); + else ppro->debugLogA("Rates: Discarding %s.", szDescr); - if (bSetupTimer) - { + if (bSetupTimer) { // in queue remained some items, setup timer ppro->m_ratesMutex->Enter(); int nDelay = ppro->m_rates->getDelayToLimitLevel(item->wGroup, waitLevel); @@ -453,20 +405,15 @@ void rates_queue::putItem(rates_queue_item *pItem, int nMinDelay) ppro->debugLogA("Rates: Delaying %s.", szDescr); listsMutex->Enter(); - if (pendingListSize) - { - for (int i = 0; i < pendingListSize; i++) - { - if (pendingList[i]->isEqual(pItem)) - { - if (duplicates == -1) - { // discard existing, append new item + if (pendingListSize) { + for (int i = 0; i < pendingListSize; i++) { + if (pendingList[i]->isEqual(pItem)) { + if (duplicates == -1) { // discard existing, append new item delete pendingList[i]; memcpy(&pendingList[i], &pendingList[i + 1], (pendingListSize - i - 1) * sizeof(rates_queue_item*)); bFound = TRUE; } - else if (duplicates == 1) - { // keep existing, ignore new + else if (duplicates == 1) { // keep existing, ignore new listsMutex->Leave(); return; } @@ -474,15 +421,13 @@ void rates_queue::putItem(rates_queue_item *pItem, int nMinDelay) } } } - if (!bFound) - { // not found, enlarge the queue + if (!bFound) { // not found, enlarge the queue pendingListSize++; pendingList = (rates_queue_item**)SAFE_REALLOC(pendingList, pendingListSize * sizeof(rates_queue_item*)); } pendingList[pendingListSize - 1] = pItem->copyItem(); - if (pendingListSize == 1) - { // queue was empty setup timer + if (pendingListSize == 1) { // queue was empty setup timer listsMutex->Leave(); ppro->m_ratesMutex->Enter(); int nDelay = ppro->m_rates->getDelayToLimitLevel(pItem->wGroup, waitLevel); @@ -502,7 +447,7 @@ int CIcqProto::handleRateItem(rates_queue_item *item, int nQueueType, int nMinDe m_ratesMutex->Enter(); switch (nQueueType) { - case RQT_REQUEST: + case RQT_REQUEST: pQueue = m_ratesQueue_Request; break; case RQT_RESPONSE: -- cgit v1.2.3