📄 func.cpp
字号:
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <time.h>
#include <sys/timeb.h>
#include <stdio.h>
#include <winsock2.h>
#include <direct.h>
#include <stdarg.h>
#include <wchar.h>
#include <STDLIB.H>
#include "api.h"
#include "gw.h"
#include "func.h"
#include "sock.h"
CArg arg;
dpl_status_t login_value_set(cmppe_login *cl);
dpl_status_t submit_value_set(cmppe_submit *cs,CMSG tm);
GW_API DWORD BeginGW(LPVOID pp)
{
DWORD dwRet;
HANDLE h[3];
DWORD dwRecv,dwSend;
WSAData wsaData;
WSAStartup(0x202,&wsaData);
if(!GetArg(&arg))
{
SleepEx(1000*10,1);
return 1;
}
if(arg.isDebug)AddLog("GW.dll is beginning.\n\n");
SendCheck(arg.szSub);
dwRet=0;
ResetEvent(arg.hGW);
h[0]=(HANDLE)_beginthreadex(NULL,0,(unsigned int(__stdcall*)(void*))MTThread,0,0,0);
h[1]=(HANDLE)_beginthreadex(NULL,0,(unsigned int(__stdcall*)(void*))MOThread,0,0,0);
h[2]=arg.hGW;
dwRecv=dwSend=0;
arg.isActive=0;
while(1)
{
dwRet=WaitForMultipleObjects(3,h,FALSE,20*1000);
switch(dwRet)
{
case WAIT_FAILED: //error:
AddLog("WSAWaitFor Error\n");
StopGW();
ExitProcess(-1);
break;
case WAIT_TIMEOUT:
SendCheck(arg.szSub);
if((dwRecv>arg.dwRecv+5)||dwSend>arg.dwSend+5)
{
if(arg.isDebug)AddLog("No data, thread will rerun.\n");
dwRecv=arg.dwRecv;
dwSend=arg.dwSend;
ResetEvent(arg.hGW);
TerminateThread(h[0],0);
TerminateThread(h[1],0);
goto RET__04;
}
dwRecv++;dwSend++;
if(dwRecv<arg.dwRecv)dwRecv=arg.dwRecv;
if(dwSend<arg.dwSend)dwSend=arg.dwSend;
break;
case WAIT_OBJECT_0: //mt_thread exit here
GetExitCodeThread(h[0],&dwRet);
if(arg.isDebug)AddLog("MTThread exit for %d.\n",dwRet);
if(dwRet==ExitFromNetClose)
{
SleepEx(1000,1);
TerminateThread(h[1],0);
StopGW();
goto RET__04;
}
CloseHandle(h[0]);
h[0]=(HANDLE)_beginthreadex(NULL,0,
(unsigned int(__stdcall*)(void*))MTThread,0,0,0);
if(h[0]==NULL)
{
TerminateThread(h[1],0);
StopGW();
goto RET__04;
}
break;
case WAIT_OBJECT_0 +1://mo thread exit here.
GetExitCodeThread(h[1],&dwRet);
if(arg.isDebug)AddLog("MOThread exit for %d.\n",dwRet);
if(dwRet==ExitFromNetClose)
{
SleepEx(1000,1);
TerminateThread(h[0],0);
StopGW();
goto RET__04;
}
CloseHandle(h[1]);
h[1]=(HANDLE)_beginthreadex(NULL,0,
(unsigned int(__stdcall*)(void*))MOThread,
0,0,0);
if(h[1]==NULL)
{
TerminateThread(h[0],0);
StopGW();
goto RET__04;
}
break;
case WAIT_OBJECT_0+2: //EventForExit
ResetEvent(arg.hGW);
AddLog("Get msg to stop GW.\n");
TerminateThread(h[0],0);
TerminateThread(h[1],0);
goto RET__04;
break;
}//switch(dwRet)
}//end fo while (1)
RET__04:
WSACleanup();
return 0;
}
void PutByteString(char* dest,
char* source,
int length)
{
int i=0;
if(source==NULL)return;
for(i=0;i<length;i++)
dest[i]=source[i];
}
void PutNByte(char* buffer,BYTE b,DWORD in)
{
int i;
if(b>4)b=4;
for(i=0;i<b;i++)
{
*(buffer+b-1-i)=char(in&0xFF);
in>>=8;
}
}
DWORD GetNByte(char* buffer,BYTE b)
{
int i;
DWORD rt=0;
if(b>4)b=4;
for(i=0;i<b;i++)
rt=rt*256+*(unsigned char*)(buffer+i);
return rt;
}
bool GetArg(CArg* arg)
{
FILE* pf=NULL;
char tchar[_MAX_PATH];
char tmp[100];
int tp;
GetModuleFileName(NULL,tchar,MAX_PATH);
sprintf(strrchr(tchar,'\\')+1,"gw.ini\0");
pf=fopen(tchar,"r");
if(pf==NULL)
{
AddLog("Load file %s Failed .\n",tchar);
return 0;
}
memset(arg,0,sizeof(CArg));
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sprintf(arg->strCnn,"Provider=sqloledb;Data Source=%s;",
strchr(tchar,'=')+1);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sprintf(tmp,"Initial Catalog=%s;",strchr(tchar,'=')+1);
sprintf(arg->strCnn+strlen(arg->strCnn),"%s",tmp);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sprintf(tmp,"User Id=%s;",strchr(tchar,'=')+1);
sprintf(arg->strCnn+strlen(arg->strCnn),"%s",tmp);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sprintf(tmp,"Password=%s",strchr(tchar,'=')+1);
sprintf(arg->strCnn+strlen(arg->strCnn),"%s\0",tmp);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
strcpy(arg->spid,strchr(tchar,'=')+1);
arg->spid[strlen(arg->spid)]=0;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
strcpy(arg->password,strchr(tchar,'=')+1);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
strcpy(arg->SMSCIP,strchr(tchar,'=')+1);
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->SMSCPORT=tp;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->nMaxReconnect=tp;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->nMaxInterval=tp;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->isDebug=tp;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
sscanf(strchr(tchar,'=')+1,"%d",&tp);
arg->iSelectInterval=tp;
fgets(tchar,_MAX_PATH,pf);if(tchar[strlen(tchar)-1]==10)tchar[strlen(tchar)-1]=0;
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);
arg->dwSend=0;
arg->dwRecv=0;
arg->hGW=CreateEvent(NULL,TRUE,FALSE,NULL);
if(arg->hGW==NULL)return 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)
{
char *p=0;
char strSQL[1024];
WORD wRead,k;
BYTE msg_format;
int check;
unsigned int seq_id;
/////////////////////////////////////////////
_bstr_t strRecord;
_ConnectionPtr pCnnGSM=NULL;
_RecordsetPtr rsGSM=NULL;
///////////////////////////////////////////
//var. for nokia api
CMSG tm;
dpl_status_t stat;
cmppe_submit cs;
cmppe_login cl;
cmppe_packet cp;
int status;
int type;
int result;
int seq;
int iFirst,iCurrent;
/////////////////////////////////////////////
AddLog("enter mt thread.\n");
memset(&cs,0,sizeof(cmppe_submit));
memset(&cl,0,sizeof(cmppe_login));
memset(&cp,0,sizeof(cmppe_packet));
/*建立与网关主机的连接*/
memset(&arg.conn,0,sizeof(arg.conn));
stat = cmpp_connect_to_ismg(arg.SMSCIP,arg.SMSCPORT,&arg.conn);
if(stat != DPL_SUCCESS)
{
AddLog("connect error: %s-%d-%d\n",arg.SMSCIP,arg.SMSCPORT,stat);
SleepEx(10000,1);
return ExitFromNetClose;
}
if(arg.isDebug)AddLog("connect to gw .\n");
login_value_set(&cl);
stat = cmpp_login(&arg.conn,&cl);
arg.dwSend++;
if(stat != DPL_SUCCESS)
{
SleepEx(1000,1);
return ExitFromNetClose;
}
stat = cmpp_recv(&arg.conn,&cp,0,&seq_id);
arg.dwRecv++;
if(stat != DPL_SUCCESS)return ExitFromNetClose;
type = cp.pk_head.pk_cmd;
status = cp.pk_head.pk_stat;
seq = cp.pk_head.pk_seq;
result = cp.result;
if((result != 0) || (status != 0))
{
if(arg.isDebug>1)AddLog("[type=%08x][status=%03x][seq=%08x][result=%02x]\n",\
type,status,seq,result);
cmpp_disconnect_from_ismg(&arg.conn);
return ExitFromNetClose;
}
arg.isActive=2;
if(arg.isDebug)AddLog("bind to ismg ok.\n");
try
{
::CoInitialize(NULL);
TESTHR(pCnnGSM.CreateInstance(__uuidof(Connection)));
pCnnGSM->ConnectionTimeout = 120;
pCnnGSM->CommandTimeout=300;
TESTHR(pCnnGSM->Open(arg.strCnn,"","",adConnectUnspecified));
TESTHR(rsGSM.CreateInstance(__uuidof(Recordset)));
}//try
catch (_com_error &e)
{
AddLog("Sender SQL error.\n");
AddLog("Description = '%s'\n", (char*) e.Description());
if(pCnnGSM->GetState())pCnnGSM->Close();
::CoUninitialize();
return 1;
}
AddLog("begin to send mt\n");
while(1)
{
try
{
/////////////////////////////////////////////////////////////////////////////////////
sprintf(strSQL,"EXEC SP_GET_MT %d",arg.iChannelNum);
rsGSM->Open(strSQL,_variant_t((IDispatch *)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();
if(arg.isDebug>2)AddLog("No msg to send\n");
tm.dwCommand=CMPPE_ACTIVE;
cmpp_active_test(&arg.conn);
tm.msg_len=0;
arg.dwSend++;
arg.dwRecv++;
stat=MOFun(&tm,pCnnGSM);
if(stat != DPL_SUCCESS)
{
if(arg.isDebug>2)AddLog("stat: %d\n",stat);
return ExitFromNetClose;
}
if(arg.isDebug>2)AddLog("stat: %d\n",stat);
SleepEx(arg.iSelectInterval,1);
continue;
}
check=k=0;
iFirst=iCurrent=0;
p=(char*)strRecord;
while(p)//the next record is avairable
{
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;
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;
#if ONESECTION1
EnterCriticalSection(&(arg.syc1));
#endif
if(msg_format==24)GBtoUCS2(tm.szContent,tm.szContent);
submit_value_set(&cs,tm);
tm.dwCommand=CMPPE_SUBMIT;
stat=cmpp_submit(&arg.conn,&cs,&tm.seq_id);
if(arg.isDebug)
AddLog("Sending to: %s(%d):<%s>\n",tm.DstTermId,iCurrent,tm.szContent);
#if ONESECTION1
LeaveCriticalSection(&(arg.syc1));
#endif
if(stat!=DPL_SUCCESS)
{
sprintf(strSQL,"exec sp_send_mt_error %d,%d,%d",
arg.iChannelNum,tm.dwSequence,stat);
pCnnGSM->Errors->Clear();
pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
if(arg.isDebug)AddLog("%s\n",strSQL);
}
tm.msg_len=1;
stat=MOFun(&tm,pCnnGSM);
if(stat)AddLog("Mo fun return: %d\n",stat);
if(stat==ExitFromNetClose||stat==CMPPE_RPACKET_ERR)//网络错误或者收到错误消息
{
iCurrent--;
break;
}
arg.dwSend++;
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);
AddLog("%s\n",strSQL);
pCnnGSM->Errors->Clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -