From 93e78f0795929ac87f4b85ffc9af077b257dbcda Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 16 Mar 2018 14:54:01 +0300 Subject: chat api: - useless locks removed, because SM_FindSession already does locking; - lock added to SM_FindSessionByIndex because it's really needed --- src/mir_app/src/chat.h | 1 + src/mir_app/src/chat_manager.cpp | 4 +++- src/mir_app/src/chat_svc.cpp | 48 ++++++++++++++++++---------------------- 3 files changed, 25 insertions(+), 28 deletions(-) (limited to 'src/mir_app') diff --git a/src/mir_app/src/chat.h b/src/mir_app/src/chat.h index fbb6fcf539..eb149a96fd 100644 --- a/src/mir_app/src/chat.h +++ b/src/mir_app/src/chat.h @@ -75,6 +75,7 @@ BOOL SM_SetStatus(const char *pszModule, SESSION_INFO *si, int wStatus); BOOL SM_TakeStatus(const wchar_t *pszID, const char *pszModule, const wchar_t *pszUID, const wchar_t *pszStatus); SESSION_INFO* SM_FindSession(const wchar_t *pszID, const char *pszModule); +SESSION_INFO* SM_FindSessionByIndex(const char *pszModule, int iItem); STATUSINFO* TM_AddStatus(STATUSINFO **ppStatusList, const wchar_t *pszStatus, int *iCount); WORD TM_StringToWord(STATUSINFO *pStatusList, const wchar_t *pszStatus); diff --git a/src/mir_app/src/chat_manager.cpp b/src/mir_app/src/chat_manager.cpp index 55c42c9d42..783b7539dd 100644 --- a/src/mir_app/src/chat_manager.cpp +++ b/src/mir_app/src/chat_manager.cpp @@ -471,9 +471,11 @@ static int SM_GetCount(const char *pszModule) return count; } -static SESSION_INFO* SM_FindSessionByIndex(const char *pszModule, int iItem) +SESSION_INFO* SM_FindSessionByIndex(const char *pszModule, int iItem) { int count = 0; + + mir_cslock lck(csChat); for (auto &si : g_arSessions) { if (!mir_strcmpi(pszModule, si->pszModule)) { if (iItem == count) diff --git a/src/mir_app/src/chat_svc.cpp b/src/mir_app/src/chat_svc.cpp index a6e17cb81b..0984708610 100644 --- a/src/mir_app/src/chat_svc.cpp +++ b/src/mir_app/src/chat_svc.cpp @@ -131,13 +131,11 @@ EXTERN_C MIR_APP_DLL(int) Chat_GetInfo(GC_INFO *gci) if (!gci || !gci->pszModule) return 1; - mir_cslock lck(csChat); - SESSION_INFO *si; if (gci->Flags & GCF_BYINDEX) - si = chatApi.SM_FindSessionByIndex(gci->pszModule, gci->iItem); + si = SM_FindSessionByIndex(gci->pszModule, gci->iItem); else - si = chatApi.SM_FindSession(gci->pszID, gci->pszModule); + si = SM_FindSession(gci->pszID, gci->pszModule); if (si == nullptr) return 1; @@ -195,7 +193,7 @@ EXTERN_C MIR_APP_DLL(GCSessionInfoBase*) Chat_NewSession( return nullptr; // try to restart a session first - SESSION_INFO *si = chatApi.SM_FindSession(ptszID, pszModule); + SESSION_INFO *si = SM_FindSession(ptszID, pszModule); if (si != nullptr) { chatApi.UM_RemoveAll(&si->pUsers); chatApi.TM_RemoveAll(&si->pStatuses); @@ -272,7 +270,7 @@ static INT_PTR __stdcall stubRoomControl(void *param) mir_cslock lck(csChat); SESSION_INFO *si = nullptr; if (p->szModule) - si = chatApi.SM_FindSession(p->wszId, p->szModule); + si = SM_FindSession(p->wszId, p->szModule); switch (p->command) { case WINDOW_HIDDEN: @@ -365,7 +363,7 @@ MIR_APP_DLL(int) Chat_Terminate(const char *szModule, const wchar_t *wszId, bool static void AddUser(GCEVENT *gce) { - SESSION_INFO *si = chatApi.SM_FindSession(gce->ptszID, gce->pszModule); + SESSION_INFO *si = SM_FindSession(gce->ptszID, gce->pszModule); if (si == nullptr) return; @@ -438,7 +436,7 @@ static INT_PTR CALLBACK sttEventStub(void *_param) return SM_SetContactStatus(gce->ptszID, gce->pszModule, gce->ptszUID, (WORD)gce->dwItemData); case GC_EVENT_TOPIC: - if (SESSION_INFO *si = chatApi.SM_FindSession(gce->ptszID, gce->pszModule)) { + if (SESSION_INFO *si = SM_FindSession(gce->ptszID, gce->pszModule)) { wchar_t *pwszNew = RemoveFormatting(gce->ptszText); if (!mir_wstrcmp(si->ptszTopic, pwszNew)) // nothing changed? exiting return 0; @@ -474,7 +472,7 @@ static INT_PTR CALLBACK sttEventStub(void *_param) case GC_EVENT_MESSAGE: case GC_EVENT_ACTION: if (!gce->bIsMe && gce->ptszID && gce->ptszText) { - SESSION_INFO *si = chatApi.SM_FindSession(gce->ptszID, gce->pszModule); + SESSION_INFO *si = SM_FindSession(gce->ptszID, gce->pszModule); bIsHighlighted = chatApi.IsHighlighted(si, gce); } break; @@ -524,7 +522,7 @@ static INT_PTR CALLBACK sttEventStub(void *_param) if (gce->dwFlags & GCEF_SILENT) return 0; - SESSION_INFO *si = chatApi.SM_FindSession(pWnd, pMod); + SESSION_INFO *si = SM_FindSession(pWnd, pMod); // fix for IRC's old style mode notifications. Should not affect any other protocol if ((gce->iType == GC_EVENT_ADDSTATUS || gce->iType == GC_EVENT_REMOVESTATUS) && !(gce->dwFlags & GCEF_ADDTOLOG)) @@ -603,11 +601,7 @@ MIR_APP_DLL(int) Chat_ChangeSessionName(const char *szModule, const wchar_t *wsz if (wszNewName == nullptr) return GC_EVENT_ERROR; - SESSION_INFO *si; - { - mir_cslock lck(csChat); - si = chatApi.SM_FindSession(wszId, szModule); - } + SESSION_INFO *si = SM_FindSession(wszId, szModule); if (si != nullptr) { // nothing really changed? exiting if (!mir_wstrcmp(si->ptszName, wszNewName)) @@ -642,8 +636,7 @@ MIR_APP_DLL(int) Chat_ChangeUserId(const char *szModule, const wchar_t *wszId, c MIR_APP_DLL(void*) Chat_GetUserInfo(const char *szModule, const wchar_t *wszId) { - mir_cslock lck(csChat); - if (SESSION_INFO *si = chatApi.SM_FindSession(wszId, szModule)) + if (SESSION_INFO *si = SM_FindSession(wszId, szModule)) return si->pItemData; return nullptr; } @@ -652,27 +645,29 @@ MIR_APP_DLL(int) Chat_SendUserMessage(const char *szModule, const wchar_t *wszId { if (wszText == nullptr || szModule == nullptr) return GC_EVENT_ERROR; - + + if (wszId != nullptr) { + SESSION_INFO *si = SM_FindSession(wszId, szModule); + if (si) + if (si->iType == GCW_CHATROOM || si->iType == GCW_PRIVMESS) + Chat_DoEventHook(si, GC_USER_MESSAGE, nullptr, wszText, 0); + return 0; + } + mir_cslock lck(csChat); for (auto &si : g_arSessions) { - if ((wszId && mir_wstrcmpi(si->ptszID, wszId)) || mir_strcmpi(si->pszModule, szModule)) + if (mir_strcmpi(si->pszModule, szModule)) continue; if (si->iType == GCW_CHATROOM || si->iType == GCW_PRIVMESS) Chat_DoEventHook(si, GC_USER_MESSAGE, nullptr, wszText, 0); - if (wszId) - break; } return 0; } MIR_APP_DLL(int) Chat_SetStatusbarText(const char *szModule, const wchar_t *wszId, const wchar_t *wszText) { - SESSION_INFO *si; - { - mir_cslock lck(csChat); - si = chatApi.SM_FindSession(wszId, szModule); - } + SESSION_INFO *si = SM_FindSession(wszId, szModule); if (si != nullptr) { replaceStrW(si->ptszStatusbarText, wszText); if (si->ptszStatusbarText) @@ -707,7 +702,6 @@ MIR_APP_DLL(int) Chat_SetStatusEx(const char *szModule, const wchar_t *wszId, in MIR_APP_DLL(int) Chat_SetUserInfo(const char *szModule, const wchar_t *wszId, void *pItemData) { - mir_cslock lck(csChat); if (SESSION_INFO *si = chatApi.SM_FindSession(wszId, szModule)) { si->pItemData = pItemData; return 0; -- cgit v1.2.3