⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tran.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
📖 第 1 页 / 共 4 页
字号:
	C_Config Config;
	char chTranConfigFile[STRING_LEN];
	char Section[STRING_LEN], Key[STRING_LEN], Value[STRING_LEN];
	int i = 1;
	char *pchAppPath = 0;
	char chAlertInfo[STRING_LEN];

	strcpy(gv_chChannelCode, argv[1]);

	//根据环境变量获得配置文件的位置
	if((pchAppPath = getenv(SETTLE_HOME)) == 0)
	{
		sprintf(chAlertInfo, "Not get the environment \"%s\" in LoadConfig.", SETTLE_HOME);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETENV_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	if(!IsDirectory(pchAppPath))
	{
		sprintf(
			chAlertInfo,
			"The directory \"%s\" is not exist!",
			pchAppPath
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	FullPath(pchAppPath);

	memset(chTranConfigFile, 0, sizeof(chTranConfigFile));

	strcpy(chTranConfigFile, pchAppPath);
	strcat(chTranConfigFile, TRAN_CONFIG_FILE);
	strcat(chTranConfigFile, argv[1]);
	strcat(chTranConfigFile, ".ini");

	if (gv_ThreadConfig != 0)
	{
		delete[] gv_ThreadConfig;
	}
	strcpy(Section, "TransferClient");

	strcpy(Key, "ServerIp");
	if (Config.GetParam(chTranConfigFile, Section, Key, Value) == false)
	{
		sprintf(
			chAlertInfo,
			"Config file error at %s-->%s-->%s",
			chTranConfigFile,
			Section,
			Key
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	strncpy(gv_chServerIp,Value,sizeof(gv_chServerIp) - 1);

	memset(gv_chClientIp, 0, sizeof(gv_chClientIp));
	if(!GetLocalIp(gv_chClientIp))
	{
		strcpy(chAlertInfo, "GetLocalIp error!");
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}

	strcpy(Key,"ServerPort");
	if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
	{
		sprintf(
			chAlertInfo,
			"Config file error at %s-->%s-->%s",
			chTranConfigFile,
			Section,
			Key
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	gv_iServerPort = atoi(Value);

	strcpy(Key,"SleepTime");
	if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
	{
		sprintf(
			chAlertInfo,
			"Config file error at %s-->%s-->%s",
			chTranConfigFile,
			Section,
			Key
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	gv_iSleepTime = atoi(Value);

	strcpy(Key,"ThreadNumber");
	if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
	{

		sprintf(
			chAlertInfo,
			"Config file error at %s-->%s-->%s",
			chTranConfigFile,
			Section,
			Key
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	gv_iThreadCount = atoi(Value);

	strcpy(Key,"RePath");
	if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
	{
		sprintf(
			chAlertInfo,
			"Config file error at %s-->%s-->%s",
			chTranConfigFile,
			Section,
			Key
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	strncpy(gv_chBackupDir,Value,sizeof(gv_chBackupDir) - 1);
	if(!IsDirectory(gv_chBackupDir))
	{
		sprintf(
			chAlertInfo,
			"The directory \"%s\" is not exist!",
			gv_chBackupDir
			);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return false;
	}
	FullPath(gv_chBackupDir);

	gv_ThreadConfig = new S_ThreadConfig[gv_iThreadCount + 1];
	memset(gv_ThreadConfig, 0, sizeof(S_ThreadConfig) * (gv_iThreadCount + 1));

	for(i = 1;i <= gv_iThreadCount; ++i)
	{
		sprintf(Section, "Thread%d", i);
		strcpy(Key, "TranType");
		if (Config.GetParam(chTranConfigFile, Section, Key, Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		gv_ThreadConfig[i].TranType = atoi(Value);

		strcpy(Key,"LocalPath");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].LocalPath,Value,sizeof(gv_ThreadConfig[i].LocalPath) - 1);
		if(!IsDirectory(gv_ThreadConfig[i].LocalPath))
		{
			sprintf(
				chAlertInfo,
				"The directory \"%s\" is not exist!",
				gv_ThreadConfig[i].LocalPath
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		FullPath(gv_ThreadConfig[i].LocalPath);

		strcpy(Key,"RemotePath");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].RemotePath,Value,sizeof(gv_ThreadConfig[i].RemotePath) - 1);
		FullPath(gv_ThreadConfig[i].RemotePath);

		strcpy(Key,"LocalTargetPath");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].LocalTargetPath,Value,sizeof(gv_ThreadConfig[i].LocalTargetPath) - 1);
		if(!IsDirectory(gv_ThreadConfig[i].LocalTargetPath))
		{
			sprintf(
				chAlertInfo,
				"The directory \"%s\" is not exist!",
				gv_ThreadConfig[i].LocalTargetPath
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		FullPath(gv_ThreadConfig[i].LocalTargetPath);

		strcpy(Key,"RemoteTargetPath");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].RemoteTargetPath, Value,sizeof(gv_ThreadConfig[i].RemoteTargetPath) - 1);
		FullPath(gv_ThreadConfig[i].RemoteTargetPath);

		strcpy(Key,"BackupPath");
		if(Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].BackupPath, Value,sizeof(gv_ThreadConfig[i].BackupPath) - 1);
		if(!IsDirectory(gv_ThreadConfig[i].BackupPath))
		{
			sprintf(
				chAlertInfo,
				"The directory \"%s\" is not exist!",
				gv_ThreadConfig[i].BackupPath
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo ,ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		FullPath(gv_ThreadConfig[i].BackupPath);

		strcpy(Key,"ServerBackupPath");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].ServerBackupPath, Value,sizeof(gv_ThreadConfig[i].ServerBackupPath) - 1);
		FullPath(gv_ThreadConfig[i].ServerBackupPath);

		strcpy(Key,"FileFilter");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		strncpy(gv_ThreadConfig[i].FileFilter,Value,sizeof(gv_ThreadConfig[i].FileFilter) - 1);

		strcpy(Key,"CompressFlag");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		gv_ThreadConfig[i].CompressFlag = atoi(Value);

		strcpy(Key,"MoveFlag");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		gv_ThreadConfig[i].MoveFlag = atoi(Value);

		strcpy(Key,"BackupFlag");
		if (Config.GetParam(chTranConfigFile,Section,Key,Value) == false)
		{
			sprintf(
				chAlertInfo,
				"Config file error at %s-->%s-->%s",
				chTranConfigFile,
				Section,
				Key
				);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETPARAM_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return false;
		}
		gv_ThreadConfig[i].BackupFlag = atoi(Value);

	}
	return true;
}
/****************************************************************
**
**	Function: .
**	Purpose:  客户端线程
** 	Input Parammeters:
			iThreadNo -- 线程号
**	Return:
**	datetime:
**
*****************************************************************/
void *ClientThread(void *iThreadNo)
{
	int iSockfd = -1;
	int iReturn = 0;
	struct	sockaddr_in ServerAddr;
	struct S_DataInfo TransData;
	char chAlertInfo[STRING_LEN];
	struct S_FileInfo ThreadFileInfo;

	char chBackupTempFile[STRING_LEN];
	char chBackupFile[STRING_LEN];

	memset(&ThreadFileInfo, 0, sizeof(S_FileInfo));
	memset(g_ReMemoryMatrix[(int)iThreadNo], 0, sizeof(g_ReMemoryMatrix[(int)iThreadNo]));

	strcpy(g_ReMemoryMatrix[(int)iThreadNo], ",0");

	g_iArrayExitFlag[(int)iThreadNo] = 0;

	pthread_detach(pthread_self());

	ThreadFileInfo.m_fp = 0;
	ThreadFileInfo.m_iThreadNo = (int)iThreadNo;

	sprintf(chBackupTempFile, "%s%d.tmp", gv_chBackupDir, (int)iThreadNo);
	sprintf(chBackupFile, "%s%d.re", gv_chBackupDir, (int)iThreadNo);
	if(ReadBackupFile(chBackupTempFile, chBackupFile, (int)iThreadNo) == -1)
	{
		sprintf(chAlertInfo, "%s error!", chBackupFile);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(READBACKUPFILE_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return 0;
	}

	strcpy(ThreadFileInfo.m_chBackupTempFile, chBackupTempFile);
	strcpy(ThreadFileInfo.m_chBackupFile, chBackupFile);

	memset(&TransData, 0, sizeof(struct S_DataInfo));

	strcpy(TransData.chFileName, gv_ThreadConfig[(int)iThreadNo].FileName);
	TransData.lFilePos = htonl(gv_ThreadConfig[(int)iThreadNo].FilePos);
	TransData.iStatus = htonl(STATUS_FILEINFO);

	#ifdef	_DEBUG_FOR_TRAN_
	printf("in thread %d\n", iThreadNo);
	#endif//_DEBUG_FOR_TRAN_

	for(;;)
	{
		if(gv_iTermFlag == 0)
		{
			g_iArrayExitFlag[(int)iThreadNo] = 0;
			pthread_exit(0);
		}
		TransData.iTransferType = htonl(gv_ThreadConfig[(int)iThreadNo].TranType);
		strcpy(TransData.chRemotePath, gv_ThreadConfig[(int)iThreadNo].RemotePath);
		strcpy(TransData.chRemoteTargetPath, gv_ThreadConfig[(int)iThreadNo].RemoteTargetPath);
		strcpy(TransData.chRemoteBackupPath, gv_ThreadConfig[(int)iThreadNo].ServerBackupPath);
		strcpy(TransData.chFileFilter, gv_ThreadConfig[(int)iThreadNo].FileFilter);
		TransData.iCompress = htonl(gv_ThreadConfig[(int)iThreadNo].CompressFlag);

		if(iSockfd < 0)
		{
			if((iSockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
			{
				sprintf(chAlertInfo, "%s%d error!", "SocketNO", iSockfd);
				pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
				WriteAlert(SOCKETINIT_ERROR, chAlertInfo, ALERT_LEVEL_3);
				pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
				iSockfd = -1;
				continue;
			}
			bzero(&ServerAddr, sizeof(ServerAddr));
			ServerAddr.sin_family = AF_INET;
			ServerAddr.sin_port = htons(gv_iServerPort);
			ServerAddr.sin_addr.s_addr = inet_addr(gv_chServerIp);

			#if defined(xlC) || defined(CC) || defined(gcc)
			if(connect(iSockfd, (const struct sockaddr *)&ServerAddr, sizeof(ServerAddr)) < 0)
			#else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -