ThreadManager.cpp
Upload User: feituo2008
Upload Date: 2013-02-02
Package Size: 493k
Code Size: 9k
Category:

Email Client

Development Platform:

Visual C++

  1. #include "StdAfx.h"
  2. #include "threadmanager.h"
  3. #include "util.h"
  4. char g_cur_path[128];
  5. DWORD WINAPI ThreadCheckStat(PVOID params);
  6. CThreadManager::CThreadManager(void)
  7. : m_thread_count(0)
  8. , m_pThreads(NULL)
  9. , m_pstart_pos(NULL)
  10. , m_fp_stat(NULL)
  11. , m_f_stop(0)
  12. , m_dict_lines_count(0)
  13. , m_pdict_buf(NULL)
  14. {
  15. m_stat_file[0] =m_dict_file[0] =m_domain_name[0] =m_mx_server[0] =0;
  16. GetCurrentDirectory(sizeof(g_cur_path), g_cur_path);
  17. m_hThreadCheckStat =NULL;
  18. m_hRasConn =NULL;
  19. }
  20. CThreadManager::~CThreadManager(void)
  21. {
  22. Stop();
  23. }
  24. int CThreadManager::Init(char *domain_name, char *mx_server, char *dial_name, int max_thread_count, int thread_count, int min_thread_num, char * stat_file, char * dict_file)
  25. {
  26. strcpy(m_domain_name, domain_name);
  27. strcpy(m_mx_server, mx_server);
  28. m_thread_count =thread_count;
  29. m_max_thread_count =max_thread_count;
  30. m_min_thread_num =min_thread_num;
  31. if(dial_name) strcpy(m_dial_name, dial_name);
  32. if(stat_file && *stat_file !=0) strcpy(m_stat_file, stat_file);
  33. else
  34. {
  35. if(m_dict_file[0])
  36. wsprintf(m_stat_file, "%s\%s_%d_%d_%d_d.stat", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  37. else wsprintf(m_stat_file, "%s\%s_%d_%d_%d.stat", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  38. }
  39. if(m_dict_file[0])
  40. {
  41. wsprintf(m_outfile_exist, "%s\%s_%d_%d_%d_d.exist", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  42. wsprintf(m_outfile_nonexist, "%s\%s_%d_%d_%d_d.nonexist", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  43. }
  44. else
  45. {
  46. wsprintf(m_outfile_exist, "%s\%s_%d_%d_%d.exist", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  47. wsprintf(m_outfile_nonexist, "%s\%s_%d_%d_%d.nonexist", g_cur_path, m_domain_name, m_max_thread_count, m_min_thread_num, m_min_thread_num+m_thread_count-1);
  48. }
  49. if(dict_file)
  50. strcpy(m_dict_file, dict_file);
  51. return 0;
  52. }
  53. int CThreadManager::Start(int f_continue_prev)
  54. {
  55. int i;
  56. FILE *fp;
  57. Stop();
  58. m_f_stop =false;
  59. if(m_dict_file[0])
  60. {
  61. if((fp =fopen(m_dict_file, "r")) ==NULL)
  62. {
  63. WriteStat("open dictinary file %s failed!", m_dict_file);
  64. return -1;
  65. }
  66. char buf[60];
  67. m_dict_lines_count =0;
  68. while(!feof(fp) && fgets(buf, sizeof(buf), fp))
  69. {
  70. strtok(buf, "rn");
  71. if(m_pdict_buf ==NULL)
  72. m_pdict_buf =(char **)malloc((m_dict_lines_count+1)*sizeof(char *));
  73. else m_pdict_buf =(char **)realloc(m_pdict_buf, (m_dict_lines_count+1)*sizeof(char *));
  74. m_pdict_buf[m_dict_lines_count] =(char *)malloc(60);
  75. strcpy(m_pdict_buf[m_dict_lines_count], buf);
  76. m_dict_lines_count++;
  77. }
  78. fclose(fp);
  79. }
  80. if(m_pstart_pos) free(m_pstart_pos);
  81. m_pstart_pos =NULL;
  82. if(f_continue_prev)
  83. {
  84. if(ReadStatFile() <0) return -1;
  85. }
  86. else
  87. {
  88. m_pstart_pos =(int *)malloc(m_thread_count*sizeof(int));
  89. memset(m_pstart_pos, 0, m_thread_count*sizeof(int));
  90. }
  91. if(m_thread_count <=0) return -1;
  92. InitAllUtilCS();
  93. DWORD id;
  94. if((m_hThreadCheckStat =CreateThread(NULL, 0, ThreadCheckStat, this, CREATE_SUSPENDED, &id)) ==NULL)
  95. {
  96. WriteStat("Create auto save thread failed!");
  97. return -1;
  98. }
  99. if(m_pThreads) free(m_pThreads);
  100. m_pThreads =new CFindMailThread[m_thread_count];
  101. for(i =0; i<m_thread_count; i++)
  102. {
  103. m_pThreads[i].m_smtp_client.m_client_num =i;
  104. m_pThreads[i].Init(m_domain_name, m_mx_server,m_outfile_exist, m_outfile_nonexist, m_min_thread_num+i, m_max_thread_count, m_pstart_pos[i], m_pdict_buf, m_dict_lines_count, (m_dial_name[0] ==0)?60:1);
  105. m_pThreads[i].Start();
  106. }
  107. ResumeThread(m_hThreadCheckStat);
  108. return 0;
  109. }
  110. int CThreadManager::Stop(void)
  111. {
  112. m_f_stop =true;
  113. int i;
  114. if(m_hThreadCheckStat)
  115. {
  116. TerminateThread(m_hThreadCheckStat, 0);
  117. CloseHandle(m_hThreadCheckStat);
  118. m_hThreadCheckStat =NULL;
  119. }
  120. if(m_fp_stat)
  121. {
  122. fclose(m_fp_stat);
  123. m_fp_stat =NULL;
  124. }
  125. DeleteAllUtilCS();
  126. if(m_pThreads)
  127. {
  128. WriteStat("stop all thread, please wait...");
  129. for(i =0; i<m_thread_count; i++)
  130. m_pThreads[i].Stop();
  131. SaveStatFile();
  132. delete[] m_pThreads;
  133. m_pThreads =NULL;
  134. m_thread_count =0;
  135. WriteStat("stop all thread ok!");
  136. }
  137. if(m_pstart_pos)
  138. {
  139. free(m_pstart_pos);
  140. m_pstart_pos =NULL;
  141. }
  142. if(m_pdict_buf)
  143. {
  144. for(i =0; i<m_dict_lines_count; i++)
  145. free(m_pdict_buf[i]);
  146. free(m_pdict_buf);
  147. m_pdict_buf =NULL;
  148. m_dict_lines_count =0;
  149. }
  150. return 0;
  151. }
  152. int CThreadManager::ReadStatFile(void)
  153. {
  154. char temp[10];
  155. GetPrivateProfileString("CONFIG", "DOMAIN_NAME", "", m_domain_name, sizeof(m_domain_name), m_stat_file);
  156. GetPrivateProfileString("CONFIG", "DICT_FILE", "", m_dict_file, sizeof(m_dict_file), m_stat_file);
  157. m_thread_count =GetPrivateProfileInt("THREADS", "COUNT", 0, m_stat_file);
  158. if(m_thread_count <=0) return -1;
  159. m_max_thread_count =GetPrivateProfileInt("THREADS", "MAX_COUNT", 0, m_stat_file);
  160. m_min_thread_num =GetPrivateProfileInt("THREADS", "MIN_NUM", 0, m_stat_file);
  161. m_pstart_pos =(int *)malloc(m_thread_count*sizeof(int));
  162. for(int i =0; i<m_thread_count; i++)
  163. {
  164. wsprintf(temp, "%d", i);
  165. m_pstart_pos[i] =GetPrivateProfileInt("THREADS", temp, -1, m_stat_file)+1;
  166. }
  167. return 0;
  168. }
  169. int CThreadManager::SaveStatFile(void)
  170. {
  171. char temp[10], temp1[10];
  172. WritePrivateProfileString("CONFIG", "FILE_NAME", m_stat_file, m_stat_file);
  173. WritePrivateProfileString("CONFIG", "DOMAIN_NAME", m_domain_name, m_stat_file);
  174. WritePrivateProfileString("CONFIG", "DICT_FILE", m_dict_file, m_stat_file);
  175. wsprintf(temp, "%d", m_thread_count);
  176. WritePrivateProfileString("THREADS", "COUNT", temp, m_stat_file);
  177. wsprintf(temp, "%d", m_max_thread_count);
  178. WritePrivateProfileString("THREADS", "MAX_COUNT", temp, m_stat_file);
  179. wsprintf(temp, "%d", m_min_thread_num);
  180. WritePrivateProfileString("THREADS", "MIN_NUM", temp, m_stat_file);
  181. for(int i =0; i<m_thread_count; i++)
  182. {
  183. wsprintf(temp1, "%d", i);
  184. wsprintf(temp, "%d", m_pThreads[i].m_cur_pos);
  185. WritePrivateProfileString("THREADS", temp1, temp, m_stat_file);
  186. }
  187. return 0;
  188. }
  189. DWORD WINAPI ThreadCheckStat(PVOID params)
  190. {
  191. time_t t1, t2;
  192. CThreadManager *pmanager =(CThreadManager *)params;
  193. time(&t1);
  194. t2 =t1;
  195. int *old_pos = new int[pmanager->m_thread_count];
  196. for(int i =0; i<pmanager->m_thread_count; i++)
  197. old_pos[i] =pmanager->m_pThreads[i].m_cur_pos;
  198. while(!pmanager->m_f_stop)
  199. {
  200. time(&t2);
  201. for(i =0; i<pmanager->m_thread_count; i++)
  202. {
  203. if(old_pos[i] !=pmanager->m_pThreads[i].m_cur_pos)
  204. {
  205. t1=t2;
  206. old_pos[i]=pmanager->m_pThreads[i].m_cur_pos;
  207. }
  208. }
  209. if(t2-t1 > 20)
  210. {
  211. if(pmanager->m_dial_name[0])
  212. {
  213. WriteError("******* Hangup, redial... ********");
  214. pmanager->SaveStatFile();
  215. pmanager->RunHangUp();
  216. Sleep(3000);
  217. pmanager->RunDial();
  218. }
  219. t1 =t2;
  220. }
  221. Sleep(1000);
  222. }
  223. free(old_pos);
  224. return 0;
  225. }
  226. int CThreadManager::RunDial(void)
  227. {
  228. RASDIALPARAMS rdParams;
  229. DWORD dwRet;
  230. BOOL f_get_password;
  231. memset(&rdParams, 0, sizeof(rdParams));
  232. rdParams.dwSize=sizeof(RASDIALPARAMS);
  233. strcpy(rdParams.szEntryName,m_dial_name);
  234. if(RasGetEntryDialParams(NULL, &rdParams, &f_get_password) !=0)
  235. {
  236. WriteError("读取拨号网络%s的用户名和密码失败", m_dial_name);
  237. return -1;
  238. }
  239. if(f_get_password ==false)
  240. {
  241. WriteError( "读取拨号网络%s的密码失败,请先使用保存密码成功拨号一次", m_dial_name);
  242. return -1;
  243. }
  244. if((dwRet=RasDial(NULL,NULL,&rdParams,0L, NULL, &m_hRasConn)) !=0)
  245. {
  246. char err_buf[256];
  247. RasGetErrorString(dwRet, err_buf, sizeof(err_buf));
  248. WriteError("拨号失败, name=%s, ret=%d:%s", m_dial_name, dwRet, err_buf);
  249. return -1;
  250. }
  251. WriteError("拨号成功,name=%s", m_dial_name);
  252. return 0;
  253. }
  254. int CThreadManager::RunHangUp(void)
  255. {
  256. //if(m_hRasConn)
  257. {
  258. // RasHangUp(m_hRasConn);
  259. m_hRasConn =NULL;
  260. }
  261. //else
  262. {
  263. RASCONN * lpRasConn;
  264. DWORD     lpcb;
  265. DWORD     lpcConnections;
  266. lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));
  267. lpRasConn->dwSize = sizeof(RASCONN);
  268. lpcb = sizeof(RASCONN);
  269.  
  270. int nRet = RasEnumConnections(lpRasConn, &lpcb, &lpcConnections);
  271. if (nRet != 0)
  272.     return -1;
  273. else
  274. {
  275. for (int i = 0; i < (int)lpcConnections; i++)
  276.     {
  277. WriteError("connect:%s", lpRasConn->szEntryName);
  278. if(!strcmpi(lpRasConn->szEntryName, m_dial_name))
  279. {
  280. if(RasHangUp(lpRasConn->hrasconn) ==0)
  281. WriteError("HangUp ok");
  282. else WriteError("HangUp failed!");
  283. break;
  284. }
  285. lpRasConn++;
  286. }
  287. }
  288. GlobalFree(lpRasConn);
  289. }
  290. return 0;
  291. }