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

📄 tran.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
📖 第 1 页 / 共 4 页
字号:
		memset(chTime, 0, sizeof(chTime));
		GetCurrentDate(chDate);
		GetCurrentTime(chTime);
		sprintf(chStartDateTime, "%s%s", chDate, chTime);

		strcpy(gv_ThreadConfig[ThreadFileInfo->m_iThreadNo].FileName, data->chFileName);
		gv_ThreadConfig[ThreadFileInfo->m_iThreadNo].FilePos = 0;

		WriteReMemory(ThreadFileInfo);
		WriteBackupFile(ThreadFileInfo);

		iResult = PutFile(iSockfd, data, chFullName, iThreadNo, ThreadFileInfo);
		if(iResult == -1)
		{
			sprintf(chAlertInfo, "%s error!", chShortName);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(PUTFILE_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			break;
		}
		else if(iResult == 1)
		{
			//中断
			break;
		}
		/*写传输日志*/
		memset(chDate, 0, sizeof(chDate));
		memset(chTime, 0, sizeof(chTime));
		GetCurrentDate(chDate);
		GetCurrentTime(chTime);
		sprintf(chEndDateTime, "%s%s", chDate, chTime);
		strcpy(chFileName, data->chFileName);
//		if(1 == IsZipFile(chFileName))
//		{
//			chFileName[strlen(chFileName)-2] = 0;
//		}
		if(COMPRESS == ntohl(data->iCompress))
		{
			chFileName[strlen(chFileName)-2] = 0;
		}
		memset(chMsgBuffer, 0, sizeof(chMsgBuffer));
		sprintf(
			chMsgBuffer,
			"%s,%s,%s,%s,%s,%s,%s,%s,%s,%ld,%s,%s,%s,%s,%s,%s,%s,%s",
			LOG_TYPE,
			MODEL_CODE,
			OUT_MODEL_CODE,
			chFileName + 1,
			chFileName + 1,
			chStartDateTime,
			chEndDateTime,
			gv_chChannelCode,
			OUT_CHANNEL_CODE,
			ntohl(data->lFileLen),
			"\0",
			"\0",
			"s",
			"p",
			gv_chServerIp,
			gv_chClientIp,
			CHANNEL_ATTR,
			"\0"
			);
		pthread_mutex_lock(&gv_GobalMutex.LogMutex);
		gv_TranLog.Write(chMsgBuffer);
		pthread_mutex_unlock(&gv_GobalMutex.LogMutex);

		data->lFilePos = htonl(0);
		memset(data->chFileName, 0, sizeof(data->chFileName));
		memset(gv_ThreadConfig[iThreadNo].FileName, 0, sizeof(gv_ThreadConfig[iThreadNo].FileName));
		gv_ThreadConfig[iThreadNo].FilePos = 0;
		memset(g_ReMemoryMatrix[iThreadNo], 0, sizeof(g_ReMemoryMatrix[iThreadNo]));
		sprintf(
			g_ReMemoryMatrix[iThreadNo],
			"%s,%ld",
			gv_ThreadConfig[iThreadNo].FileName,
			gv_ThreadConfig[iThreadNo].FilePos
			);
		WriteBackupFile(ThreadFileInfo);
	}//end for(;;)
	data->iStatus = htonl(STATUS_BYBY);
	gv_TranMonitor.AddRate(sizeof(S_DataInfo));
	if(WriteSockData(iSockfd, data) == -1)
	{
		sprintf(chAlertInfo, "%s error!", data->chFileName);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(WRITESOCKDATA_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	if(iResult == -1)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

/****************************************************************
**
**	Function: PutFile
**	Purpose:  上传文件到服务器
** 	Input Parammeters:
			iSockfd -- socket句柄
			data -- 传输数据结构体
			chFullName -- 需上传的文件名
**	Return:	-1 -- 错误  0 -- 成功
**	datetime:
**
*****************************************************************/
int PutFile(int iSockfd, struct S_DataInfo *data, char *chFullName, int iThreadNo, S_FileInfo *ThreadFileInfo)
{
	long lFileLen, lFilePos = 0;
	int iLen, iEnd;
	char chDate[8+1], chTime[6+1];
	char chAlertInfo[STRING_LEN];
	gv_TranMonitor.AddRate(sizeof(S_DataInfo));
	iLen = WriteSockData(iSockfd, data);
	if(-1 == iLen)
	{
		sprintf(chAlertInfo, "%s error!", data->chFileName);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(WRITESOCKDATA_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	gv_TranMonitor.AddRate(sizeof(S_DataInfo));
	if(GetSockData(iSockfd, data) == -1)
	{
		sprintf(chAlertInfo, "%s error!", data->chFileName);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETSOCKDATA_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	iEnd = 0;
	while(g_iArrayFlag[iThreadNo] == 1)
	{
		switch(ntohl(data->iStatus))
		{
			/*现在正在发送文件*/
			case STATUS_SENDFILE:
				if(SendFile(iSockfd, data, chFullName) == -1)
				{
					sprintf(chAlertInfo, "%s error!", data->chFileName);
					pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
					WriteAlert(SENDFILE_ERROR, chAlertInfo, ALERT_LEVEL_3);
					pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
					return -1;
				}
				strcpy(gv_ThreadConfig[iThreadNo].FileName, data->chFileName);
				gv_ThreadConfig[iThreadNo].FilePos = ntohl(data->lFilePos);
				WriteReMemory(ThreadFileInfo);
				break;

			/*这是最后的文件块*/
			case STATUS_LASTDATA:
				//modify by fengl 2004-3-10
				if(gv_ThreadConfig[iThreadNo].BackupFlag)
				{
					//如果备份开关开了,则备份
					if(BackupFile(data, iThreadNo) != 0)
					{
						sprintf(chAlertInfo, "%s error!", chFullName);
						pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
						WriteAlert(BACKUP_ERROR, chAlertInfo, ALERT_LEVEL_3);
						pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
						return -1;
					}
				}
				else
				{
					//否则不备份
					if(remove(chFullName) != 0)
					{
						sprintf(chAlertInfo, "%s error!", chFullName);
						pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
						WriteAlert(REMOVE_ERROR, chAlertInfo, ALERT_LEVEL_3);
						pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
						return -1;
					}
				}
				memset(gv_ThreadConfig[iThreadNo].FileName, 0, sizeof(gv_ThreadConfig[iThreadNo].FileName));
				gv_ThreadConfig[iThreadNo].FilePos = 0;
				WriteReMemory(ThreadFileInfo);
				WriteBackupFile(ThreadFileInfo);
				iEnd = 1;
				break;

			default:
				sprintf(chAlertInfo, "%s error!", "unknownstatus");
				pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
				WriteAlert(UNKNOWNSTATUS_ERROR, chAlertInfo, ALERT_LEVEL_3);
				pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
				return -1;
		}
		if(iEnd)
		{
			return 0;
		}
		gv_TranMonitor.AddRate(sizeof(S_DataInfo));
		if(GetSockData(iSockfd, data) == -1)
		{
			sprintf(chAlertInfo, "%s", data->chFileName);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(GETSOCKDATA_ERROR, chAlertInfo, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			return -1;
		}
	}//end while(g_iArrayFlag[iThreadNo] == 1)
	WriteBackupFile(ThreadFileInfo);
	return 1;
}
/****************************************************************
**
**	Function: UserTerm
**	Purpose:  进程中断
** 	Input Parammeters: signo -- 信号
**	Return:
**	datetime:
**
*****************************************************************/
void UserTerm(int signo)
{
	InitSignal();

	#ifdef	_DEBUG_FOR_TRAN_
	char chNowTime[STRING_LEN];
	time_t tNowTime;
	struct tm *stNowTime = 0;

	time(&tNowTime);
	stNowTime = localtime(&tNowTime);
	strftime(chNowTime, 20, "%Y-%m-%d %H:%M:%S", stNowTime);

 	fprintf(stderr, "\n<%s>	process is working now, please wait a while.\n", chNowTime);
 	fflush(stderr);
	#endif

	gv_iTermFlag = 0;
    return;
}

/****************************************************************
**
**	Function: GetCurrentDate
**	Purpose:  得到当前日期
** 	Input Parammeters: date
**	Return:	pchDate -- 当前日期
**	datetime:
**
*****************************************************************/
int GetCurrentDate(char *pchDate)
{
	time_t now;
	struct tm  *w;
	time(&now);
	w=localtime(&now);
	sprintf(pchDate,"%04d%02d%02d",w->tm_year+1900,w->tm_mon+1,w->tm_mday);
	return 0;
}
/****************************************************************
**
**	Function: GetCurrentTime
**	Purpose:  得到当前时间
** 	Input Parammeters: pchTime -- 当前时间
**	Return:
**	datetime:
**
*****************************************************************/
int GetCurrentTime(char *pchTime)
{
	time_t now;
	struct tm *w;

	time(&now);
	w = localtime(&now);
	sprintf(pchTime, "%02d%02d%02d", w->tm_hour, w->tm_min, w->tm_sec);
	return 0;
}

/****************************************************************
**
**	Function: WriteReMemory
**	Purpose:  写断点续传内存
** 	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
inline void WriteReMemory(S_FileInfo *ThreadFileInfo)
{
	memset(g_ReMemoryMatrix[ThreadFileInfo->m_iThreadNo], 0, sizeof(g_ReMemoryMatrix[ThreadFileInfo->m_iThreadNo]));

	sprintf(
			g_ReMemoryMatrix[ThreadFileInfo->m_iThreadNo],
			"%s,%ld",
			gv_ThreadConfig[ThreadFileInfo->m_iThreadNo].FileName,
			gv_ThreadConfig[ThreadFileInfo->m_iThreadNo].FilePos
			);
	return;
}

/****************************************************************
**
**	Function: WriteBackupFile
**	Purpose:  写备份文件
** 	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int WriteBackupFile(S_FileInfo *ThreadFileInfo)
{
	char chAlertInfo[STRING_LEN];
	memset(chAlertInfo, 0, sizeof(chAlertInfo));

	if(0 == (ThreadFileInfo->m_fp = fopen(ThreadFileInfo->m_chBackupTempFile, "w")))
	{
		sprintf(chAlertInfo, "%s error!", ThreadFileInfo->m_chBackupTempFile);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(FOPEN_ERROR, chAlertInfo, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	fprintf(ThreadFileInfo->m_fp, "%s",g_ReMemoryMatrix[ThreadFileInfo->m_iThreadNo]);
	fclose(ThreadFileInfo->m_fp);
	rename(ThreadFileInfo->m_chBackupTempFile, ThreadFileInfo->m_chBackupFile);
	return 0;
}
/****************************************************************
**
**	Function: ReadBackupFile
**	Purpose:  读备份文件
** 	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int ReadBackupFile(char *chBackupTempFile, char *chBackupFile, int iThreadNo)
{
	FILE *fp = 0;
	char chTemp[STRING_LEN+1], *pchTemp = 0, *pchPos = 0;

	if(access(chBackupFile, F_OK) == -1)
	{
		//备份文件不存在
		if((fp = fopen(chBackupTempFile, "w")) == 0)
		{
			return -1;
		}
		memset(gv_ThreadConfig[iThreadNo].FileName, 0, sizeof(gv_ThreadConfig[iThreadNo].FileName));
		gv_ThreadConfig[iThreadNo].FilePos = 0;
		memset(g_ReMemoryMatrix[iThreadNo], 0, sizeof(g_ReMemoryMatrix[iThreadNo]));

		sprintf(
			g_ReMemoryMatrix[iThreadNo],
			"%s,%ld",
			gv_ThreadConfig[iThreadNo].FileName,
			gv_ThreadConfig[iThreadNo].FilePos
			);
	}
	else if((fp = fopen(chBackupFile, "r")) != 0)
	{
		//备份文件存在
		memset(gv_ThreadConfig[iThreadNo].FileName, 0, sizeof(gv_ThreadConfig[iThreadNo].FileName));
		gv_ThreadConfig[iThreadNo].FilePos = 0;
		fgets(chTemp, STRING_LEN, fp);
		pchTemp = strchr(chTemp, ',');
		strncpy(gv_ThreadConfig[iThreadNo].FileName, chTemp, pchTemp-chTemp);
		pchPos = pchTemp + 1;
		gv_ThreadConfig[iThreadNo].FilePos = atol(pchPos);
	}
	else
	{
		//备份文件打不开
		return -1;
	}
	fclose(fp);
	return 0;
}

/****************************************************************
**
**	Function:FiltToTargetPath
**	Purpose:将临时目录中的文件移动到目标目录中
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int FileToTargetPath(struct S_DataInfo *data, int iThreadNo)
{
	char chFileName[STRING_LEN];
	char chFullTempPath[STRING_LEN];
	char chFullTargPath[STRING_LEN];
	char chFullTempFileName[STRING_LEN];
	char chFullTargFileName[STRING_LEN];

	memset(chFileName, 0, sizeof(chFileName));
	strcpy(chFileName, data->chFileName);
	if(COMPRESS == ntohl(data->iCompress))
	{
		chFileName[strlen(chFileName) - 2] = '\0';
	}

	strcpy(chFullTempPath, gv_ThreadConfig[(int)iThreadNo].LocalPath);
	strcpy(chFullTargPath, gv_ThreadConfig[(int)iThreadNo].LocalTargetPath);

	sprintf(chFullTempFileName, "%s%s", chFullTempPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);
	sprintf(chFullTargFileName, "%s%s", chFullTargPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);

	int iResult = 0;

	iResult = rename(chFullTempFileName, chFullTargFileName);

	if(iResult < 0)
	{
		return -1;
	}
	return 0;
}

int BackupFile(struct S_DataInfo *data, int iThreadNo)
{
	char chFileName[STRING_LEN];
	char chFullTempPath[STRING_LEN];
	char chFullTargPath[STRING_LEN];
	char chFullTempFileName[STRING_LEN];
	char chFullTargFileName[STRING_LEN];

	memset(chFileName, 0, sizeof(chFileName));
	strcpy(chFileName, data->chFileName);
	if(COMPRESS == ntohl(data->iCompress))
	{
		chFileName[strlen(chFileName) - 2] = '\0';
	}

	strcpy(chFullTempPath, gv_ThreadConfig[(int)iThreadNo].LocalPath);
	strcpy(chFullTargPath, gv_ThreadConfig[(int)iThreadNo].BackupPath);

	sprintf(chFullTempFileName, "%s%s", chFullTempPath, data->chFileName);
	sprintf(chFullTargFileName, "%s%s", chFullTargPath, chFileName[0] == '~' ? chFileName + 1 : chFileName);

	int iResult = 0;

	iResult = rename(chFullTempFileName, chFullTargFileName);

	if(iResult < 0)
	{
		return -1;
	}
	return 0;
}

void WriteAlert(const char *pchAlertCode, const char *pchAlertObject, const char *pchAlertLevel)
{
	gv_TranAlert.InsertAlert(
				MODEL_CODE,
				pchAlertLevel,
				pchAlertCode,
				gv_chChannelCode,
				pchAlertObject
				);
	return ;
}

void InitSignal(void)
{
	signal(SIGINT, UserTerm);
	signal(SIGHUP, SIG_IGN);
	signal(SIGTERM, UserTerm);

	return;
}

//---------------------------------- THE END ----------------------------------

⌨️ 快捷键说明

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