From 5df94ff719eccd04c7450df35fb5e3f2b36e2ee7 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 13 Jul 2014 10:50:59 +0000 Subject: wrapping the most evident critical sections into a class git-svn-id: http://svn.miranda-ng.org/main/trunk@9785 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/AimOscar/src/packets.cpp | 3 +- protocols/AimOscar/src/proto.cpp | 6 - protocols/AimOscar/src/proto.h | 4 +- protocols/AimOscar/src/utility.cpp | 13 +- protocols/IRCG/src/commandmonitor.cpp | 26 +-- protocols/IRCG/src/irc.h | 10 +- protocols/IRCG/src/irclib.cpp | 97 +++----- protocols/IRCG/src/ircproto.cpp | 23 +- protocols/IRCG/src/main.cpp | 2 - protocols/IRCG/src/services.cpp | 14 +- protocols/IRCG/src/tools.cpp | 38 +--- protocols/JabberG/src/jabber_disco.cpp | 280 ++++++++++++------------ protocols/JabberG/src/jabber_disco.h | 104 +++------ protocols/JabberG/src/jabber_presence_manager.h | 4 +- protocols/JabberG/src/jabber_privacy.h | 4 +- protocols/JabberG/src/jabber_rc.cpp | 59 +++-- protocols/JabberG/src/jabber_rc.h | 34 +-- 17 files changed, 298 insertions(+), 423 deletions(-) (limited to 'protocols') diff --git a/protocols/AimOscar/src/packets.cpp b/protocols/AimOscar/src/packets.cpp index 3a4a5fc7f6..f4f44ab8bf 100644 --- a/protocols/AimOscar/src/packets.cpp +++ b/protocols/AimOscar/src/packets.cpp @@ -65,7 +65,7 @@ int aim_writetlvlong64(unsigned short type, unsigned __int64 value, unsigned sho int CAimProto::aim_sendflap(HANDLE hServerConn, char type,unsigned short length,const char *buf, unsigned short &seqno) { - EnterCriticalSection(&SendingMutex); + mir_cslock lck(SendingMutex); const int slen = FLAP_SIZE + length; char* obuf = (char*)alloca(slen); flap_header *flap = (flap_header*)obuf; @@ -76,7 +76,6 @@ int CAimProto::aim_sendflap(HANDLE hServerConn, char type,unsigned short length, memcpy(&obuf[FLAP_SIZE], buf, length); int rlen= Netlib_Send(hServerConn, obuf, slen, 0); if (rlen == SOCKET_ERROR) seqno--; - LeaveCriticalSection(&SendingMutex); return rlen >= 0 ? 0 : -1; } diff --git a/protocols/AimOscar/src/proto.cpp b/protocols/AimOscar/src/proto.cpp index d492744908..6bf94c926c 100644 --- a/protocols/AimOscar/src/proto.cpp +++ b/protocols/AimOscar/src/proto.cpp @@ -28,9 +28,6 @@ CAimProto::CAimProto(const char* aProtoName, const TCHAR* aUserName) : hChatNavEvent = CreateEvent(NULL, TRUE, FALSE, NULL); hAdminEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - InitializeCriticalSection(&SendingMutex); - InitializeCriticalSection(&connMutex); - CreateProtoService(PS_CREATEACCMGRUI, &CAimProto::SvcCreateAccMgrUI); CreateProtoService(PS_GETMYAWAYMSG, &CAimProto::GetMyAwayMsg); @@ -87,9 +84,6 @@ CAimProto::~CAimProto() Netlib_CloseHandle(m_hNetlibUser); Netlib_CloseHandle(hNetlibPeer); - DeleteCriticalSection(&SendingMutex); - DeleteCriticalSection(&connMutex); - CloseHandle(hAvatarEvent); CloseHandle(hChatNavEvent); CloseHandle(hAdminEvent); diff --git a/protocols/AimOscar/src/proto.h b/protocols/AimOscar/src/proto.h index 382d600348..4382514b16 100644 --- a/protocols/AimOscar/src/proto.h +++ b/protocols/AimOscar/src/proto.h @@ -108,8 +108,8 @@ struct CAimProto : public PROTO int __cdecl OnGCMenuHook(WPARAM wParam,LPARAM lParam); //====| Data |======================================================================== - CRITICAL_SECTION SendingMutex; - CRITICAL_SECTION connMutex; + mir_cs SendingMutex; + mir_cs connMutex; char* COOKIE; int COOKIE_LENGTH; diff --git a/protocols/AimOscar/src/utility.cpp b/protocols/AimOscar/src/utility.cpp index 00af4417d4..28678182a4 100644 --- a/protocols/AimOscar/src/utility.cpp +++ b/protocols/AimOscar/src/utility.cpp @@ -135,15 +135,14 @@ bool CAimProto::wait_conn(HANDLE& hConn, HANDLE& hEvent, unsigned short service) { if (m_iStatus == ID_STATUS_OFFLINE) return false; - - EnterCriticalSection(&connMutex); - if (hConn == NULL && hServerConn) { - debugLogA("Starting Connection."); - hConn = (HANDLE)1; //set so no additional service request attempts are made while aim is still processing the request - aim_new_service_request(hServerConn, seqno, service) ;//general service connection! + mir_cslock lck(connMutex); + if (hConn == NULL && hServerConn) { + debugLogA("Starting Connection."); + hConn = (HANDLE)1; //set so no additional service request attempts are made while aim is still processing the request + aim_new_service_request(hServerConn, seqno, service);//general service connection! + } } - LeaveCriticalSection(&connMutex); if (WaitForSingleObjectEx(hEvent, 10000, TRUE) != WAIT_OBJECT_0) return false; diff --git a/protocols/IRCG/src/commandmonitor.cpp b/protocols/IRCG/src/commandmonitor.cpp index 52d4b03d20..ad1a374ce4 100644 --- a/protocols/IRCG/src/commandmonitor.cpp +++ b/protocols/IRCG/src/commandmonitor.cpp @@ -237,22 +237,22 @@ int CIrcProto::AddOutgoingMessageToDB(MCONTACT hContact, TCHAR* msg) void __cdecl CIrcProto::ResolveIPThread(LPVOID di) { IPRESOLVE* ipr = (IPRESOLVE *)di; - - EnterCriticalSection(&m_resolve); - - if (ipr != NULL && (ipr->iType == IP_AUTO && lstrlenA(m_myHost) == 0 || ipr->iType == IP_MANUAL)) { - hostent* myhost = gethostbyname(ipr->sAddr.c_str()); - if (myhost) { - IN_ADDR in; - memcpy(&in, myhost->h_addr, 4); - if (ipr->iType == IP_AUTO) - mir_snprintf(m_myHost, sizeof(m_myHost), "%s", inet_ntoa(in)); - else - mir_snprintf(m_mySpecifiedHostIP, sizeof(m_mySpecifiedHostIP), "%s", inet_ntoa(in)); + { + mir_cslock lock(m_resolve); + + if (ipr != NULL && (ipr->iType == IP_AUTO && lstrlenA(m_myHost) == 0 || ipr->iType == IP_MANUAL)) { + hostent* myhost = gethostbyname(ipr->sAddr.c_str()); + if (myhost) { + IN_ADDR in; + memcpy(&in, myhost->h_addr, 4); + if (ipr->iType == IP_AUTO) + mir_snprintf(m_myHost, sizeof(m_myHost), "%s", inet_ntoa(in)); + else + mir_snprintf(m_mySpecifiedHostIP, sizeof(m_mySpecifiedHostIP), "%s", inet_ntoa(in)); + } } } - LeaveCriticalSection(&m_resolve); delete ipr; } diff --git a/protocols/IRCG/src/irc.h b/protocols/IRCG/src/irc.h index 89f2edd82d..b74ff0b710 100644 --- a/protocols/IRCG/src/irc.h +++ b/protocols/IRCG/src/irc.h @@ -348,10 +348,10 @@ struct CIrcProto : public PROTO OBJLIST vUserhostReasons; OBJLIST vWhoInProgress; - CRITICAL_SECTION cs; - CRITICAL_SECTION m_gchook; - CRITICAL_SECTION m_resolve; - HANDLE m_evWndCreate; + mir_cs cs; + mir_cs m_gchook; + mir_cs m_resolve; + HANDLE m_evWndCreate; CMString m_statusMessage; int m_iTempCheckTime; @@ -536,7 +536,7 @@ protected : LIST m_dcc_xfers; private : - CRITICAL_SECTION m_dcc; // protect the dcc objects + mir_cs m_dcc; // protect the dcc objects void createMessageFromPchar( const char* p ); void Notify(const CIrcMessage* pmsg); diff --git a/protocols/IRCG/src/irclib.cpp b/protocols/IRCG/src/irclib.cpp index f6a57f3121..d824dbd080 100644 --- a/protocols/IRCG/src/irclib.cpp +++ b/protocols/IRCG/src/irclib.cpp @@ -459,99 +459,82 @@ void __cdecl CIrcProto::ThreadProc(void*) void CIrcProto::AddDCCSession(MCONTACT, CDccSession* dcc) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); CDccSession* p = m_dcc_chats.find(dcc); if (p) m_dcc_chats.remove(p); m_dcc_chats.insert(dcc); - - LeaveCriticalSection(&m_dcc); } void CIrcProto::AddDCCSession(DCCINFO*, CDccSession* dcc) { - EnterCriticalSection(&m_dcc); - + mir_cslock lck(m_dcc); m_dcc_xfers.insert(dcc); - - LeaveCriticalSection(&m_dcc); } void CIrcProto::RemoveDCCSession(MCONTACT hContact) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_chats.getCount(); i++) - if (m_dcc_chats[i]->di->hContact == hContact) { - m_dcc_chats.remove(i); - break; - } - - LeaveCriticalSection(&m_dcc); + if (m_dcc_chats[i]->di->hContact == hContact) { + m_dcc_chats.remove(i); + break; + } } void CIrcProto::RemoveDCCSession(DCCINFO* pdci) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); - for (int i = 0; i < m_dcc_xfers.getCount(); i++) - if (m_dcc_xfers[i]->di == pdci) { - m_dcc_xfers.remove(i); - break; + for (int i = 0; i < m_dcc_xfers.getCount(); i++) { + if (m_dcc_xfers[i]->di == pdci) { + m_dcc_xfers.remove(i); + break; + } } - - LeaveCriticalSection(&m_dcc); } CDccSession* CIrcProto::FindDCCSession(MCONTACT hContact) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_chats.getCount(); i++) - if (m_dcc_chats[i]->di->hContact == hContact) { - LeaveCriticalSection(&m_dcc); - return m_dcc_chats[i]; - } + if (m_dcc_chats[i]->di->hContact == hContact) + return m_dcc_chats[i]; - LeaveCriticalSection(&m_dcc); return 0; } CDccSession* CIrcProto::FindDCCSession(DCCINFO* pdci) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_xfers.getCount(); i++) - if (m_dcc_xfers[i]->di == pdci) { - LeaveCriticalSection(&m_dcc); - return m_dcc_xfers[i]; - } + if (m_dcc_xfers[i]->di == pdci) + return m_dcc_xfers[i]; - LeaveCriticalSection(&m_dcc); return 0; } CDccSession* CIrcProto::FindDCCSendByPort(int iPort) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_xfers.getCount(); i++) { - CDccSession* p = m_dcc_xfers[i]; - if (p->di->iType == DCC_SEND && p->di->bSender && iPort == p->di->iPort) { - LeaveCriticalSection(&m_dcc); + CDccSession *p = m_dcc_xfers[i]; + if (p->di->iType == DCC_SEND && p->di->bSender && iPort == p->di->iPort) return p; - } } - LeaveCriticalSection(&m_dcc); return 0; } CDccSession* CIrcProto::FindDCCRecvByPortAndName(int iPort, const TCHAR* szName) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_xfers.getCount(); i++) { CDccSession* p = m_dcc_xfers[i]; @@ -559,61 +542,51 @@ CDccSession* CIrcProto::FindDCCRecvByPortAndName(int iPort, const TCHAR* szName) if (!getTString(p->di->hContact, "Nick", &dbv)) { if (p->di->iType == DCC_SEND && !p->di->bSender && !lstrcmpi(szName, dbv.ptszVal) && iPort == p->di->iPort) { db_free(&dbv); - LeaveCriticalSection(&m_dcc); return p; } db_free(&dbv); } } - LeaveCriticalSection(&m_dcc); return 0; } CDccSession* CIrcProto::FindPassiveDCCSend(int iToken) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); - for (int i = 0; i < m_dcc_xfers.getCount(); i++) { - if (m_dcc_xfers[i]->iToken == iToken) { - LeaveCriticalSection(&m_dcc); + for (int i = 0; i < m_dcc_xfers.getCount(); i++) + if (m_dcc_xfers[i]->iToken == iToken) return m_dcc_xfers[i]; - } - } - LeaveCriticalSection(&m_dcc); return 0; } CDccSession* CIrcProto::FindPassiveDCCRecv(CMString sName, CMString sToken) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_xfers.getCount(); i++) { - CDccSession* p = m_dcc_xfers[i]; - if (sToken == p->di->sToken && sName == p->di->sContactName) { - LeaveCriticalSection(&m_dcc); + CDccSession *p = m_dcc_xfers[i]; + if (sToken == p->di->sToken && sName == p->di->sContactName) return p; - } } - LeaveCriticalSection(&m_dcc); + return 0; } void CIrcProto::DisconnectAllDCCSessions(bool Shutdown) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_chats.getCount(); i++) - if (m_disconnectDCCChats || Shutdown) - m_dcc_chats[i]->Disconnect(); - - LeaveCriticalSection(&m_dcc); + if (m_disconnectDCCChats || Shutdown) + m_dcc_chats[i]->Disconnect(); } void CIrcProto::CheckDCCTimeout(void) { - EnterCriticalSection(&m_dcc); + mir_cslock lck(m_dcc); for (int i = 0; i < m_dcc_chats.getCount(); i++) { CDccSession* p = m_dcc_chats[i]; @@ -626,8 +599,6 @@ void CIrcProto::CheckDCCTimeout(void) if (time(0) > p->tLastActivity + DCCSENDTIMEOUT) p->Disconnect(); } - - LeaveCriticalSection(&m_dcc); } //////////////////////////////////////////////////////////////////// diff --git a/protocols/IRCG/src/ircproto.cpp b/protocols/IRCG/src/ircproto.cpp index a212afdfdb..8b036c14c8 100644 --- a/protocols/IRCG/src/ircproto.cpp +++ b/protocols/IRCG/src/ircproto.cpp @@ -32,15 +32,13 @@ static int CompareSessions(const CDccSession* p1, const CDccSession* p2) } CIrcProto::CIrcProto(const char* szModuleName, const TCHAR* tszUserName) : -PROTO(szModuleName, tszUserName), -m_dcc_chats(10, CompareSessions), -m_dcc_xfers(10, CompareSessions), -m_ignoreItems(10), -vUserhostReasons(10), -vWhoInProgress(10) -{ - InitializeCriticalSection(&cs); - InitializeCriticalSection(&m_gchook); + PROTO(szModuleName, tszUserName), + m_dcc_chats(10, CompareSessions), + m_dcc_xfers(10, CompareSessions), + m_ignoreItems(10), + vUserhostReasons(10), + vWhoInProgress(10) +{ m_evWndCreate = ::CreateEvent(NULL, FALSE, FALSE, NULL); CreateProtoService(PS_GETMYAWAYMSG, &CIrcProto::GetMyAwayMsg); @@ -67,8 +65,6 @@ vWhoInProgress(10) CreateProtoService("/GetIrcData", &CIrcProto::Scripting_GetIrcData); codepage = CP_ACP; - InitializeCriticalSection(&m_resolve); - InitializeCriticalSection(&m_dcc); InitPrefs(); @@ -152,17 +148,12 @@ CIrcProto::~CIrcProto() Netlib_CloseHandle(m_hNetlibUser); m_hNetlibUser = NULL; Netlib_CloseHandle(hNetlibDCC); hNetlibDCC = NULL; - DeleteCriticalSection(&cs); - DeleteCriticalSection(&m_gchook); - if (hMenuRoot) CallService(MS_CLIST_REMOVEMAINMENUITEM, (WPARAM)hMenuRoot, 0); mir_free(m_alias); CloseHandle(m_evWndCreate); - DeleteCriticalSection(&m_resolve); - DeleteCriticalSection(&m_dcc); KillChatTimer(OnlineNotifTimer); KillChatTimer(OnlineNotifTimer3); } diff --git a/protocols/IRCG/src/main.cpp b/protocols/IRCG/src/main.cpp index 5dd5f1ec82..f7e10448e5 100644 --- a/protocols/IRCG/src/main.cpp +++ b/protocols/IRCG/src/main.cpp @@ -40,7 +40,6 @@ static int sttCompareProtocols(const CIrcProto *p1, const CIrcProto *p2) LIST g_Instances(1, sttCompareProtocols); -void InitTimers( void ); void UninitTimers( void ); // Information about the plugin @@ -92,7 +91,6 @@ extern "C" int __declspec(dllexport) Load() mir_getLP(&pluginInfo); InitIcons(); - InitTimers(); InitServers(); InitContactMenus(); diff --git a/protocols/IRCG/src/services.cpp b/protocols/IRCG/src/services.cpp index 02e7fdbc0d..ae0ecb7267 100644 --- a/protocols/IRCG/src/services.cpp +++ b/protocols/IRCG/src/services.cpp @@ -499,7 +499,7 @@ int __cdecl CIrcProto::GCEventHook(WPARAM wParam, LPARAM lParam) GCHOOK *gch = (GCHOOK*)lParam; CMString S = _T(""); - EnterCriticalSection(&m_gchook); + mir_cslock lock(m_gchook); // handle the hook if (gch) { @@ -769,7 +769,6 @@ int __cdecl CIrcProto::GCEventHook(WPARAM wParam, LPARAM lParam) } } - LeaveCriticalSection(&m_gchook); return 0; } @@ -906,7 +905,7 @@ int __cdecl CIrcProto::GCMenuHook(WPARAM, LPARAM lParam) int __cdecl CIrcProto::OnPreShutdown(WPARAM, LPARAM) { - EnterCriticalSection(&cs); + mir_cslock lock(cs); if (m_perform && IsConnected()) if (DoPerform("Event: Disconnect")) @@ -922,8 +921,6 @@ int __cdecl CIrcProto::OnPreShutdown(WPARAM, LPARAM) m_nickDlg->Close(); if (m_joinDlg) m_joinDlg->Close(); - - LeaveCriticalSection(&cs); return 0; } @@ -1011,9 +1008,10 @@ void __cdecl CIrcProto::ConnectServerThread(void*) nickflag = true; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)Temp, ID_STATUS_CONNECTING); Sleep(100); - EnterCriticalSection(&cs); - Connect(si); - LeaveCriticalSection(&cs); + { + mir_cslock lock(cs); + Connect(si); + } if (IsConnected()) { if (m_mySpecifiedHost[0]) ForkThread(&CIrcProto::ResolveIPThread, new IPRESOLVE(m_mySpecifiedHost, IP_MANUAL)); diff --git a/protocols/IRCG/src/tools.cpp b/protocols/IRCG/src/tools.cpp index 5d07573291..8046eebf0d 100644 --- a/protocols/IRCG/src/tools.cpp +++ b/protocols/IRCG/src/tools.cpp @@ -513,34 +513,21 @@ static int CompareTimers(const TimerPair* p1, const TimerPair* p2) } static OBJLIST timers(10, CompareTimers); -static CRITICAL_SECTION timers_cs; - -void InitTimers(void) -{ - InitializeCriticalSection(&timers_cs); -} +static mir_cs timers_cs; void UninitTimers(void) { - EnterCriticalSection(&timers_cs); + mir_cslock lck(timers_cs); timers.destroy(); - LeaveCriticalSection(&timers_cs); - DeleteCriticalSection(&timers_cs); } CIrcProto* GetTimerOwner(UINT_PTR nIDEvent) { - CIrcProto* result; + mir_cslock lck(timers_cs); - EnterCriticalSection(&timers_cs); TimerPair temp(NULL, nIDEvent); int idx = timers.getIndex(&temp); - if (idx == -1) - result = NULL; - else - result = timers[idx].ppro; - LeaveCriticalSection(&timers_cs); - return result; + return (idx == -1) ? NULL : timers[idx].ppro; } void CIrcProto::SetChatTimer(UINT_PTR &nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc) @@ -550,25 +537,24 @@ void CIrcProto::SetChatTimer(UINT_PTR &nIDEvent, UINT uElapse, TIMERPROC lpTimer nIDEvent = SetTimer(NULL, NULL, uElapse, lpTimerFunc); - EnterCriticalSection(&timers_cs); + mir_cslock lck(timers_cs); timers.insert(new TimerPair(this, nIDEvent)); - LeaveCriticalSection(&timers_cs); } void CIrcProto::KillChatTimer(UINT_PTR &nIDEvent) { - if (nIDEvent) { - EnterCriticalSection(&timers_cs); + if (nIDEvent == 0) + return; + { + mir_cslock lck(timers_cs); TimerPair temp(this, nIDEvent); int idx = timers.getIndex(&temp); if (idx != -1) timers.remove(idx); - - LeaveCriticalSection(&timers_cs); - - KillTimer(NULL, nIDEvent); - nIDEvent = NULL; } + + KillTimer(NULL, nIDEvent); + nIDEvent = NULL; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/JabberG/src/jabber_disco.cpp b/protocols/JabberG/src/jabber_disco.cpp index 452cb746ae..d3030a3b7e 100644 --- a/protocols/JabberG/src/jabber_disco.cpp +++ b/protocols/JabberG/src/jabber_disco.cpp @@ -168,12 +168,10 @@ static void sttApplyNodeIcon(HTREELISTITEM hItem, CJabberSDNode *pNode); void CJabberProto::OnIqResultServiceDiscoveryInfo(HXML iqNode, CJabberIqInfo *pInfo) { - m_SDManager.Lock(); + mir_cslockfull lck(m_SDManager.cs()); CJabberSDNode *pNode = m_SDManager.FindByIqId(pInfo->GetIqId(), TRUE); - if (!pNode) { - m_SDManager.Unlock(); + if (!pNode) return; - } if (pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT) { HXML query = xmlGetChild(iqNode , "query"); @@ -204,7 +202,7 @@ void CJabberProto::OnIqResultServiceDiscoveryInfo(HXML iqNode, CJabberIqInfo *pI pNode->SetInfoRequestId(JABBER_DISCO_RESULT_ERROR); } - m_SDManager.Unlock(); + lck.unlock(); if (m_pDlgServiceDiscovery) { ApplyNodeIcon(pNode->GetTreeItemHandle(), pNode); @@ -214,12 +212,10 @@ void CJabberProto::OnIqResultServiceDiscoveryInfo(HXML iqNode, CJabberIqInfo *pI void CJabberProto::OnIqResultServiceDiscoveryItems(HXML iqNode, CJabberIqInfo *pInfo) { - m_SDManager.Lock(); + mir_cslockfull lck(m_SDManager.cs()); CJabberSDNode *pNode = m_SDManager.FindByIqId(pInfo->GetIqId(), FALSE); - if (!pNode) { - m_SDManager.Unlock(); + if (!pNode) return; - } if (pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT) { HXML query = xmlGetChild(iqNode , "query"); @@ -227,9 +223,8 @@ void CJabberProto::OnIqResultServiceDiscoveryItems(HXML iqNode, CJabberIqInfo *p pNode->SetItemsRequestId(JABBER_DISCO_RESULT_ERROR); else { HXML item; - for (int i = 1; (item = xmlGetNthChild(query, _T("item"), i)) != NULL; i++) { + for (int i = 1; (item = xmlGetNthChild(query, _T("item"), i)) != NULL; i++) pNode->AddChildNode(xmlGetAttrValue(item, _T("jid")), xmlGetAttrValue(item, _T("node")), xmlGetAttrValue(item, _T("name"))); - } pNode->SetItemsRequestId(JABBER_DISCO_RESULT_OK); pNode->SetItemsRequestErrorText(NULL); @@ -248,7 +243,7 @@ void CJabberProto::OnIqResultServiceDiscoveryItems(HXML iqNode, CJabberIqInfo *p pNode->SetItemsRequestId(JABBER_DISCO_RESULT_ERROR); } - m_SDManager.Unlock(); + lck.unlock(); if (m_pDlgServiceDiscovery) { ApplyNodeIcon(pNode->GetTreeItemHandle(), pNode); @@ -259,19 +254,19 @@ void CJabberProto::OnIqResultServiceDiscoveryItems(HXML iqNode, CJabberIqInfo *p void CJabberProto::OnIqResultServiceDiscoveryRootInfo(HXML iqNode, CJabberIqInfo *pInfo) { if (!pInfo->m_pUserData) return; - m_SDManager.Lock(); + + mir_cslockfull lck(m_SDManager.cs()); if (pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT) { HXML query = xmlGetChild(iqNode , "query"); if (query) { HXML feature; - int i; - for (i = 1; (feature = xmlGetNthChild(query, _T("feature"), i)) != NULL; i++) { + for (int i = 1; (feature = xmlGetNthChild(query, _T("feature"), i)) != NULL; i++) { if (!lstrcmp(xmlGetAttrValue(feature, _T("var")), (TCHAR *)pInfo->m_pUserData)) { CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(pInfo->GetReceiver(), xmlGetAttrValue(iqNode, _T("node")), NULL); SendBothRequests(pNode, NULL); break; } } } } - m_SDManager.Unlock(); + lck.unlock(); UI_SAFE_NOTIFY(m_pDlgServiceDiscovery, WM_JABBER_REFRESH); } @@ -282,7 +277,7 @@ void CJabberProto::OnIqResultServiceDiscoveryRootItems(HXML iqNode, CJabberIqInf return; XmlNode packet(NULL); - m_SDManager.Lock(); + mir_cslockfull lck(m_SDManager.cs()); if (pInfo->GetIqType() == JABBER_IQ_TYPE_RESULT) { HXML query = xmlGetChild(iqNode , "query"); if (query) { @@ -298,7 +293,7 @@ void CJabberProto::OnIqResultServiceDiscoveryRootItems(HXML iqNode, CJabberIqInf iq << XQUERY(JABBER_FEAT_DISCO_INFO) << XATTR(_T("node"), szNode); xmlAddChild(packet, iq); } } } - m_SDManager.Unlock(); + lck.unlock(); if (xmlGetChild(packet ,0)) m_ThreadInfo->send(packet); @@ -393,79 +388,80 @@ void CJabberProto::PerformBrowse(HWND hwndDlg) ComboAddRecentString(hwndDlg, IDC_COMBO_JID, "discoWnd_rcJid", szJid); ComboAddRecentString(hwndDlg, IDC_COMBO_NODE, "discoWnd_rcNode", szNode); - if (_tcslen(szJid)) { - HWND hwndList = GetDlgItem(hwndDlg, IDC_TREE_DISCO); - TreeList_Reset(hwndList); + if (szJid[0] == 0) + return; - m_SDManager.Lock(); - m_SDManager.RemoveAll(); - if (!lstrcmp(szJid, _T(SD_FAKEJID_MYAGENTS))) { - sttBrowseMode = SD_BROWSE_MYAGENTS; - JABBER_LIST_ITEM *item = NULL; - LISTFOREACH(i, this, LIST_ROSTER) - { - if ((item=ListGetItemPtrFromIndex(i)) != NULL) { - if (_tcschr(item->jid, '@') == NULL && _tcschr(item->jid, '/') == NULL && item->subscription!=SUB_NONE) { - MCONTACT hContact = HContactFromJID(item->jid); - if (hContact != NULL) - setByte(hContact, "IsTransport", TRUE); + HWND hwndList = GetDlgItem(hwndDlg, IDC_TREE_DISCO); + TreeList_Reset(hwndList); - if (m_lstTransports.getIndex(item->jid) == -1) - m_lstTransports.insert(mir_tstrdup(item->jid)); + mir_cslockfull lck(m_SDManager.cs()); + m_SDManager.RemoveAll(); + if (!lstrcmp(szJid, _T(SD_FAKEJID_MYAGENTS))) { + sttBrowseMode = SD_BROWSE_MYAGENTS; + JABBER_LIST_ITEM *item = NULL; + LISTFOREACH(i, this, LIST_ROSTER) + { + if ((item=ListGetItemPtrFromIndex(i)) != NULL) { + if (_tcschr(item->jid, '@') == NULL && _tcschr(item->jid, '/') == NULL && item->subscription!=SUB_NONE) { + MCONTACT hContact = HContactFromJID(item->jid); + if (hContact != NULL) + setByte(hContact, "IsTransport", TRUE); - CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(item->jid, NULL, NULL); - SendBothRequests(pNode, NULL); - } } - } } - else if (!lstrcmp(szJid, _T(SD_FAKEJID_CONFERENCES))) { - sttBrowseMode = SD_BROWSE_CONFERENCES; - TCHAR *szServerJid = mir_a2t(m_ThreadInfo->server); - CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnIqResultServiceDiscoveryRootItems, JABBER_IQ_TYPE_GET, szServerJid); - pInfo->m_pUserData = (void*)JABBER_FEAT_MUC; - pInfo->SetTimeout(30000); - XmlNodeIq iq(pInfo); - iq << XQUERY(JABBER_FEAT_DISCO_ITEMS); - m_ThreadInfo->send(iq); - mir_free(szServerJid); - } - else if (!lstrcmp(szJid, _T(SD_FAKEJID_AGENTS))) { - sttBrowseMode = SD_BROWSE_AGENTS; - TCHAR *szServerJid = mir_a2t(m_ThreadInfo->server); - CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnIqResultServiceDiscoveryRootItems, JABBER_IQ_TYPE_GET, szServerJid); - pInfo->m_pUserData = (void*)_T("jabber:iq:gateway"); - pInfo->SetTimeout(30000); - XmlNodeIq iq(pInfo); - iq << XQUERY(JABBER_FEAT_DISCO_ITEMS); - m_ThreadInfo->send(iq); - mir_free(szServerJid); - } - else if (!lstrcmp(szJid, _T(SD_FAKEJID_FAVORITES))) { - sttBrowseMode = SD_BROWSE_FAVORITES; - int count = getDword("discoWnd_favCount", 0); - for (int i=0; i < count; i++) { - char setting[MAXMODULELABELLENGTH]; - mir_snprintf(setting, sizeof(setting), "discoWnd_favName_%d", i); - ptrT tszName( getTStringA(setting)); - if (tszName == NULL) - continue; + if (m_lstTransports.getIndex(item->jid) == -1) + m_lstTransports.insert(mir_tstrdup(item->jid)); + + CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(item->jid, NULL, NULL); + SendBothRequests(pNode, NULL); + } } + } } + else if (!lstrcmp(szJid, _T(SD_FAKEJID_CONFERENCES))) { + sttBrowseMode = SD_BROWSE_CONFERENCES; + TCHAR *szServerJid = mir_a2t(m_ThreadInfo->server); + CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnIqResultServiceDiscoveryRootItems, JABBER_IQ_TYPE_GET, szServerJid); + pInfo->m_pUserData = (void*)JABBER_FEAT_MUC; + pInfo->SetTimeout(30000); + XmlNodeIq iq(pInfo); + iq << XQUERY(JABBER_FEAT_DISCO_ITEMS); + m_ThreadInfo->send(iq); + mir_free(szServerJid); + } + else if (!lstrcmp(szJid, _T(SD_FAKEJID_AGENTS))) { + sttBrowseMode = SD_BROWSE_AGENTS; + TCHAR *szServerJid = mir_a2t(m_ThreadInfo->server); + CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnIqResultServiceDiscoveryRootItems, JABBER_IQ_TYPE_GET, szServerJid); + pInfo->m_pUserData = (void*)_T("jabber:iq:gateway"); + pInfo->SetTimeout(30000); + XmlNodeIq iq(pInfo); + iq << XQUERY(JABBER_FEAT_DISCO_ITEMS); + m_ThreadInfo->send(iq); + mir_free(szServerJid); + } + else if (!lstrcmp(szJid, _T(SD_FAKEJID_FAVORITES))) { + sttBrowseMode = SD_BROWSE_FAVORITES; + int count = getDword("discoWnd_favCount", 0); + for (int i=0; i < count; i++) { + char setting[MAXMODULELABELLENGTH]; + mir_snprintf(setting, sizeof(setting), "discoWnd_favName_%d", i); + ptrT tszName( getTStringA(setting)); + if (tszName == NULL) + continue; - mir_snprintf(setting, sizeof(setting), "discoWnd_favJID_%d", i); - ptrT dbvJid( getTStringA(setting)); - mir_snprintf(setting, sizeof(setting), "discoWnd_favNode_%d", i); - ptrT dbvNode( getTStringA(setting)); - CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(dbvJid, dbvNode, tszName); - SendBothRequests(pNode, NULL); - } - } - else { - sttBrowseMode = SD_BROWSE_NORMAL; - CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(szJid, _tcslen(szNode) ? szNode : NULL, NULL); + mir_snprintf(setting, sizeof(setting), "discoWnd_favJID_%d", i); + ptrT dbvJid( getTStringA(setting)); + mir_snprintf(setting, sizeof(setting), "discoWnd_favNode_%d", i); + ptrT dbvNode( getTStringA(setting)); + CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(dbvJid, dbvNode, tszName); SendBothRequests(pNode, NULL); } - m_SDManager.Unlock(); - - PostMessage(hwndDlg, WM_JABBER_REFRESH, 0, 0); } + else { + sttBrowseMode = SD_BROWSE_NORMAL; + CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(szJid, _tcslen(szNode) ? szNode : NULL, NULL); + SendBothRequests(pNode, NULL); + } + lck.unlock(); + + PostMessage(hwndDlg, WM_JABBER_REFRESH, 0, 0); } BOOL CJabberProto::IsNodeRegistered(CJabberSDNode *pNode) @@ -731,9 +727,10 @@ void CJabberDlgDiscovery::OnClose() void CJabberDlgDiscovery::OnDestroy() { m_proto->m_pDlgServiceDiscovery = NULL; - m_proto->m_SDManager.Lock(); - m_proto->m_SDManager.RemoveAll(); - m_proto->m_SDManager.Unlock(); + { + mir_cslock lck(m_proto->m_SDManager.cs()); + m_proto->m_SDManager.RemoveAll(); + } TreeList_Destroy(GetDlgItem(m_hwnd, IDC_TREE_DISCO)); CSuper::OnDestroy(); @@ -901,10 +898,11 @@ void CJabberDlgDiscovery::btnBookmarks_OnClick(CCtrlButton *) void CJabberDlgDiscovery::btnRefresh_OnClick(CCtrlButton *) { HTREELISTITEM hItem = (HTREELISTITEM)TreeList_GetActiveItem(GetDlgItem(m_hwnd, IDC_TREE_DISCO)); - if (!hItem) return; + if (!hItem) + return; - m_proto->m_SDManager.Lock(); XmlNode packet(NULL); + mir_cslockfull lck(m_proto->m_SDManager.cs()); CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); if (pNode) { TreeList_ResetItem(GetDlgItem(m_hwnd, IDC_TREE_DISCO), hItem); @@ -912,7 +910,7 @@ void CJabberDlgDiscovery::btnRefresh_OnClick(CCtrlButton *) m_proto->SendBothRequests(pNode, packet); TreeList_MakeFakeParent(hItem, FALSE); } - m_proto->m_SDManager.Unlock(); + lck.unlock(); if (xmlGetChild(packet ,0)) m_proto->m_ThreadInfo->send(packet); @@ -966,7 +964,7 @@ INT_PTR CJabberDlgDiscovery::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) case WM_TIMER: if (wParam == REFRESH_TIMER) { - m_proto->m_SDManager.Lock(); + mir_cslockfull lck(m_proto->m_SDManager.cs()); CJabberSDNode *pNode = m_proto->m_SDManager.GetPrimaryNode(); while (pNode) @@ -984,7 +982,7 @@ INT_PTR CJabberDlgDiscovery::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) m_proto->SyncTree(NULL, pNode); pNode = pNode->GetNext(); } - m_proto->m_SDManager.Unlock(); + lck.unlock(); TreeList_Update(GetDlgItem(m_hwnd, IDC_TREE_DISCO)); KillTimer(m_hwnd, REFRESH_TIMER); m_proto->m_dwSDLastRefresh = GetTickCount(); @@ -1005,24 +1003,25 @@ INT_PTR CJabberDlgDiscovery::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) if (iFirst < 0) return FALSE; if (iLast < 0) iLast = ListView_GetItemCount(hwndList) - 1; - m_proto->m_SDManager.Lock(); XmlNode packet(NULL); - for (int i = iFirst; i <= iLast; i++) { - LVITEM lvi = {0}; - lvi.mask = LVIF_PARAM; - lvi.iItem = i; - ListView_GetItem(hwndList, &lvi); - if (!lvi.lParam) - continue; - - CJabberSDNode *pNode = (CJabberSDNode *)TreeList_GetData((HTREELISTITEM)lvi.lParam); - if (!pNode || pNode->GetInfoRequestId()) - continue; - - m_proto->SendInfoRequest(pNode, packet); + mir_cslock lck(m_proto->m_SDManager.cs()); + for (int i = iFirst; i <= iLast; i++) + { + LVITEM lvi = {0}; + lvi.mask = LVIF_PARAM; + lvi.iItem = i; + ListView_GetItem(hwndList, &lvi); + if (!lvi.lParam) + continue; + + CJabberSDNode *pNode = (CJabberSDNode *)TreeList_GetData((HTREELISTITEM)lvi.lParam); + if (!pNode || pNode->GetInfoRequestId()) + continue; + + m_proto->SendInfoRequest(pNode, packet); + } } - m_proto->m_SDManager.Unlock(); if (xmlGetChild(packet, 0)) m_proto->m_ThreadInfo->send(packet); @@ -1069,26 +1068,24 @@ INT_PTR CJabberDlgDiscovery::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) lvi.iItem = pInfoTip->iItem; ListView_GetItem(pHeader->hwndFrom, &lvi); HTREELISTITEM hItem = (HTREELISTITEM)lvi.lParam; - m_proto->m_SDManager.Lock(); + + mir_cslock lck(m_proto->m_SDManager.cs()); CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); if (pNode) pNode->GetTooltipText(pInfoTip->pszText, pInfoTip->cchTextMax); - - m_proto->m_SDManager.Unlock(); } else if (pHeader->code == TVN_ITEMEXPANDED) { NMTREEVIEW *pNmTreeView = (NMTREEVIEW *)lParam; HTREELISTITEM hItem = (HTREELISTITEM)pNmTreeView->itemNew.hItem; - - m_proto->m_SDManager.Lock(); XmlNode packet(NULL); - CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); - if (pNode) { - m_proto->SendBothRequests(pNode, packet); - TreeList_MakeFakeParent(hItem, FALSE); + { + mir_cslock lck(m_proto->m_SDManager.cs()); + CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); + if (pNode) { + m_proto->SendBothRequests(pNode, packet); + TreeList_MakeFakeParent(hItem, FALSE); + } } - m_proto->m_SDManager.Unlock(); - if (xmlGetChild(packet)) m_proto->m_ThreadInfo->send(packet); } @@ -1283,17 +1280,16 @@ void CJabberProto::ServiceDiscoveryShowMenu(CJabberSDNode *pNode, HTREELISTITEM switch (res) { case SD_ACT_REFRESH: { - m_SDManager.Lock(); XmlNode packet(NULL); - if (pNode) { - TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hItem); - pNode->ResetInfo(); - SendBothRequests(pNode, packet); - TreeList_MakeFakeParent(hItem, FALSE); + mir_cslock lck(m_SDManager.cs()); + if (pNode) { + TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hItem); + pNode->ResetInfo(); + SendBothRequests(pNode, packet); + TreeList_MakeFakeParent(hItem, FALSE); + } } - m_SDManager.Unlock(); - if (xmlGetChild(packet)) m_ThreadInfo->send(packet); } @@ -1301,25 +1297,25 @@ void CJabberProto::ServiceDiscoveryShowMenu(CJabberSDNode *pNode, HTREELISTITEM case SD_ACT_REFRESHCHILDREN: { - m_SDManager.Lock(); XmlNode packet(NULL); - for (int iChild = TreeList_GetChildrenCount(hItem); iChild--;) { - HTREELISTITEM hNode = TreeList_GetChild(hItem, iChild); - CJabberSDNode *pNode = (CJabberSDNode *)TreeList_GetData(hNode); - if (pNode) - { - TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hNode); - pNode->ResetInfo(); - SendBothRequests(pNode, packet); - TreeList_MakeFakeParent(hNode, FALSE); - } - - if (xmlGetChildCount(packet) > 50) { - m_ThreadInfo->send(packet); - packet = XmlNode(NULL); + { + mir_cslock lck(m_SDManager.cs()); + for (int iChild = TreeList_GetChildrenCount(hItem); iChild--;) { + HTREELISTITEM hNode = TreeList_GetChild(hItem, iChild); + CJabberSDNode *pNode = (CJabberSDNode *)TreeList_GetData(hNode); + if (pNode) { + TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hNode); + pNode->ResetInfo(); + SendBothRequests(pNode, packet); + TreeList_MakeFakeParent(hNode, FALSE); + } + + if (xmlGetChildCount(packet) > 50) { + m_ThreadInfo->send(packet); + packet = XmlNode(NULL); + } } } - m_SDManager.Unlock(); if (xmlGetChildCount(packet)) m_ThreadInfo->send(packet); diff --git a/protocols/JabberG/src/jabber_disco.h b/protocols/JabberG/src/jabber_disco.h index 2514cf0119..ad534d35fa 100644 --- a/protocols/JabberG/src/jabber_disco.h +++ b/protocols/JabberG/src/jabber_disco.h @@ -114,7 +114,6 @@ public: } }; -class CJabberSDNode; class CJabberSDNode { protected: @@ -331,97 +330,65 @@ public: pNode->SetNext(m_pChild); m_pChild = pNode; - return TRUE; } - BOOL AppendString(TCHAR **ppBuffer, TCHAR *szString) - { - if (!*ppBuffer) { - *ppBuffer = mir_tstrdup(szString); - return TRUE; - } - *ppBuffer = (TCHAR *)mir_realloc(*ppBuffer, (_tcslen(*ppBuffer) + _tcslen(szString) + 1) * sizeof(TCHAR)); - _tcscat(*ppBuffer, szString); - - return TRUE; - } BOOL SetItemsRequestErrorText(TCHAR *szError) { replaceStrT(m_szItemsError, szError); return TRUE; } + BOOL SetInfoRequestErrorText(TCHAR *szError) { replaceStrT(m_szInfoError, szError); return TRUE; } + BOOL GetTooltipText(TCHAR *szText, int nMaxLength) { - TCHAR *szBuffer = NULL; + CMString tszTmp; - TCHAR szTmp[ 8192 ]; + tszTmp.AppendFormat(_T("Jid: %s\r\n"), m_szJid); - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("Jid: %s\r\n"), m_szJid); - AppendString(&szBuffer, szTmp); - - if (m_szNode) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("%s: %s\r\n"), TranslateT("Node"), m_szNode); - AppendString(&szBuffer, szTmp); - } + if (m_szNode) + tszTmp.AppendFormat(_T("%s: %s\r\n"), TranslateT("Node"), m_szNode); if (m_pIdentities) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("\r\n%s:\r\n"), TranslateT("Identities")); - AppendString(&szBuffer, szTmp); + tszTmp.AppendFormat(_T("\r\n%s:\r\n"), TranslateT("Identities")); CJabberSDIdentity *pIdentity = m_pIdentities; while (pIdentity) { if (pIdentity->GetName()) - mir_sntprintf(szTmp, SIZEOF(szTmp), _T(" %c %s (%s: %s, %s: %s)\r\n"), + tszTmp.AppendFormat(_T(" %c %s (%s: %s, %s: %s)\r\n"), CHR_BULLET, pIdentity->GetName(), TranslateT("category"), pIdentity->GetCategory(), TranslateT("type"), pIdentity->GetType()); else - mir_sntprintf(szTmp, SIZEOF(szTmp), _T(" %c %s: %s, %s: %s\r\n"), + tszTmp.AppendFormat(_T(" %c %s: %s, %s: %s\r\n"), CHR_BULLET, TranslateT("Category"), pIdentity->GetCategory(), TranslateT("Type"), pIdentity->GetType()); - AppendString(&szBuffer, szTmp); - pIdentity = pIdentity->GetNext(); } } if (m_pFeatures) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("\r\n%s:\r\n"), TranslateT("Supported features")); - AppendString(&szBuffer, szTmp); - - CJabberSDFeature *pFeature = m_pFeatures; - while (pFeature) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T(" %c %s\r\n"), CHR_BULLET, pFeature->GetVar()); - - AppendString(&szBuffer, szTmp); - - pFeature = pFeature->GetNext(); - } - } - - if (m_szInfoError) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("\r\n%s: %s\r\n"), TranslateT("Info request error"), m_szInfoError); - AppendString(&szBuffer, szTmp); - } + tszTmp.AppendFormat(_T("\r\n%s:\r\n"), TranslateT("Supported features")); - if (m_szItemsError) { - mir_sntprintf(szTmp, SIZEOF(szTmp), _T("\r\n%s: %s\r\n"), TranslateT("Items request error"), m_szItemsError); - AppendString(&szBuffer, szTmp); + for (CJabberSDFeature *pFeature = m_pFeatures; pFeature; pFeature = pFeature->GetNext()) + tszTmp.AppendFormat(_T(" %c %s\r\n"), CHR_BULLET, pFeature->GetVar()); } - szBuffer[lstrlen(szBuffer)-2] = 0; // remove CR/LF - mir_sntprintf(szText, nMaxLength, _T("%s"), szBuffer); + if (m_szInfoError) + tszTmp.AppendFormat(_T("\r\n%s: %s\r\n"), TranslateT("Info request error"), m_szInfoError); - mir_free(szBuffer); + if (m_szItemsError) + tszTmp.AppendFormat(_T("\r\n%s: %s\r\n"), TranslateT("Items request error"), m_szItemsError); + tszTmp.TrimRight(); + _tcsncpy_s(szText, nMaxLength, tszTmp, _TRUNCATE); return TRUE; } }; @@ -429,42 +396,37 @@ public: class CJabberSDManager { protected: - CRITICAL_SECTION m_cs; + mir_cs m_cs; CJabberSDNode *m_pPrimaryNodes; + public: CJabberSDManager() { m_pPrimaryNodes = NULL; - InitializeCriticalSection(&m_cs); } + ~CJabberSDManager() { - DeleteCriticalSection(&m_cs); RemoveAll(); } + + CRITICAL_SECTION& cs() { return m_cs; } + void RemoveAll() { delete m_pPrimaryNodes; m_pPrimaryNodes = NULL; } - BOOL Lock() - { - EnterCriticalSection(&m_cs); - return TRUE; - } - BOOL Unlock() - { - LeaveCriticalSection(&m_cs); - return TRUE; - } + CJabberSDNode* GetPrimaryNode() { return m_pPrimaryNodes; } + CJabberSDNode* AddPrimaryNode(const TCHAR *szJid, const TCHAR *szNode, const TCHAR *szName) { if (!szJid) - return FALSE; + return NULL; CJabberSDNode *pNode = new CJabberSDNode(szJid, szNode, szName); if (!pNode) @@ -472,19 +434,15 @@ public: pNode->SetNext(m_pPrimaryNodes); m_pPrimaryNodes = pNode; - return pNode; } + CJabberSDNode* FindByIqId(int nIqId, BOOL bInfoId = TRUE) { - CJabberSDNode *pNode = NULL; - CJabberSDNode *pTmpNode = NULL; - pNode = m_pPrimaryNodes; - while (pNode) { - if (pTmpNode = pNode->FindByIqId(nIqId, bInfoId)) + for (CJabberSDNode *pNode = m_pPrimaryNodes; pNode; pNode = pNode->GetNext()) + if (CJabberSDNode *pTmpNode = pNode->FindByIqId(nIqId, bInfoId)) return pTmpNode; - pNode = pNode->GetNext(); - } + return NULL; } }; diff --git a/protocols/JabberG/src/jabber_presence_manager.h b/protocols/JabberG/src/jabber_presence_manager.h index 97e9077c8a..ff753dfdf6 100644 --- a/protocols/JabberG/src/jabber_presence_manager.h +++ b/protocols/JabberG/src/jabber_presence_manager.h @@ -86,13 +86,12 @@ class CJabberPresenceManager { protected: CJabberProto *ppro; - CRITICAL_SECTION m_cs; + mir_cs m_cs; CJabberPresencePermanentInfo* m_pPermanentHandlers; public: CJabberPresenceManager(CJabberProto* proto) { - InitializeCriticalSection(&m_cs); m_pPermanentHandlers = NULL; ppro = proto; } @@ -105,7 +104,6 @@ public: pInfo = pTmp; } m_pPermanentHandlers = NULL; - DeleteCriticalSection(&m_cs); } BOOL Start() { diff --git a/protocols/JabberG/src/jabber_privacy.h b/protocols/JabberG/src/jabber_privacy.h index 8e7656dbbe..99a76ba966 100644 --- a/protocols/JabberG/src/jabber_privacy.h +++ b/protocols/JabberG/src/jabber_privacy.h @@ -326,7 +326,7 @@ protected: public: CJabberProto* m_proto; - CRITICAL_SECTION m_cs; + mir_cs m_cs; CPrivacyListManager(CJabberProto *ppro) { @@ -334,7 +334,6 @@ public: m_szActiveListName = NULL; m_szDefaultListName = NULL; m_pLists = NULL; - InitializeCriticalSection(&m_cs); m_bModified = FALSE; }; ~CPrivacyListManager() @@ -342,7 +341,6 @@ public: mir_free(m_szActiveListName); mir_free(m_szDefaultListName); RemoveAllLists(); - DeleteCriticalSection(&m_cs); }; void SetActiveListName(const TCHAR *szListName) { diff --git a/protocols/JabberG/src/jabber_rc.cpp b/protocols/JabberG/src/jabber_rc.cpp index cc6dedf04f..0946ac6b76 100644 --- a/protocols/JabberG/src/jabber_rc.cpp +++ b/protocols/JabberG/src/jabber_rc.cpp @@ -74,20 +74,21 @@ BOOL CJabberAdhocManager::HandleItemsRequest(HXML, CJabberIqInfo *pInfo, const T if (!_tcscmp(szNode, JABBER_FEAT_COMMANDS)) { XmlNodeIq iq(_T("result"), pInfo); HXML resultQuery = iq << XQUERY(JABBER_FEAT_DISCO_ITEMS) << XATTR(_T("node"), JABBER_FEAT_COMMANDS); + { + mir_cslock lck(m_cs); - Lock(); - CJabberAdhocNode* pNode = GetFirstNode(); - while (pNode) { - TCHAR *szJid = pNode->GetJid(); - if (!szJid) - szJid = m_pProto->m_ThreadInfo->fullJID; + CJabberAdhocNode* pNode = GetFirstNode(); + while (pNode) { + TCHAR *szJid = pNode->GetJid(); + if (!szJid) + szJid = m_pProto->m_ThreadInfo->fullJID; - resultQuery << XCHILD(_T("item")) << XATTR(_T("jid"), szJid) - << XATTR(_T("node"), pNode->GetNode()) << XATTR(_T("name"), pNode->GetName()); + resultQuery << XCHILD(_T("item")) << XATTR(_T("jid"), szJid) + << XATTR(_T("node"), pNode->GetNode()) << XATTR(_T("name"), pNode->GetName()); - pNode = pNode->GetNext(); + pNode = pNode->GetNext(); + } } - Unlock(); m_pProto->m_ThreadInfo->send(iq); return TRUE; @@ -116,24 +117,22 @@ BOOL CJabberAdhocManager::HandleInfoRequest(HXML, CJabberIqInfo *pInfo, const TC return TRUE; } - Lock(); + mir_cslockfull lck(m_cs); CJabberAdhocNode *pNode = FindNode(szNode); - if (pNode) { - XmlNodeIq iq(_T("result"), pInfo); - HXML resultQuery = iq << XQUERY(JABBER_FEAT_DISCO_INFO) << XATTR(_T("node"), JABBER_FEAT_DISCO_INFO); - resultQuery << XCHILD(_T("identity")) << XATTR(_T("name"), pNode->GetName()) - << XATTR(_T("category"), _T("automation")) << XATTR(_T("type"), _T("command-node")); + if (pNode == NULL) + return FALSE; - resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_COMMANDS); - resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_DATA_FORMS); - resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_DISCO_INFO); + XmlNodeIq iq(_T("result"), pInfo); + HXML resultQuery = iq << XQUERY(JABBER_FEAT_DISCO_INFO) << XATTR(_T("node"), JABBER_FEAT_DISCO_INFO); + resultQuery << XCHILD(_T("identity")) << XATTR(_T("name"), pNode->GetName()) + << XATTR(_T("category"), _T("automation")) << XATTR(_T("type"), _T("command-node")); - Unlock(); - m_pProto->m_ThreadInfo->send(iq); - return TRUE; - } - Unlock(); - return FALSE; + resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_COMMANDS); + resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_DATA_FORMS); + resultQuery << XCHILD(_T("feature")) << XATTR(_T("var"), JABBER_FEAT_DISCO_INFO); + lck.unlock(); + m_pProto->m_ThreadInfo->send(iq); + return TRUE; } BOOL CJabberAdhocManager::HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo, const TCHAR *szNode) @@ -142,10 +141,10 @@ BOOL CJabberAdhocManager::HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo HXML commandNode = pInfo->GetChildNode(); - Lock(); + mir_cslockfull lck(m_cs); CJabberAdhocNode* pNode = FindNode(szNode); if (!pNode) { - Unlock(); + lck.unlock(); m_pProto->m_ThreadInfo->send( XmlNodeIq(_T("error"), pInfo) @@ -161,7 +160,7 @@ BOOL CJabberAdhocManager::HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo if (szSessionId) { pSession = FindSession(szSessionId); if (!pSession) { - Unlock(); + lck.unlock(); XmlNodeIq iq(_T("error"), pInfo); HXML errorNode = iq << XCHILD(_T("error")) << XATTR(_T("type"), _T("modify")); @@ -175,7 +174,7 @@ BOOL CJabberAdhocManager::HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo pSession = AddNewSession(); if (!pSession) { - Unlock(); + lck.unlock(); m_pProto->m_ThreadInfo->send( XmlNodeIq(_T("error"), pInfo) @@ -213,7 +212,7 @@ BOOL CJabberAdhocManager::HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo RemoveSession(pSession); pSession = NULL; } - Unlock(); + return TRUE; } diff --git a/protocols/JabberG/src/jabber_rc.h b/protocols/JabberG/src/jabber_rc.h index 526598a5d6..806b42839f 100644 --- a/protocols/JabberG/src/jabber_rc.h +++ b/protocols/JabberG/src/jabber_rc.h @@ -163,7 +163,7 @@ protected: CJabberProto *m_pProto; CJabberAdhocNode* m_pNodes; CJabberAdhocSession* m_pSessions; - CRITICAL_SECTION m_cs; + mir_cs m_cs; CJabberAdhocSession* FindSession(const TCHAR *szSession) { @@ -253,26 +253,16 @@ protected: public: CJabberAdhocManager(CJabberProto* pProto) { - ZeroMemory(this, sizeof(CJabberAdhocManager)); m_pProto = pProto; - InitializeCriticalSection(&m_cs); + m_pNodes = NULL; + m_pSessions = NULL; } ~CJabberAdhocManager() { - if (m_pNodes) - delete m_pNodes; - if (m_pSessions) - delete m_pSessions; - DeleteCriticalSection(&m_cs); - } - void Lock() - { - EnterCriticalSection(&m_cs); - } - void Unlock() - { - LeaveCriticalSection(&m_cs); + delete m_pNodes; + delete m_pSessions; } + BOOL FillDefaultNodes(); BOOL AddNode(TCHAR* szJid, TCHAR* szNode, TCHAR* szName, JABBER_ADHOC_HANDLER pHandler) { @@ -280,7 +270,7 @@ public: if (!pNode) return FALSE; - Lock(); + mir_cslock lck(m_cs); if (!m_pNodes) m_pNodes = pNode; else { @@ -289,24 +279,24 @@ public: pTmp = pTmp->GetNext(); pTmp->SetNext(pNode); } - Unlock(); - return TRUE; } + CJabberAdhocNode* GetFirstNode() { return m_pNodes; } + BOOL HandleItemsRequest(HXML iqNode, CJabberIqInfo *pInfo, const TCHAR *szNode); BOOL HandleInfoRequest(HXML iqNode, CJabberIqInfo *pInfo, const TCHAR *szNode); BOOL HandleCommandRequest(HXML iqNode, CJabberIqInfo *pInfo, const TCHAR *szNode); BOOL ExpireSessions() { - Lock(); + mir_cslock lck(m_cs); DWORD dwExpireTime = GetTickCount() - JABBER_ADHOC_SESSION_EXPIRE_TIME; - while (_ExpireSession(dwExpireTime)); - Unlock(); + while (_ExpireSession(dwExpireTime)) + ; return TRUE; } }; -- cgit v1.2.3