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

📄 common.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
📖 第 1 页 / 共 2 页
字号:
/*
* Copyright (c) 2002, 南京联创系统集成股份有限公司综合结算产品部
* All rights reserved.
*
* 文件名称:common.h
* 摘    要:传输客户端所用到的一些基本函数
*
* 当前版本:
* 作    者:冯亮(fengl@lianchuang.com)
* 完成日期:
*/

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/file.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <netdb.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>

#include "common.h"

/****************************************************************
**
**	Function:GetFullName
**	Purpose:获得文件的绝对路径名
**	Input Parammeters:data:存放有路径的结构体,chFullName:出口参数
**	Return:无
**	datetime:
**
*****************************************************************/
void GetFullName(struct S_DataInfo *data, char *chFullName)
{

	if(data->chRemotePath[strlen(data->chRemotePath)-1] == '/')
	{
		sprintf(chFullName, "%s%s", data->chRemotePath, data->chFileName);
	}
	else
	{
		sprintf(chFullName, "%s/%s", data->chRemotePath, data->chFileName);
	}
	return ;
}


/****************************************************************
**
**	Function:print_data

**	Purpose:打印结构体里的所有数据
**	Input Parammeters:
			data:要打印的结构体
**	Return:无
**	datetime:
**
*****************************************************************/
void print_data(struct S_DataInfo *data)
{
	printf("iTransferType=%d\n", ntohl(data->iTransferType));
	printf("chRemotePath=%s\n", data->chRemotePath);
	printf("chFileFilter=%s\n", data->chFileFilter);
	printf("lFilePos=%ld\n", ntohl(data->lFilePos));
	printf("iCompress=%d\n", ntohl(data->iCompress));
	printf("iStatus=%d\n", ntohl(data->iStatus));
	printf("chErrorMsg=%s\n", data->chErrorMsg);
	printf("chFileName=%s\n", data->chFileName);
	printf("lFileLen=%ld\n", ntohl(data->lFileLen));
	printf("lZipSize=%ld\n", ntohl(data->lZipSize));
	printf("lDataLen=%ld\n", ntohl(data->lDataLen));
	printf("iCheckvalue=%ld\n", ntohl(data->lCheckValue));
	printf("\n");
	return ;
}

/****************************************************************
**
**	Function:GetFileSize
**	Purpose:获取文件的大小
**	Input Parammeters:
			chFileName:文件的名字
**	Return:成功,返回文件的大小。出错,返回-1。
**	datetime:
**
*****************************************************************/
long GetFileSize(char *chFileName)
{
	struct stat buf;
	char chAlertMsg[STRING_LEN];
	if(stat(chFileName, &buf) < 0)
	{
		sprintf(chAlertMsg, "%s error!", chFileName);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(GETFILESIZE_ERROR, chAlertMsg, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	return buf.st_size;
}

/****************************************************************
**
**	Function:GetSockData
**	Purpose:从对方获得一个S_DataInfo结构体大小的数据。
**	Input Parammeters:
			iSockfd:本地的socket。
			data:  要传输的结构体
**	Return:成功返回0,出错返回-1。
**	datetime:
**
*****************************************************************/
int GetSockData(int iSockfd, struct S_DataInfo *data)
{
	long lReadLen = sizeof(struct S_DataInfo);
	long lHasRead;
	char chAlertMsg[STRING_LEN];
	char *pch = (char *)data;

	memset(data, 0, sizeof(struct S_DataInfo));

	while(lReadLen > 0)
	{
		if(TimeWait(iSockfd, MODE_READ) == -1)
		{
			sprintf(chAlertMsg, "%s error!", data->chFileName);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(TIMEWAIT_ERROR, chAlertMsg, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			//close(iSockfd);
			//iSockfd = -1;
			return -1;
		}
		if((lHasRead = read(iSockfd, pch, lReadLen)) < 0)
		{
			if (errno == EINTR || errno == EBADF || errno == 0)
			{
				lHasRead = 0;
			}
			else
			{
				sprintf(chAlertMsg, "%s error!", data->chFileName);
				pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
				WriteAlert(READ_ERROR, chAlertMsg, ALERT_LEVEL_3);
				pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
				//close(iSockfd);
				//iSockfd = -1;
				return -1;
			}
		}
		pch += lHasRead;
		lReadLen -= lHasRead;
	}
	if(CheckData(data) == -1)
	{
		sprintf(chAlertMsg, "%s error!", data->chFileName);
		pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
		WriteAlert(CHECKDATA_ERROR, chAlertMsg, ALERT_LEVEL_3);
		pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
		return -1;
	}
	return 0;
}

/****************************************************************
**
**	Function:WriteSockData
**	Purpose:向对方写入一个S_DataInfo结构体大小的数据。
**	Input Parammeters:
**	Return:成功,返回0,出错返回-1。
**	datetime:
**
*****************************************************************/
int WriteSockData(int iSockfd, struct S_DataInfo *data)
{
	long lWriteLen = sizeof(struct S_DataInfo);
	long lHasWrite = 0;
	char chAlertMsg[STRING_LEN];
	const char *pch = (const char *)data;

	SetCheckValue(data);

	while(lWriteLen > 0)
	{
		if(TimeWait(iSockfd, MODE_WRITE) == -1)
		{
			sprintf(chAlertMsg, "%s error!", data->chFileName);
			pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
			WriteAlert(TIMEWAIT_ERROR, chAlertMsg, ALERT_LEVEL_3);
			pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
			//close(iSockfd);
			//iSockfd = -1;
			
			return -1;
		}
		if((lHasWrite = write(iSockfd, pch, lWriteLen)) < 0)
		{
			if (errno == EINTR || errno == EBADF || errno == 0)
			{
				lHasWrite = 0;
			}
			else
			{
				sprintf(chAlertMsg, "%s error!", data->chFileName);
				pthread_mutex_lock(&gv_GobalMutex.AlertMutex);
				WriteAlert(WRITE_ERROR, chAlertMsg, ALERT_LEVEL_3);
				pthread_mutex_unlock(&gv_GobalMutex.AlertMutex);
				//close(iSockfd);
				//iSockfd = -1;
				return -1;
			}
		}
		pch += lHasWrite;
		lWriteLen -= lHasWrite;
	}
	return 0;
}

/****************************************************************
**
**	Function:TimeWait
**	Purpose:设置超时,等待套接口可读
**	Input Parammeters:成功检测到套接口可读返回1,超时或出错返回-1。
**	Return:
**	datetime:
**
*****************************************************************/
int TimeWait(int fd, int Mode)
{
	fd_set rset;
	bool flag = true;
	struct timeval timewait;
	FD_ZERO(&rset);
	FD_SET(fd, &rset);
	timewait.tv_sec = SOCK_TIME_OUT;
	timewait.tv_usec = 0;

	while(flag)
	{
		if(MODE_WRITE == Mode)
		{
			if(select(fd+1, NULL, &rset, NULL, &timewait) <= 0)
			{
				if (errno == EINTR || errno == EBADF || errno == 0)
				{
					continue;
				}
				else
				{
					return -1;
				}
			}
		}
		else if(MODE_READ == Mode)
		{
			if(select(fd+1, &rset, NULL, NULL, &timewait) <= 0)
			{
				if (errno == EINTR || errno == EBADF || errno == 0)
				{
					continue;
				}
				else
				{
					return -1;
				}
			}
		}
		else
		{
			return -1;
		}
		flag = false;
	}
	if(!FD_ISSET(fd, &rset))
	{
		return -1;
	}
	return 1;
}

/****************************************************************
**
**	Function:SendFile
**	Purpose:发送文件
**	Input Parammeters:
**	Return:
**	datetime:
**
*****************************************************************/
int SendFile(int iSockfd, struct S_DataInfo *data, char *chFullName)
{
	long lReadLen;
	long lDataLen;
	char chAlertMsg[STRING_LEN];
	FILE *fp = NULL;

	lDataLen = sizeof(data->chData);

	fp = fopen(chFullName, "rb");

⌨️ 快捷键说明

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