From 16db7a568d04c491298f7ef74162668dc8cf3d85 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 17 Sep 2014 14:12:57 +0000 Subject: complete extinction of seld-made arrays in Jabber git-svn-id: http://svn.miranda-ng.org/main/trunk@10499 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/JabberG/src/jabber_iq.cpp | 291 ++++++++++++-------------------- protocols/JabberG/src/jabber_iq.h | 121 +++++-------- protocols/JabberG/src/jabber_thread.cpp | 2 +- 3 files changed, 155 insertions(+), 259 deletions(-) diff --git a/protocols/JabberG/src/jabber_iq.cpp b/protocols/JabberG/src/jabber_iq.cpp index 5adc5c3116..07ccb6568f 100644 --- a/protocols/JabberG/src/jabber_iq.cpp +++ b/protocols/JabberG/src/jabber_iq.cpp @@ -30,19 +30,22 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "jabber_ibb.h" #include "jabber_rc.h" -static int CompareItems(const CJabberIqPermanentInfo *p1, const CJabberIqPermanentInfo *p2) -{ - return p1->getPriority() - p2->getPriority(); +static int CompareIqs(const CJabberIqInfo *p1, const CJabberIqInfo *p2) +{ return p1->GetPriority() - p2->GetPriority(); +} + +static int ComparePermanent(const CJabberIqPermanentInfo *p1, const CJabberIqPermanentInfo *p2) +{ return p1->getPriority() - p2->getPriority(); } ///////////////////////////////////////////////////////////////////////////////////////// // CJabberIqManager class CJabberIqManager::CJabberIqManager(CJabberProto *proto) : - m_arHandlers(10, &CompareItems) + m_arIqs(10, CompareIqs), + m_arHandlers(10, ComparePermanent) { m_dwLastUsedHandle = 0; - m_pIqs = NULL; m_hExpirerThread = NULL; ppro = proto; } @@ -136,7 +139,7 @@ void __cdecl CJabberProto::ExpirerThread(void* pParam) void CJabberIqManager::ExpirerThread() { while (!m_bExpirerThreadShutdownRequest) { - CJabberIqInfo *pInfo = DetachExpired(); + CJabberIqInfo *pInfo = DetouchExpired(); if (!pInfo) { for (int i=0; !m_bExpirerThreadShutdownRequest && (i < 10); i++) Sleep(50); @@ -146,7 +149,6 @@ void CJabberIqManager::ExpirerThread() continue; } ExpireInfo(pInfo); - delete pInfo; } if (!m_bExpirerThreadShutdownRequest) { @@ -155,7 +157,7 @@ void CJabberIqManager::ExpirerThread() } } -void CJabberIqManager::ExpireInfo(CJabberIqInfo *pInfo, void*) +void CJabberIqManager::ExpireInfo(CJabberIqInfo *pInfo) { if (!pInfo) return; @@ -169,60 +171,35 @@ void CJabberIqManager::ExpireInfo(CJabberIqInfo *pInfo, void*) pInfo->m_nIqType = JABBER_IQ_TYPE_FAIL; (ppro->*(pInfo->m_pHandler))(NULL, pInfo); + + delete pInfo; } -bool CJabberIqManager::ExpireIq(int nIqId) +void CJabberIqManager::ExpireIq(int nIqId) { - CJabberIqInfo *pInfo = DetachInfo(nIqId); - if (pInfo == NULL) - return false; - - do { + while (CJabberIqInfo *pInfo = DetouchInfo(nIqId)) ExpireInfo(pInfo); - delete pInfo; - } - while ((pInfo = DetachInfo(nIqId)) != NULL); - return true; } bool CJabberIqManager::ExpireByUserData(void *pUserData) { - while (true) { - CJabberIqInfo *pInfo = DetachInfo(pUserData); - if (!pInfo) - return false; - - ExpireInfo(pInfo, NULL); - delete pInfo; - return true; + bool bRet = false; + while (CJabberIqInfo *pInfo = DetouchInfo(pUserData)) { + ExpireInfo(pInfo); + bRet = true; } + return bRet; } -bool CJabberIqManager::ExpireAll(void *pUserData) +void CJabberIqManager::ExpireAll() { - while (true) { - CJabberIqInfo *pInfo; - { - mir_cslock lck(m_cs); - pInfo = m_pIqs; - if (pInfo) - m_pIqs = m_pIqs->m_pNext; - } - if (!pInfo) - break; - pInfo->m_pNext = NULL; - ExpireInfo(pInfo, pUserData); - delete pInfo; - } - return true; + while (CJabberIqInfo *pInfo = DetouchInfo()) + ExpireInfo(pInfo); } CJabberIqInfo* CJabberIqManager::AddHandler(JABBER_IQ_HANDLER pHandler, int nIqType, const TCHAR *szReceiver, DWORD dwParamsToParse, int nIqId, void *pUserData, int iPriority) { CJabberIqInfo *pInfo = new CJabberIqInfo(); - if (!pInfo) - return NULL; - pInfo->m_pHandler = pHandler; if (nIqId == -1) nIqId = ppro->SerialNext(); @@ -234,10 +211,29 @@ CJabberIqInfo* CJabberIqManager::AddHandler(JABBER_IQ_HANDLER pHandler, int nIqT pInfo->m_dwTimeout = JABBER_DEFAULT_IQ_REQUEST_TIMEOUT; pInfo->m_iPriority = iPriority; pInfo->SetReceiver(szReceiver); - InsertIq(pInfo); + + mir_cslock lck(m_cs); + m_arIqs.insert(pInfo); return pInfo; } +bool CJabberIqManager::DeleteHandler(CJabberIqInfo *pInfo) +{ + // returns TRUE when pInfo found, or FALSE otherwise + mir_cslockfull lck(m_cs); + + for (int i = 0; i < m_arIqs.getCount(); i++) { + if (m_arIqs[i] == pInfo) { + m_arIqs.remove(i); + lck.unlock(); + ExpireInfo(pInfo); // must expire it to allow the handler to free m_pUserData if necessary + return true; + } + } + + return false; +} + bool CJabberIqManager::HandleIq(int nIqId, HXML pNode) { if (nIqId == -1 || pNode == NULL) @@ -255,7 +251,7 @@ bool CJabberIqManager::HandleIq(int nIqId, HXML pNode) else return false; - CJabberIqInfo *pInfo = DetachInfo(nIqId); + CJabberIqInfo *pInfo = DetouchInfo(nIqId); if (pInfo == NULL) return false; @@ -285,7 +281,7 @@ bool CJabberIqManager::HandleIq(int nIqId, HXML pNode) (ppro->*(pInfo->m_pHandler))(pNode, pInfo); delete pInfo; } - while ((pInfo = DetachInfo(nIqId)) != NULL); + while ((pInfo = DetouchInfo(nIqId)) != NULL); return true; } @@ -307,143 +303,101 @@ bool CJabberIqManager::HandleIqPermanent(HXML pNode) else return FALSE; - if (pInfo.m_nIqTypes & iqInfo.m_nIqType) { - HXML pFirstChild = xmlGetChild(pNode , 0); - if (!pFirstChild || !xmlGetName(pFirstChild)) - return FALSE; - - const TCHAR *szTagName = xmlGetName(pFirstChild); - const TCHAR *szXmlns = xmlGetAttrValue(pFirstChild, _T("xmlns")); - - if ((!pInfo.m_szXmlns || (szXmlns && !_tcscmp(pInfo.m_szXmlns, szXmlns))) && - (!pInfo.m_szTag || !_tcscmp(pInfo.m_szTag, szTagName))) - { - // node suits handler criteria, call the handler - iqInfo.m_pChildNode = pFirstChild; - iqInfo.m_szChildTagName = (TCHAR*)szTagName; - iqInfo.m_szChildTagXmlns = (TCHAR*)szXmlns; - iqInfo.m_szId = (TCHAR*)xmlGetAttrValue(pNode, _T("id")); - iqInfo.m_pUserData = pInfo.m_pUserData; - - if (pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_TO) - iqInfo.m_szTo = (TCHAR*)xmlGetAttrValue(pNode, _T("to")); - - if (pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_FROM) - iqInfo.m_szFrom = (TCHAR*)xmlGetAttrValue(pNode, _T("from")); - - if ((pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_HCONTACT) && (iqInfo.m_szFrom)) - iqInfo.m_hContact = ppro->HContactFromJID(iqInfo.m_szFrom, 3); - - ppro->debugLog(_T("Handling iq id %s, type %s, from %s"), iqInfo.m_szId, szType, iqInfo.m_szFrom); - if ((ppro->*(pInfo.m_pHandler))(pNode, &iqInfo)) - return true; - } + if (!(pInfo.m_nIqTypes & iqInfo.m_nIqType)) + continue; + + HXML pFirstChild = xmlGetChild(pNode , 0); + if (!pFirstChild || !xmlGetName(pFirstChild)) + return FALSE; + + const TCHAR *szTagName = xmlGetName(pFirstChild); + const TCHAR *szXmlns = xmlGetAttrValue(pFirstChild, _T("xmlns")); + + if ((!pInfo.m_szXmlns || (szXmlns && !_tcscmp(pInfo.m_szXmlns, szXmlns))) && + (!pInfo.m_szTag || !_tcscmp(pInfo.m_szTag, szTagName))) + { + // node suits handler criteria, call the handler + iqInfo.m_pChildNode = pFirstChild; + iqInfo.m_szChildTagName = (TCHAR*)szTagName; + iqInfo.m_szChildTagXmlns = (TCHAR*)szXmlns; + iqInfo.m_szId = (TCHAR*)xmlGetAttrValue(pNode, _T("id")); + iqInfo.m_pUserData = pInfo.m_pUserData; + + if (pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_TO) + iqInfo.m_szTo = (TCHAR*)xmlGetAttrValue(pNode, _T("to")); + + if (pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_FROM) + iqInfo.m_szFrom = (TCHAR*)xmlGetAttrValue(pNode, _T("from")); + + if ((pInfo.m_dwParamsToParse & JABBER_IQ_PARSE_HCONTACT) && (iqInfo.m_szFrom)) + iqInfo.m_hContact = ppro->HContactFromJID(iqInfo.m_szFrom, 3); + + ppro->debugLog(_T("Handling iq id %s, type %s, from %s"), iqInfo.m_szId, szType, iqInfo.m_szFrom); + if ((ppro->*(pInfo.m_pHandler))(pNode, &iqInfo)) + return true; } } return false; } -CJabberIqInfo* CJabberIqManager::DetachInfo(int nIqId) +CJabberIqInfo* CJabberIqManager::DetouchInfo() +{ + mir_cslock lck(m_cs); + + CJabberIqInfo *pInfo = m_arIqs[0]; + if (pInfo) + m_arIqs.remove(0); + return pInfo; +} + +CJabberIqInfo* CJabberIqManager::DetouchInfo(int nIqId) { mir_cslock lck(m_cs); - if (!m_pIqs) - return NULL; - - CJabberIqInfo *pInfo = m_pIqs; - if (m_pIqs->m_nIqId == nIqId) { - m_pIqs = pInfo->m_pNext; - pInfo->m_pNext = NULL; - return pInfo; - } - while (pInfo->m_pNext) { - if (pInfo->m_pNext->m_nIqId == nIqId) { - CJabberIqInfo *pRetVal = pInfo->m_pNext; - pInfo->m_pNext = pInfo->m_pNext->m_pNext; - pRetVal->m_pNext = NULL; - return pRetVal; + for (int i = 0; i < m_arIqs.getCount(); i++) { + CJabberIqInfo *pInfo = m_arIqs[i]; + if (pInfo->m_nIqId == nIqId) { + m_arIqs.remove(i); + return pInfo; } - pInfo = pInfo->m_pNext; } return NULL; } -CJabberIqInfo* CJabberIqManager::DetachInfo(void *pUserData) +CJabberIqInfo* CJabberIqManager::DetouchInfo(void *pUserData) { mir_cslock lck(m_cs); - if (!m_pIqs) - return NULL; - - CJabberIqInfo *pInfo = m_pIqs; - if (m_pIqs->m_pUserData == pUserData) { - m_pIqs = pInfo->m_pNext; - pInfo->m_pNext = NULL; - return pInfo; - } - while (pInfo->m_pNext) { - if (pInfo->m_pNext->m_pUserData == pUserData) { - CJabberIqInfo *pRetVal = pInfo->m_pNext; - pInfo->m_pNext = pInfo->m_pNext->m_pNext; - pRetVal->m_pNext = NULL; - return pRetVal; + for (int i = 0; i < m_arIqs.getCount(); i++) { + CJabberIqInfo *pInfo = m_arIqs[i]; + if (pInfo->m_pUserData == pUserData) { + m_arIqs.remove(i); + return pInfo; } - pInfo = pInfo->m_pNext; } return NULL; } -CJabberIqInfo* CJabberIqManager::DetachExpired() +CJabberIqInfo* CJabberIqManager::DetouchExpired() { - mir_cslock lck(m_cs); - if (!m_pIqs) - return NULL; - DWORD dwCurrentTime = GetTickCount(); - CJabberIqInfo *pInfo = m_pIqs; - if (dwCurrentTime - pInfo->m_dwRequestTime > pInfo->m_dwTimeout) { - m_pIqs = pInfo->m_pNext; - pInfo->m_pNext = NULL; - return pInfo; - } + mir_cslock lck(m_cs); - while (pInfo->m_pNext) { - if (dwCurrentTime - pInfo->m_pNext->m_dwRequestTime > pInfo->m_pNext->m_dwTimeout) { - CJabberIqInfo *pRetVal = pInfo->m_pNext; - pInfo->m_pNext = pInfo->m_pNext->m_pNext; - pRetVal->m_pNext = NULL; - return pRetVal; + for (int i = 0; i < m_arIqs.getCount(); i++) { + CJabberIqInfo *pInfo = m_arIqs[i]; + if (dwCurrentTime - pInfo->m_dwRequestTime > pInfo->m_dwTimeout) { + m_arIqs.remove(i); + return pInfo; } - pInfo = pInfo->m_pNext; } + return NULL; } - -// inserts pInfo at a place determined by pInfo->m_iPriority -bool CJabberIqManager::InsertIq(CJabberIqInfo *pInfo) -{ - mir_cslock lck(m_cs); - if (!m_pIqs) - m_pIqs = pInfo; - else { - if (m_pIqs->m_iPriority > pInfo->m_iPriority) { - pInfo->m_pNext = m_pIqs; - m_pIqs = pInfo; - } - else { - CJabberIqInfo *pTmp = m_pIqs; - while (pTmp->m_pNext && pTmp->m_pNext->m_iPriority <= pInfo->m_iPriority) - pTmp = pTmp->m_pNext; - pInfo->m_pNext = pTmp->m_pNext; - pTmp->m_pNext = pInfo; - } - } - return true; -} -// fucking params, maybe just return CJabberIqRequestInfo pointer ? +///////////////////////////////////////////////////////////////////////////////////////// + CJabberIqPermanentInfo* CJabberIqManager::AddPermanentHandler( JABBER_PERMANENT_IQ_HANDLER pHandler, int nIqTypes, @@ -477,32 +431,3 @@ bool CJabberIqManager::DeletePermanentHandler(CJabberIqPermanentInfo *pInfo) mir_cslock lck(m_cs); return m_arHandlers.remove(pInfo) == 1; } - -bool CJabberIqManager::DeleteHandler(CJabberIqInfo *pInfo) -{ - // returns TRUE when pInfo found, or FALSE otherwise - mir_cslockfull lck(m_cs); - if (!m_pIqs) - return false; - - if (m_pIqs == pInfo) { // check first item - m_pIqs = m_pIqs->m_pNext; - lck.unlock(); - ExpireInfo(pInfo); // must expire it to allow the handler to free m_pUserData if necessary - delete pInfo; - return true; - } - - CJabberIqInfo *pTmp = m_pIqs; - while (pTmp->m_pNext) { - if (pTmp->m_pNext == pInfo) { - pTmp->m_pNext = pTmp->m_pNext->m_pNext; - lck.unlock(); - ExpireInfo(pInfo); // must expire it to allow the handler to free m_pUserData if necessary - delete pInfo; - return true; - } - pTmp = pTmp->m_pNext; - } - return false; -} diff --git a/protocols/JabberG/src/jabber_iq.h b/protocols/JabberG/src/jabber_iq.h index 89c28039d7..676fc27435 100644 --- a/protocols/JabberG/src/jabber_iq.h +++ b/protocols/JabberG/src/jabber_iq.h @@ -54,79 +54,50 @@ class CJabberIqInfo protected: friend class CJabberIqManager; JABBER_IQ_HANDLER m_pHandler; - CJabberIqInfo *m_pNext; - int m_nIqId; - DWORD m_dwParamsToParse; - DWORD m_dwRequestTime; - DWORD m_dwTimeout; - TCHAR *m_szReceiver; - int m_iPriority; + int m_nIqId; + DWORD m_dwParamsToParse; + DWORD m_dwRequestTime; + DWORD m_dwTimeout; + TCHAR *m_szReceiver; + int m_iPriority; public: - void *m_pUserData; - int m_nIqType; - TCHAR *m_szFrom; - TCHAR *m_szChildTagXmlns; - TCHAR *m_szChildTagName; - HXML m_pChildNode; + void *m_pUserData; + int m_nIqType; + TCHAR *m_szFrom; + TCHAR *m_szChildTagXmlns; + TCHAR *m_szChildTagName; + HXML m_pChildNode; MCONTACT m_hContact; - TCHAR *m_szTo; - TCHAR *m_szId; + TCHAR *m_szTo; + TCHAR *m_szId; public: __forceinline CJabberIqInfo() - { - memset(this, 0, sizeof(*this)); + { memset(this, 0, sizeof(*this)); } __forceinline ~CJabberIqInfo() - { - mir_free(m_szReceiver); - } + { mir_free(m_szReceiver); + } + + __forceinline void SetReceiver(const TCHAR *szReceiver) { replaceStrT(m_szReceiver, szReceiver); } + __forceinline void SetParamsToParse(DWORD dwParamsToParse) { m_dwParamsToParse = dwParamsToParse; } + __forceinline void SetTimeout(DWORD dwTimeout) { m_dwTimeout = dwTimeout; } + + __forceinline int GetIqId() const { return m_nIqId; } + __forceinline DWORD GetRequestTime() const { return m_dwRequestTime; } + __forceinline int GetIqType() const { return m_nIqType; } + __forceinline void* GetUserData() const { return m_pUserData; } + __forceinline TCHAR* GetFrom() const { return m_szFrom; } + __forceinline TCHAR* GetTo() const { return m_szTo; } + __forceinline TCHAR* GetIdStr() const { return m_szId; } + __forceinline MCONTACT GetHContact() const { return m_hContact; } + __forceinline HXML GetChildNode() const { return m_pChildNode; } + __forceinline TCHAR* GetChildNodeName() const { return m_szChildTagName; } + __forceinline TCHAR* GetReceiver() const { return m_szReceiver; } + __forceinline int GetPriority() const { return m_iPriority; } - __forceinline void SetReceiver(const TCHAR *szReceiver) - { replaceStrT(m_szReceiver, szReceiver); - } - __forceinline TCHAR* GetReceiver() - { return m_szReceiver; - } - __forceinline void SetParamsToParse(DWORD dwParamsToParse) - { m_dwParamsToParse = dwParamsToParse; - } - __forceinline void SetTimeout(DWORD dwTimeout) - { m_dwTimeout = dwTimeout; - } - __forceinline int GetIqId() - { return m_nIqId; - } - __forceinline DWORD GetRequestTime() - { return m_dwRequestTime; - } - __forceinline int GetIqType() - { return m_nIqType; - } - __forceinline void* GetUserData() - { return m_pUserData; - } - __forceinline TCHAR* GetFrom() - { return m_szFrom; - } - __forceinline TCHAR* GetTo() - { return m_szTo; - } - __forceinline TCHAR* GetIdStr() - { return m_szId; - } - __forceinline MCONTACT GetHContact() - { return m_hContact; - } - __forceinline HXML GetChildNode() - { return m_pChildNode; - } - __forceinline TCHAR* GetChildNodeName() - { return m_szChildTagName; - } - char* GetCharIqType() { switch (m_nIqType) { @@ -169,22 +140,22 @@ class CJabberIqManager { protected: CJabberProto *ppro; + mir_cs m_cs; - DWORD m_dwLastUsedHandle; - CJabberIqInfo *m_pIqs; // list of iqs ordered by priority + DWORD m_dwLastUsedHandle; + HANDLE m_hExpirerThread; - BOOL m_bExpirerThreadShutdownRequest; + BOOL m_bExpirerThreadShutdownRequest; + LIST m_arIqs; OBJLIST m_arHandlers; - CJabberIqInfo* DetachInfo(int nIqId); - CJabberIqInfo* DetachInfo(void *pUserData); - CJabberIqInfo* DetachExpired(); + CJabberIqInfo* DetouchInfo(); + CJabberIqInfo* DetouchInfo(int nIqId); + CJabberIqInfo* DetouchInfo(void *pUserData); + CJabberIqInfo* DetouchExpired(); - void ExpireInfo(CJabberIqInfo *pInfo, void *pUserData = NULL); - - // inserts pInfo at a place determined by pInfo->m_iPriority - bool InsertIq(CJabberIqInfo *pInfo); + void ExpireInfo(CJabberIqInfo *pInfo); public: CJabberIqManager(CJabberProto* proto); @@ -204,10 +175,10 @@ public: bool HandleIq(int nIqId, HXML pNode); bool HandleIqPermanent(HXML pNode); - bool ExpireIq(int nIqId); + void ExpireIq(int nIqId); void ExpirerThread(void); bool ExpireByUserData(void *pUserData); - bool ExpireAll(void *pUserData = NULL); + void ExpireAll(); void FillPermanentHandlers(); }; diff --git a/protocols/JabberG/src/jabber_thread.cpp b/protocols/JabberG/src/jabber_thread.cpp index 67992d8192..12691746bb 100644 --- a/protocols/JabberG/src/jabber_thread.cpp +++ b/protocols/JabberG/src/jabber_thread.cpp @@ -530,7 +530,7 @@ recvRest: } if (info->type == JABBER_SESSION_NORMAL) { - m_iqManager.ExpireAll(info); + m_iqManager.ExpireAll(); m_bJabberOnline = FALSE; info->zlibUninit(); EnableMenuItems(FALSE); -- cgit v1.2.3