archive

来源:互联网 发布:电子软件采购合同 编辑:程序博客网 时间:2024/05/02 05:01


//int 长度
#define INT_LEN (sizeof(int))
//unsigned int 长度
#define UINT_LEN (sizeof(unsigned int))
//long 长度
#define LONG_LEN (sizeof(long))
//unsigned long 长度
#define ULONG_LEN (sizeof(unsigned long))
//unsigned short 长度
#define USHORT_LEN (sizeof(unsigned short))
//short 长度
#define SHORT_LEN (sizeof(short))
//char 长度
#define CHAR_LEN (sizeof(char))
//unsigned char 长度
#define UCHAR_LEN (sizeof(unsigned char))
//float 长度
#define FLOAT_LEN (sizeof(float))
//double 长度
#define DOUBLE_LEN (sizeof(double))




//最大的buf长度
#define MAX_STREAM_BUFFER_SIZE (1024*8)


//包头长度
#define PkgHeadLength (7*ULONG_LEN + 4*LONG_LEN + 3*UINT_LEN + 32)
//向一个字节缓冲区中增加内容,返回增加后缓冲区内容的尾部指针
char* AddToBuffer(char* pBuffer,const char* dest,unsigned int size);
//从缓冲区获取一段内容,并移动指针
char* GetFromBuffer(char* pBuffer,char* dest,unsigned int size);




//将缓冲区内容转换成基本类型
template<typename T>
char* GetBaseTypeFromBuffer(char* pBuffer,T& value)
{
memcpy(&value,pBuffer,sizeof(value));
return (pBuffer + sizeof(value));
}


//将基本类型转成字节
template<typename T>
char* AddBaseTypeToBuffer(char* pBuffer,T& value)
{
memcpy(pBuffer,&value,sizeof(value));
return (pBuffer + sizeof(value));
}


//一个可以通用的通信包(头)
struct PkgHeadCtl
{
unsigned long PkgLength;//包的长度
unsigned int Version;//版本
unsigned int PID;//进程号
unsigned int MorePkg;//是否有后续包
unsigned long StartNo;//开始序号
unsigned long EndNo;//结束序号
unsigned long RequestID;//请求id
unsigned long ResponeID;//回应id
unsigned long Sequence;//请求编号,包的额序号
long ErrorCode;//错误码
long reserved1;//保留字段1
long reserved2;//保留字段2
long reserved3;//保留字段3
char IP[32];//IP
unsigned long Port;//端口


PkgHeadCtl()
{
PkgLength = PkgHeadLength;
Version = 1;
PID = 0;
MorePkg = 0;
StartNo = 0;
EndNo = 0;
ErrorCode = 0;
RequestID = 0;
ResponeID = 0;
Sequence = 0;
reserved1 = 0;
reserved2 = 0;
reserved3 = 0;
memset(IP,0,32);
Port = 0;
}


void Save(MAchive& ar);


void Load(MAchive& ar);


};


//向一个字节缓冲区中增加内容,返回增加后缓冲区内容的尾部指针
char* AddToBuffer(char* pBuffer,const char* dest,unsigned int size)
{
memcpy(pBuffer,dest,size);
return (pBuffer + size);
}


//从缓冲区获取一段内容,并移动指针
char* GetFromBuffer(char* pBuffer,char* dest,unsigned int size)
{
memcpy(dest,pBuffer,size);
return (pBuffer + size);
}


void PkgHeadCtl::Save(MAchive& ar)
{
ar << this->EndNo;
ar << this->ErrorCode;
ar.Save_pointer(this->IP,32);
ar << this->MorePkg;
ar << this->PID;
ar << this->PkgLength;
ar << this->Port;
ar << this->RequestID;
ar << this->reserved1;
ar << this->reserved2;
ar << this->reserved3;
ar << this->ResponeID;
ar << this->Sequence;
ar << this->StartNo;
ar << this->Version; 
}


void PkgHeadCtl::Load(MAchive& ar)
{
ar >> this->EndNo;
ar >> this->ErrorCode;
ar.Load_pointer(this->IP);
ar >> this->MorePkg;
ar >> this->PID;
ar >> this->PkgLength;
ar >> this->Port;
ar >> this->RequestID;
ar >> this->reserved1;
ar >> this->reserved2;
ar >> this->reserved3;
ar >> this->ResponeID;
ar >> this->Sequence;
ar >> this->StartNo;
ar >> this->Version;
}



class FileBase:public MObject
{
public:
virtual void Close()
{


}


virtual M_ULONG Read(void* pBuf,M_ULONG uLength) 
{
return 0;
}

virtual void Write(const char* pBuf,M_ULONG uLength)
{


}


virtual void Flush()
{


}


virtual bool IsOpen()
{
return false;
}
};


class SocketFile:public FileBase
{
public:
SocketFile(char* pBuffer)
{
m_pBuffer = NULL;
m_pPos = NULL;


if (pBuffer == NULL)
{
return;
}
m_pBuffer = pBuffer;
m_pPos = m_pBuffer;
}


~SocketFile()
{
}


void Close()
{
m_pBuffer = NULL;
m_pPos = NULL;
}


void Write(const char* pBuf,M_ULONG uLength)
{
if (m_pBuffer == NULL)
{
return;
}


m_pPos = AddToBuffer(m_pPos,pBuf,uLength);
}


M_ULONG Read(void* pBuf,M_ULONG uLength)
{
if (m_pBuffer == NULL)
{
return 0;
}


m_pPos = GetFromBuffer(m_pPos,(char*)pBuf,uLength);
return uLength;
}

private:
char*  m_pBuffer;
char* m_pPos;
};


class XO:public MObject
{
public:
char sz[64];
int x;
double y;
long z;


void Save(MAchive& ar)
{
ar.Save_pointer(sz,64);
ar << x;
ar << y;
ar << z;
}


void Load(MAchive& ar)
{
ar.Load_pointer(sz);
ar >> x;
ar >> y;
ar >> z;
}
};


class YO:public MObject
{
public:
char sz[64];
int x;
double y;


void Save(MAchive& ar)
{
ar.Save_pointer(sz,64);
ar << x;
ar << y;
}


void Load(MAchive& ar)
{
ar.Load_pointer(sz);
ar >> x;
ar >> y;
}
};


char szBuf[1024*8];


PkgHeadCtl srcHead;
PkgHeadCtl destHead;


XO srcXo,destXo;
YO srcYo,destYo;


memcpy(srcXo.sz,"wo kao!!!",64);
memcpy(srcYo.sz,"wo cao!!!",64);


memcpy(srcHead.IP,"10.132.10.11",32);
srcHead.PID = 1000;


SocketFile file(szBuf);
MAchive ar(file);


srcHead.Save(ar);


srcXo.Save(ar);


srcYo.Save(ar);


file.Close();





SocketFile destfile(szBuf);
MAchive destAr(destfile);


destHead.Load(destAr);


destXo.Load(destAr);


destYo.Load(destAr);


char* buf;


buf = 0;

原创粉丝点击