📄 tmlsession.cpp
字号:
// TmlSession.cpp: implementation of the CTmlSession class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "pimserver.h"
#include "TmlSession.h"
#include "b64.h"
#include "mydb.h"
#include "builder_wrapper.h"
#include "ccharcode.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define MAX_B64BUFFER 100000
#define MAX_CLIENTBUFFER 5000
extern CMydb workdb;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int _get_new_anchor(char*_buf);
int _insert_pimresource(CTmlSession*pSess);
int _update_pimmagicmmsdownfile(CTmlSession*pSess);
char *_database_get_filepath(char *_locuri,char*_userid);
int _update_pimdownfile(CTmlSession*pSess);
int _insert_pimdownfile(CTmlSession*pSess);
int _get_filelength(char*_filepath);
int _load_pimresource(CTmlSession*pSess,char*locuri);
int _insert_pimcontact(CTmlSession*pSess,TmlCmdContact_s*pCont);
int _update_pimcontact(CTmlSession*pSess,TmlCmdContact_s*pCont);
int _database_get_txlnumberofdownload(CTmlSession*_pSess);
int _update_finishpimcontact(CTmlSession*pSess);
void Gb2312ToUnicode(unsigned short* pOut,char *gbBuffer);
CTmlSession::CTmlSession()
{
m_pEnd = m_pHead =NULL;
m_sUser = m_sPwd = NULL;
m_eCredState = TmlCred_NoOffer;
m_eSessState = TmlSess_NoCredit;
m_nNextItem = m_nItem = m_nAll = 0;
m_fpFile =0;
m_nSessFlag=0;
m_nActiveTime = 0;
m_nRevData =0;
m_nStoreFlag = 0;
m_sCurLocURI = NULL;
m_nRevOK = 0;
m_sCurMagicLocURI = NULL;
m_sMessage =NULL;
m_sAction=NULL;
m_sArea=NULL;
m_sStyle=NULL;
m_sType=NULL;
m_sSno=NULL;
m_nSendOK=0;
m_nSendData=0;
m_nFileLength = 0;
m_nIsSend = 0;
m_sSyncID = NULL;
m_nTxlFlag = 0;
m_nCount = 0;
}
int CTmlSession::ElapsedIdleSeconds()
{
int nSec = 0;
SYSTEMTIME systemTime;
GetLocalTime(&systemTime);
time((time_t *)(&nSec));
return nSec-m_nActiveTime;
}
CTmlSession::~CTmlSession()
{
}
void CTmlSession::HandleStartMessage(InstanceID_t id,TmlCmdTHdr_s*pTHdr)
{
TmlHdr_t hdr;
tmlLibMemset(&hdr,0,(MemSize_t)sizeof(TmlHdr_t));
hdr.elementType = TML_PE_THDR;
hdr.version = tmlString2Pcdata("1.0");
if(m_eSessState==TmlSess_NoOffer)
hdr.sessionID = tmlString2Pcdata("");
else
hdr.sessionID = tmlString2Pcdata(m_sSessID);
tmlStartMessageExt(id, &hdr, TML_VERS_1_1);
if(pTHdr->credit!=NULL)
{
TmlTStatus_t tstatus;
tmlLibMemset(&tstatus,0,(MemSize_t)sizeof(TmlTStatus_t));
tstatus.elementType = TML_PE_TSTATUS;
tstatus.cmdidRef = tmlString2Pcdata("0");
if(m_eSessState==TmlSess_NoOffer)
tstatus.data = tmlString2Pcdata("500");
else
tstatus.data = tmlString2Pcdata("200");
ClearSyncID();
tmlTStatus(id,&tstatus);
}
}
int GetRandKey(int n)
{
srand( (unsigned int)time(NULL));
char s[100]="",t[100]="";
for(int i=0;i<n;i++)
{
sprintf(t,"%d",rand()%10);
strcat(s,t);
}
return atoi(s);
}
void CTmlSession::CreateSyncID()
{
XPTDEBUG(("CreateSyncID()\n"));
if(m_sSyncID==NULL)
{
char syncid [1024];
sprintf(syncid,"syncid%d",GetRandKey(6));
XPTDEBUG(("CreateSyncID(%s)\n",syncid));
m_sSyncID =(char*) tmlLibMalloc(tmlLibStrlen(syncid));
tmlLibStrcpy(m_sSyncID,syncid);
}
}
void CTmlSession::ClearSyncID()
{
if(m_sSyncID!=NULL)
{
m_sSyncID=NULL;
}
}
void CTmlSession::HandleResource(InstanceID_t id,TmlCmdResource_s*pRes)
{
int _already_revok = 0;//1处理文件是已经完成的
int _return_code = 200;//默认成功
int _search_locuri_result = 0; //0没有记录在pimfile 1已经有但是未完成,2已完成
int _item = atoi(pRes->item);
int _all = atoi(pRes->all);
XPTDEBUG(("HandleResource(item%d,all%d)\n",_item,_all));
//当前不为空并且当前的文件不等于要处理的文件
if(pRes->locuri==NULL||tmlLibStrlen(pRes->locuri)==0)
{
//tmlLibM
char newlocuri[100];
_get_new_anchor(newlocuri);
int _length = tmlLibStrlen(newlocuri);
pRes->locuri =(char*) tmlLibMalloc(_length);
tmlLibStrcpy(pRes->locuri,newlocuri);
}
if(m_sCurLocURI!=NULL)
{
int _cmp_locuri = tmlLibStrcmp(m_sCurLocURI,pRes->locuri);
if(_cmp_locuri!=0)
{
//搜索当前的locuri,并且保存当前locuri信息
_search_locuri_result = _search_locuri_pimupfile(this,m_sCurLocURI);
if(_search_locuri_result==1)
{
//搜索到locuri,并且未完成
//保存传送信息 不用保存resource信息,因为resource信息每次上传是都有
_update_pimupfile(this);
}
else if(_search_locuri_result==2)
{
//搜索到locuri,并且接收完成revok
//不太可能出现 ,因为接收完成时,当前locuri等信息都是复位的
//_already_revok =1;
}
else
{
//没有搜索到locuri,插入表格
//不太可能出现,因为上次传输的,应该都已经保存到数据库中的
_insert_pimupfile(this);
}
//如果当前和待处理文件不同,则复位当前文件状态
if(m_fpFile!=NULL)
{
XPTDEBUG(("关闭文件\n"));
fclose(m_fpFile);
m_fpFile = NULL;
}
m_sCurLocURI =NULL;
m_sRType=NULL;
m_sRName=NULL;
}
}
if(m_sCurLocURI ==NULL)//第一次,或者变更文件
{
_search_locuri_result = _search_locuri_pimupfile(this,pRes->locuri);
if(_search_locuri_result==1)
{
//搜索到并且未完成传输,恢复传输信息
_load_pimupfile(this,pRes->locuri);
//在session中保存resource信息
m_sRType = (String_t )tmlLibMalloc(tmlLibStrlen(pRes->rtype));
tmlLibMemset(m_sRType,0,tmlLibStrlen(pRes->rtype));
tmlLibStrcpy(m_sRType,pRes->rtype);
m_sRName = (String_t )tmlLibMalloc(tmlLibStrlen(pRes->rname));
tmlLibMemset(m_sRName,0,tmlLibStrlen(pRes->rname));
tmlLibStrcpy(m_sRName,pRes->rname);
}
else if(_search_locuri_result==2)
{
//搜索到并且先前完成传输
_already_revok =1;
_return_code = 202;
}
else
{
//locuri的第一个包(item必须等于1,若不等于1,发送等于1的nextitem)
if(_item!=1)
{
_return_code = 201;//第一个item不等于1
}
else
{
//如果未保存到pimfile中,基本说明是第二,三...文件第一个包
char * time = _get_nowtime();
char resfile[512];
sprintf(resfile,"%s.%s",time,pRes->rtype);
m_sPath = (String_t )tmlLibMalloc(tmlLibStrlen(resfile));
tmlLibMemset(m_sPath,0,tmlLibStrlen(resfile));
tmlLibStrcpy(m_sPath,resfile);
m_sCurLocURI = (String_t )tmlLibMalloc(tmlLibStrlen(pRes->locuri));
tmlLibMemset(m_sCurLocURI,0,tmlLibStrlen(pRes->locuri));
tmlLibStrcpy(m_sCurLocURI,pRes->locuri);
m_sRType = (String_t )tmlLibMalloc(tmlLibStrlen(pRes->rtype));
tmlLibMemset(m_sRType,0,tmlLibStrlen(pRes->rtype));
tmlLibStrcpy(m_sRType,pRes->rtype);
m_sRName = (String_t )tmlLibMalloc(tmlLibStrlen(pRes->rname));
tmlLibMemset(m_sRName,0,tmlLibStrlen(pRes->rname));
tmlLibStrcpy(m_sRName,pRes->rname);
m_nNextItem = 1;//
m_nRevData = 0;
m_nRevOK =0;
_insert_pimupfile(this);
}
}
}
//续传文件
//到此为止,pSess->m_sCurLocURI!=NULL&&pSess->m_sCurLocURI==pRes->locuri
if(_item!=m_nNextItem)
{
_return_code=201;
}
if(_return_code==200)
{
BYTE _b64_buffer[MAX_B64BUFFER];
TmlRStatus_t _rstatus;
size_t _length = tmlLibStrlen(pRes->content);
if(m_fpFile==NULL)
{
XPTDEBUG(("打开文件\n"));
m_fpFile = fopen(m_sPath,"ab");
}
int _decode_length = base64Decode (_b64_buffer, MAX_B64BUFFER, (BYTE*)pRes->content,&_length);
_b64_buffer[_decode_length]='\0';
XPTDEBUG(("b64(%d,%d)\n",tmlLibStrlen(pRes->content),_decode_length));
//fwrite(pRes->data,_length,1,m_fpFile);
fwrite(_b64_buffer,_decode_length,1,m_fpFile);
fflush(m_fpFile);
//m_nRevData += _length;
m_nRevData += _decode_length;
if(_item==_all)
{
m_nRevOK=1;
}
//每次写文件时候必须打开文件,写完后必须关闭文件
//如果不这样,异常关闭后,文件大小不是我们所愿的大小
fclose(m_fpFile);
XPTDEBUG(("关闭文件\n"));
m_fpFile = 0;
tmlLibMemset(&_rstatus,0,(MemSize_t)sizeof(TmlRStatus_t));
_rstatus.elementType = TML_PE_RSTATUS;
_rstatus.locuri = tmlString2Pcdata(pRes->locuri);
_rstatus.cmdidRef = tmlString2Pcdata(pRes->cmdid);
_rstatus.itemref = tmlString2Pcdata(pRes->item);
m_nNextItem = atoi(pRes->item)+1;
char _nextitem [10];
sprintf(_nextitem,"%d",m_nNextItem );
_rstatus.nextitem = tmlString2Pcdata(_nextitem);
_rstatus.data = tmlString2Pcdata("200");
tmlRStatus(id,&_rstatus);
_update_pimupfile(this);
if(_item==_all)
{
_insert_pimresource(this);
m_sCurLocURI = NULL;
m_sRType=NULL;
m_sRName=NULL;
m_nRevData = 0;
m_nRevOK = 0;
}
}
else
{
TmlRStatus_t _rstatus;
tmlLibMemset(&_rstatus,0,(MemSize_t)sizeof(TmlRStatus_t));
_rstatus.elementType = TML_PE_RSTATUS;
_rstatus.locuri = tmlString2Pcdata(pRes->locuri);
_rstatus.cmdidRef = tmlString2Pcdata(pRes->cmdid);
_rstatus.itemref = tmlString2Pcdata(pRes->item);
char _nextitem [10];
sprintf(_nextitem,"%d",m_nNextItem );
_rstatus.nextitem = tmlString2Pcdata(_nextitem);
if(_return_code==201)
_rstatus.data = tmlString2Pcdata("201");
else
_rstatus.data = tmlString2Pcdata("202");
tmlRStatus(id,&_rstatus);
}
}
void CTmlSession::HandleSendResource(InstanceID_t id,TmlCmdResource_s*pRes)
{
XPTDEBUG(("HandleSendResource()\n"));
int _return_code = 200;//默认成功
char* szFile=NULL;
if(pRes->locuri==NULL||tmlLibStrlen(pRes->locuri)==0)
{
_return_code=500;
}
if(_return_code!=500)
{
if(_load_pimresource(this,pRes->locuri)==0)
_return_code=500;
}
if(_return_code!=500)
{
m_nFileLength = _get_filelength(m_sPath);
if(m_nFileLength==0)
_return_code= 500;
}
if(_return_code!=500)
{
m_nAll = m_nFileLength / MAX_CLIENTBUFFER+1;
if(pRes->item==NULL||tmlLibStrlen(pRes->item)==0)
m_nItem = 1;
else
m_nItem = atoi(pRes->item);
}
if(m_nItem==0)
m_nItem=1;
if(_return_code!=500)
{
if(m_nItem>m_nAll)
_return_code = 500;
}
char readBuf[MAX_CLIENTBUFFER+1];
char b64Buf[MAX_B64BUFFER];
if(_return_code!=500)
{
int _length = tmlLibStrlen(pRes->locuri);
m_sCurLocURI =(char*) tmlLibMalloc(_length);
tmlLibStrcpy(m_sCurLocURI,pRes->locuri);
_insert_pimdownfile(this);
unsigned int cbOffset = 0; // <<<<< ????
char abSave [4];
abSave [0] = abSave [1] = abSave [2] = abSave [3] = '\0';
m_fpFile = fopen(m_sPath,"rb");
size_t _length_buffer = MAX_CLIENTBUFFER;
if(m_nFileLength> MAX_CLIENTBUFFER)
{
m_nSendData = MAX_CLIENTBUFFER;
fseek (m_fpFile , (m_nItem-1)*MAX_CLIENTBUFFER , SEEK_SET );
fread(readBuf,MAX_CLIENTBUFFER,1,m_fpFile);
fclose(m_fpFile);
size_t t = base64Encode((BYTE*)b64Buf,MAX_B64BUFFER,(BYTE*)readBuf,&_length_buffer,&cbOffset,1,(unsigned char *)abSave);
b64Buf[t]=0;
}
else
{
m_nSendData = m_nFileLength;
fread(readBuf,m_nFileLength,1,m_fpFile);
fclose(m_fpFile);
size_t t = base64Encode((BYTE*)b64Buf,MAX_B64BUFFER,(BYTE*)readBuf,&m_nFileLength,&cbOffset,1,(unsigned char *)abSave);
b64Buf[t]=0;
}
}
TmlResources_t _resources;
tmlLibMemset(&_resources,0,(MemSize_t)sizeof(TmlResources_t));
_resources.elementType = TML_PE_RESOURCES_START;
_resources.count = tmlString2Pcdata("1");
tmlResources(id,&_resources);
TmlResource_t _resource;
tmlLibMemset(&_resource,0,(MemSize_t)sizeof(TmlResource_t));
if(_return_code!=500)
{
_resource.elementType = TML_PE_RESOURCE;
_resource.cmdID = tmlString2Pcdata("1");
_resource.cmd = tmlString2Pcdata("send");
_resource.locuri = tmlString2Pcdata(m_sCurLocURI);
_resource.rtype = tmlString2Pcdata(m_sRType);
_resource.rname = tmlString2Pcdata(m_sRName);
_resource.data = tmlString2Pcdata("200");
char vert[10];
sprintf(vert , "%d",m_nItem);
_resource.item = tmlString2Pcdata(vert);
sprintf(vert , "%d",m_nAll);
_resource.all = tmlString2Pcdata(vert);
_resource.content = tmlString2Pcdata(b64Buf);
tmlResource(id,&_resource);
}
else
{
_resource.elementType = TML_PE_RESOURCE;
_resource.cmdID = tmlString2Pcdata("1");
_resource.cmd = tmlString2Pcdata("send");
_resource.locuri = tmlString2Pcdata("");
_resource.rtype = tmlString2Pcdata("");
_resource.rname = tmlString2Pcdata("");
_resource.data = tmlString2Pcdata("500");
char vert[10];
sprintf(vert , "%d",1);
_resource.item = tmlString2Pcdata(vert);
sprintf(vert , "%d",1);
_resource.all = tmlString2Pcdata(vert);
_resource.content = tmlString2Pcdata("");
tmlResource(id,&_resource);
}
tmlEndResources(id);
if(m_nItem==m_nAll)
{
//只有一个包
m_nSendOK =1;
_update_pimdownfile(this);
m_nSendData = 0;
m_sPath = NULL;
m_sCurLocURI=NULL;
m_sMessage =NULL;
m_sAction=NULL;
m_sSno =NULL;
m_sType =NULL;
}
else
{
_update_pimdownfile(this);
}
}
void CTmlSession::HandleMagicmmss(InstanceID_t id,TmlCmdMagicmmss_s*pMagicmmss)
{
XPTDEBUG(("HandleMagicmmss()\n"));
int _count = atoi(pMagicmmss->count);
}
void CTmlSession::HandleFinal(InstanceID_t id,TmlCmdFinal_s*pFinal)
{
XPTDEBUG(("HandleFinal()\n"));
int _final = atoi(pFinal->final);
if(_final==1)
{
//_update_finishpimcontact(this);
}
}
void CTmlSession::HandleCStatuss(InstanceID_t id,TmlCmdCStatuss_s*pCStatuss)
{
XPTDEBUG(("HandleCStatuss()\n"));
int _count = atoi(pCStatuss->count);
}
void CTmlSession::HandleMagicmms(InstanceID_t id,TmlCmdMagicmms_s*pRes)
{
XPTDEBUG(("HandleMagicmms()\n"));
char _sHost[1024];
char _smmsServer[1024];
char _sdocName[1024];
char _swplName[10000];
int _nokHttp = 0;
size_t bytesSent = 0;
size_t bytesReceived = 0;
size_t pkgbyteReceived = 0;
XptServiceID_t serviceID;
XptCommunicationID_t _httpconn = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -