Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
ThreadManager.cpp
Package: FindMail.zip [view]
Upload User: feituo2008
Upload Date: 2013-02-02
Package Size: 493k
Code Size: 9k
Category:
Email Client
Development Platform:
Visual C++
- #include "StdAfx.h"
- #include "threadmanager.h"
- #include "util.h"
- char g_cur_path[128];
- DWORD WINAPI ThreadCheckStat(PVOID params);
- CThreadManager::CThreadManager(void)
- : m_thread_count(0)
- , m_pThreads(NULL)
- , m_pstart_pos(NULL)
- , m_fp_stat(NULL)
- , m_f_stop(0)
- , m_dict_lines_count(0)
- , m_pdict_buf(NULL)
- {
- m_stat_file[0] =m_dict_file[0] =m_domain_name[0] =m_mx_server[0] =0;
- GetCurrentDirectory(sizeof(g_cur_path), g_cur_path);
- m_hThreadCheckStat =NULL;
- m_hRasConn =NULL;
- }
- CThreadManager::~CThreadManager(void)
- {
- Stop();
- }
- 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)
- {
- strcpy(m_domain_name, domain_name);
- strcpy(m_mx_server, mx_server);
- m_thread_count =thread_count;
- m_max_thread_count =max_thread_count;
- m_min_thread_num =min_thread_num;
- if(dial_name) strcpy(m_dial_name, dial_name);
- if(stat_file && *stat_file !=0) strcpy(m_stat_file, stat_file);
- else
- {
- if(m_dict_file[0])
- 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);
- 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);
- }
- if(m_dict_file[0])
- {
- 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);
- 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);
- }
- else
- {
- 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);
- 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);
- }
- if(dict_file)
- strcpy(m_dict_file, dict_file);
- return 0;
- }
- int CThreadManager::Start(int f_continue_prev)
- {
- int i;
- FILE *fp;
- Stop();
- m_f_stop =false;
- if(m_dict_file[0])
- {
- if((fp =fopen(m_dict_file, "r")) ==NULL)
- {
- WriteStat("open dictinary file %s failed!", m_dict_file);
- return -1;
- }
- char buf[60];
- m_dict_lines_count =0;
- while(!feof(fp) && fgets(buf, sizeof(buf), fp))
- {
- strtok(buf, "rn");
- if(m_pdict_buf ==NULL)
- m_pdict_buf =(char **)malloc((m_dict_lines_count+1)*sizeof(char *));
- else m_pdict_buf =(char **)realloc(m_pdict_buf, (m_dict_lines_count+1)*sizeof(char *));
- m_pdict_buf[m_dict_lines_count] =(char *)malloc(60);
- strcpy(m_pdict_buf[m_dict_lines_count], buf);
- m_dict_lines_count++;
- }
- fclose(fp);
- }
- if(m_pstart_pos) free(m_pstart_pos);
- m_pstart_pos =NULL;
- if(f_continue_prev)
- {
- if(ReadStatFile() <0) return -1;
- }
- else
- {
- m_pstart_pos =(int *)malloc(m_thread_count*sizeof(int));
- memset(m_pstart_pos, 0, m_thread_count*sizeof(int));
- }
- if(m_thread_count <=0) return -1;
- InitAllUtilCS();
- DWORD id;
- if((m_hThreadCheckStat =CreateThread(NULL, 0, ThreadCheckStat, this, CREATE_SUSPENDED, &id)) ==NULL)
- {
- WriteStat("Create auto save thread failed!");
- return -1;
- }
- if(m_pThreads) free(m_pThreads);
- m_pThreads =new CFindMailThread[m_thread_count];
- for(i =0; i<m_thread_count; i++)
- {
- m_pThreads[i].m_smtp_client.m_client_num =i;
- 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);
- m_pThreads[i].Start();
- }
- ResumeThread(m_hThreadCheckStat);
- return 0;
- }
- int CThreadManager::Stop(void)
- {
- m_f_stop =true;
- int i;
- if(m_hThreadCheckStat)
- {
- TerminateThread(m_hThreadCheckStat, 0);
- CloseHandle(m_hThreadCheckStat);
- m_hThreadCheckStat =NULL;
- }
- if(m_fp_stat)
- {
- fclose(m_fp_stat);
- m_fp_stat =NULL;
- }
- DeleteAllUtilCS();
- if(m_pThreads)
- {
- WriteStat("stop all thread, please wait...");
- for(i =0; i<m_thread_count; i++)
- m_pThreads[i].Stop();
- SaveStatFile();
- delete[] m_pThreads;
- m_pThreads =NULL;
- m_thread_count =0;
- WriteStat("stop all thread ok!");
- }
- if(m_pstart_pos)
- {
- free(m_pstart_pos);
- m_pstart_pos =NULL;
- }
- if(m_pdict_buf)
- {
- for(i =0; i<m_dict_lines_count; i++)
- free(m_pdict_buf[i]);
- free(m_pdict_buf);
- m_pdict_buf =NULL;
- m_dict_lines_count =0;
- }
- return 0;
- }
- int CThreadManager::ReadStatFile(void)
- {
- char temp[10];
- GetPrivateProfileString("CONFIG", "DOMAIN_NAME", "", m_domain_name, sizeof(m_domain_name), m_stat_file);
- GetPrivateProfileString("CONFIG", "DICT_FILE", "", m_dict_file, sizeof(m_dict_file), m_stat_file);
- m_thread_count =GetPrivateProfileInt("THREADS", "COUNT", 0, m_stat_file);
- if(m_thread_count <=0) return -1;
- m_max_thread_count =GetPrivateProfileInt("THREADS", "MAX_COUNT", 0, m_stat_file);
- m_min_thread_num =GetPrivateProfileInt("THREADS", "MIN_NUM", 0, m_stat_file);
- m_pstart_pos =(int *)malloc(m_thread_count*sizeof(int));
- for(int i =0; i<m_thread_count; i++)
- {
- wsprintf(temp, "%d", i);
- m_pstart_pos[i] =GetPrivateProfileInt("THREADS", temp, -1, m_stat_file)+1;
- }
- return 0;
- }
- int CThreadManager::SaveStatFile(void)
- {
- char temp[10], temp1[10];
- WritePrivateProfileString("CONFIG", "FILE_NAME", m_stat_file, m_stat_file);
- WritePrivateProfileString("CONFIG", "DOMAIN_NAME", m_domain_name, m_stat_file);
- WritePrivateProfileString("CONFIG", "DICT_FILE", m_dict_file, m_stat_file);
- wsprintf(temp, "%d", m_thread_count);
- WritePrivateProfileString("THREADS", "COUNT", temp, m_stat_file);
- wsprintf(temp, "%d", m_max_thread_count);
- WritePrivateProfileString("THREADS", "MAX_COUNT", temp, m_stat_file);
- wsprintf(temp, "%d", m_min_thread_num);
- WritePrivateProfileString("THREADS", "MIN_NUM", temp, m_stat_file);
- for(int i =0; i<m_thread_count; i++)
- {
- wsprintf(temp1, "%d", i);
- wsprintf(temp, "%d", m_pThreads[i].m_cur_pos);
- WritePrivateProfileString("THREADS", temp1, temp, m_stat_file);
- }
- return 0;
- }
- DWORD WINAPI ThreadCheckStat(PVOID params)
- {
- time_t t1, t2;
- CThreadManager *pmanager =(CThreadManager *)params;
- time(&t1);
- t2 =t1;
- int *old_pos = new int[pmanager->m_thread_count];
- for(int i =0; i<pmanager->m_thread_count; i++)
- old_pos[i] =pmanager->m_pThreads[i].m_cur_pos;
- while(!pmanager->m_f_stop)
- {
- time(&t2);
- for(i =0; i<pmanager->m_thread_count; i++)
- {
- if(old_pos[i] !=pmanager->m_pThreads[i].m_cur_pos)
- {
- t1=t2;
- old_pos[i]=pmanager->m_pThreads[i].m_cur_pos;
- }
- }
- if(t2-t1 > 20)
- {
- if(pmanager->m_dial_name[0])
- {
- WriteError("******* Hangup, redial... ********");
- pmanager->SaveStatFile();
- pmanager->RunHangUp();
- Sleep(3000);
- pmanager->RunDial();
- }
- t1 =t2;
- }
- Sleep(1000);
- }
- free(old_pos);
- return 0;
- }
- int CThreadManager::RunDial(void)
- {
- RASDIALPARAMS rdParams;
- DWORD dwRet;
- BOOL f_get_password;
- memset(&rdParams, 0, sizeof(rdParams));
- rdParams.dwSize=sizeof(RASDIALPARAMS);
- strcpy(rdParams.szEntryName,m_dial_name);
- if(RasGetEntryDialParams(NULL, &rdParams, &f_get_password) !=0)
- {
- WriteError("读取拨号网络%s的用户名和密码失败", m_dial_name);
- return -1;
- }
- if(f_get_password ==false)
- {
- WriteError( "读取拨号网络%s的密码失败,请先使用保存密码成功拨号一次", m_dial_name);
- return -1;
- }
- if((dwRet=RasDial(NULL,NULL,&rdParams,0L, NULL, &m_hRasConn)) !=0)
- {
- char err_buf[256];
- RasGetErrorString(dwRet, err_buf, sizeof(err_buf));
- WriteError("拨号失败, name=%s, ret=%d:%s", m_dial_name, dwRet, err_buf);
- return -1;
- }
- WriteError("拨号成功,name=%s", m_dial_name);
- return 0;
- }
- int CThreadManager::RunHangUp(void)
- {
- //if(m_hRasConn)
- {
- // RasHangUp(m_hRasConn);
- m_hRasConn =NULL;
- }
- //else
- {
- RASCONN * lpRasConn;
- DWORD lpcb;
- DWORD lpcConnections;
- lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));
- lpRasConn->dwSize = sizeof(RASCONN);
- lpcb = sizeof(RASCONN);
- int nRet = RasEnumConnections(lpRasConn, &lpcb, &lpcConnections);
- if (nRet != 0)
- return -1;
- else
- {
- for (int i = 0; i < (int)lpcConnections; i++)
- {
- WriteError("connect:%s", lpRasConn->szEntryName);
- if(!strcmpi(lpRasConn->szEntryName, m_dial_name))
- {
- if(RasHangUp(lpRasConn->hrasconn) ==0)
- WriteError("HangUp ok");
- else WriteError("HangUp failed!");
- break;
- }
- lpRasConn++;
- }
- }
- GlobalFree(lpRasConn);
- }
- return 0;
- }