123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #include <set>
- #include <string>
- #include <list>
- #include <algorithm>
- #include <iterator>
- #include <cctype>
- #include "sp_checkEntity.h"
- #include "modCheck.h"
- #include <winpr/windows.h>
- #include <winpr/synch.h>
- #include <winpr/thread.h>
- using namespace std;
- class spCheckLock
- {
- public:
- spCheckLock() {
- static bool isInit = false;
- if (!isInit)
- {
- InitializeCriticalSection(&cs_);
- isInit = true;
- }
- EnterCriticalSection(&cs_);
- }
- ~spCheckLock() {
- LeaveCriticalSection(&cs_);
- }
- private:
- spCheckLock(const spCheckLock&);
- spCheckLock& operator =(const spCheckLock&);
- private:
- static CRITICAL_SECTION cs_;
- };
- CRITICAL_SECTION spCheckLock::cs_;
- class spModInfo
- {
- public:
- spModInfo(string name);
- bool operator == (const string name){
- //全部转换成小写再进行比较,原因为发现ent中的实体名不一致
- string dstCompareName = name, dstModName = m_name, dstAliasName = m_aliasName;
- transform(dstCompareName.begin(), dstCompareName.end(), dstCompareName.begin(), (int(*)(int))tolower);
- transform(dstModName.begin(), dstModName.end(), dstModName.begin(), (int(*)(int))tolower);
- transform(dstAliasName.begin(), dstAliasName.end(), dstAliasName.begin(), (int(*)(int))tolower);
- return (dstCompareName == dstModName) || (0 != dstAliasName.length() ? dstCompareName == dstAliasName : false);
- }
- bool operator == (const void *module){
- return module == this->m_module;
- }
- void setspModule(void *module){
- m_module = module;
- }
- void AddModThread(DWORD threadId){
- toolkit_SetthreadGroup(threadId, m_name.c_str());
- }
- bool findModThread(DWORD threadId){
- return toolkit_checkThreadInName(threadId, m_name.c_str());
- }
- //return the SpModule class object pointer
- void* getspModule(){
- return m_module;
- }
- void setAliasName(const string name){
- m_aliasName = name;
- }
- EntityGloabalResource* getEntityResource(){
- return &m_entityResource;
- }
- const string getEntityName(){
- return m_name;
- }
- private:
- //实体资源,实体关闭时需销毁
- string m_name;
- string m_aliasName;
- void* m_module;
- void* m_pool;
- EntityGloabalResource m_entityResource;
- };
- list<spModInfo*> g_moduleArr;
- spModInfo *g_singleModule = NULL; //for spshell.exe or group 0 entity
- spModInfo::spModInfo(string name)
- :m_name(name),
- m_module(NULL),
- m_aliasName("")
- {
- }
- spModInfo* findModuleInfo(string name)
- {
- if (NULL != g_singleModule)
- return g_singleModule;
- for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
- {
- if (**i == name)
- return *i;
- }
- return NULL;
- }
- spModInfo* findThreadModuleInfo(DWORD threadId)
- {
- if (NULL != g_singleModule)
- {
- SetthreadGroup(GetCurrentThreadId(), g_singleModule->getEntityName().c_str());
- return g_singleModule;
- }
-
- for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
- {
- if ((*i)->findModThread(threadId))
- return (*i);
- }
- return NULL;
- }
- bool CreateModuleInfo(const char *name)
- {
- if (NULL == name || NULL != findModuleInfo(name) || NULL != g_singleModule)
- return false;
- spCheckLock curLock;
- toolkit_CreateModuleInfo(name);
- spModInfo *curModInfo = new spModInfo(name);
- if (!strcmp(name, ENTITY_SINGLE_GROUPNAME))
- g_singleModule = curModInfo;
- else
- g_moduleArr.push_back(curModInfo);
- return true;
- }
- bool SetSpModule(const char *name, void *module)
- {
- if (NULL == name || NULL == module)
- return false;
- spCheckLock curLock;
- spModInfo *curModInfo = findModuleInfo(name);
- if (NULL != curModInfo)
- {
- curModInfo->setspModule((void*)module);
- return true;
- }
- return false;
- }
- bool CleanModuleThread(const char *name)
- {
- if (NULL == name)
- return false;
- if (NULL != g_singleModule)
- return true;
- spCheckLock curLock;
- string modName = name;
- for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
- {
- if (**i == modName)
- {
- //remove threads
- toolkit_TerminateThreadExcept(GetCurrentThreadId(), name);
- return true;
- }
- }
- return false;
- }
- bool DestoryModuleInfo(const char *name)
- {
- if (NULL == name)
- return false;
- if (NULL != g_singleModule)
- return true;
- spCheckLock curLock;
- string modName = name;
- for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
- {
- if (**i == modName)
- {
- delete *i;
- g_moduleArr.erase(i);
- return true;
- }
- }
- return false;
- }
- bool SetthreadGroup(DWORD threadId, const char *name)
- {
- if (NULL == name)
- return false;
- spCheckLock curLock;
- spModInfo *curModInfo = findModuleInfo(name);
- if (NULL == curModInfo)
- return false;
- curModInfo->AddModThread(threadId);
- return true;
- }
- bool SetThreadGroupByResource(DWORD threadId, void *resource)
- {
- if (NULL == resource)
- return false;
- spCheckLock curLock;
- if (NULL != g_singleModule)
- {
- if (resource == g_singleModule->getEntityResource()->m_timer)
- {
- g_singleModule->AddModThread(threadId);
- return true;
- }
- else
- return false;
- }
-
- for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
- {
- EntityGloabalResource *curResource = (*i)->getEntityResource();
- if (NULL != curResource && curResource->m_timer == resource)
- {
- (*i)->AddModThread(threadId);
- return true;
- }
- }
- return false;
- }
- bool findThreadModule(DWORD threadId, void **module)
- {
- spCheckLock curLock;
- spModInfo *curModInfo = findThreadModuleInfo(threadId);
- if (NULL != curModInfo)
- {
- *module = curModInfo->getspModule();
- return true;
- }
- return false;
- }
- void* findModuleByName(const char *name)
- {
- if (NULL == name)
- return NULL;
- spCheckLock curLock;
- return findModuleInfo(name);
- }
- bool setModuleAliasName(const char *aliasName)
- {
- spCheckLock curLock;
- spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
- if (NULL != curModInfo)
- {
- curModInfo->setAliasName(aliasName);
- return true;
- }
- return false;
- }
- EntityGloabalResource* getEntityResource()
- {
- spCheckLock curLock;
- spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
- if (NULL != curModInfo)
- return curModInfo->getEntityResource();
- return NULL;
- }
|