📄 func.cpp
字号:
while(1)
{
#if 1
if(arg.isActive<1)
{
SleepEx(500,1);continue;
}
if(arg.dwRet>=COUNT2SMSC)
{
SleepEx(500,1);continue;
}
#endif
try
{
/////////////////////////////////////////////////////////////////////////////////////
sprintf(strSQL,
"SELECT TOP %d MSG_ID,STATION_ID,MSG_PERIOD,MSG_FORMAT,CALLED,MSG "
"FROM MSG_SMSC WHERE MSG_STATE=101"
"AND RESEND<%d ORDER BY MSG_ID",110,arg.nMaxResend);
rsGSM->Open(strSQL,_variant_t((IDispatch *)pCnnGSM),
adOpenStatic,adLockPessimistic,adCmdText);
if((!rsGSM->BOF)&&(!rsGSM->EndOfFile))
{
//COPY the data into the buffer
if(rsGSM->GetRecordCount())
strRecord=rsGSM->GetString(adClipString,
rsGSM->RecordCount,"\1","\2","\0");
rsGSM->Close();
}
else
{
rsGSM->Close();
SleepEx(300,1);
sprintf(strSQL,"exec sp_beforesend");
pCnnGSM->Errors->Clear();
pCnnGSM->Execute(strSQL,NULL,adExecuteNoRecords);
continue;
}
#if _DEBUG
AddLog("before sending msgs.\n");
#endif
i=j=k=0;
p=(char*)strRecord;
while(p)//the next record is avairable
{
if(arg.isActive<1)
{
SleepEx(100,1);
continue;
}
if(arg.dwRet>=COUNT2SMSC)
{
SleepEx(500,1);continue;
}
//MSG_ID,STATION_ID,MSG_PERIOD,CALLED,CONTENT
//p is the head of fields
msg_id=atoi(p);p=strchr(p,1)+1;
station_id=atoi(p);p=strchr(p,1)+1;
msg_period=atoi(p);p=strchr(p,1)+1;
msg_format=atoi(p);p=strchr(p,1)+1;
strncpy(mobile,p,strchr(p,1)-p);
mobile[strchr(p,1)-p]=0;
p=strchr(p,1)+1;
if(strchr(p,2))k=2;
else k=0;
strncpy(msg,p,strchr(p,k)-p);
msg[strchr(p,k)-p]=0;
if(k==0)p=NULL;
else
{
p=strchr(p,k)+1;
if(strlen(p)<1)p=NULL;
}
//if currrent record isn't the end of the rows
//p should be the head of the next record, otherwise p=NULL;
wRead=MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,msg,strlen(msg),NULL,0);
if((wRead!=strlen(msg))&&wRead>MSGLEN/2)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,msg,strlen(msg),
(LPWSTR)buffer,MSGLEN);
wRead=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,(LPWSTR)buffer,MSGLEN/2,
msg,MSGLEN,NULL,NULL);
msg[wRead]=0;
}
wRead=InitMsg(SUBMIT,buffer,mobile,msg,
msg_id,msg_format,msg_period,station_id);
if(wRead>1)
{
k=tk=0;
while(k<wRead)
{
tk=send(arg.s,buffer+k,wRead-k,0);
SleepEx(arg.nMaxInterval,1);
k+=tk;
if(tk<=0)
{
sendno[j]=msg_id;j++;
#if _DEBUG
AddLog("发送错误: %d\n",msg_id);
#endif
arg.isActive=0;
k=wRead;
}//13922089281
}
if(tk>0)
{
arg.dwRet++;
#if _DEBUG
//AddLog("Sending to: %d\n",msg_id);
#endif
}
}
else
{
errorno[i]=msg_id;//errorno contains the msgs can not be sent.
i++;
}
}//end of while not eof record set.
pCnnGSM->Errors->Clear();
pCnnGSM->Execute("exec sp_aftersend",NULL,adExecuteNoRecords);
k=0;
while(COUNT2CHANGE*k<j)
{//
sprintf(buffer,"(");
for(wRead=COUNT2CHANGE*k;wRead<j&&(wRead<COUNT2CHANGE*(k+1));wRead++)
sprintf(buffer+strlen(buffer),"%d,",sendno[wRead]);
sprintf(buffer+strlen(buffer)-1,")");
sprintf(strSQL,"UPDATE MSG_SMSC SET MSG_STATE=100,RESEND=RESEND-1 "
"WHERE MSG_ID IN %s",buffer);
pCnnGSM->Execute(strSQL,NULL,adExecuteNoRecords);
k++;
}
k=0;
while(COUNT2CHANGE*k<i)
{//
sprintf(buffer,"(");
for(wRead=COUNT2CHANGE*k;wRead<i&&(wRead<COUNT2CHANGE*(k+1));wRead++)
sprintf(buffer+strlen(buffer),"%d,",errorno[wRead]);
sprintf(buffer+strlen(buffer)-1,")");
sprintf(strSQL,"UPDATE MSG_SMSC SET MSG_STATE=103 "
"WHERE MSG_ID IN %s",buffer);
pCnnGSM->Execute(strSQL,NULL,adExecuteNoRecords);
k++;
}
#if _DEBUG
AddLog("After sending msg.\n");
#endif
}//try
catch (_com_error &e)
{
AddLog("SenderMsg执行SQL错误<%s>.\n",strSQL);
AddLog("错误描述:'%s'\n", (char*) e.Description());
if(pCnnGSM->GetState())pCnnGSM->Close();
::CoUninitialize();
SleepEx(500,1);
#if 0
goto resend;
#else
return 1;
#endif
}
}//END of while(1)
return NOERROR;
}
DWORD WINAPI DeliverMsg(LPVOID pp)
{
HANDLE hEvent=arg.hGW;
char msg[50];
char tp[50];
struct _timeb timebuffer;
while(1)
{
switch(WaitForSingleObject(hEvent,arg.nMaxInterval*60000))
{
case WAIT_FAILED: //error:
AddLog("DeliverMsg调用WSAWaitFor失败。\n");
return ExitFromWaitFailed;
break;
case WAIT_TIMEOUT://insert msg into the database to tell the manager the time
if(arg.isActive)
{
_strdate(tp);
_ftime(&timebuffer);
strcpy(tp+strlen(tp),ctime(&(timebuffer.time)));
sprintf(msg,"%.4s-%.2s-%.2s %.8s.%d",strrchr(tp,' ')+1,tp,tp+3,strchr(tp,':')-2,timebuffer.millitm);
sprintf(tp,"exec sp_insertmessage '%s','%s'",arg.szManager,msg);
Exec(tp);
}
break;
case WAIT_OBJECT_0: //thread exit here
return 0;
break;
}
}
}
GW_API void StopGW()
{
WSACleanup();
AddLog("调用StopGW,发送消息终止子服务.\n");
if(arg.hGW!=NULL)SetEvent(arg.hGW);
#if ONESECTION1
DeleteCriticalSection(&(arg.syc1));
#endif
#if ONESECTION2
DeleteCriticalSection(&(arg.syc2));
#endif
#if ONESECTION3
DeleteCriticalSection(&(arg.syc3));
#endif
}
int TranState(char* buffer)
{
char sstate[8];
int state=0;
strncpy(sstate,buffer,7);sstate[7]=0;
if(strcmp(sstate,"EN_ROUT")==0)state=41;
if(strcmp(sstate,"DELIVRD")==0)state=44;
if(strcmp(sstate,"EXPIRED")==0)state=42;
if(strcmp(sstate,"DELETED")==0)state=43;
if(strcmp(sstate,"UNDELIV")==0)state=45;
if(strcmp(sstate,"ACCEPTE")==0)state=44;
if(strcmp(sstate,"INVALID")==0)state=40;
return state;
}
char** AddSQL(char** ppStr,int index,char* str)
{
if(str==NULL&&index>0&&index<=10)
{
ppStr=new char*[index+1];
ppStr[0]=new char[3];
sprintf(ppStr[0],"%d",index);
}
else if(str)
{
ppStr[index]=new char[strlen(str)+1];
memcpy(ppStr[index],str,strlen(str));
ppStr[index][strlen(str)]=0;
}
return ppStr;
}
void AddBinLog(char* buffer,int i)
{
int j=0;
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[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");
fprintf(pfLog,"\n");
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,"\n");
}
fprintf(pfLog,"\n");
fflush(pfLog);
fclose(pfLog);
}
DWORD testfun()
{
char msg[50];
char tp[50];
struct _timeb timebuffer;
BYTE b;
sprintf(tp,"0x%.2s","a1b2");
sscanf(tp,"%x",&b);
for(int i=0;i<8;i++)
sprintf(tp+i*2,"%.2x",msg[i]);
_strdate(tp);
_ftime(&timebuffer);
strcpy(tp+strlen(tp),ctime(&(timebuffer.time)));
sprintf(msg,"%.4s-%.2s-%.2s %.8s.%d",strrchr(tp,' ')+1,tp,tp+3,strchr(tp,':')-2,timebuffer.millitm);
sprintf(tp,"exec sp_insertmessage '%s','%s'",arg.szManager,msg);
Exec(tp);
return 0;
}
void testfun2(SOCKET s)
{
char buffer[1024];
int i,j,k;
UINT count=6;
DWORD com[]={BIND,ALIVE,SUBMIT,UNBIND,CANCEL,QUERYSTATUS};
//////////////////////////////////////////////////////////////////////////////////////////
#if 0
_ConnectionPtr pCnn=NULL;
try
{
::CoInitialize(NULL);
TESTHR(pCnn.CreateInstance(__uuidof(Connection)));
TESTHR(pCnn->Open(arg.strCnn,"","",adConnectUnspecified));
AddLog("begin to insert the 100,000 msg into database.\n");
for(i=0;i<100000;i++)
{
sprintf(buffer,"exec sp_INSERTmessage '13910304490','test longggggggggggggggggggggggggggggggggg"
"ggggggggggggggggggggggggggggggggggggggggggggggggggggg");
pCnn->Errors->Clear();
pCnn->Execute(buffer,NULL,adExecuteNoRecords);
}
AddLog("After sending the 100,000 msg.\n");
pCnn->Close();
return;
}
catch (_com_error &e)
{
AddLog("Exec Error<%s>.\n",buffer);
AddLog("Description = '%s'\n", (char*) e.Description());
return;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////
i=InitMsg(BIND,buffer,0,0);
send(s,buffer,i,0);
i=j=0;
ZeroMemory(buffer,1024);
while(j<4)
{
i=recv(s,buffer+j,4-j,0);
j+=i;
if(i<1)break;
}//get the msg header
while(j<(int)GetNByte(buffer,4)&&i>0)
{
i=recv(s,buffer+j,GetNByte(buffer,4)-j,0);
j+=i;
if(i<1)break;
}//get the msg body
AddBinLog(buffer,j);
k=0;
while(1){
k++;
switch(GetNByte(buffer+4,4))
{
case BINDRESP:
if(buffer[12])
{
AddLog("Bind to SMSC error for <%d>\n",buffer[12]);
StopGW();
return;
}
AddLog("Bind result is coming.\n");
arg.isActive=1;
arg.dwRet=0;
break;
case ALIVE://over
i=InitMsg(ALIVERESP,buffer,0,0);
i=send(arg.s,buffer,i,0);
arg.isActive=1;
break;
case ALIVERESP://over
AddLog("Server is alive.\n");
arg.isActive=1;
break;
case SUBMITRESP:
arg.isActive=1;
arg.dwRet--;
if(buffer[20])//submit with error
{
AddLog("submit error for<%d>\n",buffer[20]);
}
else
{
AddLog("Send to: %d\n",GetNByte(buffer+8,4));
}
break;
case UNBINDRESP:
AddLog("Unbind from VAS platform.\n");
break;
case DELIVERY:
if(buffer[75]==0)//deliver the msg to esme
{//no processing to this currently
#if _DEBUG
AddLog("deliver: %.4d-%.4d\n",GetNByte(buffer+12,4),GetNByte(buffer+16,4));
AddLog("dest no: %.21s\n",buffer+20);
AddLog("service id :%.10s\n",buffer+41);
AddLog("src_term: %.21s\n",buffer+54);//src mobile
AddLog("msg : %s\n",buffer+77);
#endif
}
else//the status report to msg from esme
{
AddLog("status to %d-%d\n",GetNByte(buffer+77,4),GetNByte(buffer+81,4));
}
i=InitMsg(DELIVERYRESP,buffer,0,0);
send(arg.s,buffer,i,0);
arg.isActive=1;
break;
case QUERYSTATUSRESP:
AddLog("query msg state is return.\n");
arg.isActive=1;
break;
case CANCELRESP:
//GetID(msg,buffer+12);
AddLog("Cancel the message <%d-%d>.\n",GetNByte(buffer+12,3),GetNByte(buffer+18,2));
break;
}//end of swtich the type of the package
//end of processing a package.
i=InitMsg(com[rand()%count],buffer,"13910304490","test msg for server.",k+162,0,20,atoi(arg.ESMEID));
send(s,buffer,i,0);
i=j=0;
ZeroMemory(buffer,1024);
while(j<4)
{
i=recv(s,buffer+j,4-j,0);
j+=i;
if(i<1)break;
}//get the msg header
while(j<(int)GetNByte(buffer,4)&&i>0)
{
i=recv(s,buffer+j,GetNByte(buffer,4)-j,0);
j+=i;
if(i<1)break;
}//get the msg body
AddBinLog(buffer,j);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -