📄 func.cpp
字号:
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->iSelectInterval=tp;
fgets(tchar,_MAX_PATH,pf);
sscanf(strchr(tchar,'=')+1,"%s",arg->szService);
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;
fclose(pf);
GetModuleFileName(NULL,tchar,MAX_PATH);
sprintf(strrchr(tchar,'\\')+1,"servicename.ini\0");
pf=fopen(tchar,"r");
if(pf==NULL)
{
AddLog("Load file %s Failed .\n",tchar);
return 0;
}
fgets(arg->szSub,_MAX_PATH,pf);
while(arg->szSub[strlen(arg->szSub)-1]<32)
arg->szSub[strlen(arg->szSub)-1]=0;
fclose(pf);
try
{
::CoInitialize(NULL);
TESTHR(arg->pCnnGSM.CreateInstance(__uuidof(Connection)));
arg->pCnnGSM->ConnectionTimeout = 120;
arg->pCnnGSM->CommandTimeout=300;
TESTHR(arg->pCnnGSM->Open(arg->strCnn,"","",adConnectUnspecified));
arg->pCnnGSM->Errors->Clear();
}//try
catch (_com_error &e)
{
AddLog("db connection open error.\n");
AddLog("Description = '%s'\n", (char*) e.Description());
if(arg->pCnnGSM->GetState())arg->pCnnGSM->Close();
::CoUninitialize();
return false;
}
arg->hGW=CreateEvent(NULL,TRUE,FALSE,NULL);
if(arg->hGW==NULL)return 0;
arg->isActive=0;
arg->dwRecv=arg->dwSend=0;
arg->dwSequence=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;
WORD msg_format;
WORD wRead,i,j,k;
DWORD dwRet;
int check;
int iFirst,iCurrent;
_bstr_t strRecord;
char strSQL[512];
char szService[10];
char szLastMobile[21];
_RecordsetPtr rsGSM=NULL;
if(arg.isMO%2==1)while(1)SleepEx(10000,1);
AddLog("entering the mt thread.\n");
try
{
TESTHR(rsGSM.CreateInstance(__uuidof(Recordset)));
}//try
catch (_com_error &e)
{
AddLog("Sender SQL error.\n");
AddLog("Description = '%s'\n", (char*) e.Description());
SleepEx(arg.iSelectInterval,1);
return 1;
}
memset(&tm,0,sizeof(CMSG));
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
{
arg.dwSend++;
rsGSM->Close();
if(arg.isStop)return 0;
SleepEx(arg.iSelectInterval,1);
continue;
}
sprintf(szService,"%d",arg.SMSCPORT);
while(1)
{
arg.s=connectTCP(arg.SMSCIP,szService);
if(((int)arg.s)<1)
{
SleepEx(arg.iSelectInterval,1);
if(arg.isStop)return 0;
continue;
}
tm.dwCommand=SGIP_BIND;
tm.dwSequence=0;
if(dwRet=SendData(&tm))
{
SleepEx(arg.iSelectInterval,1);
return dwRet;
}
else break;
}
check=i=j=k=0;
iFirst=iCurrent=0;
p=(char*)strRecord;
strcpy(szLastMobile,"0");
while(p)//the next record is avairable
{
if(arg.isStop>0)
{
iCurrent--;
break;
}
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;
tm.FeeType=atoi(p);p=strchr(p,1)+1;
tm.FeeCode=atoi(p);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;
strncpy(tm.sLinkID,p,strchr(p,1)-p);
if(tm.sLinkID[0])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=SGIP_SUBMIT;
if(arg.isDebug)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++;
dwRet=SendData(&tm);
if(arg.isDebug)AddLog("Return: %d\n",dwRet);
if(dwRet==ExitFromNetClose)
{
iCurrent--;
break;
}
else if(dwRet||tm.szContent[0]==0)
{
sprintf(strSQL,"exec sp_send_mt_error %d,%d,%d",
arg.iChannelNum,tm.dwSequence,dwRet);
if(arg.isDebug)AddLog("error: %s\n",strSQL);
arg.pCnnGSM->Errors->Clear();
arg.pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
}else
{
//channle,msg_id,cp_id,dst,src,feedst,service_id,feetype,isReply,ismg_id
sprintf(strSQL,"exec sp_send_mt_resp %d,%d,'%.20s'",
arg.iChannelNum,tm.dwSequence,tm.szContent);
if(arg.isDebug)AddLog("resp: %s\n",strSQL);
AddResp("Resp",0,"%d,%d,%.20s\n",
arg.iChannelNum,tm.dwSequence,tm.szContent);
arg.pCnnGSM->Errors->Clear();
arg.pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
}
strcpy(szLastMobile,tm.DstTermId);
if(arg.isStop>0)
{
iCurrent--;
break;
}
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);
closesocket(arg.s);
if(arg.isStop>0)return 0;
}//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()
{
if(arg.ms>0)closesocket(arg.ms);
if(arg.s>0)closesocket(arg.s);
WSACleanup();
AddLog("Send msg to stop GW.\n");
if(arg.hGW!=NULL)SetEvent(arg.hGW);
}
void AddBinLog(char* buffer,int i,int flag)
{
int j=0;
FILE* pfLog;
char current[36];
char tp[36];
struct _timeb timebuffer;
char lpFileName[MAX_PATH];
if(arg.isDebug<2)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);
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>\n",
tp,timebuffer.millitm,flag);
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(isprint(buffer[j-15+k]))
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(isprint(buffer[i-j+k]))
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(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);
}
int RecvBuffer(SOCKET S,char* buffer)
{
int i,j;
i=j=0;
while(i<4)
{
j=recv(S,buffer+i,4-i,0);
if(j<1)
{
AddLog("recv socket error1 for: %d-%d\n",j,WSAGetLastError());
if(i>0)AddBinLog(buffer,i,0);
return ExitFromNetClose;
}
i+=j;
}
while(i<(int)GetNByte(buffer,4))
{
j=recv(S,buffer+i,GetNByte(buffer,4)-i,0);
if(j<1)
{
AddLog("recv socket error2 for: %d-%d\n",j,WSAGetLastError());
if(i>0)AddBinLog(buffer,i,0);
return ExitFromNetClose;
}
i+=j;
}
buffer[i]=0;
AddBinLog(buffer,i,0);
return 0;
}
CArg::~CArg()
{
}
void SendCheck(char* szService)
{
SOCKET s=connectTCP("127.0.0.1","9009");
if(s==INVALID_SOCKET)return;
SendBuffer(s,szService,strlen(szService));
closesocket(s);
}
int SendBuffer(SOCKET s,char* buffer,int buf_len)
{
int i,j;
i=j=0;
while(i<buf_len)
{
j=send(s,buffer+i,buf_len-i,0);
if(j<1)
{
AddLog("send socket error for: %d\n",WSAGetLastError());
if(i>0)AddBinLog(buffer,i,1);
return ExitFromNetClose;
}
i+=j;
}
AddBinLog(buffer,i,0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -