sp_checkEntity.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <set>
  2. #include <string>
  3. #include <list>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <cctype>
  7. #include "sp_checkEntity.h"
  8. #include "modCheck.h"
  9. #include <winpr/windows.h>
  10. #include <winpr/synch.h>
  11. #include <winpr/thread.h>
  12. using namespace std;
  13. class spCheckLock
  14. {
  15. public:
  16. spCheckLock() {
  17. static bool isInit = false;
  18. if (!isInit)
  19. {
  20. InitializeCriticalSection(&cs_);
  21. isInit = true;
  22. }
  23. EnterCriticalSection(&cs_);
  24. }
  25. ~spCheckLock() {
  26. LeaveCriticalSection(&cs_);
  27. }
  28. private:
  29. spCheckLock(const spCheckLock&);
  30. spCheckLock& operator =(const spCheckLock&);
  31. private:
  32. static CRITICAL_SECTION cs_;
  33. };
  34. CRITICAL_SECTION spCheckLock::cs_;
  35. class spModInfo
  36. {
  37. public:
  38. spModInfo(string name);
  39. bool operator == (const string name){
  40. //全部转换成小写再进行比较,原因为发现ent中的实体名不一致
  41. string dstCompareName = name, dstModName = m_name, dstAliasName = m_aliasName;
  42. transform(dstCompareName.begin(), dstCompareName.end(), dstCompareName.begin(), (int(*)(int))tolower);
  43. transform(dstModName.begin(), dstModName.end(), dstModName.begin(), (int(*)(int))tolower);
  44. transform(dstAliasName.begin(), dstAliasName.end(), dstAliasName.begin(), (int(*)(int))tolower);
  45. return (dstCompareName == dstModName) || (0 != dstAliasName.length() ? dstCompareName == dstAliasName : false);
  46. }
  47. bool operator == (const void *module){
  48. return module == this->m_module;
  49. }
  50. void setspModule(void *module){
  51. m_module = module;
  52. }
  53. void AddModThread(DWORD threadId){
  54. toolkit_SetthreadGroup(threadId, m_name.c_str());
  55. }
  56. bool findModThread(DWORD threadId){
  57. return toolkit_checkThreadInName(threadId, m_name.c_str());
  58. }
  59. //return the SpModule class object pointer
  60. void* getspModule(){
  61. return m_module;
  62. }
  63. void setAliasName(const string name){
  64. m_aliasName = name;
  65. }
  66. EntityGloabalResource* getEntityResource(){
  67. return &m_entityResource;
  68. }
  69. const string getEntityName(){
  70. return m_name;
  71. }
  72. private:
  73. //实体资源,实体关闭时需销毁
  74. string m_name;
  75. string m_aliasName;
  76. void* m_module;
  77. void* m_pool;
  78. EntityGloabalResource m_entityResource;
  79. };
  80. list<spModInfo*> g_moduleArr;
  81. spModInfo *g_singleModule = NULL; //for spshell.exe or group 0 entity
  82. spModInfo::spModInfo(string name)
  83. :m_name(name),
  84. m_module(NULL),
  85. m_aliasName("")
  86. {
  87. }
  88. spModInfo* findModuleInfo(string name)
  89. {
  90. if (NULL != g_singleModule)
  91. return g_singleModule;
  92. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  93. {
  94. if (**i == name)
  95. return *i;
  96. }
  97. return NULL;
  98. }
  99. spModInfo* findThreadModuleInfo(DWORD threadId)
  100. {
  101. if (NULL != g_singleModule)
  102. {
  103. SetthreadGroup(GetCurrentThreadId(), g_singleModule->getEntityName().c_str());
  104. return g_singleModule;
  105. }
  106. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  107. {
  108. if ((*i)->findModThread(threadId))
  109. return (*i);
  110. }
  111. return NULL;
  112. }
  113. bool CreateModuleInfo(const char *name)
  114. {
  115. if (NULL == name || NULL != findModuleInfo(name) || NULL != g_singleModule)
  116. return false;
  117. spCheckLock curLock;
  118. toolkit_CreateModuleInfo(name);
  119. spModInfo *curModInfo = new spModInfo(name);
  120. if (!strcmp(name, ENTITY_SINGLE_GROUPNAME))
  121. g_singleModule = curModInfo;
  122. else
  123. g_moduleArr.push_back(curModInfo);
  124. return true;
  125. }
  126. bool SetSpModule(const char *name, void *module)
  127. {
  128. if (NULL == name || NULL == module)
  129. return false;
  130. spCheckLock curLock;
  131. spModInfo *curModInfo = findModuleInfo(name);
  132. if (NULL != curModInfo)
  133. {
  134. curModInfo->setspModule((void*)module);
  135. return true;
  136. }
  137. return false;
  138. }
  139. bool CleanModuleThread(const char *name)
  140. {
  141. if (NULL == name)
  142. return false;
  143. if (NULL != g_singleModule)
  144. return true;
  145. spCheckLock curLock;
  146. string modName = name;
  147. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  148. {
  149. if (**i == modName)
  150. {
  151. //remove threads
  152. toolkit_TerminateThreadExcept(GetCurrentThreadId(), name);
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. bool DestoryModuleInfo(const char *name)
  159. {
  160. if (NULL == name)
  161. return false;
  162. if (NULL != g_singleModule)
  163. return true;
  164. spCheckLock curLock;
  165. string modName = name;
  166. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  167. {
  168. if (**i == modName)
  169. {
  170. delete *i;
  171. g_moduleArr.erase(i);
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. bool SetthreadGroup(DWORD threadId, const char *name)
  178. {
  179. if (NULL == name)
  180. return false;
  181. spCheckLock curLock;
  182. spModInfo *curModInfo = findModuleInfo(name);
  183. if (NULL == curModInfo)
  184. return false;
  185. curModInfo->AddModThread(threadId);
  186. return true;
  187. }
  188. bool SetThreadGroupByResource(DWORD threadId, void *resource)
  189. {
  190. if (NULL == resource)
  191. return false;
  192. spCheckLock curLock;
  193. if (NULL != g_singleModule)
  194. {
  195. if (resource == g_singleModule->getEntityResource()->m_timer)
  196. {
  197. g_singleModule->AddModThread(threadId);
  198. return true;
  199. }
  200. else
  201. return false;
  202. }
  203. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  204. {
  205. EntityGloabalResource *curResource = (*i)->getEntityResource();
  206. if (NULL != curResource && curResource->m_timer == resource)
  207. {
  208. (*i)->AddModThread(threadId);
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. bool findThreadModule(DWORD threadId, void **module)
  215. {
  216. spCheckLock curLock;
  217. spModInfo *curModInfo = findThreadModuleInfo(threadId);
  218. if (NULL != curModInfo)
  219. {
  220. *module = curModInfo->getspModule();
  221. return true;
  222. }
  223. return false;
  224. }
  225. void* findModuleByName(const char *name)
  226. {
  227. if (NULL == name)
  228. return NULL;
  229. spCheckLock curLock;
  230. return findModuleInfo(name);
  231. }
  232. bool setModuleAliasName(const char *aliasName)
  233. {
  234. spCheckLock curLock;
  235. spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
  236. if (NULL != curModInfo)
  237. {
  238. curModInfo->setAliasName(aliasName);
  239. return true;
  240. }
  241. return false;
  242. }
  243. EntityGloabalResource* getEntityResource()
  244. {
  245. spCheckLock curLock;
  246. spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
  247. if (NULL != curModInfo)
  248. return curModInfo->getEntityResource();
  249. return NULL;
  250. }