for job
This commit is contained in:
commit
a83e13ff21
62
EL/README.md
Normal file
62
EL/README.md
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# 面试专用贴
|
||||||
|
|
||||||
|
面试官你好,该框架非本人开发,仅作学习整理,但是常用,且熟悉函数源码、原理和流程。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# EL框架
|
||||||
|
|
||||||
|
EL框架整理了多个大佬的开源框架,可以提升开发效率,源码方便学习参考
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 使用
|
||||||
|
|
||||||
|
拉下来make即可
|
||||||
|
|
||||||
|
注:使用动态链接库时,需要把动态链接库地址添加到在linux环境变量中。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 主要类
|
||||||
|
|
||||||
|
```
|
||||||
|
微秒计时器
|
||||||
|
class ctimer
|
||||||
|
|
||||||
|
目录文件列表
|
||||||
|
class cdir
|
||||||
|
|
||||||
|
写文件的类
|
||||||
|
class cofile
|
||||||
|
|
||||||
|
读取文件的类
|
||||||
|
class cifile
|
||||||
|
|
||||||
|
自旋锁
|
||||||
|
class spinlock_mutex
|
||||||
|
|
||||||
|
日志文件
|
||||||
|
class clogfile
|
||||||
|
|
||||||
|
进程心跳
|
||||||
|
class cpactive
|
||||||
|
|
||||||
|
信号量
|
||||||
|
class csemp
|
||||||
|
|
||||||
|
循环队列
|
||||||
|
template <class TT, int MaxLength>
|
||||||
|
class squeue
|
||||||
|
|
||||||
|
socket通讯的服务端类
|
||||||
|
class ctcpserver
|
||||||
|
|
||||||
|
socket通讯的客户端类
|
||||||
|
class ctcpclient
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
58
EL/_cmel.h
Normal file
58
EL/_cmel.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/****************************************************************************************
|
||||||
|
* _cmel.h,开发框架公用头文件
|
||||||
|
*****************************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _cmel_H
|
||||||
|
#define _cmel_H 1
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <utime.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/timerfd.h>
|
||||||
|
#include <sys/signalfd.h>
|
||||||
|
#include <atomic>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <forward_list>
|
||||||
|
#include <vector>
|
||||||
|
#include <deque>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <queue>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#endif
|
1828
EL/_el.cpp
Normal file
1828
EL/_el.cpp
Normal file
File diff suppressed because it is too large
Load Diff
824
EL/_el.h
Normal file
824
EL/_el.h
Normal file
@ -0,0 +1,824 @@
|
|||||||
|
/****************************************************************************************
|
||||||
|
* _el.h,公共函数和类声明文件。
|
||||||
|
****************************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __EL_H
|
||||||
|
#define __EL_H 1
|
||||||
|
|
||||||
|
#include "_cmel.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace eviwbh
|
||||||
|
{
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// 字符串操作
|
||||||
|
// 删除字符串左边指定的字符。
|
||||||
|
// str:待处理的字符串。
|
||||||
|
// cc:需要删除的字符,缺省删除空格。
|
||||||
|
char* deletelchr(char* str, const int cc=' ');
|
||||||
|
string& deletelchr(string &str, const int cc=' ');
|
||||||
|
|
||||||
|
// 删除字符串右边指定的字符。
|
||||||
|
// str:待处理的字符串。
|
||||||
|
// cc:需要删除的字符,缺省删除空格。
|
||||||
|
char* deleterchr(char *str,const int cc=' ');
|
||||||
|
string& deleterchr(string &str,const int cc=' ');
|
||||||
|
|
||||||
|
// 删除字符串左右两边指定的字符。
|
||||||
|
// str:待处理的字符串。
|
||||||
|
// chr:需要删除的字符,缺省删除空格。
|
||||||
|
char* deletelrchr(char *str,const int cc=' ');
|
||||||
|
string& deletelrchr(string &str,const int cc=' ');
|
||||||
|
|
||||||
|
// 把字符串中的小写字母转换成大写,忽略不是字母的字符。
|
||||||
|
// str:待转换的字符串。
|
||||||
|
char* toupper(char *str);
|
||||||
|
string& toupper(string &str);
|
||||||
|
|
||||||
|
// 把字符串中的大写字母转换成小写,忽略不是字母的字符。
|
||||||
|
// str:待转换的字符串。
|
||||||
|
char* tolower(char *str);
|
||||||
|
string& tolower(string &str);
|
||||||
|
|
||||||
|
// 字符串替换函数。
|
||||||
|
// 在字符串str中,如果存在字符串str1,就替换为字符串str2。
|
||||||
|
// str:待处理的字符串。
|
||||||
|
// str1:旧的内容。
|
||||||
|
// str2:新的内容。
|
||||||
|
// bloop:是否循环执行替换。
|
||||||
|
// 注意:
|
||||||
|
// 1、如果str2比str1要长,替换后str会变长,所以必须保证str有足够的空间,否则内存会溢出(C++风格字符串不存在这个问题)。
|
||||||
|
// 2、如果str2中包含了str1的内容,且bloop为true,这种做法存在逻辑错误,replacestr将什么也不做。
|
||||||
|
// 3、如果str2为空,表示删除str中str1的内容。
|
||||||
|
bool replacestr(char *str ,const string &str1,const string &str2,const bool bloop=false);
|
||||||
|
bool replacestr(string &str,const string &str1,const string &str2,const bool bloop=false);
|
||||||
|
|
||||||
|
// 从一个字符串中提取出数字、符号和小数点,存放到另一个字符串中。
|
||||||
|
// src:原字符串。
|
||||||
|
// dest:目标字符串。
|
||||||
|
// bsigned:是否提取符号(+和-),true-包括;false-不包括。
|
||||||
|
// bdot:是否提取小数点(.),true-包括;false-不包括。
|
||||||
|
// 注意:src和dest可以是同一个变量。
|
||||||
|
char* picknumber(const string &src,char *dest,const bool bsigned=false,const bool bdot=false);
|
||||||
|
string& picknumber(const string &src,string &dest,const bool bsigned=false,const bool bdot=false);
|
||||||
|
string picknumber(const string &src,const bool bsigned=false,const bool bdot=false);
|
||||||
|
|
||||||
|
// 正则表达式,判断一个字符串是否匹配另一个字符串。
|
||||||
|
// str:需要判断的字符串,是精确表示的,如文件名"_public.cpp"。
|
||||||
|
// rules:匹配规则的表达式,用星号"*"代表任意字符,多个表达式之间用半角的逗号分隔,如"*.h,*.cpp"。
|
||||||
|
// 注意:1)str参数不需要支持"*",rules参数支持"*";2)函数在判断str是否匹配rules的时候,会忽略字母的大小写。
|
||||||
|
bool matchstr(const string &str,const string &rules);
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// ccmdstr类用于拆分有分隔符的字符串。
|
||||||
|
// 字符串的格式为:字段内容1+分隔符+字段内容2+分隔符+字段内容3+分隔符+...+字段内容n。
|
||||||
|
// 例如:"messi,10,striker,30,1.72,68.5,Barcelona",这是足球运动员梅西的资料。
|
||||||
|
// 包括:姓名、球衣号码、场上位置、年龄、身高、体重和效力的俱乐部,字段之间用半角的逗号分隔。
|
||||||
|
class ccmdstr
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
vector<string> m_cmdstr; // 存放拆分后的字段内容。
|
||||||
|
|
||||||
|
ccmdstr(const ccmdstr &) = delete; // 禁用拷贝构造函数。
|
||||||
|
ccmdstr &operator=(const ccmdstr &) = delete; // 禁用赋值函数。
|
||||||
|
public:
|
||||||
|
ccmdstr() { } // 构造函数。
|
||||||
|
ccmdstr(const string &buffer,const string &sepstr,const bool bdelspace=false);
|
||||||
|
|
||||||
|
const string& operator[](int ii) const // 重载[]运算符,可以像访问数组一样访问m_cmdstr成员。
|
||||||
|
{
|
||||||
|
return m_cmdstr[ii];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把字符串拆分到m_cmdstr容器中。
|
||||||
|
// buffer:待拆分的字符串。
|
||||||
|
// sepstr:buffer中采用的分隔符,注意,sepstr参数的数据类型不是字符,是字符串,如","、" "、"|"、"~!~"。
|
||||||
|
// bdelspace:拆分后是否删除字段内容前后的空格,true-删除;false-不删除,缺省不删除。
|
||||||
|
void splittocmd(const string &buffer,const string &sepstr,const bool bdelspace=false);
|
||||||
|
|
||||||
|
// 获取拆分后字段的个数,即m_cmdstr容器的大小。
|
||||||
|
int size() const { return m_cmdstr.size(); }
|
||||||
|
int cmdcount() const { return m_cmdstr.size(); } // 兼容以前的项目。
|
||||||
|
|
||||||
|
// 从m_cmdstr容器获取字段内容。
|
||||||
|
// ii:字段的顺序号,类似数组的下标,从0开始。
|
||||||
|
// value:传入变量的地址,用于存放字段内容。
|
||||||
|
// 返回值:true-成功;如果ii的取值超出了m_cmdstr容器的大小,返回失败。
|
||||||
|
bool getvalue(const int ii,string &value,const int ilen=0) const; // C++风格字符串。视频中没有第三个参数,加上第三个参数更好。
|
||||||
|
bool getvalue(const int ii,char *value,const int ilen=0) const; // C风格字符串,ilen缺省值为0-全部长度。
|
||||||
|
bool getvalue(const int ii,int &value) const; // int整数。
|
||||||
|
bool getvalue(const int ii,unsigned int &value) const; // unsigned int整数。
|
||||||
|
bool getvalue(const int ii,long &value) const; // long整数。
|
||||||
|
bool getvalue(const int ii,unsigned long &value) const; // unsigned long整数。
|
||||||
|
bool getvalue(const int ii,double &value) const; // 双精度double。
|
||||||
|
bool getvalue(const int ii,float &value) const; // 单精度float。
|
||||||
|
bool getvalue(const int ii,bool &value) const; // bool型。
|
||||||
|
|
||||||
|
~ccmdstr(); // 析构函数。
|
||||||
|
};
|
||||||
|
|
||||||
|
// 重载<<运算符,输出ccmdstr::m_cmdstr中的内容,方便调试。
|
||||||
|
ostream& operator<<(ostream& out, const ccmdstr& cc);
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 解析xml格式字符串的函数族。
|
||||||
|
// xml格式的字符串的内容如下:
|
||||||
|
// <filename>/tmp/_public.h</filename><mtime>2020-01-01 12:20:35</mtime><size>18348</size>
|
||||||
|
// <filename>/tmp/_public.cpp</filename><mtime>2020-01-01 10:10:15</mtime><size>50945</size>
|
||||||
|
// xmlbuffer:待解析的xml格式字符串。
|
||||||
|
// fieldname:字段的标签名。
|
||||||
|
// value:传入变量的地址,用于存放字段内容,支持bool、int、insigned int、long、
|
||||||
|
// unsigned long、double和char[]。
|
||||||
|
// 注意:当value参数的数据类型为char []时,必须保证value数组的内存足够,否则可能发生内存溢出的问题,
|
||||||
|
// 也可以用ilen参数限定获取字段内容的长度,ilen的缺省值为0,表示不限长度。
|
||||||
|
// 返回值:true-成功;如果fieldname参数指定的标签名不存在,返回失败。
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,string &value,const int ilen=0); // 视频中没有第三个参数,加上第三个参数更好。
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,char *value,const int ilen=0);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,bool &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,int &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned int &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,long &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned long &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,double &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,float &value);
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
|
||||||
|
// C++格式化输出函数模板。
|
||||||
|
template< typename... Args >
|
||||||
|
bool sformat(string &str,const char* fmt, Args... args )
|
||||||
|
{
|
||||||
|
int len = snprintf( nullptr, 0, fmt, args... ); // 得到格式化输出后字符串的总长度。
|
||||||
|
if (len < 0) return false; // 如果调用snprintf失败,返回-1。
|
||||||
|
if (len == 0) { str.clear(); return true; } // 如果调用snprintf返回0,表示格式化输出的内容为空。
|
||||||
|
|
||||||
|
str.resize(len); // 为string分配内存。
|
||||||
|
snprintf(&str[0], len + 1, fmt, args... ); // linux平台第二个参数是len+1,windows平台是len。
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
template< typename... Args >
|
||||||
|
string sformat(const char* fmt, Args... args )
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
int len = snprintf( nullptr, 0, fmt, args... ); // 得到格式化后字符串的长度。
|
||||||
|
if (len < 0) return str; // 如果调用snprintf失败,返回-1。
|
||||||
|
if (len == 0) return str; // 如果调用snprintf返回0,表示格式化输出的内容为空。;
|
||||||
|
|
||||||
|
str.resize(len); // 为string分配内存。
|
||||||
|
snprintf(&str[0], len + 1, fmt, args... ); // linux平台第二个参数是len+1,windows平台是len。
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 时间操作的若干函数。
|
||||||
|
/*
|
||||||
|
取操作系统的时间(用字符串表示)。
|
||||||
|
strtime:用于存放获取到的时间。
|
||||||
|
timetvl:时间的偏移量,单位:秒,0是缺省值,表示当前时间,30表示当前时间30秒之后的时间点,-30表示当前时间30秒之前的时间点。
|
||||||
|
fmt:输出时间的格式,fmt每部分的含义:yyyy-年份;mm-月份;dd-日期;hh24-小时;mi-分钟;ss-秒,
|
||||||
|
缺省是"yyyy-mm-dd hh24:mi:ss",目前支持以下格式:
|
||||||
|
"yyyy-mm-dd hh24:mi:ss"
|
||||||
|
"yyyymmddhh24miss"
|
||||||
|
"yyyy-mm-dd"
|
||||||
|
"yyyymmdd"
|
||||||
|
"hh24:mi:ss"
|
||||||
|
"hh24miss"
|
||||||
|
"hh24:mi"
|
||||||
|
"hh24mi"
|
||||||
|
"hh24"
|
||||||
|
"mi"
|
||||||
|
注意:
|
||||||
|
1)小时的表示方法是hh24,不是hh,这么做的目的是为了保持与数据库的时间表示方法一致;
|
||||||
|
2)以上列出了常用的时间格式,如果不能满足你应用开发的需求,请修改源代码timetostr()函数增加更多的格式支持;
|
||||||
|
3)调用函数的时候,如果fmt与上述格式都匹配,strtime的内容将为空。
|
||||||
|
4)时间的年份是四位,其它的可能是一位和两位,如果不足两位,在前面补0。
|
||||||
|
*/
|
||||||
|
string& ltime(string &strtime,const string &fmt="",const int timetvl=0);
|
||||||
|
char * ltime(char *strtime ,const string &fmt="",const int timetvl=0);
|
||||||
|
// 为了避免重载的岐义,增加ltime1()函数。
|
||||||
|
string ltime1(const string &fmt="",const int timetvl=0);
|
||||||
|
|
||||||
|
// 把整数表示的时间转换为字符串表示的时间。
|
||||||
|
// ttime:整数表示的时间。
|
||||||
|
// strtime:字符串表示的时间。
|
||||||
|
// fmt:输出字符串时间strtime的格式,与ltime()函数的fmt参数相同,如果fmt的格式不正确,strtime将为空。
|
||||||
|
string& timetostr(const time_t ttime,string &strtime,const string &fmt="");
|
||||||
|
char* timetostr(const time_t ttime,char *strtime ,const string &fmt="");
|
||||||
|
// 为了避免重载的岐义,增加timetostr1()函数。
|
||||||
|
string timetostr1(const time_t ttime,const string &fmt="");
|
||||||
|
|
||||||
|
// 把字符串表示的时间转换为整数表示的时间。
|
||||||
|
// strtime:字符串表示的时间,格式不限,但一定要包括yyyymmddhh24miss,一个都不能少,顺序也不能变。
|
||||||
|
// 返回值:整数表示的时间,如果strtime的格式不正确,返回-1。
|
||||||
|
time_t strtotime(const string &strtime);
|
||||||
|
|
||||||
|
// 把字符串表示的时间加上一个偏移的秒数后得到一个新的字符串表示的时间。
|
||||||
|
// in_stime:输入的字符串格式的时间,格式不限,但一定要包括yyyymmddhh24miss,一个都不能少,顺序也不能变。
|
||||||
|
// out_stime:输出的字符串格式的时间。
|
||||||
|
// timetvl:需要偏移的秒数,正数往后偏移,负数往前偏移。
|
||||||
|
// fmt:输出字符串时间out_stime的格式,与ltime()函数的fmt参数相同。
|
||||||
|
// 注意:in_stime和out_stime参数可以是同一个变量的地址,如果调用失败,out_stime的内容会清空。
|
||||||
|
// 返回值:true-成功,false-失败,如果返回失败,可以认为是in_stime的格式不正确。
|
||||||
|
bool addtime(const string &in_stime,char *out_stime ,const int timetvl,const string &fmt="");
|
||||||
|
bool addtime(const string &in_stime,string &out_stime,const int timetvl,const string &fmt="");
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 这是一个精确到微秒的计时器。
|
||||||
|
class ctimer
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
struct timeval m_start; // 计时开始的时间点。
|
||||||
|
struct timeval m_end; // 计时结束的时间点。
|
||||||
|
public:
|
||||||
|
ctimer(); // 构造函数中会调用start方法。
|
||||||
|
|
||||||
|
void start(); // 开始计时。
|
||||||
|
|
||||||
|
// 计算已逝去的时间,单位:秒,小数点后面是微秒。
|
||||||
|
// 每调用一次本方法之后,自动调用start方法重新开始计时。
|
||||||
|
double elapsed();
|
||||||
|
};
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// 根据绝对路径的文件名或目录名逐级的创建目录。
|
||||||
|
// pathorfilename:绝对路径的文件名或目录名。
|
||||||
|
// bisfilename:指定pathorfilename的类型,true-pathorfilename是文件名,否则是目录名,缺省值为true。
|
||||||
|
// 返回值:true-成功,false-失败,如果返回失败,原因有大概有三种情况:
|
||||||
|
// 1)权限不足;2)pathorfilename参数不是合法的文件名或目录名;3)磁盘空间不足。
|
||||||
|
bool newdir(const string &pathorfilename,bool bisfilename=true);
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 文件操作相关的函数
|
||||||
|
|
||||||
|
// 重命名文件,类似Linux系统的mv命令。
|
||||||
|
// srcfilename:原文件名,建议采用绝对路径的文件名。
|
||||||
|
// dstfilename:目标文件名,建议采用绝对路径的文件名。
|
||||||
|
// 返回值:true-成功;false-失败,失败的主要原因是权限不足或磁盘空间不够,如果原文件和目标文件不在同一个磁盘分区,重命名也可能失败。
|
||||||
|
// 注意,在重命名文件之前,会自动创建dstfilename参数中包含的目录。
|
||||||
|
// 在应用开发中,可以用renamefile()函数代替rename()库函数。
|
||||||
|
bool renamefile(const string &srcfilename,const string &dstfilename);
|
||||||
|
|
||||||
|
// 复制文件,类似Linux系统的cp命令。
|
||||||
|
// srcfilename:原文件名,建议采用绝对路径的文件名。
|
||||||
|
// dstfilename:目标文件名,建议采用绝对路径的文件名。
|
||||||
|
// 返回值:true-成功;false-失败,失败的主要原因是权限不足或磁盘空间不够。
|
||||||
|
// 注意:
|
||||||
|
// 1)在复制文件之前,会自动创建dstfilename参数中的目录名。
|
||||||
|
// 2)复制文件的过程中,采用临时文件命名的方法,复制完成后再改名为dstfilename,避免中间状态的文件被读取。
|
||||||
|
// 3)复制后的文件的时间与原文件相同,这一点与Linux系统cp命令不同。
|
||||||
|
bool copyfile(const string &srcfilename,const string &dstfilename);
|
||||||
|
|
||||||
|
// 获取文件的大小。
|
||||||
|
// filename:待获取的文件名,建议采用绝对路径的文件名。
|
||||||
|
// 返回值:如果文件不存在或没有访问权限,返回-1,成功返回文件的大小,单位是字节。
|
||||||
|
int filesize(const string &filename);
|
||||||
|
|
||||||
|
// 获取文件的时间。
|
||||||
|
// filename:待获取的文件名,建议采用绝对路径的文件名。
|
||||||
|
// mtime:用于存放文件的时间,即stat结构体的st_mtime。
|
||||||
|
// fmt:设置时间的输出格式,与ltime()函数相同,但缺省是"yyyymmddhh24miss"。
|
||||||
|
// 返回值:如果文件不存在或没有访问权限,返回false,成功返回true。
|
||||||
|
bool filemtime(const string &filename,char *mtime ,const string &fmt="yyyymmddhh24miss");
|
||||||
|
bool filemtime(const string &filename,string &mtime,const string &fmt="yyyymmddhh24miss");
|
||||||
|
|
||||||
|
// 重置文件的修改时间属性。
|
||||||
|
// filename:待重置的文件名,建议采用绝对路径的文件名。
|
||||||
|
// mtime:字符串表示的时间,格式不限,但一定要包括yyyymmddhh24miss,一个都不能少,顺序也不能变。
|
||||||
|
// 返回值:true-成功;false-失败,失败的原因保存在errno中。
|
||||||
|
bool setmtime(const string &filename,const string &mtime);
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 获取某目录及其子目录中的文件列表的类。
|
||||||
|
class cdir
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
vector<string> m_filelist; // 存放文件列表的容器(绝对路径的文件名)。
|
||||||
|
int m_pos; // 从文件列表m_filelist中已读取文件的位置。
|
||||||
|
string m_fmt; // 文件时间格式,缺省"yyyymmddhh24miss"。
|
||||||
|
|
||||||
|
cdir(const cdir &) = delete; // 禁用拷贝构造函数。
|
||||||
|
cdir &operator=(const cdir &) = delete; // 禁用赋值函数。
|
||||||
|
public:
|
||||||
|
// /project/public/_public.h
|
||||||
|
string m_dirname; // 目录名,例如:/project/public
|
||||||
|
string m_filename; // 文件名,不包括目录名,例如:_public.h
|
||||||
|
string m_ffilename; // 绝对路径的文件,例如:/project/public/_public.h
|
||||||
|
int m_filesize; // 文件的大小,单位:字节。
|
||||||
|
string m_mtime; // 文件最后一次被修改的时间,即stat结构体的st_mtime成员。
|
||||||
|
string m_ctime; // 文件生成的时间,即stat结构体的st_ctime成员。
|
||||||
|
string m_atime; // 文件最后一次被访问的时间,即stat结构体的st_atime成员。
|
||||||
|
|
||||||
|
cdir():m_pos(0),m_fmt("yyyymmddhh24miss") {} // 构造函数。
|
||||||
|
|
||||||
|
// 设置文件时间的格式,支持"yyyy-mm-dd hh24:mi:ss"和"yyyymmddhh24miss"两种,缺省是后者。
|
||||||
|
void setfmt(const string &fmt);
|
||||||
|
|
||||||
|
// 打开目录,获取目录中文件的列表,存放在m_filelist容器中。
|
||||||
|
// dirname,目录名,采用绝对路径,如/tmp/root。
|
||||||
|
// rules,文件名的匹配规则,不匹配的文件将被忽略。
|
||||||
|
// maxfiles,本次获取文件的最大数量,缺省值为10000个,如果文件太多,可能消耗太多的内存。
|
||||||
|
// bandchild,是否打开各级子目录,缺省值为false-不打开子目录。
|
||||||
|
// bsort,是否按文件名排序,缺省值为false-不排序。
|
||||||
|
// 返回值:true-成功,false-失败。
|
||||||
|
bool opendir(const string &dirname,const string &rules,const int maxfiles=10000,const bool bandchild=false,bool bsort=false);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 这是一个递归函数,被opendir()的调用,在cdir类的外部不需要调用它。
|
||||||
|
bool _opendir(const string &dirname,const string &rules,const int maxfiles,const bool bandchild);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// 从m_filelist容器中获取一条记录(文件名),同时获取该文件的大小、修改时间等信息。
|
||||||
|
// 调用opendir方法时,m_filelist容器被清空,m_pos归零,每调用一次readdir方法m_pos加1。
|
||||||
|
// 当m_pos小于m_filelist.size(),返回true,否则返回false。
|
||||||
|
bool readdir();
|
||||||
|
|
||||||
|
unsigned int size() { return m_filelist.size(); }
|
||||||
|
|
||||||
|
~cdir(); // 析构函数。
|
||||||
|
};
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 写文件的类。
|
||||||
|
class cofile // class out file
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ofstream fout; // 写入文件的对象。
|
||||||
|
string m_filename; // 文件名,建议采用绝对路径。
|
||||||
|
string m_filenametmp; // 临时文件名,在m_filename后面加".tmp"。
|
||||||
|
public:
|
||||||
|
cofile() {}
|
||||||
|
bool isopen() const { return fout.is_open(); } // 文件是否已打开。
|
||||||
|
|
||||||
|
// 打开文件。
|
||||||
|
// filename,待打开的文件名。
|
||||||
|
// btmp,是否采用临时文件的方案。
|
||||||
|
// mode,打开文件的模式。
|
||||||
|
// benbuffer,是否启用文件缓冲区。
|
||||||
|
bool open(const string &filename,const bool btmp=true,const ios::openmode mode=ios::out,const bool benbuffer=true);
|
||||||
|
|
||||||
|
// 把数据以文本的方式格式化输出到文件。
|
||||||
|
template< typename... Args >
|
||||||
|
bool writeline(const char* fmt, Args... args)
|
||||||
|
{
|
||||||
|
if (fout.is_open()==false) return false;
|
||||||
|
|
||||||
|
fout << sformat(fmt,args...);
|
||||||
|
|
||||||
|
return fout.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重载<<运算符,把数据以文本的方式输出到文件。
|
||||||
|
// 注意:换行只能用\n,不能用endl。
|
||||||
|
template<typename T>
|
||||||
|
cofile& operator<<(const T &value)
|
||||||
|
{
|
||||||
|
fout << value; return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把二进制数据写入文件。
|
||||||
|
bool write(void *buf,int bufsize);
|
||||||
|
|
||||||
|
// 关闭文件,并且把临时文件名改为正式文件名。
|
||||||
|
bool closeandrename();
|
||||||
|
|
||||||
|
// 关闭文件,如果有临时文件,则删除它。
|
||||||
|
void close();
|
||||||
|
|
||||||
|
~cofile() { close(); };
|
||||||
|
};
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 读取文件的类。
|
||||||
|
class cifile // class in file
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ifstream fin; // 读取文件的对象。
|
||||||
|
string m_filename; // 文件名,建议采用绝对路径。
|
||||||
|
public:
|
||||||
|
cifile() {}
|
||||||
|
|
||||||
|
// 判断文件是否已打开。
|
||||||
|
bool isopen() const { return fin.is_open(); }
|
||||||
|
|
||||||
|
// 打开文件。
|
||||||
|
// filename,待打开的文件名。
|
||||||
|
// mode,打开文件的模式。
|
||||||
|
bool open(const string &filename,const ios::openmode mode=ios::in);
|
||||||
|
|
||||||
|
// 以行的方式读取文本文件,endbz指定行的结尾标志,缺省为空,没有结尾标志。
|
||||||
|
bool readline(string &buf,const string& endbz="");
|
||||||
|
|
||||||
|
// 读取二进制文件,返回实际读取到的字节数。
|
||||||
|
int read(void *buf,const int bufsize);
|
||||||
|
|
||||||
|
// 关闭并删除文件。
|
||||||
|
bool closeandremove();
|
||||||
|
|
||||||
|
// 只关闭文件。
|
||||||
|
void close();
|
||||||
|
|
||||||
|
~cifile() { close(); }
|
||||||
|
};
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 自旋锁。
|
||||||
|
class spinlock_mutex
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
atomic_flag flag;
|
||||||
|
|
||||||
|
spinlock_mutex(const spinlock_mutex&) = delete;
|
||||||
|
spinlock_mutex& operator=(const spinlock_mutex) = delete;
|
||||||
|
public:
|
||||||
|
spinlock_mutex()
|
||||||
|
{
|
||||||
|
flag.clear();
|
||||||
|
}
|
||||||
|
void lock() // 加锁。
|
||||||
|
{
|
||||||
|
while (flag.test_and_set())
|
||||||
|
;
|
||||||
|
}
|
||||||
|
void unlock() // 解锁。
|
||||||
|
{
|
||||||
|
flag.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
// 日志文件。
|
||||||
|
class clogfile
|
||||||
|
{
|
||||||
|
ofstream fout; // 日志文件对象。
|
||||||
|
string m_filename; // 日志文件名,建议采用绝对路径。
|
||||||
|
ios::openmode m_mode; // 日志文件的打开模式。
|
||||||
|
bool m_backup; // 是否自动切换日志。
|
||||||
|
int m_maxsize; // 当日志文件的大小超过本参数时,自动切换日志。
|
||||||
|
bool m_enbuffer; // 是否启用文件缓冲区。
|
||||||
|
spinlock_mutex m_splock; // 自旋锁,用于多线程程序中给写日志的操作加锁。
|
||||||
|
|
||||||
|
public:
|
||||||
|
// 构造函数,日志文件的大小缺省100M。
|
||||||
|
clogfile(int maxsize=100):m_maxsize(maxsize){}
|
||||||
|
|
||||||
|
// 打开日志文件。
|
||||||
|
// filename:日志文件名,建议采用绝对路径,如果文件名中的目录不存在,就先创建目录。
|
||||||
|
// openmode:日志文件的打开模式,缺省值是ios::app。
|
||||||
|
// bbackup:是否自动切换(备份),true-切换,false-不切换,在多进程的服务程序中,如果多个进程共用一个日志文件,bbackup必须为false。
|
||||||
|
// benbuffer:是否启用文件缓冲机制,true-启用,false-不启用,如果启用缓冲区,那么写进日志文件中的内容不会立即写入文件,缺省是不启用。
|
||||||
|
// 注意,在多进程的程序中,多个进程往同一日志文件写入大量的日志时,可能会出现小混乱,但是,多线程不会。
|
||||||
|
// 1)多个进程往同一日志文件写入大量的日志时,可能会出现小混乱,这个问题并不严重,可以容忍;
|
||||||
|
// 2)只有同时写大量日志时才会出现混乱,在实际开发中,这种情况不多见。
|
||||||
|
// 3)如果业务无法容忍,可以用信号量加锁。
|
||||||
|
bool open(const string &filename,const ios::openmode mode=ios::app,const bool bbackup=true,const bool benbuffer=false);
|
||||||
|
|
||||||
|
// 把日志内容以文本的方式格式化输出到日志文件,并且,在日志内容前面写入时间。
|
||||||
|
template< typename... Args >
|
||||||
|
bool write(const char* fmt, Args... args)
|
||||||
|
{
|
||||||
|
if (fout.is_open()==false) return false;
|
||||||
|
|
||||||
|
backup(); // 判断是否需要切换日志文件。
|
||||||
|
|
||||||
|
m_splock.lock(); // 加锁。
|
||||||
|
fout << ltime1() << " " << sformat(fmt,args...); // 把当前时间和日志内容写入日志文件。
|
||||||
|
m_splock.unlock(); // 解锁。
|
||||||
|
|
||||||
|
return fout.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重载<<运算符,把日志内容以文本的方式输出到日志文件,不会在日志内容前面写时间。
|
||||||
|
// 注意:内容换行用\n,不能用endl。
|
||||||
|
template<typename T>
|
||||||
|
clogfile& operator<<(const T &value)
|
||||||
|
{
|
||||||
|
m_splock.lock();
|
||||||
|
fout << value;
|
||||||
|
m_splock.unlock();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 如果日志文件的大小超过m_maxsize的值,就把当前的日志文件名改为历史日志文件名,再创建新的当前日志文件。
|
||||||
|
// 备份后的文件会在日志文件名后加上日期时间,如/tmp/log/filetodb.log.20200101123025。
|
||||||
|
// 注意,在多进程的程序中,日志文件不可切换,多线的程序中,日志文件可以切换。
|
||||||
|
bool backup();
|
||||||
|
public:
|
||||||
|
void close() { fout.close(); }
|
||||||
|
|
||||||
|
~clogfile() { close(); };
|
||||||
|
};
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// 以下是socket通讯的函数和类
|
||||||
|
|
||||||
|
// socket通讯的客户端类
|
||||||
|
class ctcpclient
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int m_connfd; // 客户端的socket.
|
||||||
|
string m_ip; // 服务端的ip地址。
|
||||||
|
int m_port; // 服务端通讯的端口。
|
||||||
|
public:
|
||||||
|
ctcpclient(): m_connfd(-1),m_port(0) { } // 构造函数。
|
||||||
|
|
||||||
|
// 向服务端发起连接请求。
|
||||||
|
// ip:服务端的ip地址。
|
||||||
|
// port:服务端通讯的端口。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool connect(const string &ip,const int port);
|
||||||
|
|
||||||
|
// 接收对端发送过来的数据。
|
||||||
|
// buffer:存放接收数据缓冲区。
|
||||||
|
// ibuflen: 打算接收数据的大小。
|
||||||
|
// itimeout:等待数据的超时时间(秒):-1-不等待;0-无限等待;>0-等待的秒数。
|
||||||
|
// 返回值:true-成功;false-失败,失败有两种情况:1)等待超时;2)socket连接已不可用。
|
||||||
|
bool read(string &buffer,const int itimeout=0); // 接收文本数据。
|
||||||
|
bool read(void *buffer,const int ibuflen,const int itimeout=0); // 接收二进制数据。
|
||||||
|
|
||||||
|
// 向对端发送数据。
|
||||||
|
// buffer:待发送数据缓冲区。
|
||||||
|
// ibuflen:待发送数据的大小。
|
||||||
|
// 返回值:true-成功;false-失败,如果失败,表示socket连接已不可用。
|
||||||
|
bool write(const string &buffer); // 发送文本数据。
|
||||||
|
bool write(const void *buffer,const int ibuflen); // 发送二进制数据。
|
||||||
|
|
||||||
|
// 断开与服务端的连接
|
||||||
|
void close();
|
||||||
|
|
||||||
|
~ctcpclient(); // 析构函数自动关闭socket,释放资源。
|
||||||
|
};
|
||||||
|
|
||||||
|
// socket通讯的服务端类
|
||||||
|
class ctcpserver
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int m_socklen; // 结构体struct sockaddr_in的大小。
|
||||||
|
struct sockaddr_in m_clientaddr; // 客户端的地址信息。
|
||||||
|
struct sockaddr_in m_servaddr; // 服务端的地址信息。
|
||||||
|
int m_listenfd; // 服务端用于监听的socket。
|
||||||
|
int m_connfd; // 客户端连接上来的socket。
|
||||||
|
public:
|
||||||
|
ctcpserver():m_listenfd(-1),m_connfd(-1) {} // 构造函数。
|
||||||
|
|
||||||
|
// 服务端初始化。
|
||||||
|
// port:指定服务端用于监听的端口。
|
||||||
|
// 返回值:true-成功;false-失败,一般情况下,只要port设置正确,没有被占用,初始化都会成功。
|
||||||
|
bool initserver(const unsigned int port,const int backlog=5);
|
||||||
|
|
||||||
|
// 从已连接队列中获取一个客户端连接,如果已连接队列为空,将阻塞等待。
|
||||||
|
// 返回值:true-成功的获取了一个客户端连接,false-失败,如果accept失败,可以重新accept。
|
||||||
|
bool accept();
|
||||||
|
|
||||||
|
// 获取客户端的ip地址。
|
||||||
|
// 返回值:客户端的ip地址,如"192.168.1.100"。
|
||||||
|
char *getip();
|
||||||
|
|
||||||
|
// 接收对端发送过来的数据。
|
||||||
|
// buffer:存放接收数据的缓冲区。
|
||||||
|
// ibuflen: 打算接收数据的大小。
|
||||||
|
// itimeout:等待数据的超时时间(秒):-1-不等待;0-无限等待;>0-等待的秒数。
|
||||||
|
// 返回值:true-成功;false-失败,失败有两种情况:1)等待超时;2)socket连接已不可用。
|
||||||
|
bool read(string &buffer,const int itimeout=0); // 接收文本数据。
|
||||||
|
bool read(void *buffer,const int ibuflen,const int itimeout=0); // 接收二进制数据。
|
||||||
|
|
||||||
|
// 向对端发送数据。
|
||||||
|
// buffer:待发送数据缓冲区。
|
||||||
|
// ibuflen:待发送数据的大小。
|
||||||
|
// 返回值:true-成功;false-失败,如果失败,表示socket连接已不可用。
|
||||||
|
bool write(const string &buffer); // 发送文本数据。
|
||||||
|
bool write(const void *buffer,const int ibuflen); // 发送二进制数据。
|
||||||
|
|
||||||
|
// 关闭监听的socket,即m_listenfd,常用于多进程服务程序的子进程代码中。
|
||||||
|
void closelisten();
|
||||||
|
|
||||||
|
// 关闭客户端的socket,即m_connfd,常用于多进程服务程序的父进程代码中。
|
||||||
|
void closeclient();
|
||||||
|
|
||||||
|
~ctcpserver(); // 析构函数自动关闭socket,释放资源。
|
||||||
|
};
|
||||||
|
|
||||||
|
// 接收socket的对端发送过来的数据。
|
||||||
|
// sockfd:可用的socket连接。
|
||||||
|
// buffer:接收数据缓冲区的地址。
|
||||||
|
// ibuflen:本次成功接收数据的字节数。
|
||||||
|
// itimeout:读取数据超时的时间,单位:秒,-1-不等待;0-无限等待;>0-等待的秒数。
|
||||||
|
// 返回值:true-成功;false-失败,失败有两种情况:1)等待超时;2)socket连接已不可用。
|
||||||
|
bool tcpread(const int sockfd,string &buffer,const int itimeout=0); // 读取文本数据。
|
||||||
|
bool tcpread(const int sockfd,void *buffer,const int ibuflen,const int itimeout=0); // 读取二进制数据。
|
||||||
|
|
||||||
|
// 向socket的对端发送数据。
|
||||||
|
// sockfd:可用的socket连接。
|
||||||
|
// buffer:待发送数据缓冲区的地址。
|
||||||
|
// ibuflen:待发送数据的字节数。
|
||||||
|
// 返回值:true-成功;false-失败,如果失败,表示socket连接已不可用。
|
||||||
|
bool tcpwrite(const int sockfd,const string &buffer); // 写入文本数据。
|
||||||
|
bool tcpwrite(const int sockfd,const void *buffer,const int ibuflen); // 写入二进制数据。
|
||||||
|
|
||||||
|
// 从已经准备好的socket中读取数据。
|
||||||
|
// sockfd:已经准备好的socket连接。
|
||||||
|
// buffer:存放数据的地址。
|
||||||
|
// n:本次打算读取数据的字节数。
|
||||||
|
// 返回值:成功接收到n字节的数据后返回true,socket连接不可用返回false。
|
||||||
|
bool readn(const int sockfd,char *buffer,const size_t n);
|
||||||
|
|
||||||
|
// 向已经准备好的socket中写入数据。
|
||||||
|
// sockfd:已经准备好的socket连接。
|
||||||
|
// buffer:待写入数据的地址。
|
||||||
|
// n:待写入数据的字节数。
|
||||||
|
// 返回值:成功写入完n字节的数据后返回true,socket连接不可用返回false。
|
||||||
|
bool writen(const int sockfd,const char *buffer,const size_t n);
|
||||||
|
|
||||||
|
// 以上是socket通讯的函数和类
|
||||||
|
///////////////////////////////////// /////////////////////////////////////
|
||||||
|
|
||||||
|
// 忽略关闭全部的信号、关闭全部的IO,缺省只忽略信号,不关IO。
|
||||||
|
void closeioandsignal(bool bcloseio=false);
|
||||||
|
|
||||||
|
// 循环队列。
|
||||||
|
template <class TT, int MaxLength>
|
||||||
|
class squeue
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
bool m_inited; // 队列被初始化标志,true-已初始化;false-未初始化。
|
||||||
|
TT m_data[MaxLength]; // 用数组存储循环队列中的元素。
|
||||||
|
int m_head; // 队列的头指针。
|
||||||
|
int m_tail; // 队列的尾指针,指向队尾元素。
|
||||||
|
int m_length; // 队列的实际长度。
|
||||||
|
squeue(const squeue &) = delete; // 禁用拷贝构造函数。
|
||||||
|
squeue &operator=(const squeue &) = delete; // 禁用赋值函数。
|
||||||
|
public:
|
||||||
|
|
||||||
|
squeue() { init(); } // 构造函数。
|
||||||
|
|
||||||
|
// 循环队列的初始化操作。
|
||||||
|
// 注意:如果用于共享内存的队列,不会调用构造函数,必须调用此函数初始化。
|
||||||
|
void init()
|
||||||
|
{
|
||||||
|
if (m_inited!=true) // 循环队列的初始化只能执行一次。
|
||||||
|
{
|
||||||
|
m_head=0; // 头指针。
|
||||||
|
m_tail=MaxLength-1; // 为了方便写代码,初始化时,尾指针指向队列的最后一个位置。
|
||||||
|
m_length=0; // 队列的实际长度。
|
||||||
|
memset(m_data,0,sizeof(m_data)); // 数组元素清零。
|
||||||
|
m_inited=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 元素入队,返回值:false-失败;true-成功。
|
||||||
|
bool push(const TT &ee)
|
||||||
|
{
|
||||||
|
if (full() == true)
|
||||||
|
{
|
||||||
|
cout << "循环队列已满,入队失败。\n"; return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先移动队尾指针,然后再拷贝数据。
|
||||||
|
m_tail=(m_tail+1)%MaxLength; // 队尾指针后移。
|
||||||
|
m_data[m_tail]=ee;
|
||||||
|
m_length++;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 求循环队列的长度,返回值:>=0-队列中元素的个数。
|
||||||
|
int size()
|
||||||
|
{
|
||||||
|
return m_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断循环队列是否为空,返回值:true-空,false-非空。
|
||||||
|
bool empty()
|
||||||
|
{
|
||||||
|
if (m_length == 0) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断循环队列是否已满,返回值:true-已满,false-未满。
|
||||||
|
bool full()
|
||||||
|
{
|
||||||
|
if (m_length == MaxLength) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查看队头元素的值,元素不出队。
|
||||||
|
TT& front()
|
||||||
|
{
|
||||||
|
return m_data[m_head];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 元素出队,返回值:false-失败;true-成功。
|
||||||
|
bool pop()
|
||||||
|
{
|
||||||
|
if (empty() == true) return false;
|
||||||
|
|
||||||
|
m_head=(m_head+1)%MaxLength; // 队列头指针后移。
|
||||||
|
m_length--;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示循环队列中全部的元素。
|
||||||
|
// 这是一个临时的用于调试的函数,队列中元素的数据类型支持cout输出才可用。
|
||||||
|
void printqueue()
|
||||||
|
{
|
||||||
|
for (int ii = 0; ii < size(); ii++)
|
||||||
|
{
|
||||||
|
cout << "m_data[" << (m_head+ii)%MaxLength << "],value=" \
|
||||||
|
<< m_data[(m_head+ii)%MaxLength] << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 信号量。
|
||||||
|
class csemp
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
union semun // 用于信号量操作的共同体。
|
||||||
|
{
|
||||||
|
int val;
|
||||||
|
struct semid_ds *buf;
|
||||||
|
unsigned short *arry;
|
||||||
|
};
|
||||||
|
|
||||||
|
int m_semid; // 信号量id(描述符)。
|
||||||
|
|
||||||
|
// 如果把sem_flg设置为SEM_UNDO,操作系统将跟踪进程对信号量的修改情况,
|
||||||
|
// 在全部修改过信号量的进程(正常或异常)终止后,操作系统将把信号量恢复为初始值。
|
||||||
|
// 如果信号量用于互斥锁,设置为SEM_UNDO。
|
||||||
|
// 如果信号量用于生产消费者模型,设置为0。
|
||||||
|
short m_sem_flg;
|
||||||
|
|
||||||
|
csemp(const csemp &) = delete; // 禁用拷贝构造函数。
|
||||||
|
csemp &operator=(const csemp &) = delete; // 禁用赋值函数。
|
||||||
|
public:
|
||||||
|
csemp():m_semid(-1){}
|
||||||
|
|
||||||
|
// 如果信号量已存在,获取信号量;如果信号量不存在,则创建它并初始化为value。
|
||||||
|
// 如果用于互斥锁,value填1,sem_flg填SEM_UNDO。
|
||||||
|
// 如果用于生产消费者模型,value填0,sem_flg填0。
|
||||||
|
bool init(key_t key,unsigned short value=1,short sem_flg=SEM_UNDO);
|
||||||
|
bool wait(short value=-1); // 信号量的P操作,如果信号量的值是0,将阻塞等待,直到信号量的值大于0。
|
||||||
|
bool post(short value=1); // 信号量的V操作。
|
||||||
|
int getvalue(); // 获取信号量的值,成功返回信号量的值,失败返回-1。
|
||||||
|
bool destroy(); // 销毁信号量。
|
||||||
|
~csemp();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 进程心跳信息的结构体。
|
||||||
|
struct st_procinfo
|
||||||
|
{
|
||||||
|
int pid=0; // 进程id。
|
||||||
|
char pname[51]={0}; // 进程名称,可以为空。
|
||||||
|
int timeout=0; // 超时时间,单位:秒。
|
||||||
|
time_t atime=0; // 最后一次心跳的时间,用整数表示。
|
||||||
|
st_procinfo() = default; // 有了自定义的构造函数,编译器将不提供默认构造函数,所以启用默认构造函数。
|
||||||
|
st_procinfo(const int in_pid,const string & in_pname,const int in_timeout, const time_t in_atime)
|
||||||
|
:pid(in_pid),timeout(in_timeout),atime(in_atime) { strncpy(pname,in_pname.c_str(),50); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// 以下几个宏用于进程的心跳。
|
||||||
|
#define MAXNUMP 1000 // 最大的进程数量。
|
||||||
|
#define SHMKEYP 0x5095 // 共享内存的key。
|
||||||
|
#define SEMKEYP 0x5095 // 信号量的key。
|
||||||
|
|
||||||
|
// 查看共享内存: ipcs -m
|
||||||
|
// 删除共享内存: ipcrm -m shmid
|
||||||
|
// 查看信号量: ipcs -s
|
||||||
|
// 删除信号量: ipcrm sem semid
|
||||||
|
|
||||||
|
// 进程心跳操作类。
|
||||||
|
class cpactive
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int m_shmid; // 共享内存的id。
|
||||||
|
int m_pos; // 当前进程在共享内存进程组中的位置。
|
||||||
|
st_procinfo *m_shm; // 指向共享内存的地址空间。
|
||||||
|
|
||||||
|
public:
|
||||||
|
cpactive(); // 初始化成员变量。
|
||||||
|
|
||||||
|
// 把当前进程的信息加入共享内存进程组中。
|
||||||
|
bool addpinfo(const int timeout,const string &pname="",clogfile *logfile=nullptr);
|
||||||
|
|
||||||
|
// 更新共享内存进程组中当前进程的心跳时间。
|
||||||
|
bool uptatime();
|
||||||
|
|
||||||
|
~cpactive(); // 从共享内存中删除当前进程的心跳记录。
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
232
EL/_ftp.cpp
Normal file
232
EL/_ftp.cpp
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
/****************************************************************************************/
|
||||||
|
/* 程序名:_ftp.cpp,此程序是开发框架的ftp客户端工具的类的定义文件。 */
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
#include "_ftp.h"
|
||||||
|
|
||||||
|
namespace eviwbh
|
||||||
|
{
|
||||||
|
|
||||||
|
cftpclient::cftpclient()
|
||||||
|
{
|
||||||
|
m_ftpconn=0;
|
||||||
|
|
||||||
|
initdata();
|
||||||
|
|
||||||
|
FtpInit();
|
||||||
|
|
||||||
|
m_connectfailed=false;
|
||||||
|
m_loginfailed=false;
|
||||||
|
m_optionfailed=false;
|
||||||
|
}
|
||||||
|
|
||||||
|
cftpclient::~cftpclient()
|
||||||
|
{
|
||||||
|
logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
void cftpclient::initdata()
|
||||||
|
{
|
||||||
|
m_size=0;
|
||||||
|
|
||||||
|
m_mtime.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::login(const string &host,const string &username,const string &password,const int imode)
|
||||||
|
{
|
||||||
|
if (m_ftpconn != 0) { FtpQuit(m_ftpconn); m_ftpconn=0; }
|
||||||
|
|
||||||
|
m_connectfailed=m_loginfailed=m_optionfailed=false;
|
||||||
|
|
||||||
|
if (FtpConnect(host.c_str(),&m_ftpconn) == false) { m_connectfailed=true; return false; }
|
||||||
|
|
||||||
|
if (FtpLogin(username.c_str(),password.c_str(),m_ftpconn) == false) { m_loginfailed=true; return false; }
|
||||||
|
|
||||||
|
if (FtpOptions(FTPLIB_CONNMODE,(long)imode,m_ftpconn) == false) { m_optionfailed=true; return false; }
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::logout()
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
FtpQuit(m_ftpconn);
|
||||||
|
|
||||||
|
m_ftpconn=0;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::get(const string &remotefilename,const string &localfilename,const bool bcheckmtime)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
// 创建本地文件目录。
|
||||||
|
newdir(localfilename);
|
||||||
|
|
||||||
|
// 生成本地文件的临时文件名。
|
||||||
|
string strlocalfilenametmp=localfilename+".tmp";
|
||||||
|
|
||||||
|
// 获取远程服务器的文件的时间。
|
||||||
|
if (mtime(remotefilename) == false) return false;
|
||||||
|
|
||||||
|
// 取文件。
|
||||||
|
if (FtpGet(strlocalfilenametmp.c_str(),remotefilename.c_str(),FTPLIB_IMAGE,m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
// 判断文件下载前和下载后的时间,如果时间不同,表示在文件传输的过程中已发生了变化,返回失败。
|
||||||
|
if (bcheckmtime==true)
|
||||||
|
{
|
||||||
|
string strmtime=m_mtime;
|
||||||
|
|
||||||
|
if (mtime(remotefilename) == false) return false;
|
||||||
|
|
||||||
|
if (m_mtime!=strmtime) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重置文件时间。
|
||||||
|
setmtime(strlocalfilenametmp,m_mtime);
|
||||||
|
|
||||||
|
// 改为正式的文件。
|
||||||
|
if (rename(strlocalfilenametmp.c_str(),localfilename.c_str()) != 0) return false;
|
||||||
|
|
||||||
|
// 获取文件的大小。
|
||||||
|
m_size=filesize(localfilename);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::mtime(const string &remotefilename)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
m_mtime.clear();
|
||||||
|
|
||||||
|
string strmtime;
|
||||||
|
strmtime.resize(14);
|
||||||
|
|
||||||
|
if (FtpModDate(remotefilename.c_str(),&strmtime[0],14,m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
// 把UTC时间转换为本地时间。
|
||||||
|
addtime(strmtime,m_mtime,0+8*60*60,"yyyymmddhh24miss");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::size(const string &remotefilename)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
m_size=0;
|
||||||
|
|
||||||
|
if (FtpSize(remotefilename.c_str(),&m_size,FTPLIB_IMAGE,m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::chdir(const string &remotedir)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpChdir(remotedir.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::mkdir(const string &remotedir)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpMkdir(remotedir.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::rmdir(const string &remotedir)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpRmdir(remotedir.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::nlist(const string &remotedir,const string &listfilename)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
newdir(listfilename.c_str()); // 创建本地list文件目录
|
||||||
|
|
||||||
|
if (FtpNlst(listfilename.c_str(),remotedir.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::put(const string &localfilename,const string &remotefilename,const bool bchecksize)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
// 生成服务器文件的临时文件名。
|
||||||
|
string strremotefilenametmp=remotefilename+".tmp";
|
||||||
|
|
||||||
|
string filetime1,filetime2;
|
||||||
|
filemtime(localfilename,filetime1); // 获取上传文件之前的时间。
|
||||||
|
|
||||||
|
// 发送文件。
|
||||||
|
if (FtpPut(localfilename.c_str(),strremotefilenametmp.c_str(),FTPLIB_IMAGE,m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
filemtime(localfilename,filetime2); // 获取上传文件之后的时间。
|
||||||
|
|
||||||
|
// 如果文件上传前后的时间不一致,说明本地有修改文件,放弃本次上传。
|
||||||
|
if (filetime1!=filetime2) { ftpdelete(strremotefilenametmp); return false; }
|
||||||
|
|
||||||
|
// 重命名文件。
|
||||||
|
if (FtpRename(strremotefilenametmp.c_str(),remotefilename.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
// 判断已上传的文件的大小与本地文件是否相同,确保上传成功。
|
||||||
|
// 一般来说,不会出现文件大小不一致的情况,如果有,应该是服务器方的原因,不太好处理。
|
||||||
|
if (bchecksize==true)
|
||||||
|
{
|
||||||
|
if (size(remotefilename) == false) return false;
|
||||||
|
|
||||||
|
if (m_size != filesize(localfilename)) { ftpdelete(remotefilename); return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::ftpdelete(const string &remotefilename)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpDelete(remotefilename.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::ftprename(const string &srcremotefilename,const string &dstremotefilename)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpRename(srcremotefilename.c_str(),dstremotefilename.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cftpclient::site(const string &command)
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return false;
|
||||||
|
|
||||||
|
if (FtpSite(command.c_str(),m_ftpconn) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *cftpclient::response()
|
||||||
|
{
|
||||||
|
if (m_ftpconn == 0) return 0;
|
||||||
|
|
||||||
|
return FtpLastResponse(m_ftpconn);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end namespace idc
|
116
EL/_ftp.h
Normal file
116
EL/_ftp.h
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
/****************************************************************************************/
|
||||||
|
/* 程序名:_ftp.h,此程序是开发框架的ftp客户端工具的类的声明文件。 */
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __FTP_H
|
||||||
|
#define __FTP_H
|
||||||
|
|
||||||
|
#include "_el.h"
|
||||||
|
#include "ftplib.h"
|
||||||
|
|
||||||
|
namespace eviwbh
|
||||||
|
{
|
||||||
|
|
||||||
|
class cftpclient
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
netbuf *m_ftpconn; // ftp连接句柄。
|
||||||
|
public:
|
||||||
|
unsigned int m_size; // 文件的大小,单位:字节。
|
||||||
|
string m_mtime; // 文件的修改时间,格式:yyyymmddhh24miss。
|
||||||
|
|
||||||
|
// 以下三个成员变量用于存放login方法登录失败的原因。
|
||||||
|
bool m_connectfailed; // 如果网络连接失败,该成员的值为true。
|
||||||
|
bool m_loginfailed; // 如果登录失败,用户名和密码不正确,或没有登录权限,该成员的值为true。
|
||||||
|
bool m_optionfailed; // 如果设置传输模式失败,该成员变量的值为true。
|
||||||
|
|
||||||
|
cftpclient(); // 类的构造函数。
|
||||||
|
~cftpclient(); // 类的析构函数。
|
||||||
|
|
||||||
|
cftpclient(const cftpclient&) = delete;
|
||||||
|
cftpclient& operator=(const cftpclient) = delete;
|
||||||
|
|
||||||
|
void initdata(); // 初始化m_size和m_mtime成员变量。
|
||||||
|
|
||||||
|
// 登录ftp服务器。
|
||||||
|
// host:ftp服务器ip地址和端口,中间用":"分隔,如"192.168.1.1:21"。
|
||||||
|
// username:登录ftp服务器用户名。
|
||||||
|
// password:登录ftp服务器的密码。
|
||||||
|
// imode:传输模式,1-FTPLIB_PASSIVE是被动模式,2-FTPLIB_PORT是主动模式,缺省是被动模式。
|
||||||
|
bool login(const string &host,const string &username,const string &password,const int imode=FTPLIB_PASSIVE);
|
||||||
|
|
||||||
|
// 注销。
|
||||||
|
bool logout();
|
||||||
|
|
||||||
|
// 获取ftp服务器上文件的时间。
|
||||||
|
// remotefilename:待获取的文件名。
|
||||||
|
// 返回值:false-失败;true-成功,获取到的文件时间存放在m_mtime成员变量中。
|
||||||
|
bool mtime(const string &remotefilename);
|
||||||
|
|
||||||
|
// 获取ftp服务器上文件的大小。
|
||||||
|
// remotefilename:待获取的文件名。
|
||||||
|
// 返回值:false-失败;true-成功,获取到的文件大小存放在m_size成员变量中。
|
||||||
|
bool size(const string &remotefilename);
|
||||||
|
|
||||||
|
// 改变ftp服务器的当前工作目录。
|
||||||
|
// remotedir:ftp服务器上的目录名。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool chdir(const string &remotedir);
|
||||||
|
|
||||||
|
// 在ftp服务器上创建目录。
|
||||||
|
// remotedir:ftp服务器上待创建的目录名。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool mkdir(const string &remotedir);
|
||||||
|
|
||||||
|
// 删除ftp服务器上的目录。
|
||||||
|
// remotedir:ftp服务器上待删除的目录名。
|
||||||
|
// 返回值:true-成功;如果权限不足、目录不存在或目录不为空会返回false。
|
||||||
|
bool rmdir(const string &remotedir);
|
||||||
|
|
||||||
|
// 发送NLST命令列出ftp服务器目录中的子目录名和文件名。
|
||||||
|
// remotedir:ftp服务器的目录名。
|
||||||
|
// listfilename:用于保存从服务器返回的目录和文件名列表。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
// 注意:如果列出的是ftp服务器当前目录,remotedir用"","*","."都可以,但是,不规范的ftp服务器可能有差别。
|
||||||
|
bool nlist(const string &remotedir,const string &listfilename);
|
||||||
|
|
||||||
|
// 从ftp服务器上获取文件。
|
||||||
|
// remotefilename:待获取ftp服务器上的文件名。
|
||||||
|
// localfilename:保存到本地的文件名。
|
||||||
|
// bcheckmtime:文件传输完成后,是否核对远程文件传输前后的时间,保证文件的完整性。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
// 注意:文件在传输的过程中,采用临时文件命名的方法,即在localfilename后加".tmp",在传输
|
||||||
|
// 完成后才正式改为localfilename。
|
||||||
|
bool get(const string &remotefilename,const string &localfilename,const bool bcheckmtime=true);
|
||||||
|
|
||||||
|
// 向ftp服务器发送文件。
|
||||||
|
// localfilename:本地待发送的文件名。
|
||||||
|
// remotefilename:发送到ftp服务器上的文件名。
|
||||||
|
// bchecksize:文件传输完成后,是否核对本地文件和远程文件的大小,保证文件的完整性。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
// 注意:文件在传输的过程中,采用临时文件命名的方法,即在remotefilename后加".tmp",在传输
|
||||||
|
// 完成后才正式改为remotefilename。
|
||||||
|
bool put(const string &localfilename,const string &remotefilename,const bool bchecksize=true);
|
||||||
|
|
||||||
|
// 删除ftp服务器上的文件。
|
||||||
|
// remotefilename:待删除的ftp服务器上的文件名。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool ftpdelete(const string &remotefilename);
|
||||||
|
|
||||||
|
// 重命名ftp服务器上的文件。
|
||||||
|
// srcremotefilename:ftp服务器上的原文件名。
|
||||||
|
// dstremotefilename:ftp服务器上的目标文件名。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool ftprename(const string &srcremotefilename,const string &dstremotefilename);
|
||||||
|
|
||||||
|
// 向ftp服务器发送site命令。
|
||||||
|
// command:命令的内容。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool site(const string &command);
|
||||||
|
|
||||||
|
// 获取服务器返回信息的最后一条(return a pointer to the last response received)。
|
||||||
|
char *response();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // end namespace idc
|
||||||
|
#endif
|
1455
EL/ftplib.c
Normal file
1455
EL/ftplib.c
Normal file
File diff suppressed because it is too large
Load Diff
120
EL/ftplib.h
Normal file
120
EL/ftplib.h
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/***************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* ftplib.h - header file for callable ftp access routines */
|
||||||
|
/* Copyright (C) 1996-2001, 2013, 2016 Thomas Pfau, tfpfau@gmail.com */
|
||||||
|
/* 1407 Thomas Ave, North Brunswick, NJ, 08902 */
|
||||||
|
/* */
|
||||||
|
/* This library is free software. You can redistribute it and/or */
|
||||||
|
/* modify it under the terms of the Artistic License 2.0. */
|
||||||
|
/* */
|
||||||
|
/* This library is distributed in the hope that it will be useful, */
|
||||||
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||||
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||||
|
/* Artistic License 2.0 for more details. */
|
||||||
|
/* */
|
||||||
|
/* See the file LICENSE or */
|
||||||
|
/* http://www.perlfoundation.org/artistic_license_2_0 */
|
||||||
|
/* */
|
||||||
|
/***************************************************************************/
|
||||||
|
|
||||||
|
#if !defined(__FTPLIB_H)
|
||||||
|
#define __FTPLIB_H
|
||||||
|
|
||||||
|
#if defined(__unix__) || defined(VMS)
|
||||||
|
#define GLOBALDEF
|
||||||
|
#define GLOBALREF extern
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#if defined BUILDING_LIBRARY
|
||||||
|
#define GLOBALDEF __declspec(dllexport)
|
||||||
|
#define GLOBALREF __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define GLOBALREF __declspec(dllimport)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
/* FtpAccess() type codes */
|
||||||
|
#define FTPLIB_DIR 1
|
||||||
|
#define FTPLIB_DIR_VERBOSE 2
|
||||||
|
#define FTPLIB_FILE_READ 3
|
||||||
|
#define FTPLIB_FILE_WRITE 4
|
||||||
|
|
||||||
|
/* FtpAccess() mode codes */
|
||||||
|
#define FTPLIB_ASCII 'A'
|
||||||
|
#define FTPLIB_IMAGE 'I'
|
||||||
|
#define FTPLIB_TEXT FTPLIB_ASCII
|
||||||
|
#define FTPLIB_BINARY FTPLIB_IMAGE
|
||||||
|
|
||||||
|
/* connection modes */
|
||||||
|
#define FTPLIB_PASSIVE 1
|
||||||
|
#define FTPLIB_PORT 2
|
||||||
|
|
||||||
|
/* connection option names */
|
||||||
|
#define FTPLIB_CONNMODE 1
|
||||||
|
#define FTPLIB_CALLBACK 2
|
||||||
|
#define FTPLIB_IDLETIME 3
|
||||||
|
#define FTPLIB_CALLBACKARG 4
|
||||||
|
#define FTPLIB_CALLBACKBYTES 5
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__UINT64_MAX)
|
||||||
|
typedef uint64_t fsz_t;
|
||||||
|
#else
|
||||||
|
typedef uint32_t fsz_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct NetBuf netbuf;
|
||||||
|
typedef int (*FtpCallback)(netbuf *nControl, fsz_t xfered, void *arg);
|
||||||
|
|
||||||
|
typedef struct FtpCallbackOptions {
|
||||||
|
FtpCallback cbFunc; /* function to call */
|
||||||
|
void *cbArg; /* argument to pass to function */
|
||||||
|
unsigned int bytesXferred; /* callback if this number of bytes transferred */
|
||||||
|
unsigned int idleTime; /* callback if this many milliseconds have elapsed */
|
||||||
|
} FtpCallbackOptions;
|
||||||
|
|
||||||
|
GLOBALREF int ftplib_debug;
|
||||||
|
GLOBALREF void FtpInit(void);
|
||||||
|
GLOBALREF char *FtpLastResponse(netbuf *nControl);
|
||||||
|
GLOBALREF int FtpConnect(const char *host, netbuf **nControl);
|
||||||
|
GLOBALREF int FtpOptions(int opt, long val, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpSetCallback(const FtpCallbackOptions *opt, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpClearCallback(netbuf *nControl);
|
||||||
|
GLOBALREF int FtpLogin(const char *user, const char *pass, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
|
||||||
|
netbuf **nData);
|
||||||
|
GLOBALREF int FtpRead(void *buf, int max, netbuf *nData);
|
||||||
|
GLOBALREF int FtpWrite(const void *buf, int len, netbuf *nData);
|
||||||
|
GLOBALREF int FtpClose(netbuf *nData);
|
||||||
|
GLOBALREF int FtpSite(const char *cmd, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpSysType(char *buf, int max, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpMkdir(const char *path, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpChdir(const char *path, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpCDUp(netbuf *nControl);
|
||||||
|
GLOBALREF int FtpRmdir(const char *path, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpPwd(char *path, int max, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpNlst(const char *output, const char *path, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpDir(const char *output, const char *path, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpSize(const char *path, unsigned int *size, char mode, netbuf *nControl);
|
||||||
|
#if defined(__UINT64_MAX)
|
||||||
|
GLOBALREF int FtpSizeLong(const char *path, fsz_t *size, char mode, netbuf *nControl);
|
||||||
|
#endif
|
||||||
|
GLOBALREF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpGet(const char *output, const char *path, char mode,
|
||||||
|
netbuf *nControl);
|
||||||
|
GLOBALREF int FtpPut(const char *input, const char *path, char mode,
|
||||||
|
netbuf *nControl);
|
||||||
|
GLOBALREF int FtpRename(const char *src, const char *dst, netbuf *nControl);
|
||||||
|
GLOBALREF int FtpDelete(const char *fnm, netbuf *nControl);
|
||||||
|
GLOBALREF void FtpQuit(netbuf *nControl);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __FTPLIB_H */
|
16
EL/makefile
Normal file
16
EL/makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
all: lib_el.a lib_el.so libftp.a libftp.so
|
||||||
|
|
||||||
|
lib_el.a:_el.h _el.cpp
|
||||||
|
g++ -c -o lib_el.a _el.cpp
|
||||||
|
|
||||||
|
lib_el.so:_el.h _el.cpp
|
||||||
|
g++ -fPIC -shared -o lib_el.so _el.cpp
|
||||||
|
|
||||||
|
libftp.a:ftplib.h ftplib.c
|
||||||
|
gcc -c -o libftp.a ftplib.c
|
||||||
|
|
||||||
|
libftp.so:ftplib.h ftplib.c
|
||||||
|
gcc -fPIC -shared -o libftp.so ftplib.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f lib_el.a lib_el.so libftp.a libftp.so
|
638
README.md
Normal file
638
README.md
Normal file
@ -0,0 +1,638 @@
|
|||||||
|
# 网络编程TCPdemo
|
||||||
|
|
||||||
|
tcpgetfiles tcppullfiles分别为上传和下载模块
|
||||||
|
|
||||||
|
demo中使用的框架类函数定义和实现,在EL文件夹中
|
||||||
|
|
||||||
|
## socket通讯类定义
|
||||||
|
|
||||||
|
```
|
||||||
|
// socket通讯的客户端类
|
||||||
|
class ctcpclient
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int m_connfd; // 客户端的socket.
|
||||||
|
string m_ip; // 服务端的ip地址。
|
||||||
|
int m_port; // 服务端通讯的端口。
|
||||||
|
public:
|
||||||
|
ctcpclient(): m_connfd(-1),m_port(0) { } // 构造函数。
|
||||||
|
|
||||||
|
// 向服务端发起连接请求。
|
||||||
|
// ip:服务端的ip地址。
|
||||||
|
// port:服务端通讯的端口。
|
||||||
|
// 返回值:true-成功;false-失败。
|
||||||
|
bool connect(const string &ip,const int port);
|
||||||
|
|
||||||
|
// 接收对端发送过来的数据。
|
||||||
|
// buffer:存放接收数据缓冲区。
|
||||||
|
// ibuflen: 打算接收数据的大小。
|
||||||
|
// itimeout:等待数据的超时时间(秒):-1-不等待;0-无限等待;>0-等待的秒数。
|
||||||
|
// 返回值:true-成功;false-失败,失败有两种情况:1)等待超时;2)socket连接已不可用。
|
||||||
|
bool read(string &buffer,const int itimeout=0); // 接收文本数据。
|
||||||
|
bool read(void *buffer,const int ibuflen,const int itimeout=0); // 接收二进制数据。
|
||||||
|
|
||||||
|
// 向对端发送数据。
|
||||||
|
// buffer:待发送数据缓冲区。
|
||||||
|
// ibuflen:待发送数据的大小。
|
||||||
|
// 返回值:true-成功;false-失败,如果失败,表示socket连接已不可用。
|
||||||
|
bool write(const string &buffer); // 发送文本数据。
|
||||||
|
bool write(const void *buffer,const int ibuflen); // 发送二进制数据。
|
||||||
|
|
||||||
|
// 断开与服务端的连接
|
||||||
|
void close();
|
||||||
|
|
||||||
|
~ctcpclient(); // 析构函数自动关闭socket,释放资源。
|
||||||
|
};
|
||||||
|
|
||||||
|
// socket通讯的服务端类
|
||||||
|
class ctcpserver
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int m_socklen; // 结构体struct sockaddr_in的大小。
|
||||||
|
struct sockaddr_in m_clientaddr; // 客户端的地址信息。
|
||||||
|
struct sockaddr_in m_servaddr; // 服务端的地址信息。
|
||||||
|
int m_listenfd; // 服务端用于监听的socket。
|
||||||
|
int m_connfd; // 客户端连接上来的socket。
|
||||||
|
public:
|
||||||
|
ctcpserver():m_listenfd(-1),m_connfd(-1) {} // 构造函数。
|
||||||
|
|
||||||
|
// 服务端初始化。
|
||||||
|
// port:指定服务端用于监听的端口。
|
||||||
|
// 返回值:true-成功;false-失败,一般情况下,只要port设置正确,没有被占用,初始化都会成功。
|
||||||
|
bool initserver(const unsigned int port,const int backlog=5);
|
||||||
|
|
||||||
|
// 从已连接队列中获取一个客户端连接,如果已连接队列为空,将阻塞等待。
|
||||||
|
// 返回值:true-成功的获取了一个客户端连接,false-失败,如果accept失败,可以重新accept。
|
||||||
|
bool accept();
|
||||||
|
|
||||||
|
// 获取客户端的ip地址。
|
||||||
|
// 返回值:客户端的ip地址,如"192.168.1.100"。
|
||||||
|
char *getip();
|
||||||
|
|
||||||
|
// 接收对端发送过来的数据。
|
||||||
|
// buffer:存放接收数据的缓冲区。
|
||||||
|
// ibuflen: 打算接收数据的大小。
|
||||||
|
// itimeout:等待数据的超时时间(秒):-1-不等待;0-无限等待;>0-等待的秒数。
|
||||||
|
// 返回值:true-成功;false-失败,失败有两种情况:1)等待超时;2)socket连接已不可用。
|
||||||
|
bool read(string &buffer,const int itimeout=0); // 接收文本数据。
|
||||||
|
bool read(void *buffer,const int ibuflen,const int itimeout=0); // 接收二进制数据。
|
||||||
|
|
||||||
|
// 向对端发送数据。
|
||||||
|
// buffer:待发送数据缓冲区。
|
||||||
|
// ibuflen:待发送数据的大小。
|
||||||
|
// 返回值:true-成功;false-失败,如果失败,表示socket连接已不可用。
|
||||||
|
bool write(const string &buffer); // 发送文本数据。
|
||||||
|
bool write(const void *buffer,const int ibuflen); // 发送二进制数据。
|
||||||
|
|
||||||
|
// 关闭监听的socket,即m_listenfd,常用于多进程服务程序的子进程代码中。
|
||||||
|
void closelisten();
|
||||||
|
|
||||||
|
// 关闭客户端的socket,即m_connfd,常用于多进程服务程序的父进程代码中。
|
||||||
|
void closeclient();
|
||||||
|
|
||||||
|
~ctcpserver(); // 析构函数自动关闭socket,释放资源。
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## socket通讯类实现
|
||||||
|
|
||||||
|
```
|
||||||
|
bool ctcpclient::connect(const string &ip,const int port)
|
||||||
|
{
|
||||||
|
// 如果已连接到服务端,则断开,这种处理方法没有特别的原因,不要纠结。
|
||||||
|
if (m_connfd!=-1) { ::close(m_connfd); m_connfd=-1; }
|
||||||
|
|
||||||
|
// 忽略SIGPIPE信号,防止程序异常退出。
|
||||||
|
// 如果send到一个disconnected socket上,内核就会发出SIGPIPE信号。这个信号
|
||||||
|
// 的缺省处理方法是终止进程,大多数时候这都不是我们期望的。我们重新定义这
|
||||||
|
// 个信号的处理方法,大多数情况是直接屏蔽它。
|
||||||
|
signal(SIGPIPE,SIG_IGN);
|
||||||
|
|
||||||
|
m_ip=ip;
|
||||||
|
m_port=port;
|
||||||
|
|
||||||
|
struct hostent* h;
|
||||||
|
struct sockaddr_in servaddr;
|
||||||
|
|
||||||
|
if ( (m_connfd = socket(AF_INET,SOCK_STREAM,0) ) < 0) return false;
|
||||||
|
|
||||||
|
if ( !(h = gethostbyname(m_ip.c_str())) )
|
||||||
|
{
|
||||||
|
::close(m_connfd); m_connfd=-1; return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&servaddr,0,sizeof(servaddr));
|
||||||
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_port = htons(m_port); // 指定服务端的通讯端口
|
||||||
|
memcpy(&servaddr.sin_addr,h->h_addr,h->h_length);
|
||||||
|
|
||||||
|
if (::connect(m_connfd, (struct sockaddr *)&servaddr,sizeof(servaddr)) != 0)
|
||||||
|
{
|
||||||
|
::close(m_connfd); m_connfd=-1; return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ctcpclient::close()
|
||||||
|
{
|
||||||
|
if (m_connfd >= 0) ::close(m_connfd);
|
||||||
|
|
||||||
|
m_connfd=-1;
|
||||||
|
m_port=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctcpclient::~ctcpclient()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::initserver(const unsigned int port,const int backlog)
|
||||||
|
{
|
||||||
|
// 如果服务端的socket>0,关掉它,这种处理方法没有特别的原因,不要纠结。
|
||||||
|
if (m_listenfd > 0) { ::close(m_listenfd); m_listenfd=-1; }
|
||||||
|
|
||||||
|
if ( (m_listenfd = socket(AF_INET,SOCK_STREAM,0))<=0) return false;
|
||||||
|
|
||||||
|
// 忽略SIGPIPE信号,防止程序异常退出。
|
||||||
|
// 如果往已关闭的socket继续写数据,会产生SIGPIPE信号,它的缺省行为是终止程序,所以要忽略它。
|
||||||
|
signal(SIGPIPE,SIG_IGN);
|
||||||
|
|
||||||
|
// 打开SO_REUSEADDR选项,当服务端连接处于TIME_WAIT状态时可以再次启动服务器,
|
||||||
|
// 否则bind()可能会不成功,报:Address already in use。
|
||||||
|
int opt = 1;
|
||||||
|
setsockopt(m_listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
|
||||||
|
|
||||||
|
memset(&m_servaddr,0,sizeof(m_servaddr));
|
||||||
|
m_servaddr.sin_family = AF_INET;
|
||||||
|
m_servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // 任意ip地址。
|
||||||
|
m_servaddr.sin_port = htons(port);
|
||||||
|
if (bind(m_listenfd,(struct sockaddr *)&m_servaddr,sizeof(m_servaddr)) != 0 )
|
||||||
|
{
|
||||||
|
closelisten(); return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(m_listenfd,backlog) != 0 )
|
||||||
|
{
|
||||||
|
closelisten(); return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::accept()
|
||||||
|
{
|
||||||
|
if (m_listenfd==-1) return false;
|
||||||
|
|
||||||
|
int m_socklen = sizeof(struct sockaddr_in);
|
||||||
|
if ((m_connfd=::accept(m_listenfd,(struct sockaddr *)&m_clientaddr,(socklen_t*)&m_socklen)) < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ctcpserver::getip()
|
||||||
|
{
|
||||||
|
return(inet_ntoa(m_clientaddr.sin_addr));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::read(void *buffer,const int ibuflen,const int itimeout) // 接收二进制数据。
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpread(m_connfd,buffer,ibuflen,itimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::read(string &buffer,const int itimeout) // 接收文本数据。
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpread(m_connfd,buffer,itimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpclient::read(void *buffer,const int ibuflen,const int itimeout) // 接收二进制数据。
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpread(m_connfd,buffer,ibuflen,itimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpclient::read(string &buffer,const int itimeout) // 接收文本数据。
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpread(m_connfd,buffer,itimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::write(const void *buffer,const int ibuflen) // 发送二进制数据。
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpwrite(m_connfd,(char*)buffer,ibuflen));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpserver::write(const string &buffer)
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpwrite(m_connfd,buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpclient::write(const void *buffer,const int ibuflen)
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpwrite(m_connfd,(char*)buffer,ibuflen));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ctcpclient::write(const string &buffer)
|
||||||
|
{
|
||||||
|
if (m_connfd==-1) return false;
|
||||||
|
|
||||||
|
return(tcpwrite(m_connfd,buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ctcpserver::closelisten()
|
||||||
|
{
|
||||||
|
if (m_listenfd >= 0)
|
||||||
|
{
|
||||||
|
::close(m_listenfd); m_listenfd=-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ctcpserver::closeclient()
|
||||||
|
{
|
||||||
|
if (m_connfd >= 0)
|
||||||
|
{
|
||||||
|
::close(m_connfd); m_connfd=-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctcpserver::~ctcpserver()
|
||||||
|
{
|
||||||
|
closelisten(); closeclient();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tcpread(const int sockfd,void *buffer,const int ibuflen,const int itimeout) // 接收二进制数据。
|
||||||
|
{
|
||||||
|
if (sockfd==-1) return false;
|
||||||
|
|
||||||
|
// 如果itimeout>0,表示需要等待itimeout秒,如果itimeout秒后还没有数据到达,返回false。
|
||||||
|
if (itimeout>0)
|
||||||
|
{
|
||||||
|
struct pollfd fds;
|
||||||
|
fds.fd=sockfd;
|
||||||
|
fds.events=POLLIN;
|
||||||
|
if ( poll(&fds,1,itimeout*1000) <= 0 ) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果itimeout==-1,表示不等待,立即判断socket的缓冲区中是否有数据,如果没有,返回false。
|
||||||
|
if (itimeout==-1)
|
||||||
|
{
|
||||||
|
struct pollfd fds;
|
||||||
|
fds.fd=sockfd;
|
||||||
|
fds.events=POLLIN;
|
||||||
|
if ( poll(&fds,1,0) <= 0 ) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取报文内容。
|
||||||
|
if (readn(sockfd,(char*)buffer,ibuflen) == false) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 日志类定义
|
||||||
|
|
||||||
|
```
|
||||||
|
// 日志文件
|
||||||
|
class clogfile
|
||||||
|
{
|
||||||
|
ofstream fout; // 日志文件对象。
|
||||||
|
string m_filename; // 日志文件名,建议采用绝对路径。
|
||||||
|
ios::openmode m_mode; // 日志文件的打开模式。
|
||||||
|
bool m_backup; // 是否自动切换日志。
|
||||||
|
int m_maxsize; // 当日志文件的大小超过本参数时,自动切换日志。
|
||||||
|
bool m_enbuffer; // 是否启用文件缓冲区。
|
||||||
|
spinlock_mutex m_splock; // 自旋锁,用于多线程程序中给写日志的操作加锁。
|
||||||
|
|
||||||
|
public:
|
||||||
|
// 构造函数,日志文件的大小缺省100M。
|
||||||
|
clogfile(int maxsize=100):m_maxsize(maxsize){}
|
||||||
|
|
||||||
|
// 打开日志文件。
|
||||||
|
// filename:日志文件名,建议采用绝对路径,如果文件名中的目录不存在,就先创建目录。
|
||||||
|
// openmode:日志文件的打开模式,缺省值是ios::app。
|
||||||
|
// bbackup:是否自动切换(备份),true-切换,false-不切换,在多进程的服务程序中,如果多个进程共用一个日志文件,bbackup必须为false。
|
||||||
|
// benbuffer:是否启用文件缓冲机制,true-启用,false-不启用,如果启用缓冲区,那么写进日志文件中的内容不会立即写入文件,缺省是不启用。
|
||||||
|
// 注意,在多进程的程序中,多个进程往同一日志文件写入大量的日志时,可能会出现小混乱,但是,多线程不会。
|
||||||
|
// 1)多个进程往同一日志文件写入大量的日志时,可能会出现小混乱,这个问题并不严重,可以容忍;
|
||||||
|
// 2)只有同时写大量日志时才会出现混乱,在实际开发中,这种情况不多见。
|
||||||
|
// 3)如果业务无法容忍,可以用信号量加锁。
|
||||||
|
bool open(const string &filename,const ios::openmode mode=ios::app,const bool bbackup=true,const bool benbuffer=false);
|
||||||
|
|
||||||
|
// 把日志内容以文本的方式格式化输出到日志文件,并且,在日志内容前面写入时间。
|
||||||
|
template< typename... Args >
|
||||||
|
bool write(const char* fmt, Args... args)
|
||||||
|
{
|
||||||
|
if (fout.is_open()==false) return false;
|
||||||
|
|
||||||
|
backup(); // 判断是否需要切换日志文件。
|
||||||
|
|
||||||
|
m_splock.lock(); // 加锁。
|
||||||
|
fout << ltime1() << " " << sformat(fmt,args...); // 把当前时间和日志内容写入日志文件。
|
||||||
|
m_splock.unlock(); // 解锁。
|
||||||
|
|
||||||
|
return fout.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重载<<运算符,把日志内容以文本的方式输出到日志文件,不会在日志内容前面写时间。
|
||||||
|
// 注意:内容换行用\n,不能用endl。
|
||||||
|
template<typename T>
|
||||||
|
clogfile& operator<<(const T &value)
|
||||||
|
{
|
||||||
|
m_splock.lock();
|
||||||
|
fout << value;
|
||||||
|
m_splock.unlock();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 如果日志文件的大小超过m_maxsize的值,就把当前的日志文件名改为历史日志文件名,再创建新的当前日志文件。
|
||||||
|
// 备份后的文件会在日志文件名后加上日期时间,如/tmp/log/filetodb.log.20200101123025。
|
||||||
|
// 注意,在多进程的程序中,日志文件不可切换,多线的程序中,日志文件可以切换。
|
||||||
|
bool backup();
|
||||||
|
public:
|
||||||
|
void close() { fout.close(); }
|
||||||
|
|
||||||
|
~clogfile() { close(); };
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 日志类实现
|
||||||
|
|
||||||
|
```
|
||||||
|
bool clogfile::open(const string &filename,const ios::openmode mode,const bool bbackup,const bool benbuffer)
|
||||||
|
{
|
||||||
|
// 如果日志文件是打开的状态,先关闭它。
|
||||||
|
if (fout.is_open()) fout.close();
|
||||||
|
|
||||||
|
m_filename=filename; // 日志文件名。
|
||||||
|
m_mode=mode; // 打开模式。
|
||||||
|
m_backup=bbackup; // 是否自动备份。
|
||||||
|
m_enbuffer=benbuffer; // 是否启用文件缓冲区。
|
||||||
|
|
||||||
|
newdir(m_filename,true); // 如果日志文件的目录不存在,创建它。
|
||||||
|
|
||||||
|
fout.open(m_filename,m_mode); // 打开日志文件。
|
||||||
|
|
||||||
|
if (m_enbuffer==false) fout << unitbuf; // 是否启用文件缓冲区。
|
||||||
|
|
||||||
|
return fout.is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool clogfile::backup()
|
||||||
|
{
|
||||||
|
// 不备份
|
||||||
|
if (m_backup == false) return true;
|
||||||
|
|
||||||
|
if (fout.is_open() == false) return false;
|
||||||
|
|
||||||
|
// 如果当前日志文件的大小超过m_maxsize,备份日志。
|
||||||
|
if (fout.tellp() > m_maxsize*1024*1024)
|
||||||
|
{
|
||||||
|
m_splock.lock(); // 加锁。
|
||||||
|
|
||||||
|
fout.close(); // 关闭当前日志文件。
|
||||||
|
|
||||||
|
// 拼接备份日志文件名。
|
||||||
|
string bak_filename=m_filename+"."+ltime1("yyyymmddhh24miss");
|
||||||
|
|
||||||
|
rename(m_filename.c_str(),bak_filename.c_str()); // 把当前日志文件改名为备份日志文件。
|
||||||
|
|
||||||
|
fout.open(m_filename,m_mode); // 重新打开当前日志文件。
|
||||||
|
|
||||||
|
if (m_enbuffer==false) fout << unitbuf; // 判断是否启动文件缓冲区。
|
||||||
|
|
||||||
|
m_splock.unlock(); // 解锁。
|
||||||
|
|
||||||
|
return fout.is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool cifile::open(const string &filename,const ios::openmode mode)
|
||||||
|
{
|
||||||
|
// 如果文件是打开的状态,先关闭它。
|
||||||
|
if (fin.is_open()) fin.close();
|
||||||
|
|
||||||
|
m_filename=filename;
|
||||||
|
|
||||||
|
fin.open(m_filename,mode);
|
||||||
|
|
||||||
|
return fin.is_open();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## xml函数族定义
|
||||||
|
|
||||||
|
```
|
||||||
|
// 解析xml格式字符串的函数族。
|
||||||
|
// xml格式的字符串的内容如下:
|
||||||
|
// <filename>/tmp/_public.h</filename><mtime>2020-01-01 12:20:35</mtime><size>18348</size>
|
||||||
|
// <filename>/tmp/_public.cpp</filename><mtime>2020-01-01 10:10:15</mtime><size>50945</size>
|
||||||
|
// xmlbuffer:待解析的xml格式字符串。
|
||||||
|
// fieldname:字段的标签名。
|
||||||
|
// value:传入变量的地址,用于存放字段内容,支持bool、int、insigned int、long、unsigned long、double和char[]。
|
||||||
|
// 注意:当value参数的数据类型为char []时,必须保证value数组的内存足够,否则可能发生内存溢出的问题,也可以用ilen参数限定获取字段内容的长度,ilen的缺省值为0,表示不限长度。
|
||||||
|
// 返回值:true-成功;如果fieldname参数指定的标签名不存在,返回失败。
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,string &value,const int ilen=0);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,char *value,const int ilen=0);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,bool &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,int &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned int &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,long &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned long &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,double &value);
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,float &value);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## xml函数族实现
|
||||||
|
|
||||||
|
```
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,string &value,const int ilen)
|
||||||
|
{
|
||||||
|
string start="<"+fieldname+">"; // 数据项开始的标签。
|
||||||
|
string end="</"+fieldname+">"; // 数据项结束的标签。
|
||||||
|
|
||||||
|
int startp=xmlbuffer.find(start); // 在xml中查找数据项开始的标签的位置。
|
||||||
|
if (startp==string::npos) return false;
|
||||||
|
|
||||||
|
int endp=xmlbuffer.find(end); // 在xml中查找数据项结束的标签的位置。
|
||||||
|
if (endp==string::npos) return false;
|
||||||
|
|
||||||
|
// 从xml中截取数据项的内容。
|
||||||
|
int itmplen=endp-startp-start.length();
|
||||||
|
if ( (ilen>0) && (ilen<itmplen) ) itmplen=ilen;
|
||||||
|
value=xmlbuffer.substr(startp+start.length(),itmplen);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,char *value,const int len)
|
||||||
|
{
|
||||||
|
if (value==nullptr) return false;
|
||||||
|
|
||||||
|
if (len>0) memset(value,0,len+1); // 调用者必须保证value的空间足够,否则这里会内存溢出。
|
||||||
|
|
||||||
|
string str;
|
||||||
|
getxmlbuffer(xmlbuffer,fieldname,str);
|
||||||
|
|
||||||
|
if ( (str.length()<=(unsigned int)len) || (len==0) )
|
||||||
|
{
|
||||||
|
str.copy(value,str.length());
|
||||||
|
value[str.length()]=0; // string的copy函数不会给C风格字符串的结尾加0。
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
str.copy(value,len);
|
||||||
|
value[len]=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,bool &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
toupper(str); // 转换为大写来判断(也可以转换为小写,效果相同)。
|
||||||
|
|
||||||
|
if (str=="TRUE") value=true;
|
||||||
|
else value=false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,int &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stoi(picknumber(str,true)); // stoi有异常,需要处理异常。
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned int &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stoi(picknumber(str)); // stoi有异常,需要处理异常。不提取符号 + -
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,long &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stol(picknumber(str,true)); // stol有异常,需要处理异常。
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,unsigned long &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stoul(picknumber(str)); // stoul有异常,需要处理异常。不提取符号 + -
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,double &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stod(picknumber(str,true,true)); // stod有异常,需要处理异常。提取符号和小数点。
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getxmlbuffer(const string &xmlbuffer,const string &fieldname,float &value)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
|
||||||
|
if (getxmlbuffer(xmlbuffer,fieldname,str)==false) return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
value = stof(picknumber(str,true,true)); // stof有异常,需要处理异常。提取符号和小数点。
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
29
client.cpp
Normal file
29
client.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// 测试同步通讯的客户端程序。
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=3) { cout << "Using: ./client ip port\n"; return -1;}
|
||||||
|
|
||||||
|
ctcpclient tcpclient;
|
||||||
|
if (tcpclient.connect(argv[1],atoi(argv[2]))==false)
|
||||||
|
{
|
||||||
|
printf("tcpclient.connect failed.\n"); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
clogfile logfile;
|
||||||
|
logfile.open("/tmp/demo07.log");
|
||||||
|
|
||||||
|
string strsendbuffer,strrecvbuffer;
|
||||||
|
|
||||||
|
for (int ii=1;ii<=100;ii++)
|
||||||
|
{
|
||||||
|
sformat(strsendbuffer,"这是第%d个。",ii);
|
||||||
|
logfile.write("%s\n",strsendbuffer.c_str());
|
||||||
|
tcpclient.write(strsendbuffer); // 向服务端发送报文。
|
||||||
|
|
||||||
|
tcpclient.read(strrecvbuffer); // 等待服务端的回应。
|
||||||
|
logfile.write("%s\n",strrecvbuffer.c_str());
|
||||||
|
}
|
||||||
|
}
|
39
client_fork.cpp
Normal file
39
client_fork.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// 多进程版本的异步通讯的客户端程序。
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=3) { cout << "Using: ./client_fork ip port\n"; return -1;}
|
||||||
|
|
||||||
|
ctcpclient tcpclient;
|
||||||
|
if (tcpclient.connect(argv[1],atoi(argv[2]))==false)
|
||||||
|
{
|
||||||
|
printf("tcpclient.connect failed.\n"); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
clogfile logfile;
|
||||||
|
logfile.open("/tmp/demo09.log");
|
||||||
|
|
||||||
|
if (fork()==0)
|
||||||
|
{
|
||||||
|
string strsendbuffer;
|
||||||
|
|
||||||
|
for (int ii=1;ii<=1000000;ii++)
|
||||||
|
{
|
||||||
|
sformat(strsendbuffer,"这是第%d个。",ii);
|
||||||
|
logfile.write("%s\n",strsendbuffer.c_str());
|
||||||
|
tcpclient.write(strsendbuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string strrecvbuffer;
|
||||||
|
|
||||||
|
for (int ii=1;ii<=1000000;ii++)
|
||||||
|
{
|
||||||
|
tcpclient.read(strrecvbuffer);
|
||||||
|
logfile.write("%s\n",strrecvbuffer.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
client_poll.cpp
Normal file
43
client_poll.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// I/O复用版本的异步通讯的客户端程序。
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=3) { cout << "Using: ./client_poll ip port\n"; return -1;}
|
||||||
|
|
||||||
|
ctcpclient tcpclient;
|
||||||
|
if (tcpclient.connect(argv[1],atoi(argv[2]))==false)
|
||||||
|
{
|
||||||
|
printf("tcpclient.connect failed.\n"); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
clogfile logfile;
|
||||||
|
logfile.open("/tmp/demo10.log");
|
||||||
|
|
||||||
|
string strsendbuffer,strrecvbuffer;
|
||||||
|
|
||||||
|
int ack=0; // 已接收回应报文的计数器。
|
||||||
|
|
||||||
|
for (int ii=1;ii<=1000000;ii++)
|
||||||
|
{
|
||||||
|
sformat(strsendbuffer,"这是第%d个。",ii);
|
||||||
|
logfile.write("%s\n",strsendbuffer.c_str());
|
||||||
|
tcpclient.write(strsendbuffer); // 向服务端发送报文。
|
||||||
|
|
||||||
|
while (tcpclient.read(strrecvbuffer,-1)==true) // 检查tcp的缓冲区中是否有服务端回应报文。
|
||||||
|
{
|
||||||
|
logfile.write("%s\n",strrecvbuffer.c_str());
|
||||||
|
ack++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ack<1000000)
|
||||||
|
{
|
||||||
|
if (tcpclient.read(strrecvbuffer)==true) // 等待服务端回应报文。
|
||||||
|
{
|
||||||
|
logfile.write("%s\n",strrecvbuffer.c_str());
|
||||||
|
ack++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
server.cpp
Normal file
25
server.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// 测试同步/异步通讯的服务端程序。
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=2) { cout << "Using: ./server port\n"; return -1;}
|
||||||
|
|
||||||
|
ctcpserver tcpserver;
|
||||||
|
if (tcpserver.initserver(atoi(argv[1]))==false)
|
||||||
|
{
|
||||||
|
printf("tcpserver.initserver() failed.\n"); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpserver.accept();
|
||||||
|
|
||||||
|
string strsendbuffer,strrecvbuffer;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (tcpserver.read(strrecvbuffer)==false) break; // 接收客户端的报文。
|
||||||
|
|
||||||
|
sformat(strsendbuffer,"回复:%s",strrecvbuffer.c_str());
|
||||||
|
if (tcpserver.write(strsendbuffer)==false) break; // 向客户发送回应报文。
|
||||||
|
}
|
||||||
|
}
|
285
tcpgetfiles.cpp
Normal file
285
tcpgetfiles.cpp
Normal file
@ -0,0 +1,285 @@
|
|||||||
|
/*
|
||||||
|
* 程序名:tcpgetfiles.cpp,采用tcp协议,实现文件下载的客户端。
|
||||||
|
*/
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
// 程序运行的参数结构体。
|
||||||
|
struct st_arg
|
||||||
|
{
|
||||||
|
int clienttype; // 客户端类型,1-上传文件;2-下载文件,本程序固定填2。
|
||||||
|
char ip[31]; // 服务端的IP地址。
|
||||||
|
int port; // 服务端的端口。
|
||||||
|
char srvpath[256]; // 服务端文件存放的根目录。
|
||||||
|
int ptype; // 文件下载成功后服务端文件的处理方式:1-删除文件;2-移动到备份目录。
|
||||||
|
char srvpathbak[256]; // 文件成功下载后,服务端文件备份的根目录,当ptype==2时有效。
|
||||||
|
bool andchild; // 是否下载srvpath目录下各级子目录的文件,true-是;false-否。
|
||||||
|
char matchname[256]; // 待下载文件名的匹配规则,如"*.TXT,*.XML"。
|
||||||
|
char clientpath[256]; // 客户端文件存放的根目录。
|
||||||
|
int timetvl; // 扫描服务端目录文件的时间间隔,单位:秒。
|
||||||
|
int timeout; // 进程心跳的超时时间。
|
||||||
|
char pname[51]; // 进程名,建议用"tcpgetfiles_后缀"的方式。
|
||||||
|
} starg;
|
||||||
|
|
||||||
|
clogfile logfile;
|
||||||
|
|
||||||
|
// 程序退出和信号2、15的处理函数。
|
||||||
|
void EXIT(int sig);
|
||||||
|
|
||||||
|
void _help();
|
||||||
|
|
||||||
|
// 把xml解析到参数starg结构中。
|
||||||
|
bool _xmltoarg(const char *strxmlbuffer);
|
||||||
|
|
||||||
|
ctcpclient tcpclient;
|
||||||
|
|
||||||
|
bool login(const char *argv); // 登录业务。
|
||||||
|
|
||||||
|
string strrecvbuffer; // 发送报文的buffer。
|
||||||
|
string strsendbuffer; // 接收报文的buffer。
|
||||||
|
|
||||||
|
// 文件下载的主函数。
|
||||||
|
void _tcpgetfiles();
|
||||||
|
|
||||||
|
// 接收文件的内容。
|
||||||
|
bool recvfile(const string &filename,const string &mtime,int filesize);
|
||||||
|
|
||||||
|
cpactive pactive; // 进程心跳。
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=3) { _help(); return -1; }
|
||||||
|
|
||||||
|
// 关闭全部的信号和输入输出。
|
||||||
|
// 设置信号,在shell状态下可用 "kill + 进程号" 正常终止些进程。
|
||||||
|
// 但请不要用 "kill -9 +进程号" 强行终止。
|
||||||
|
//closeioandsignal(false);
|
||||||
|
signal(SIGINT,EXIT); signal(SIGTERM,EXIT);
|
||||||
|
|
||||||
|
// 打开日志文件。
|
||||||
|
if (logfile.open(argv[1])==false)
|
||||||
|
{
|
||||||
|
printf("打开日志文件失败(%s)。\n",argv[1]); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析xml,得到程序运行的参数。
|
||||||
|
if (_xmltoarg(argv[2])==false) return -1;
|
||||||
|
|
||||||
|
pactive.addpinfo(starg.timeout,starg.pname); // 把进程的心跳信息写入共享内存。
|
||||||
|
|
||||||
|
// 向服务端发起连接请求。
|
||||||
|
if (tcpclient.connect(starg.ip,starg.port)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.connect(%s,%d) failed.\n",starg.ip,starg.port); EXIT(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录业务。
|
||||||
|
if (login(argv[2])==false) { logfile.write("login() failed.\n"); EXIT(-1); }
|
||||||
|
|
||||||
|
// 调用文件下载的主函数。
|
||||||
|
_tcpgetfiles();
|
||||||
|
|
||||||
|
EXIT(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 登录业务。
|
||||||
|
bool login(const char *argv)
|
||||||
|
{
|
||||||
|
sformat(strsendbuffer,"%s<clienttype>2</clienttype>",argv);
|
||||||
|
// xxxxxxxxxxxxxx logfile.write("发送:%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false) return false; // 向服务端发送请求报文。
|
||||||
|
|
||||||
|
if (tcpclient.read(strrecvbuffer,10)==false) return false; // 接收服务端的回应报文。
|
||||||
|
// xxxxxxxxxxxxxx logfile.write("接收:%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
logfile.write("登录(%s:%d)成功。\n",starg.ip,starg.port);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EXIT(int sig)
|
||||||
|
{
|
||||||
|
logfile.write("程序退出,sig=%d\n\n",sig);
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _help()
|
||||||
|
{
|
||||||
|
printf("\n");
|
||||||
|
printf("Using:/project/tools/bin/tcpgetfiles logfilename xmlbuffer\n\n");
|
||||||
|
|
||||||
|
printf("Sample:/project/tools/bin/procctl 20 /project/tools/bin/tcpgetfiles /log/idc/tcpgetfiles_surfdata.log "
|
||||||
|
"\"<ip>192.168.150.128</ip><port>5005</port>"\
|
||||||
|
"<clientpath>/tmp/client</clientpath>"
|
||||||
|
"<ptype>1</ptype><srvpath>/tmp/server</srvpath>"\
|
||||||
|
"<andchild>true</andchild><matchname>*</matchname>"
|
||||||
|
"<timetvl>10</timetvl><timeout>50</timeout><pname>tcpgetfiles_surfdata</pname>\"\n");
|
||||||
|
|
||||||
|
printf("本程序是数据中心的公共功能模块,采用tcp协议从服务端下载文件。\n");
|
||||||
|
printf("logfilename 本程序运行的日志文件。\n");
|
||||||
|
printf("xmlbuffer 本程序运行的参数,如下:\n");
|
||||||
|
printf("ip 服务端的IP地址。\n");
|
||||||
|
printf("port 服务端的端口。\n");
|
||||||
|
printf("ptype 文件下载成功后服务端文件的处理方式:1-删除文件;2-移动到备份目录。\n");
|
||||||
|
printf("srvpath 服务端文件存放的根目录。\n");
|
||||||
|
printf("srvpathbak 文件成功下载后,服务端文件备份的根目录,当ptype==2时有效。\n");
|
||||||
|
printf("andchild 是否下载srvpath目录下各级子目录的文件,true-是;false-否,缺省为false。\n");
|
||||||
|
printf("matchname 待下载文件名的匹配规则,如\"*.TXT,*.XML\"\n");
|
||||||
|
printf("clientpath 客户端文件存放的根目录。\n");
|
||||||
|
printf("timetvl 扫描服务目录文件的时间间隔,单位:秒,取值在1-30之间。\n");
|
||||||
|
printf("timeout 本程序的超时时间,单位:秒,视文件大小和网络带宽而定,建议设置50以上。\n");
|
||||||
|
printf("pname 进程名,尽可能采用易懂的、与其它进程不同的名称,方便故障排查。\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把xml解析到参数starg结构
|
||||||
|
bool _xmltoarg(const char *strxmlbuffer)
|
||||||
|
{
|
||||||
|
memset(&starg,0,sizeof(struct st_arg));
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"ip",starg.ip);
|
||||||
|
if (strlen(starg.ip)==0) { logfile.write("ip is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"port",starg.port);
|
||||||
|
if ( starg.port==0) { logfile.write("port is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"ptype",starg.ptype);
|
||||||
|
if ((starg.ptype!=1)&&(starg.ptype!=2)) { logfile.write("ptype not in (1,2).\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"srvpath",starg.srvpath);
|
||||||
|
if (strlen(starg.srvpath)==0) { logfile.write("srvpath is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"srvpathbak",starg.srvpathbak);
|
||||||
|
if ((starg.ptype==2)&&(strlen(starg.srvpathbak)==0)) { logfile.write("srvpathbak is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"andchild",starg.andchild);
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"matchname",starg.matchname);
|
||||||
|
if (strlen(starg.matchname)==0) { logfile.write("matchname is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"clientpath",starg.clientpath);
|
||||||
|
if (strlen(starg.clientpath)==0) { logfile.write("clientpath is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"timetvl",starg.timetvl);
|
||||||
|
if (starg.timetvl==0) { logfile.write("timetvl is null.\n"); return false; }
|
||||||
|
|
||||||
|
// 扫描服务端目录文件的时间间隔(执行下载任务的时间间隔),单位:秒。
|
||||||
|
// starg.timetvl没有必要超过30秒。
|
||||||
|
if (starg.timetvl>30) starg.timetvl=30;
|
||||||
|
|
||||||
|
// 进程心跳的超时时间,一定要大于starg.timetvl。
|
||||||
|
getxmlbuffer(strxmlbuffer,"timeout",starg.timeout);
|
||||||
|
if (starg.timeout==0) { logfile.write("timeout is null.\n"); return false; }
|
||||||
|
if (starg.timeout<=starg.timetvl) { logfile.write("starg.timeout(%d) <= starg.timetvl(%d).\n",starg.timeout,starg.timetvl); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"pname",starg.pname,50);
|
||||||
|
//if (strlen(starg.pname)==0) { logfile.write("pname is null.\n"); return false; }
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件下载的主函数。
|
||||||
|
void _tcpgetfiles()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
pactive.uptatime();
|
||||||
|
|
||||||
|
// 接收服务端的报文。
|
||||||
|
if (tcpclient.read(strrecvbuffer,starg.timetvl+10)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.read() failed.\n"); return;
|
||||||
|
}
|
||||||
|
// logfile.write("strrecvbuffer=%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
// 处理心跳报文。
|
||||||
|
if (strrecvbuffer=="<activetest>ok</activetest>")
|
||||||
|
{
|
||||||
|
strsendbuffer="ok";
|
||||||
|
// xxxxxxxxxxx logfile.write("strsendbuffer=%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.write() failed.\n"); return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理下载文件的请求报文。
|
||||||
|
if (strrecvbuffer.find("<filename>") != string::npos)
|
||||||
|
{
|
||||||
|
// 解析下载文件请求报文的xml。
|
||||||
|
string serverfilename;
|
||||||
|
string mtime;
|
||||||
|
int filesize=0;
|
||||||
|
getxmlbuffer(strrecvbuffer,"filename",serverfilename);
|
||||||
|
getxmlbuffer(strrecvbuffer,"mtime",mtime);
|
||||||
|
getxmlbuffer(strrecvbuffer,"size",filesize);
|
||||||
|
|
||||||
|
// 客户端和服务端文件的目录是不一样的,以下代码生成客户端的文件名。
|
||||||
|
// 把文件名中的srvpath替换成clientpath,要小心第三个参数
|
||||||
|
string clientfilename;
|
||||||
|
clientfilename=serverfilename;
|
||||||
|
replacestr(clientfilename,starg.srvpath,starg.clientpath,false);
|
||||||
|
|
||||||
|
// 接收文件的内容。
|
||||||
|
logfile.write("recv %s(%d) ...",clientfilename.c_str(),filesize);
|
||||||
|
if (recvfile(clientfilename,mtime,filesize)==true)
|
||||||
|
{
|
||||||
|
logfile << "ok.\n";
|
||||||
|
sformat(strsendbuffer,"<filename>%s</filename><result>ok</result>",serverfilename.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logfile << "failed.\n";
|
||||||
|
sformat(strsendbuffer,"<filename>%s</filename><result>failed</result>",serverfilename.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把接收结果返回给对端。
|
||||||
|
// xxxxxxxxxxx logfile.write("strsendbuffer=%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.write() failed.\n"); return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接收文件的内容。
|
||||||
|
bool recvfile(const string &filename,const string &mtime,int filesize)
|
||||||
|
{
|
||||||
|
int totalbytes=0; // 已接收文件的总字节数。
|
||||||
|
int onread=0; // 本次打算接收的字节数。
|
||||||
|
char buffer[1000]; // 接收文件内容的缓冲区。
|
||||||
|
cofile ofile;
|
||||||
|
|
||||||
|
newdir(filename);
|
||||||
|
|
||||||
|
if (ofile.open(filename,true,ios::out|ios::binary)==false) return false;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
|
||||||
|
// 计算本次应该接收的字节数。
|
||||||
|
if (filesize-totalbytes>1000) onread=1000;
|
||||||
|
else onread=filesize-totalbytes;
|
||||||
|
|
||||||
|
// 接收文件内容。
|
||||||
|
if (tcpclient.read(buffer,onread)==false) return false;
|
||||||
|
|
||||||
|
// 把接收到的内容写入文件。
|
||||||
|
ofile.write(buffer,onread);
|
||||||
|
|
||||||
|
// 计算已接收文件的总字节数,如果文件接收完,跳出循环。
|
||||||
|
totalbytes=totalbytes+onread;
|
||||||
|
|
||||||
|
if (totalbytes==filesize) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ofile.closeandrename();
|
||||||
|
|
||||||
|
// 重置文件的时间。
|
||||||
|
setmtime(filename,mtime);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
353
tcpputfiles.cpp
Normal file
353
tcpputfiles.cpp
Normal file
@ -0,0 +1,353 @@
|
|||||||
|
/*
|
||||||
|
* 程序名:tcpputfiles.cpp,采用tcp协议,实现文件上传的客户端。
|
||||||
|
*/
|
||||||
|
#include "_el.h"
|
||||||
|
using namespace eviwbh;
|
||||||
|
|
||||||
|
// 程序运行的参数结构体。
|
||||||
|
struct st_arg
|
||||||
|
{
|
||||||
|
int clienttype; // 客户端类型,1-上传文件;2-下载文件,本程序固定填1。
|
||||||
|
char ip[31]; // 服务端的IP地址。
|
||||||
|
int port; // 服务端的端口。
|
||||||
|
char clientpath[256]; // 本地文件存放的根目录。 /data /data/aaa /data/bbb
|
||||||
|
int ptype; // 文件上传成功后本地文件的处理方式:1-删除文件;2-移动到备份目录。
|
||||||
|
char clientpathbak[256]; // 文件成功上传后,本地文件备份的根目录,当ptype==2时有效。
|
||||||
|
bool andchild; // 是否上传clientpath目录下各级子目录的文件,true-是;false-否。
|
||||||
|
char matchname[256]; // 待上传文件名的匹配规则,如"*.TXT,*.XML"。
|
||||||
|
char srvpath[256]; // 服务端文件存放的根目录。/data1 /data1/aaa /data1/bbb
|
||||||
|
int timetvl; // 扫描本地目录文件的时间间隔(执行文件上传任务的时间间隔),单位:秒。
|
||||||
|
int timeout; // 进程心跳的超时时间。
|
||||||
|
char pname[51]; // 进程名,建议用"tcpputfiles_后缀"的方式。
|
||||||
|
} starg;
|
||||||
|
|
||||||
|
// 帮助文档。
|
||||||
|
void _help();
|
||||||
|
|
||||||
|
// 把xml解析到参数starg结构中。
|
||||||
|
bool _xmltoarg(const char *strxmlbuffer);
|
||||||
|
|
||||||
|
clogfile logfile; // 日志对象。
|
||||||
|
ctcpclient tcpclient; // 创建tcp通讯的客户端对象。
|
||||||
|
|
||||||
|
// 程序退出和信号2、15的处理函数。
|
||||||
|
void EXIT(int sig);
|
||||||
|
|
||||||
|
bool activetest(); // 心跳。
|
||||||
|
|
||||||
|
string strsendbuffer; // 发送报文的buffer。
|
||||||
|
string strrecvbuffer; // 接收报文的buffer。
|
||||||
|
|
||||||
|
// 向服务端发送登录报文,把客户端程序的参数传递给服务端。
|
||||||
|
bool login(const char *argv);
|
||||||
|
|
||||||
|
// 文件上传的主函数,执行一次文件上传的任务。
|
||||||
|
bool _tcpputfiles(bool &bcontinue);
|
||||||
|
|
||||||
|
// 处理传输文件的响应报文(删除或者转存本地的文件)。
|
||||||
|
bool ackmessage(const string &strrecvbuffer);
|
||||||
|
|
||||||
|
// 把文件的内容发送给对端。
|
||||||
|
bool sendfile(const string &filename,const int filesize);
|
||||||
|
|
||||||
|
cpactive pactive; // 进程心跳。
|
||||||
|
|
||||||
|
int main(int argc,char *argv[])
|
||||||
|
{
|
||||||
|
if (argc!=3) { _help(); return -1; }
|
||||||
|
|
||||||
|
// 关闭全部的信号和输入输出。
|
||||||
|
// 设置信号,在shell状态下可用 "kill + 进程号" 正常终止些进程。
|
||||||
|
// 但请不要用 "kill -9 +进程号" 强行终止。
|
||||||
|
// 在网络通讯程序中,一般不关IO,因为某些函数可能会往1和2中输出信息
|
||||||
|
// 如果关了1和2,那么1和2会被socket重用,向1和2输出的信息会发送到网络中。
|
||||||
|
//closeioandsignal(false);
|
||||||
|
signal(SIGINT,EXIT); signal(SIGTERM,EXIT);
|
||||||
|
|
||||||
|
// 打开日志文件。
|
||||||
|
if (logfile.open(argv[1])==false)
|
||||||
|
{
|
||||||
|
printf("打开日志文件失败(%s)。\n",argv[1]); return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析xml,得到程序运行的参数。
|
||||||
|
if (_xmltoarg(argv[2])==false) return -1;
|
||||||
|
|
||||||
|
pactive.addpinfo(starg.timeout,starg.pname); // 把进程的心跳信息写入共享内存。
|
||||||
|
|
||||||
|
// 向服务端发起连接请求。
|
||||||
|
if (tcpclient.connect(starg.ip,starg.port)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.connect(%s,%d) failed.\n",starg.ip,starg.port); EXIT(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 向服务端发送登录报文,把客户端程序的参数传递给服务端。
|
||||||
|
if (login(argv[2])==false) { logfile.write("login() failed.\n"); EXIT(-1); }
|
||||||
|
|
||||||
|
bool bcontinue=true; // 如果调用_tcpputfiles()发送了文件,bcontinue为true,否则为false。
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// 调用文件上传的主函数,执行一次文件上传的任务。
|
||||||
|
if (_tcpputfiles(bcontinue)==false) { logfile.write("_tcpputfiles() failed.\n"); EXIT(-1); }
|
||||||
|
|
||||||
|
// 如果刚才执行文件上传任务的时候上传了文件,在上传的过程中,可能有新的文件陆续已生成,
|
||||||
|
// 那么,为保证文件被尽快上传,进程不体眠。(只有在刚才执行文件上传任务的时候没有上传文件的情况下才休眠)
|
||||||
|
if (bcontinue==false)
|
||||||
|
{
|
||||||
|
sleep(starg.timetvl);
|
||||||
|
|
||||||
|
// 发送心跳报文。
|
||||||
|
if (activetest()==false) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
pactive.uptatime();
|
||||||
|
}
|
||||||
|
|
||||||
|
EXIT(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 心跳。
|
||||||
|
bool activetest()
|
||||||
|
{
|
||||||
|
strsendbuffer="<activetest>ok</activetest>";
|
||||||
|
// xxxxxxxxxx logfile.write("发送:%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false) return false; // 向服务端发送请求报文。
|
||||||
|
|
||||||
|
if (tcpclient.read(strrecvbuffer,10)==false) return false; // 接收服务端的回应报文。
|
||||||
|
// xxxxxxxxxx logfile.write("接收:%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
// 心跳机制的代码可简单化处理,只需要收到对端的回应就行了,不必判断回应的内容。
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EXIT(int sig)
|
||||||
|
{
|
||||||
|
logfile.write("程序退出,sig=%d\n\n",sig);
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 程序的帮助文档。
|
||||||
|
void _help()
|
||||||
|
{
|
||||||
|
printf("\n");
|
||||||
|
printf("Using:/project/tools/bin/tcpputfiles logfilename xmlbuffer\n\n");
|
||||||
|
|
||||||
|
printf("Sample:/project/tools/bin/procctl 20 /project/tools/bin/tcpputfiles /log/idc/tcpputfiles_surfdata.log "\
|
||||||
|
"\"<ip>192.168.150.128</ip><port>5005</port>"\
|
||||||
|
"<clientpath>/tmp/client</clientpath><ptype>1</ptype>"
|
||||||
|
"<srvpath>/tmp/server</srvpath>"\
|
||||||
|
"<andchild>true</andchild><matchname>*.xml,*.txt,*.csv</matchname><timetvl>10</timetvl>"\
|
||||||
|
"<timeout>50</timeout><pname>tcpputfiles_surfdata</pname>\"\n\n");
|
||||||
|
|
||||||
|
printf("本程序是数据中心的公共功能模块,采用tcp协议把文件上传给服务端。\n");
|
||||||
|
printf("logfilename 本程序运行的日志文件。\n");
|
||||||
|
printf("xmlbuffer 本程序运行的参数,如下:\n");
|
||||||
|
printf("ip 服务端的IP地址。\n");
|
||||||
|
printf("port 服务端的端口。\n");
|
||||||
|
printf("ptype 文件上传成功后的处理方式:1-删除文件;2-移动到备份目录。\n");
|
||||||
|
printf("clientpath 本地文件存放的根目录。\n");
|
||||||
|
printf("clientpathbak 文件成功上传后,本地文件备份的根目录,当ptype==2时有效。\n");
|
||||||
|
printf("andchild 是否上传clientpath目录下各级子目录的文件,true-是;false-否,缺省为false。\n");
|
||||||
|
printf("matchname 待上传文件名的匹配规则,如\"*.TXT,*.XML\"\n");
|
||||||
|
printf("srvpath 服务端文件存放的根目录。\n");
|
||||||
|
printf("timetvl 扫描本地目录文件的时间间隔,单位:秒,取值在1-30之间。\n");
|
||||||
|
printf("timeout 本程序的超时时间,单位:秒,视文件大小和网络带宽而定,建议设置50以上。\n");
|
||||||
|
printf("pname 进程名,尽可能采用易懂的、与其它进程不同的名称,方便故障排查。\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把xml解析到参数starg结构。
|
||||||
|
bool _xmltoarg(const char *strxmlbuffer)
|
||||||
|
{
|
||||||
|
memset(&starg,0,sizeof(struct st_arg));
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"ip",starg.ip);
|
||||||
|
if (strlen(starg.ip)==0) { logfile.write("ip is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"port",starg.port);
|
||||||
|
if ( starg.port==0) { logfile.write("port is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"ptype",starg.ptype);
|
||||||
|
if ((starg.ptype!=1)&&(starg.ptype!=2)) { logfile.write("ptype not in (1,2).\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"clientpath",starg.clientpath);
|
||||||
|
if (strlen(starg.clientpath)==0) { logfile.write("clientpath is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"clientpathbak",starg.clientpathbak);
|
||||||
|
if ((starg.ptype==2)&&(strlen(starg.clientpathbak)==0)) { logfile.write("clientpathbak is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"andchild",starg.andchild);
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"matchname",starg.matchname);
|
||||||
|
if (strlen(starg.matchname)==0) { logfile.write("matchname is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"srvpath",starg.srvpath);
|
||||||
|
if (strlen(starg.srvpath)==0) { logfile.write("srvpath is null.\n"); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"timetvl",starg.timetvl);
|
||||||
|
if (starg.timetvl==0) { logfile.write("timetvl is null.\n"); return false; }
|
||||||
|
|
||||||
|
// 扫描本地目录文件的时间间隔(执行上传任务的时间间隔),单位:秒。
|
||||||
|
// starg.timetvl没有必要超过30秒。
|
||||||
|
if (starg.timetvl>30) starg.timetvl=30;
|
||||||
|
|
||||||
|
// 进程心跳的超时时间,一定要大于starg.timetvl。
|
||||||
|
getxmlbuffer(strxmlbuffer,"timeout",starg.timeout);
|
||||||
|
if (starg.timeout==0) { logfile.write("timeout is null.\n"); return false; }
|
||||||
|
if (starg.timeout<=starg.timetvl) { logfile.write("starg.timeout(%d) <= starg.timetvl(%d).\n",starg.timeout,starg.timetvl); return false; }
|
||||||
|
|
||||||
|
getxmlbuffer(strxmlbuffer,"pname",starg.pname,50);
|
||||||
|
//if (strlen(starg.pname)==0) { logfile.write("pname is null.\n"); return false; }
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 向服务端发送登录报文,把客户端程序的参数传递给服务端。
|
||||||
|
bool login(const char *argv)
|
||||||
|
{
|
||||||
|
sformat(strsendbuffer,"%s<clienttype>1</clienttype>",argv);
|
||||||
|
// xxxxxxxxxx logfile.write("发送:%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false) return false; // 向服务端发送请求报文。
|
||||||
|
|
||||||
|
if (tcpclient.read(strrecvbuffer,10)==false) return false; // 接收服务端的回应报文。
|
||||||
|
// xxxxxxxxxx logfile.write("接收:%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
logfile.write("登录(%s:%d)成功。\n",starg.ip,starg.port);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件上传的主函数,执行一次文件上传的任务。
|
||||||
|
bool _tcpputfiles(bool &bcontinue)
|
||||||
|
{
|
||||||
|
bcontinue=false;
|
||||||
|
|
||||||
|
cdir dir;
|
||||||
|
|
||||||
|
// 打开starg.clientpath目录。
|
||||||
|
if (dir.opendir(starg.clientpath,starg.matchname,10000,starg.andchild)==false)
|
||||||
|
{
|
||||||
|
logfile.write("dir.opendir(%s) 失败。\n",starg.clientpath); return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int delayed=0; // 未收到对端确认报文的文件数量,发送了一个文件就加1,接收到了一个回应就减1。
|
||||||
|
|
||||||
|
// 遍历目录中的每个文件
|
||||||
|
while (dir.readdir())
|
||||||
|
{
|
||||||
|
bcontinue=true;
|
||||||
|
|
||||||
|
// 把文件名、修改时间、文件大小组成报文,发送给对端。
|
||||||
|
sformat(strsendbuffer,"<filename>%s</filename><mtime>%s</mtime><size>%d</size>",
|
||||||
|
dir.m_ffilename.c_str(),dir.m_mtime.c_str(),dir.m_filesize);
|
||||||
|
|
||||||
|
// xxxxxxxxxxxxxxx logfile.write("strsendbuffer=%s\n",strsendbuffer.c_str());
|
||||||
|
if (tcpclient.write(strsendbuffer)==false)
|
||||||
|
{
|
||||||
|
logfile.write("tcpclient.write() failed.\n"); return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送文件内容。
|
||||||
|
logfile.write("send %s(%d) ...",dir.m_ffilename.c_str(),dir.m_filesize);
|
||||||
|
if (sendfile(dir.m_ffilename,dir.m_filesize)==true)
|
||||||
|
{
|
||||||
|
logfile << "ok.\n"; delayed++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logfile << "failed.\n"; tcpclient.close(); return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pactive.uptatime();
|
||||||
|
|
||||||
|
// 接收服务端的确认报文。
|
||||||
|
while (delayed>0)
|
||||||
|
{
|
||||||
|
if (tcpclient.read(strrecvbuffer,-1)==false) break;
|
||||||
|
// xxxxxxxxxxxxxxx logfile.write("strrecvbuffer=%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
// 处理服务端的确认报文(删除本地文件或把本地文件移动到备份目录)。
|
||||||
|
ackmessage(strrecvbuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 继续接收对端的确认报文。
|
||||||
|
while (delayed>0)
|
||||||
|
{
|
||||||
|
if (tcpclient.read(strrecvbuffer,10)==false) break;
|
||||||
|
// xxxxxxxxxxxxxxx logfile.write("strrecvbuffer=%s\n",strrecvbuffer.c_str());
|
||||||
|
|
||||||
|
// 处理传输文件的响应报文(删除或者转存本地的文件)。
|
||||||
|
delayed--;
|
||||||
|
ackmessage(strrecvbuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把文件的内容发送给对端。
|
||||||
|
bool sendfile(const string &filename,const int filesize)
|
||||||
|
{
|
||||||
|
int onread=0; // 每次打算从文件中读取的字节数。
|
||||||
|
char buffer[1000]; // 存放读取数据的buffer,buffer的大小可参考硬盘一次读取数据量(4K为宜)。
|
||||||
|
int totalbytes=0; // 从文件中已读取的字节总数。
|
||||||
|
cifile ifile; // 读取文件的对象。
|
||||||
|
|
||||||
|
// 必须以二进制的方式操作文件。
|
||||||
|
if (ifile.open(filename,ios::in|ios::binary)==false) return false;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
|
||||||
|
// 计算本次应该读取的字节数,如果剩余的数据超过1000字节,就读1000字节。
|
||||||
|
if (filesize-totalbytes>1000) onread=1000;
|
||||||
|
else onread=filesize-totalbytes;
|
||||||
|
|
||||||
|
// 从文件中读取数据。
|
||||||
|
ifile.read(buffer,onread);
|
||||||
|
|
||||||
|
// 把读取到的数据发送给对端。
|
||||||
|
if (tcpclient.write(buffer,onread)==false) { return false; }
|
||||||
|
|
||||||
|
// 计算文件已读取的字节总数,如果文件已读完,跳出循环。
|
||||||
|
totalbytes=totalbytes+onread;
|
||||||
|
|
||||||
|
if (totalbytes==filesize) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理传输文件的响应报文(删除或者转存本地的文件)。
|
||||||
|
bool ackmessage(const string &strrecvbuffer)
|
||||||
|
{
|
||||||
|
// <filename>/tmp/client/2.xml</filename><result>ok</result>
|
||||||
|
string filename; // 本地文件名。
|
||||||
|
string result; // 对端接收文件的结果。
|
||||||
|
getxmlbuffer(strrecvbuffer,"filename",filename);
|
||||||
|
getxmlbuffer(strrecvbuffer,"result",result);
|
||||||
|
|
||||||
|
// 如果服务端接收文件不成功,直接返回(下次执行文件传输任务时将会重传)。
|
||||||
|
if (result!="ok") return true;
|
||||||
|
|
||||||
|
// 如果starg.ptype==1,删除文件。
|
||||||
|
if (starg.ptype==1)
|
||||||
|
{
|
||||||
|
if (remove(filename.c_str())!=0) { logfile.write("remove(%s) failed.\n",filename.c_str()); return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果starg.ptype==2,移动到备份目录。
|
||||||
|
if (starg.ptype==2)
|
||||||
|
{
|
||||||
|
// 生成转存后的备份目录文件名。 例如:/tmp/client/2.xml /tmp/clientbak/2.xml
|
||||||
|
string bakfilename=filename;
|
||||||
|
replacestr(bakfilename,starg.clientpath,starg.clientpathbak,false); // 注意,第三个参数一定要填false。
|
||||||
|
if (renamefile(filename,bakfilename)==false)
|
||||||
|
{ logfile.write("renamefile(%s,%s) failed.\n",filename.c_str(),bakfilename.c_str()); return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user