📄 func.cpp
字号:
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->nMaxInterval=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->isDebug=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->iSelectInterval=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->isMO=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->iChannelNum=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->WINLEN=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%s",arg->szLogDir);
fclose(pf);
arg->hGW=CreateEvent(NULL,TRUE,FALSE,NULL);
if(arg->hGW==NULL)return 0;
arg->isActive=0;
arg->dwRecv=arg->dwSend=0;
arg->dwSequence=0;
arg->dwSequence2=0;
#if ONESECTION1
InitializeCriticalSection(&(arg->syc1));
#endif
#if ONESECTION2
InitializeCriticalSection(&(arg->syc2));
#endif
#if ONESECTION3
InitializeCriticalSection(&(arg->syc3));
#endif
return 1;
}
DWORD WINAPI MTThread(LPVOID pp)
{
CMSG tm;
char *p=0;
BYTE msg_format;
WORD wRead,i,j,k;
DWORD dwRet;
int index,check;
int iFirst,iCurrent;
_bstr_t strRecord;
char strSQL[512];
char szLastMobile[21];
_RecordsetPtr rsGSM=NULL;
ZeroMemory(&tm,sizeof(CMSG));
while(arg.isMO==1)SleepEx(1000,1);
try
{
::CoInitialize(NULL);
TESTHR(rsGSM.CreateInstance(__uuidof(Recordset)));
}//try
catch (_com_error &e)
{
AddLog("Sender SQL error.\n");
AddLog("Description = '%s'\n", (char*) e.Description());
return 0;
}
AddLog("begin to send mt.\n");
while(1)
{
try
{
sprintf(strSQL,"EXEC SP_GET_MT %d",arg.iChannelNum);
rsGSM->Open(strSQL,_variant_t((IDispatch *)arg.pCnnGSM),
adOpenStatic,adLockPessimistic,adCmdText);
if(!rsGSM->EndOfFile&&!rsGSM->BOF)
{
if(rsGSM->GetRecordCount())
strRecord=rsGSM->GetString(adClipString,
rsGSM->RecordCount,"\1","\2","\0");
rsGSM->Close();
}
else
{
rsGSM->Close();
SleepEx(arg.iSelectInterval,1);
arg.dwSend++;
continue;
}
check=index=i=j=k=0;
iFirst=iCurrent=0;
p=(char*)strRecord;
strcpy(szLastMobile,"0");
while(p)//the next record is avairable
{
if(arg.dwStopFlag)return 0;
index++;
tm.nStation=atoi(p);p=strchr(p,1)+1;
tm.dwSequence=atoi(p);p=strchr(p,1)+1;
if(iFirst==0)iFirst=tm.dwSequence;
strncpy(tm.szServiceID,p,strchr(p,1)-p);
if(tm.szServiceID[0])tm.szServiceID[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
strncpy(tm.FeeType,p,strchr(p,1)-p);
if(tm.FeeType[0])tm.FeeType[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
strncpy(tm.FeeCode,p,strchr(p,1)-p);
if(tm.FeeCode[0])tm.FeeCode[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
strncpy(tm.SrcTermId,p,strchr(p,1)-p);
if(tm.SrcTermId[0])tm.SrcTermId[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
strncpy(tm.DstTermId,p,strchr(p,1)-p);
if(tm.DstTermId[0])tm.DstTermId[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
msg_format=atoi(p);p=strchr(p,1)+1;
tm.bPID=atoi(p);p=strchr(p,1)+1;
tm.bUDHI=atoi(p);p=strchr(p,1)+1;
tm.bPK_TOTAL=atoi(p);p=strchr(p,1)+1;
tm.bPK_NUM=atoi(p);p=strchr(p,1)+1;
tm.bReason=atoi(p);p=strchr(p,1)+1;
tm.isReply=atoi(p);p=strchr(p,1)+1;
tm.bFeeUserType=atoi(p);p=strchr(p,1)+1;
memcpy(tm.FeeUser,p,strchr(p,1)-p);
tm.FeeUser[strchr(p,1)-p]=0;p=strchr(p,1)+1;
memcpy(tm.sLinkID,p,strchr(p,1)-p);
tm.sLinkID[strchr(p,1)-p]=0;p=strchr(p,1)+1;
if(strchr(p,2))k=2;else k=0;
memcpy(tm.szContent,p,strchr(p,k)-p);
tm.szContent[strchr(p,k)-p]=0;
p=strchr(p,k);
if(p)
{
p+=1;
if(strlen(p)==0)p=NULL;
}
if(iCurrent==(int)tm.dwSequence)continue;else iCurrent=tm.dwSequence;
//if currrent record isn't the end of the rows
//p should be the head of the next record, otherwise p=NULL;
if(msg_format==0||msg_format==15)
{
wRead=MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,tm.szContent,strlen(tm.szContent),NULL,0);
if((wRead!=strlen(tm.szContent))&&wRead>70)
{
#if 0
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,tm.szContent,strlen(tm.szContent),
(LPWSTR)buffer,140);
wRead=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,(LPWSTR)buffer,70,
tm.szContent,140,NULL,NULL);
tm.szContent[wRead]=0;
#endif
msg_format=15;
}else
{
if(wRead!=strlen(tm.szContent))
msg_format=15;else msg_format=0;
wRead=strlen(tm.szContent);
if(wRead>160)wRead=160;
tm.szContent[wRead]=0;
}
}
tm.msg_format=msg_format;
tm.dwCommand=nCMPP_SUBMIT;
AddLog("Sending to: %s(%d)<%s>\n",tm.DstTermId,tm.dwSequence,tm.szContent);
if(strcmp(szLastMobile,tm.DstTermId)==0)SleepEx(arg.nMaxInterval,1);
arg.dwSend++;
while(arg.nLost>arg.WINLEN)SleepEx(1000,1);
dwRet=SendOnlyData(&tm);
if(arg.isDebug&&dwRet)AddLog("dwRet: %d\n",dwRet);
if(dwRet)
{
if(dwRet==ExitFromNetClose)
{
SetWindow(-arg.nLost);
closesocket(arg.s);
iCurrent--;
break;
}else//other error
{
sprintf(strSQL,"exec sp_send_mt_error %d,%d,%d",
arg.iChannelNum,tm.dwSequence,dwRet);
arg.pCnnGSM->Errors->Clear();
arg.pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
if(arg.isDebug)AddLog("%s\n",strSQL);
}
}
SetWindow(1);
if(arg.dwStopFlag)return 0;
if(strcmp(szLastMobile,tm.DstTermId)==0)SleepEx(2*arg.nMaxInterval,1);
else strcpy(szLastMobile,tm.DstTermId);
SleepEx(arg.nMaxInterval,1);
}//end of while not eof record set.
sprintf(strSQL,"exec sp_send_mt_ok %d,%d,%d",arg.iChannelNum,iFirst,iCurrent);
arg.pCnnGSM->Errors->Clear();
arg.pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
AddLog("%s\n",strSQL);
}//try
catch (_com_error &e)
{
AddLog("Sender SQL error<%s>.\n",strSQL);
AddLog("Description = '%s'\n", (char*) e.Description());
return 0;
}
}//END of while(1)
return NOERROR;
}
GW_API void StopGW()
{
AddLog("Send msg to stop GW.\n");
arg.dwStopFlag=0x7e;
if(arg.hGW!=NULL)SetEvent(arg.hGW);
else ExitProcess(0);
}
void AddBinLog(char* buffer,int i,int flag=0)
{
int j=0;
FILE* pfLog;
char current[36];
char tp[36];
struct _timeb timebuffer;
char lpFileName[MAX_PATH];
if(arg.isDebug<3)return;
_strdate(current);
_ftime(&timebuffer);
strcpy(tp,ctime(&(timebuffer.time)));
current[2]='_';
current[5]=0;
tp[strlen(tp)-1]=0;
GetModuleFileName(NULL,lpFileName,MAX_PATH);
if(strlen(arg.szLogDir)>5)
sprintf(lpFileName,"%s\\Bin%s_%s.txt\0",arg.szLogDir,strrchr(tp,' ')+1,current);
else
sprintf(strrchr(lpFileName,'\\')+1,"Bin%s_%s.txt\0",strrchr(tp,' ')+1,current);
current[2]='-';
tp[strrchr(tp,' ')-tp]=0;
pfLog=fopen(lpFileName,"a");
if(pfLog==NULL)return;
_strtime(tp);
fprintf(pfLog,"<%s.%.3u\t%d-%d>\n",
tp,timebuffer.millitm,flag,arg.iChannelNum);
for(j=0;j<i;j++)
{
if((j%16)==0)fprintf(pfLog,"0x%.8x ",j);
fprintf(pfLog,"%.2x ",*(BYTE*)(buffer+j));
if((j+1)%16==0)
{
fprintf(pfLog," ");
for(int k=0;k<16;k++)
if(buffer[j-15+k]<126&&buffer[j-15+k]>20)
fwrite(buffer+j-15+k,1,1,pfLog);
else fprintf(pfLog,".");
fprintf(pfLog,"\n");
}
}
if(i%16>0)
{
j=16-i%16;
for(;j>0;j--)
fprintf(pfLog," ");
fprintf(pfLog," ");
for(int k=0;k<j;k++)
if(buffer[j-15+k]<126&&buffer[j-15+k]>20)
fwrite(buffer+i-j+k,1,1,pfLog);
else fprintf(pfLog,".");
}
fprintf(pfLog,"\n");
fflush(pfLog);
fclose(pfLog);
}
void AddResp(char* filename,BYTE isHour,const char* format,...)
{
FILE* pfLog;
char current[36];
char tp[36];
struct _timeb timebuffer;
char lpFileName[MAX_PATH];
_strdate(current);
_ftime(&timebuffer);
strcpy(tp,ctime(&(timebuffer.time)));
current[2]=current[3];
current[3]=current[4];
current[4]=0;
tp[strlen(tp)-1]=0;
strcpy(tp,strchr(tp,' ')+1);
strcpy(tp,strchr(tp,' ')+1);
strcpy(tp,strchr(tp,' ')+1);
GetModuleFileName(NULL,lpFileName,MAX_PATH);
if(strlen(arg.szLogDir)>5)
sprintf(lpFileName,"%s\\Log\0",arg.szLogDir);
if(isHour!=0)
sprintf(strrchr(lpFileName,'\\')+1,
"%s%s%s%.2s.txt\0",filename,strrchr(tp,' ')+1,current,tp);//create file in each hour
else
sprintf(strrchr(lpFileName,'\\')+1,
"%s%s%s.txt\0",filename,strrchr(tp,' ')+1,current);//create file in each day
pfLog=fopen(lpFileName,"a");
if(pfLog==NULL)return;
va_list args;
va_start(args,format);
#if _DEBUG
vfprintf(stdout,format,args);
#endif
vfprintf(pfLog,format,args);
va_end(args);
fflush(pfLog);
fclose(pfLog);
}
void wtos(char* msgid,DWORD id_time,DWORD id_sn)
{
DWORD tp;
tp=0x0f;//0000 1111
tp<<=28;
tp&=id_time;
tp>>=28;
sprintf(msgid,"%.2d",tp);//msgid="11"
tp=0x1f;//0001 1111
tp<<=23;
tp&=id_time;
tp>>=23;
sprintf(msgid+strlen(msgid),"%.2d",tp);//msgid="1111"
tp=0x1f;//0001 1111
tp<<=18;
tp&=id_time;
tp>>=18;
sprintf(msgid+strlen(msgid),"%.2d",tp);//msgid="111122"
tp=0x3f;//0011 1111
tp<<=12;
tp&=id_time;
tp>>=12;
sprintf(msgid+strlen(msgid),"%.2d",tp);//msgid="11112203"
tp=0x3f;//0011 1111
tp<<=6;
tp&=id_time;
tp>>=6;
sprintf(msgid+strlen(msgid),"%.2d",tp);//msgid="1111220342"
tp=0xffff;//1111 1111 1111 1111
tp<<=16;
tp&=id_sn;
tp>>=16;
tp+=((0x3f&id_time)<<16);
sprintf(msgid+strlen(msgid),"%.5d",tp);//msgid="111122034201001"
tp=0xffff&id_sn;
sprintf(msgid+strlen(msgid),"%.5d\0",tp);//msgid="11112203420100134764"
}
void stow(char* msgid,DWORD* id_time,DWORD* id_sn)
{
char msg[10];
DWORD id1,id2,tp;
id1=id2=0;
strncpy(msg,msgid,2);//mm
tp=atoi(msg);tp<<=28;
id1|=tp;
strncpy(msg,msgid+2,2);//dd
tp=atoi(msg);tp<<=23;
id1|=tp;
strncpy(msg,msgid+4,2);//hh
tp=atoi(msg);tp<<=18;
id1|=tp;
strncpy(msg,msgid+6,2);//mi
tp=atoi(msg);tp<<=12;
id1|=tp;
strncpy(msg,msgid+8,2);//ss
tp=atoi(msg);tp<<=6;
id1|=tp;
strncpy(msg,msgid+10,5);//gw code
tp=atoi(msg);tp>>=16;
id1|=tp;
tp=atoi(msg);tp<<=16;
id2|=tp;
strcpy(msg,msgid+15);//seq no.
tp=atoi(msg);tp<<=16;tp>>=16;
id2|=tp;
*id_time=id1;
*id_sn=id2;
}
void SetWindow(int i)
{
#if ONESECTION3
EnterCriticalSection(&(arg.syc3));
#endif
arg.nLost+=i;
#if ONESECTION3
LeaveCriticalSection(&(arg.syc3));
#endif
}
CArg::~CArg()
{
}
void AddLog(const char* format,...)
{
FILE* pfLog;
char current[36];
char tp[36];
struct _timeb timebuffer;
char lpFileName[MAX_PATH];
char buffer[10240];
if(arg.isDebug==0)return;
//if(arg.dwStopFlag)return;
_strdate(current);
_ftime(&timebuffer);
strcpy(tp,ctime(&(timebuffer.time)));
current[2]='_';
current[5]=0;
tp[strlen(tp)-1]=0;
GetModuleFileName(NULL,lpFileName,MAX_PATH);
if(strlen(arg.szLogDir)>5)
sprintf(lpFileName,"%s\\Log%s_%s.txt\0",arg.szLogDir,strrchr(tp,' ')+1,current);
else
sprintf(strrchr(lpFileName,'\\')+1,
"Log%s_%s.txt\0",strrchr(tp,' ')+1,current);
current[2]='-';
tp[strrchr(tp,' ')-tp]=0;
pfLog=fopen(lpFileName,"a");
if(pfLog==NULL)return;
va_list args;
va_start(args,format);
#if _DEBUG
vfprintf(stdout,format,args);
#endif
sprintf(buffer,"%s<%s.%.3u-%d> ",current,
strrchr(tp,' ')+1,timebuffer.millitm,arg.iChannelNum);
vsprintf(buffer+strlen(buffer),format,args);
fwrite(buffer,1,strlen(buffer),pfLog);
va_end(args);
fflush(pfLog);
fclose(pfLog);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -