www.pudn.com > FindMail.zip > ThreadManager.cpp


#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, "\r\n"); 
			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; im_thread_count]; 
	 
	for(int i =0; im_thread_count; i++) 
		old_pos[i] =pmanager->m_pThreads[i].m_cur_pos; 
	 
	while(!pmanager->m_f_stop) 
	{ 
		time(&t2); 
 
		for(i =0; im_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; 
}