1887 lines
46 KiB
C++
1887 lines
46 KiB
C++
// Smgp.cpp: implementation of the CSmgp class.
|
||
//
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
#include "stdafx.h"
|
||
#include "Smgp.h"
|
||
|
||
//#include "MSmgp.h"
|
||
|
||
|
||
/*
|
||
#ifdef _DEBUG
|
||
#undef THIS_FILE
|
||
static char THIS_FILE[]=__FILE__;
|
||
#define new DEBUG_NEW
|
||
#endif
|
||
*/
|
||
//////////////////////////////////////////////////////////////////////
|
||
// Construction/Destruction
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
#include "..\..\..\public\md53\\md5_L.h"
|
||
|
||
|
||
CSmgp::CSmgp()
|
||
{
|
||
m_hSocket = INVALID_SOCKET;
|
||
memset( m_szIP , 0 , sizeof(m_szIP) );
|
||
m_lPort = 0;
|
||
m_bQuitThread = false;
|
||
m_dwEndSendTime = 0;
|
||
m_dwEndRecvTime = 0;
|
||
m_dwTestCount = 0;
|
||
m_bRecvFrame = false;
|
||
|
||
m_lID = 1;
|
||
m_pRecvProc = NULL;
|
||
m_bConnect = false;
|
||
m_bLoginSmgp= -1;
|
||
m_bInitSmgp = false;
|
||
m_lLoginType = 0;
|
||
|
||
memset(m_szSmgpIP ,0,sizeof(m_szSmgpIP));
|
||
memset(m_szSmgpUser ,0,sizeof(m_szSmgpUser));
|
||
memset(m_szSmgpPasswd,0,sizeof(m_szSmgpPasswd));
|
||
memset(m_szSmgpCorpID,0,sizeof(m_szSmgpCorpID));
|
||
|
||
WSADATA wsaData; //用于初始化socket的结构
|
||
//以下为初始化SOCKET环境
|
||
int iTemp=WSAStartup(0x0101,&wsaData); //初始化socket环境
|
||
|
||
InitializeCriticalSection(&m_CriSendSms);
|
||
InitializeCriticalSection(&m_CriRespList);
|
||
InitializeCriticalSection(&m_CriDeliverList);
|
||
InitializeCriticalSection(&m_CriStatusList);
|
||
InitializeCriticalSection(&m_CriSmgpExchangeResp);
|
||
InitializeCriticalSection(&m_CriErrMsg);
|
||
|
||
|
||
//定义队列
|
||
m_lSendID = 1;
|
||
m_pSendList = new Smgp_Send_List[SMGP_SMS_MAX_LIST];
|
||
m_pRespList = new Smgp_Send_Resp[SMGP_SMS_MAX_RESP];
|
||
m_pDeliverList = new Smgp_Deliver_List[SMGP_SMS_MAX_DELIVER];
|
||
m_pStatusList = new Smgp_Deliver_List[SMGP_SMS_MAX_STATUS];
|
||
m_pSmgpExchangeResp = new SmgpExchangeResp[SMGP_SMS_MAX_SMGPEXCHANGERESP];
|
||
|
||
memset(m_pSendList,0,sizeof(Smgp_Send_List)*SMGP_SMS_MAX_LIST);
|
||
memset(m_pRespList,0,sizeof(Smgp_Send_Resp)*SMGP_SMS_MAX_RESP);
|
||
memset(m_pDeliverList,0,sizeof(Smgp_Deliver_List)*SMGP_SMS_MAX_DELIVER);
|
||
memset(m_pStatusList,0,sizeof(Smgp_Deliver_List)*SMGP_SMS_MAX_STATUS);
|
||
memset(m_pSmgpExchangeResp,0,sizeof(SmgpExchangeResp)*SMGP_SMS_MAX_SMGPEXCHANGERESP);
|
||
|
||
m_lSendList = 0;
|
||
m_lRespList = 0;
|
||
m_lDeliverList=0;
|
||
m_lStatusList=0;
|
||
m_lSmgpExchangeRespList=0;
|
||
|
||
m_pSmgpExchange = NULL;
|
||
m_lSmgpExchange = 0;
|
||
|
||
m_bAdc = false;
|
||
|
||
//用于控制发送速度
|
||
m_dwEndSocketSendTime = 0;
|
||
m_lSendInterval = 0;
|
||
m_lSendIntervalCount = 1;
|
||
m_lSendIntervalIndex = 0;
|
||
|
||
m_hThread = INVALID_HANDLE_VALUE;
|
||
m_hThread_Send = INVALID_HANDLE_VALUE;
|
||
}
|
||
|
||
CSmgp::~CSmgp()
|
||
{
|
||
Stop();
|
||
|
||
|
||
//删除数据
|
||
if ( m_pSendList )
|
||
{delete m_pSendList;m_pSendList=NULL;}
|
||
|
||
if ( m_pRespList )
|
||
{delete m_pRespList;m_pRespList=NULL;}
|
||
|
||
if ( m_pDeliverList )
|
||
{delete m_pDeliverList;m_pDeliverList=NULL;}
|
||
|
||
if ( m_pStatusList )
|
||
{delete m_pStatusList;m_pStatusList=NULL;}
|
||
|
||
if ( m_pSmgpExchangeResp )
|
||
{delete m_pSmgpExchangeResp;m_pSmgpExchangeResp=NULL;}
|
||
|
||
DeleteCriticalSection(&m_CriSendSms);
|
||
DeleteCriticalSection(&m_CriRespList);
|
||
DeleteCriticalSection(&m_CriDeliverList);
|
||
DeleteCriticalSection(&m_CriStatusList);
|
||
DeleteCriticalSection(&m_CriSmgpExchangeResp);
|
||
DeleteCriticalSection(&m_CriErrMsg);
|
||
|
||
//退出Socket
|
||
WSACleanup();
|
||
}
|
||
|
||
BOOL CSmgp::Connect(const char *pAddr, long lPort, BOOL bReConnect)
|
||
{
|
||
if ( !bReConnect && m_hSocket!=INVALID_SOCKET) //已连接,并且无需重连
|
||
{
|
||
return true;
|
||
}
|
||
|
||
this->Stop(); //重新连接,先让原来的线程停止
|
||
|
||
strcpy( m_szIP,pAddr );
|
||
m_lPort = lPort;
|
||
|
||
sockaddr_in sAddr;
|
||
m_hSocket=socket(AF_INET,SOCK_STREAM,0);
|
||
sAddr.sin_family=AF_INET;
|
||
sAddr.sin_port=0;
|
||
sAddr.sin_addr.s_addr=htonl(INADDR_ANY);
|
||
if (bind(m_hSocket,(LPSOCKADDR)&sAddr,sizeof(sAddr))==SOCKET_ERROR)
|
||
{
|
||
return false;
|
||
}
|
||
sAddr.sin_port=htons((unsigned short)lPort);
|
||
sAddr.sin_addr.s_addr=inet_addr(pAddr);
|
||
if (sAddr.sin_addr.s_addr == INADDR_NONE)
|
||
{
|
||
LPHOSTENT lphost;
|
||
lphost = gethostbyname(pAddr);
|
||
if (lphost != NULL)
|
||
sAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
|
||
else
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
}
|
||
if (connect(m_hSocket,(LPSOCKADDR)&sAddr,sizeof(sAddr))==SOCKET_ERROR)
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
|
||
//设置Socket
|
||
u_long ulTemp=1; //将SOCKET设成非阻塞式的SOCKET
|
||
//u_long ulTemp=0; //将SOCKET设成阻塞式的SOCKET
|
||
ioctlsocket( m_hSocket,FIONBIO,&ulTemp );
|
||
ulTemp=1024000;
|
||
setsockopt( m_hSocket , SOL_SOCKET, SO_SNDBUF ,(const char *)&ulTemp ,sizeof(ulTemp)); //设绶冲
|
||
ulTemp=1024000;
|
||
setsockopt( m_hSocket , SOL_SOCKET, SO_RCVBUF ,(const char *)&ulTemp ,sizeof(ulTemp)); //设绶冲
|
||
|
||
m_bQuitThread = false;
|
||
DWORD dwTemp;
|
||
m_hThread=CreateThread(NULL,0,SocketRecv,(LPVOID)this,0,&dwTemp);
|
||
// if ( !::AfxBeginThread((AFX_THREADPROC)CNetSocket::SocketRecv,(LPVOID)this,THREAD_PRIORITY_LOWEST) )
|
||
if ( m_hThread == INVALID_HANDLE_VALUE )
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
SetThreadPriority(m_hThread,THREAD_PRIORITY_LOWEST); //设置优先级
|
||
m_hThread_Send = CreateThread(NULL, 0, SocketSend, (LPVOID)this, 0, &dwTemp);
|
||
// if ( !::AfxBeginThread((AFX_THREADPROC)CNetSocket::SocketRecv,(LPVOID)this,THREAD_PRIORITY_LOWEST) )
|
||
if (m_hThread_Send == INVALID_HANDLE_VALUE)
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
SetThreadPriority(m_hThread_Send, THREAD_PRIORITY_LOWEST); //设置优先级
|
||
|
||
m_bConnect = true;
|
||
return true;
|
||
}
|
||
|
||
BOOL CSmgp::Connect2()
|
||
{
|
||
sockaddr_in sAddr;
|
||
m_hSocket=socket(AF_INET,SOCK_STREAM,0);
|
||
sAddr.sin_family=AF_INET;
|
||
sAddr.sin_port=0;
|
||
sAddr.sin_addr.s_addr=htonl(INADDR_ANY);
|
||
if (bind(m_hSocket,(LPSOCKADDR)&sAddr,sizeof(sAddr))==SOCKET_ERROR)
|
||
{
|
||
return false;
|
||
}
|
||
sAddr.sin_port=htons((unsigned short)m_lPort);
|
||
sAddr.sin_addr.s_addr=inet_addr(m_szIP);
|
||
if (sAddr.sin_addr.s_addr == INADDR_NONE)
|
||
{
|
||
LPHOSTENT lphost;
|
||
lphost = gethostbyname(m_szIP);
|
||
if (lphost != NULL)
|
||
sAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
|
||
else
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
}
|
||
if (connect(m_hSocket,(LPSOCKADDR)&sAddr,sizeof(sAddr))==SOCKET_ERROR)
|
||
{
|
||
Close();
|
||
return false;
|
||
}
|
||
|
||
//设置Socket
|
||
u_long ulTemp=1; //将SOCKET设成非阻塞式的SOCKET
|
||
//u_long ulTemp=0; //将SOCKET设成阻塞式的SOCKET
|
||
ioctlsocket( m_hSocket,FIONBIO,&ulTemp );
|
||
ulTemp=1024000;
|
||
setsockopt( m_hSocket , SOL_SOCKET, SO_SNDBUF ,(const char *)&ulTemp ,sizeof(ulTemp)); //设绶冲
|
||
ulTemp=1024000;
|
||
setsockopt( m_hSocket , SOL_SOCKET, SO_RCVBUF ,(const char *)&ulTemp ,sizeof(ulTemp)); //设绶冲
|
||
return true;
|
||
}
|
||
|
||
BOOL CSmgp::isConnect()
|
||
{
|
||
return m_bConnect;
|
||
}
|
||
|
||
void CSmgp::Stop()
|
||
{
|
||
m_bQuitThread = true;
|
||
if (m_hThread != INVALID_HANDLE_VALUE && WaitForSingleObject(m_hThread,2000) == WAIT_TIMEOUT ) //等待5秒,让线程退出
|
||
{
|
||
TerminateThread(m_hThread,0); //如果退出超时,强制结束线程
|
||
}
|
||
m_hThread = INVALID_HANDLE_VALUE;
|
||
|
||
if (m_hThread_Send != INVALID_HANDLE_VALUE && WaitForSingleObject(m_hThread_Send, 2000) == WAIT_TIMEOUT) //等待5秒,让线程退出
|
||
{
|
||
TerminateThread(m_hThread_Send, 0); //如果退出超时,强制结束线程
|
||
}
|
||
m_hThread_Send = INVALID_HANDLE_VALUE;
|
||
|
||
Close();
|
||
}
|
||
|
||
BOOL CSmgp::isSocketClose(SOCKET s)
|
||
{
|
||
BOOL bConnDropped = FALSE;
|
||
INT iRet = 0;
|
||
BOOL bOK = TRUE;
|
||
|
||
if (s == INVALID_SOCKET)
|
||
return TRUE;
|
||
|
||
struct timeval timeout = { 0, 0 };
|
||
fd_set readSocketSet;
|
||
FD_ZERO(&readSocketSet);
|
||
FD_SET(s, &readSocketSet);
|
||
iRet = ::select(0, &readSocketSet, NULL, NULL, &timeout);
|
||
bOK = (iRet > 0);
|
||
if(bOK)
|
||
{
|
||
bOK = FD_ISSET(s, &readSocketSet);
|
||
}
|
||
|
||
if(bOK)
|
||
{
|
||
CHAR szBuffer[1] = "";
|
||
iRet = recv(s, szBuffer, 1, MSG_PEEK);
|
||
bOK = (iRet > 0);
|
||
if(!bOK)
|
||
{
|
||
INT iError = WSAGetLastError();
|
||
bConnDropped = (( iError == WSAENETRESET) ||
|
||
(iError == WSAECONNABORTED) ||
|
||
(iError == WSAECONNRESET) ||
|
||
(iError == WSAEINVAL) ||
|
||
(iRet == 0));
|
||
}
|
||
}
|
||
return(bConnDropped);
|
||
}
|
||
|
||
void CSmgp::Close()
|
||
{
|
||
if ( m_hSocket == INVALID_SOCKET )
|
||
return;
|
||
|
||
if ( m_hSocket != INVALID_SOCKET )
|
||
closesocket(m_hSocket);
|
||
|
||
m_hSocket = INVALID_SOCKET;
|
||
m_bConnect = false;
|
||
}
|
||
|
||
long CSmgp::Send(SOCKET sock,BYTE *pData, long lLen)
|
||
{
|
||
#define SOCKET_SEND_TIMEOUT 10000 //10秒发送超时
|
||
if ( sock == INVALID_SOCKET )
|
||
return 0;
|
||
|
||
long lSended = 0;
|
||
DWORD lTimeOut = ::GetTickCount();
|
||
while ( 1 )
|
||
{
|
||
long lRet = send(sock,(char*)(pData+lSended), lLen-lSended,0);
|
||
if ( lRet == SOCKET_ERROR ) //发送错误
|
||
{
|
||
if ( GetLastError() != WSAEWOULDBLOCK ) //绶区已满
|
||
return SOCKET_ERROR;
|
||
}
|
||
|
||
if ( lRet > 0 )
|
||
{
|
||
lSended += lRet;
|
||
if ( lSended >= lLen )
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
Sleep_Lu( 20 ); //绶冲区已满,稍等
|
||
}
|
||
if ( ::GetTickCount() - lTimeOut > SOCKET_SEND_TIMEOUT )
|
||
break;
|
||
}
|
||
return lSended;
|
||
}
|
||
|
||
DWORD WINAPI CSmgp::SocketRecv(LPVOID lParam)
|
||
{
|
||
CSmgp * pNet = (CSmgp *)lParam;
|
||
Smgp_Data_Recv Drecv={0};
|
||
pNet->m_dwEndRecvTime = ::GetTickCount(); //保存最后接收到帧的时间
|
||
DWORD dwEndSend=GetTickCount();
|
||
while ( !pNet->m_bQuitThread )
|
||
{
|
||
BOOL bReConnect=false;
|
||
if ( isSocketClose(pNet->m_hSocket) )
|
||
{
|
||
pNet->Close();
|
||
//重新接收数据
|
||
if ( Drecv.pRecvFrame )
|
||
delete Drecv.pRecvFrame;
|
||
memset(&Drecv,0,sizeof(Drecv));
|
||
if ( pNet->m_bInitSmgp ) //原已连接的,重新连接
|
||
{
|
||
bReConnect = true;
|
||
}
|
||
else
|
||
{
|
||
pNet->m_bLoginSmgp = -4;
|
||
}
|
||
}
|
||
if ( ReadFrame(pNet->m_hSocket,&Drecv) ) //判断是否有接收到新的Frame
|
||
{
|
||
pNet->m_dwEndRecvTime = ::GetTickCount(); //保存最后接收到帧的时间
|
||
pNet->m_bRecvFrame = true;
|
||
pNet->RecvFrame(Drecv); //处理接收到的帧
|
||
Drecv.lDataLen = 0;
|
||
delete Drecv.pRecvFrame;
|
||
Drecv.pRecvFrame = NULL;
|
||
}
|
||
//判断接收是否超时
|
||
//if ( pNet->m_dwEndSendTime > 0 && !pNet->m_bRecvFrame)
|
||
if ( pNet->m_dwEndSendTime>0 && ::GetTickCount() - pNet->m_dwEndRecvTime >SMGP_ACTIVE_TIMEOUT*2 ) //接收超时
|
||
{
|
||
pNet->m_dwEndRecvTime = ::GetTickCount()-SMGP_ACTIVE_TIMEOUT; //如果接收超时,重新计数
|
||
pNet->Close();
|
||
//重新接收数据
|
||
if ( Drecv.pRecvFrame )
|
||
delete Drecv.pRecvFrame;
|
||
memset(&Drecv,0,sizeof(Drecv));
|
||
if ( pNet->m_bInitSmgp ) //原已连接的,重新连接
|
||
bReConnect = true;
|
||
}
|
||
//判断是否应该发送Active_Test指令
|
||
if ( pNet->m_dwEndSendTime > 0 )
|
||
{
|
||
if ( ::GetTickCount() - pNet->m_dwEndSendTime > SMGP_ACTIVE_TESTTIME ) //重新连接
|
||
{
|
||
ULONG lSeq=0;
|
||
pNet->SendFrame(pNet->m_hSocket,pNet->m_lID,pNet->m_dwEndSendTime, SMGP_ACTIVE_TEST , NULL , 0 ,lSeq );
|
||
}
|
||
}
|
||
if ( bReConnect )
|
||
{
|
||
for ( int i=0 ; i<30 && !pNet->m_bQuitThread; i++ ) //延时5秒
|
||
{
|
||
Sleep_Lu(100);
|
||
}
|
||
pNet->SetErrMsg("Smgp消息:正在重连网关...");
|
||
if ( !pNet->m_bQuitThread )
|
||
pNet->InitSmgp2(); //重新连接到服务器
|
||
}
|
||
}
|
||
if ( Drecv.pRecvFrame )
|
||
delete Drecv.pRecvFrame;
|
||
pNet->Close();
|
||
return 1;
|
||
}
|
||
|
||
DWORD WINAPI CSmgp::SocketSend(LPVOID lParam)
|
||
{
|
||
CSmgp* pNet = (CSmgp*)lParam;
|
||
while (!pNet->m_bQuitThread)
|
||
{
|
||
//发送短信
|
||
long lCount = pNet->Back_Send();
|
||
if (lCount > 0)
|
||
Sleep_Lu(1);
|
||
else
|
||
Sleep_Lu(5);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
void CSmgp::SetRecvProc(SGM_PRECVPROC proc)
|
||
{
|
||
m_pRecvProc = proc;
|
||
}
|
||
|
||
BOOL CSmgp::ReadFrame(SOCKET s, Smgp_Data_Recv *pRecv)
|
||
{
|
||
ULONG nBytes=0;
|
||
ioctlsocket(s,FIONREAD, &nBytes);
|
||
if (nBytes <= 0 )
|
||
{
|
||
Sleep_Lu(1); //没有数据,等待一段时间
|
||
return false;
|
||
}
|
||
if ( pRecv->lDataLen <= 0 && nBytes<= sizeof(pRecv->lDataLen) ) //未收够长度信息
|
||
{
|
||
Sleep_Lu(1); //没有数据,等待一段时间
|
||
return false;
|
||
}
|
||
long lRet;
|
||
if (pRecv->lDataLen <= 0 ) //未收到数据,现在开始接收
|
||
{
|
||
lRet = recv(s, (char*)&pRecv->lDataLen,sizeof(pRecv->lDataLen),0);
|
||
if ( lRet > 0 )
|
||
{
|
||
pRecv->lDataLen = ntohl(pRecv->lDataLen);
|
||
if ( pRecv->lDataLen > 0 )
|
||
{
|
||
if ( pRecv->pRecvFrame )
|
||
delete pRecv->pRecvFrame;
|
||
pRecv->pRecvFrame = new BYTE[pRecv->lDataLen];
|
||
*((long*)pRecv->pRecvFrame) = pRecv->lDataLen; //存好前4个字节
|
||
pRecv->lRecvLen = sizeof(pRecv->lDataLen);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//计算需要接收的总字节数
|
||
ULONG lBeRecv = pRecv->lDataLen-pRecv->lRecvLen;
|
||
lRet = recv(s,(char*)pRecv->pRecvFrame+pRecv->lRecvLen , lBeRecv ,0);
|
||
if ( lRet > 0 )
|
||
pRecv->lRecvLen += lRet ;
|
||
if ( pRecv->lRecvLen == pRecv->lDataLen ) //Frame 已收满
|
||
{
|
||
pRecv->lDataLen = 0; //重新开始接收
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::RecvFrame(Smgp_Data_Recv &Drecv)
|
||
{
|
||
Smgp_Head * pHead=(Smgp_Head *)Drecv.pRecvFrame;
|
||
if ( pHead->Total_Length != Drecv.lRecvLen )
|
||
return false;
|
||
|
||
pHead->Command_Id = ntohl(pHead->Command_Id);
|
||
pHead->Sequence_Id= ntohl(pHead->Sequence_Id);
|
||
|
||
BYTE * pData = (BYTE*)(Drecv.pRecvFrame+sizeof(Smgp_Head));
|
||
long lLen = pHead->Total_Length-sizeof(Smgp_Head);
|
||
long lRet = 0;
|
||
switch( pHead->Command_Id )
|
||
{
|
||
case SMGP_LOGIN_RESP:
|
||
lRet = Process_Connect(*pHead,pData,lLen);
|
||
break;
|
||
case SMGP_SUBMIT_RESP:
|
||
lRet = Process_Submit(*pHead,pData,lLen);
|
||
break;
|
||
case SMGP_ACTIVE_TEST:
|
||
lRet = Process_Active(*pHead,pData,lLen);
|
||
break;
|
||
case SMGP_DELIVER:
|
||
lRet = Process_Deliver(*pHead,pData,lLen);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
return lRet;
|
||
}
|
||
|
||
long CSmgp::Smgp_Init(const char *pIP,long lPort, const char *pUser, const char *pPasswd,const char * pCorpID,long lLoginType)
|
||
{
|
||
if ( !Connect(pIP,lPort) )
|
||
{
|
||
return -1; //连接服务器失败
|
||
}
|
||
|
||
//记录信息
|
||
strcpy(m_szSmgpIP,pIP);
|
||
m_lPort = lPort;
|
||
strcpy(m_szSmgpUser,pUser);
|
||
strcpy(m_szSmgpPasswd,pPasswd);
|
||
strcpy(m_szSmgpCorpID,pCorpID);
|
||
m_lLoginType = lLoginType;
|
||
|
||
//生成Auth
|
||
UCHAR Auth[64]={0};
|
||
long lAdd=0;
|
||
strcpy((char*)Auth,pUser);
|
||
lAdd += strlen(pUser);
|
||
lAdd += 7;
|
||
strcpy((char*)(Auth+lAdd),pPasswd);
|
||
lAdd += strlen(pPasswd);
|
||
char str[256]={0};
|
||
SYSTEMTIME t;GetLocalTime(&t);
|
||
sprintf( str,"%02d%02d%02d%02d%02d",t.wMonth,t.wDay,t.wHour,t.wMinute,t.wSecond);
|
||
strcpy((char*)(Auth+lAdd),str);
|
||
lAdd += strlen(str);
|
||
|
||
|
||
MD5_L md5;
|
||
BYTE bMd5[64]={0};
|
||
memcpy(bMd5 , md5.MD5Byte(Auth,lAdd),16);
|
||
|
||
|
||
Smgp_Login connect={0};
|
||
strcpy(connect.ClientID,pUser);
|
||
memcpy(connect.AuthenticatorClient,bMd5,16);
|
||
|
||
connect.LoginMode = (UCHAR)m_lLoginType;
|
||
connect.ClientVersion = 0x13;
|
||
connect.TimeStamp = atol(str);
|
||
connect.TimeStamp = htonl(connect.TimeStamp);
|
||
|
||
|
||
m_bLoginSmgp = -1;
|
||
m_bInitSmgp = false;
|
||
ULONG lSeq=0;
|
||
if ( !SendFrame(m_hSocket,m_lID,m_dwEndSendTime,SMGP_LOGIN,(UCHAR*)&connect,sizeof(connect),lSeq) )
|
||
return -2; //发送数据失败
|
||
|
||
long lTime = ::GetTickCount();
|
||
while ( m_bLoginSmgp == -1 )
|
||
{
|
||
if ( ::GetTickCount()-lTime > 30000 ) //超时了
|
||
break;
|
||
Sleep_Lu(20);
|
||
}
|
||
if ( m_bLoginSmgp==0 )
|
||
m_bInitSmgp = true; //已初始化
|
||
if ( m_bLoginSmgp == -1 )
|
||
return -3; //登录超时
|
||
else
|
||
return m_bLoginSmgp; //登录返回成功,具体请看错误码
|
||
|
||
return -4; //其它错误
|
||
}
|
||
|
||
long CSmgp::SendFrame(SOCKET sock,ULONG &lID,DWORD &dwEndSendTime,ULONG lCommandID,UCHAR *pData, long lDataLen,ULONG & lSeq )
|
||
{
|
||
Smgp_Head head={0};
|
||
long lAllLen;
|
||
head.Total_Length = sizeof(head)+lDataLen;
|
||
lAllLen = head.Total_Length;
|
||
head.Command_Id = lCommandID;
|
||
if ( lID >= 0xF000000 ) //重新开始序号
|
||
lID = 1;
|
||
if ( lSeq > 0 )
|
||
head.Sequence_Id = lSeq; //直接使用相同序号的发送
|
||
else
|
||
head.Sequence_Id = lID++;
|
||
lSeq = head.Sequence_Id; //返回
|
||
|
||
head.Total_Length = htonl(head.Total_Length);
|
||
head.Command_Id = htonl(head.Command_Id);
|
||
head.Sequence_Id = htonl(head.Sequence_Id);
|
||
|
||
dwEndSendTime = ::GetTickCount(); //记录最后发送时间
|
||
|
||
BYTE * pSData = new BYTE[lAllLen];
|
||
memcpy(pSData,&head,sizeof(head));
|
||
memcpy(pSData+sizeof(head),pData,lDataLen);
|
||
BOOL bOK = Send(sock,(BYTE*)pSData , lAllLen );
|
||
delete pSData;
|
||
if ( bOK )
|
||
return head.Sequence_Id;
|
||
else
|
||
return 0;
|
||
return 0;
|
||
}
|
||
|
||
long CSmgp::Process_Connect(Smgp_Head head,BYTE *pData, long lLen)
|
||
{
|
||
Smgp_Login_Resp * pResp = (Smgp_Login_Resp *)pData;
|
||
pResp->Status = ntohl(pResp->Status);
|
||
|
||
if ( m_bInitSmgp && pResp->Status != 0 ) //需要重新登录
|
||
{
|
||
//登录不成功,关闭连接
|
||
this->Close();
|
||
}
|
||
|
||
CString str;
|
||
str.Format(_T("Smgp消息:连接网关返回代码:%d") , pResp->Status );
|
||
SetErrMsg(str);
|
||
|
||
m_bLoginSmgp = pResp->Status;
|
||
return 1;
|
||
}
|
||
|
||
BOOL CSmgp::SendSms(Smgp_Send_List sms)
|
||
{
|
||
for (int j = 0; j < 200; j++)
|
||
{
|
||
if (Sms_Add(sms))
|
||
return sms.lSendID;
|
||
/*
|
||
//从当前点到结尾
|
||
EnterCriticalSection(&m_CriSendSms); //防止冲突
|
||
for ( int i=0 ; i< SMS_MAX_LIST; i++ )
|
||
{
|
||
if ( m_pSendList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
sms.lSendTime = 0;
|
||
sms.lReSendCount = 0;
|
||
m_pSendList[i] = sms;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return sms.lSendID;
|
||
break;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
*/
|
||
Sleep_Lu(20); //延时10秒
|
||
}
|
||
return false;
|
||
/*
|
||
for ( int j=0 ; j< 200 ; j++ )
|
||
{
|
||
//从当前点到结尾
|
||
EnterCriticalSection(&m_CriSendSms); //防止冲突
|
||
for ( int i=0 ; i< SMGP_SMS_MAX_LIST; i++ )
|
||
{
|
||
if ( m_pSendList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
sms.lSendTime = 0;
|
||
sms.lReSendCount = 0;
|
||
m_pSendList[i] = sms;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return sms.lSendID;
|
||
break;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
Sleep_Lu(100); //延时10秒
|
||
}
|
||
return false;
|
||
*/
|
||
}
|
||
|
||
BOOL CSmgp::InitSmgp2()
|
||
{
|
||
m_bLoginSmgp = -1;
|
||
|
||
if ( !Connect2() )
|
||
{
|
||
return false; //连接服务器失败
|
||
}
|
||
|
||
//生成Auth
|
||
UCHAR Auth[64]={0};
|
||
long lAdd=0;
|
||
strcpy((char*)Auth,m_szSmgpUser);
|
||
lAdd += strlen(m_szSmgpUser);
|
||
lAdd += 7;
|
||
strcpy((char*)(Auth+lAdd),m_szSmgpPasswd);
|
||
lAdd += strlen(m_szSmgpPasswd);
|
||
char str[256]={0};
|
||
SYSTEMTIME t;GetLocalTime(&t);
|
||
sprintf( str,"%02d%02d%02d%02d%02d",t.wMonth,t.wDay,t.wHour,t.wMinute,t.wSecond);
|
||
strcpy((char*)(Auth+lAdd),str);
|
||
lAdd += strlen(str);
|
||
|
||
MD5_L md5;
|
||
BYTE bMd5[64]={0};
|
||
memcpy(bMd5 , md5.MD5Byte(Auth,lAdd),16);
|
||
|
||
Smgp_Login connect={0};
|
||
strcpy(connect.ClientID,m_szSmgpUser);
|
||
//if ( m_lPort == SMGP_EXCHANGE_PORT || m_lPort == SMGP_EXCHANGE_PORT2 )
|
||
// ;
|
||
//else
|
||
memcpy(connect.AuthenticatorClient,bMd5,16);
|
||
|
||
connect.LoginMode = (UCHAR)m_lLoginType;
|
||
connect.ClientVersion = 0x13;
|
||
connect.TimeStamp = atol(str);
|
||
connect.TimeStamp = htonl(connect.TimeStamp);
|
||
|
||
|
||
m_bLoginSmgp = -1;
|
||
ULONG lSeq=0;
|
||
if ( !SendFrame(m_hSocket,m_lID,m_dwEndSendTime,SMGP_LOGIN,(UCHAR*)&connect,sizeof(connect),lSeq) )
|
||
return -2; //发送数据失败
|
||
|
||
|
||
//重新连接时,需要重发短信
|
||
|
||
memset(m_pSendList,0,sizeof(Smgp_Send_List)*SMGP_SMS_MAX_LIST);
|
||
/*
|
||
for ( int i=0 ; i< SMGP_SMS_MAX_LIST ;i++ )
|
||
{
|
||
if ( m_pSendList[i].lSendID > 0 )
|
||
{
|
||
m_pSendList[i].lReSendCount = 0;
|
||
m_pSendList[i].lSendTime = 0;
|
||
m_pSendList[i].lSeq = 0;
|
||
}
|
||
}
|
||
*/
|
||
return -4; //其它错误
|
||
}
|
||
|
||
long CSmgp::Back_Send()
|
||
{
|
||
|
||
|
||
long lCount = 0;
|
||
for (int i = 0; i < 100; i++)
|
||
{
|
||
Smgp_Send_List* pSend = 0;
|
||
if (Sms_Get(pSend) && pSend)
|
||
{
|
||
|
||
BOOL bSend = false;
|
||
if (pSend->lReSendCount > 0) //前已发送过,看是否到达重发时间
|
||
{
|
||
if (::GetTickCount() - pSend->lSendTime > SMGP_SENDSMS_TIMEOUT)
|
||
bSend = true;
|
||
else
|
||
bSend = false;
|
||
}
|
||
else
|
||
{
|
||
bSend = true; //第一次发送
|
||
}
|
||
if (bSend)
|
||
{
|
||
if (pSend->lReSendCount >= SMGP_SENDSMS_RECOUNT) //发送失败
|
||
{
|
||
//插入发送返回
|
||
Smgp_Send_Resp Resp = { 0 };
|
||
Resp.lSendID = pSend->lSendID;
|
||
Resp.lReSendCount = pSend->lReSendCount;
|
||
Resp.lResult = -1; //发送失败
|
||
SendResp_Add(Resp);
|
||
pSend->lSendID = 0; //已删除
|
||
}
|
||
else
|
||
{
|
||
|
||
if (m_lSendInterval > 0) //控制发送速度
|
||
{
|
||
m_lSendIntervalIndex++;
|
||
if (m_lSendIntervalIndex >= m_lSendIntervalCount)
|
||
{
|
||
Sleep_Lu(m_lSendInterval);
|
||
m_lSendIntervalIndex = 0;
|
||
}
|
||
}
|
||
|
||
m_dwEndSocketSendTime = GetTickCount(); //用于控制发送速度
|
||
|
||
|
||
//以下为群发
|
||
//sendList.AddTail(pSend);
|
||
//lCount ++;
|
||
|
||
//以下为单发
|
||
if (Socket_SendSms(pSend))
|
||
lCount++;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*
|
||
if ( m_bLoginSmgp != 0 ) //还没登录成功,不能发短信
|
||
return 0;
|
||
|
||
EnterCriticalSection(&m_CriSendSms); //防止冲突
|
||
long lCount = 0;
|
||
for ( int i=0 ; i<SMGP_SMS_MAX_LIST;i++ )
|
||
{
|
||
Smgp_Send_List * pSend = &m_pSendList[i];
|
||
if ( pSend->lSendID == 0 )
|
||
continue;
|
||
BOOL bSend = false;
|
||
if ( pSend->lReSendCount > 0 ) //前已发送过,看是否到达重发时间
|
||
{
|
||
if ( ::GetTickCount()-pSend->lSendTime>SMGP_SENDSMS_TIMEOUT )
|
||
bSend = true;
|
||
else
|
||
bSend = false;
|
||
}
|
||
else
|
||
{
|
||
bSend = true; //第一次发送
|
||
}
|
||
if ( bSend )
|
||
{
|
||
if ( pSend->lReSendCount >= SMGP_SENDSMS_RECOUNT ) //发送失败
|
||
{
|
||
//插入发送返回
|
||
Smgp_Send_Resp Resp={0};
|
||
Resp.lSendID = pSend->lSendID;
|
||
Resp.lReSendCount = pSend->lReSendCount;
|
||
Resp.lResult = -1; //发送失败
|
||
SendResp_Add(Resp);
|
||
pSend->lSendID = 0 ; //已删除
|
||
}
|
||
else
|
||
{
|
||
if (m_lSendInterval>0 ) //控制发送速度
|
||
{
|
||
long lDelay=m_lSendInterval-(long)(GetTickCount()-m_dwEndSocketSendTime);
|
||
if ( lDelay>0 && lDelay<m_lSendInterval+5 )
|
||
//Sleep_Lu(lDelay-lDelay/15);
|
||
Sleep_Lu(lDelay);
|
||
}
|
||
m_dwEndSocketSendTime = GetTickCount(); //用于控制发送速度
|
||
|
||
if ( Socket_SendSms(pSend) )
|
||
lCount ++;
|
||
|
||
}
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
*/
|
||
return lCount;
|
||
}
|
||
|
||
BOOL CSmgp::Socket_SendSms(Smgp_Send_List *pSend)
|
||
{
|
||
long lSize = sizeof(pSend->Submit)+pSend->Submit.MsgLength+sizeof(pSend->SubmitEx); //计算发送信息的总长度
|
||
|
||
BYTE * pData = new BYTE[lSize];
|
||
memset(pData,0,lSize);
|
||
Smgp_Submit * pSubmit = (Smgp_Submit*)pData;
|
||
BYTE * pMsg = (BYTE*)(pData+sizeof(Smgp_Submit));
|
||
Smgp_Submit_Ex * pSubmitEx = (Smgp_Submit_Ex*)(pData+sizeof(Smgp_Submit)+pSend->Submit.MsgLength);
|
||
|
||
//拷贝实际内容。
|
||
*pSubmit = pSend->Submit;
|
||
memcpy(pMsg,pSend->Msg,pSend->Submit.MsgLength);
|
||
*pSubmitEx = pSend->SubmitEx;
|
||
ULONG lSeq=0;
|
||
BOOL b = SendFrame(m_hSocket,m_lID,m_dwEndSendTime,SMGP_SUBMIT,pData,lSize,lSeq);
|
||
//if ( b )
|
||
//{
|
||
pSend->lReSendCount ++;
|
||
pSend->lSendTime = ::GetTickCount();
|
||
pSend->lSeq = lSeq;
|
||
//}
|
||
delete pData;
|
||
return b;
|
||
}
|
||
|
||
long CSmgp::Process_Submit(Smgp_Head head,BYTE *pData, long lLen)
|
||
{
|
||
Smgp_Submit_Resp * pResp = (Smgp_Submit_Resp *)pData;
|
||
|
||
pResp->Status = ntohl(pResp->Status);
|
||
|
||
|
||
//收到发送返回,在队列中查找发送信息
|
||
Smgp_Send_List * pSendSms = NULL;
|
||
BOOL bFind = false;
|
||
ULONG lSeq = head.Sequence_Id;
|
||
for ( int i=0 ; i<SMGP_SMS_MAX_LIST;i++ )
|
||
{
|
||
pSendSms = &m_pSendList[i];
|
||
if ( pSendSms->lSendID!=0 && pSendSms->lSeq == lSeq )
|
||
{
|
||
bFind = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if ( bFind )
|
||
{
|
||
if ( pSendSms->lSendID <= MAX_CMPPEXCHANGE+10 ) //小于-100表示cmpp转接所发
|
||
{
|
||
SmgpExchangeResp resp={0};
|
||
resp.lType = 0;
|
||
resp.lSendID = pSendSms->lSendID;
|
||
resp.lLen = lLen;
|
||
resp.lCmd = head.Command_Id;
|
||
resp.lSeq = pSendSms->lSmgpSeq;
|
||
memcpy(resp.Data , pData , lLen );
|
||
SmgpExchangeResp_Add(resp);
|
||
|
||
pSendSms->lSendID = 0; //删除此记录
|
||
}
|
||
else
|
||
{
|
||
Smgp_Send_Resp Resp={0};
|
||
Resp.lSendID = pSendSms->lSendID;
|
||
Resp.lReSendCount = pSendSms->lReSendCount;
|
||
memcpy(Resp.lMsgID , pResp->MsgID,sizeof(pResp->MsgID));
|
||
//pSend->lMsgID = pResp->Msg_ID;
|
||
Resp.lResult = pResp->Status;
|
||
SendResp_Add(Resp);
|
||
|
||
pSendSms->lSendID = 0; //删除此记录
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
BOOL CSmgp::GetSendResp(Smgp_Send_Resp &resp)
|
||
{
|
||
EnterCriticalSection(&m_CriRespList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lRespList ; i< SMGP_SMS_MAX_RESP; i++ )
|
||
{
|
||
if ( m_pRespList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lRespList = i+1;
|
||
resp = m_pRespList[i];
|
||
m_pRespList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lRespList; i++ )
|
||
{
|
||
if ( m_pRespList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lRespList = i+1;
|
||
resp = m_pRespList[i];
|
||
m_pRespList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
m_lRespList = 0;
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
long CSmgp::Process_Active(Smgp_Head head, BYTE *pData, long lLen)
|
||
{
|
||
m_dwTestCount = 0; //收到active指令,检测次数为0
|
||
|
||
ULONG lReq = head.Sequence_Id;
|
||
SendFrame(m_hSocket, m_lID, m_dwEndSendTime, SMGP_ACTIVE_TEST_RESP, NULL, 0, lReq);
|
||
|
||
return 1;
|
||
}
|
||
|
||
long CSmgp::Process_Deliver(Smgp_Head head, BYTE *pData, long lLen)
|
||
{
|
||
Smgp_Deliver * pDeliver = (Smgp_Deliver *)pData;
|
||
BYTE * pMsgData = pData+sizeof(Smgp_Deliver);
|
||
//判断是否cmpp转接的短信内容
|
||
BOOL bExchange=false;
|
||
if ( m_pSmgpExchange )
|
||
{
|
||
long lExNum=0;
|
||
for ( int i=0 ; i<m_lSmgpExchange;i++ )
|
||
{
|
||
if (m_pSmgpExchange[i].sock != 0 && m_pSmgpExchange[i].sock != INVALID_SOCKET &&
|
||
strlen(m_pSmgpExchange[i].szNum)>0 &&
|
||
!strncmp((char*)pDeliver->DestTermID,m_pSmgpExchange[i].szNum,strlen(m_pSmgpExchange[i].szNum) ) )
|
||
{
|
||
SmgpExchangeResp resp={0};
|
||
resp.lType = 1;
|
||
strcpy(resp.szNum , m_pSmgpExchange[i].szNum );
|
||
resp.lExNum = lExNum; //adc特殊转发时用到
|
||
resp.lLen = lLen;
|
||
resp.lCmd = head.Command_Id;
|
||
resp.lSeq = head.Sequence_Id;
|
||
memcpy(resp.Data , pData , lLen );
|
||
SmgpExchangeResp_Add(resp);
|
||
bExchange = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if ( !bExchange)
|
||
{
|
||
Smgp_Deliver_List Deliver={0};
|
||
Deliver.lSendID = 1;
|
||
|
||
Deliver.deliver = *pDeliver;
|
||
|
||
//取ID
|
||
memcpy(Deliver.deliver.MsgID , pDeliver->MsgID , sizeof(pDeliver->MsgID));
|
||
|
||
strcpy((char*)Deliver.deliver.DestTermID , (char*)pDeliver->DestTermID);
|
||
|
||
//Deliver.deliver.TP_pid = pDeliver->TP_pid;
|
||
//Deliver.deliver.TP_udhi = pDeliver->TP_udhi;
|
||
|
||
Deliver.deliver.MsgFormat = pDeliver->MsgFormat;
|
||
strcpy((char*)Deliver.deliver.SrcTermID,(char*)pDeliver->SrcTermID);
|
||
Deliver.deliver.IsReport = pDeliver->IsReport;
|
||
Deliver.deliver.MsgLength = pDeliver->MsgLength;
|
||
/*
|
||
if ( pMsgData[0] == 0x59 && pMsgData[1] == 0x4E && (pMsgData[2] >= 0x01 && pMsgData[2] <= 0x04) && (pMsgData[3] >= 0x01 && pMsgData[3] <= 0x31) )
|
||
{
|
||
if ( Deliver.deliver.MsgLength > 70 )
|
||
Deliver.deliver.MsgLength = 70;
|
||
AsciiToHex((char*)pMsgData,(TCHAR*)Deliver.szMsg,Deliver.deliver.MsgLength); //转换成hex码
|
||
Deliver.deliver.MsgLength= Deliver.deliver.MsgLength*2;
|
||
Deliver.deliver.MsgFormat = 0; //改变编码
|
||
//memcpy(Deliver.szMsg,pMsgData,Deliver.deliver.MsgLength );
|
||
}
|
||
else
|
||
{
|
||
*/
|
||
memcpy(Deliver.szMsg,pMsgData,Deliver.deliver.MsgLength ); //取信息内容
|
||
//}
|
||
|
||
|
||
if ( Deliver.deliver.IsReport ) //是状态报告
|
||
{
|
||
memcpy(&Deliver.status,pMsgData,sizeof(Deliver.status));
|
||
Status_Add(Deliver); //添加到列表
|
||
}
|
||
else
|
||
{
|
||
Deliver_Add(Deliver); //添加到列表
|
||
}
|
||
}
|
||
|
||
//发送返回
|
||
Smgp_Deliver_Resp resp={0};
|
||
memcpy(resp.MsgID , pDeliver->MsgID , sizeof(pDeliver->MsgID));
|
||
resp.Status = 0;
|
||
ULONG lReq=head.Sequence_Id;
|
||
SendFrame(m_hSocket,m_lID,m_dwEndSendTime,SMGP_DELIVER_RESP,(BYTE*)&resp,sizeof(resp),lReq);
|
||
|
||
return true;
|
||
}
|
||
|
||
BOOL CSmgp::GetDeliver(Smgp_Deliver_List &deliver)
|
||
{
|
||
EnterCriticalSection(&m_CriDeliverList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lDeliverList ; i< SMGP_SMS_MAX_DELIVER; i++ )
|
||
{
|
||
if ( m_pDeliverList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lDeliverList = i+1;
|
||
deliver = m_pDeliverList[i];
|
||
m_pDeliverList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lDeliverList; i++ )
|
||
{
|
||
if ( m_pDeliverList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lDeliverList = i+1;
|
||
deliver = m_pDeliverList[i];
|
||
m_pDeliverList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
m_lDeliverList = 0;
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::Smgp_Exit()
|
||
{
|
||
m_bInitSmgp = false; //已初始化
|
||
ULONG lSeq=0;
|
||
SendFrame(m_hSocket,m_lID,m_dwEndSendTime,SMGP_EXIT,NULL,0,lSeq);
|
||
|
||
Stop(); //停止
|
||
return true;
|
||
|
||
}
|
||
void CSmgp::UCS2ToAscii2(char *pUCS2, char *pAscii, long lSrcLen)
|
||
{
|
||
UINT nLen = 0;
|
||
for(int i = 0; i < lSrcLen/2; i++)
|
||
*((unsigned short*)pUCS2 + i) = ntohs(*((unsigned short*)pUCS2 + i));
|
||
|
||
nLen = WideCharToMultiByte(936, WC_COMPOSITECHECK,
|
||
(WCHAR*)pUCS2, lSrcLen/2, pAscii,lSrcLen, NULL, NULL);
|
||
}
|
||
|
||
BOOL CSmgp::SendResp_Add(Smgp_Send_Resp resp)
|
||
{
|
||
EnterCriticalSection(&m_CriRespList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lRespList ; i< SMGP_SMS_MAX_RESP; i++ )
|
||
{
|
||
if ( m_pRespList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pRespList[i] = resp;
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lRespList; i++ )
|
||
{
|
||
if ( m_pRespList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pRespList[i] = resp;
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriRespList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::Deliver_Add(Smgp_Deliver_List Deliver)
|
||
{
|
||
EnterCriticalSection(&m_CriDeliverList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lDeliverList ; i< SMGP_SMS_MAX_DELIVER; i++ )
|
||
{
|
||
if ( m_pDeliverList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pDeliverList[i] = Deliver;
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lDeliverList; i++ )
|
||
{
|
||
if ( m_pDeliverList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pDeliverList[i] = Deliver;
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriDeliverList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
long CSmgp::Smgp_SendSms(const char * SendNo , const char * RecvNo,long Msg_Fmt,long udhi,long Msg_Length,BOOL bHandFree,const BYTE * Msg ,const char * Service_Id,long Fee_UserType,const char * FeeType, const char * FeeCode ,long Registered_Delivery,const char *ValId_Time,const char *At_Time,long lSendID,char * MsgID, BOOL bNo86)
|
||
{
|
||
if ( !m_bInitSmgp )
|
||
return -1; //接口未初始化或暂时断开连接
|
||
|
||
//m_lSendID ++; //发送ID加1
|
||
m_lSendID = lSendID; //使用传过来的ID
|
||
Smgp_Send_List list={0};
|
||
list.lSendID = m_lSendID; //SendID
|
||
list.SubmitEx.TP_Pid_Tag=ntohs(0x1);
|
||
list.SubmitEx.TP_Pid_Length=ntohs(1);
|
||
list.SubmitEx.TP_udhi_Tag=ntohs(0x2);
|
||
list.SubmitEx.TP_udhi_Length=ntohs(1);
|
||
list.SubmitEx.PkTotal_Tag=ntohs(0x9);
|
||
list.SubmitEx.PkTotal_Length=ntohs(1);
|
||
//list.SubmitEx.PkTotal=1;
|
||
list.SubmitEx.PkTotal=1;
|
||
list.SubmitEx.PkNumber_Tag=ntohs(0xA);
|
||
list.SubmitEx.PkNumber_Length=ntohs(1);
|
||
list.SubmitEx.PkNumber=1;
|
||
|
||
list.Submit.MsgType = 6; //*0=MO消息(终端发给SP);6=MT消息(SP发给终端,包括WEB上发送的点对点短消息);7=点对点短消息;*/
|
||
list.Submit.NeedReport = (unsigned char )Registered_Delivery;//状态报告
|
||
list.Submit.Priority = 1; //*0=低优先级;1=普通优先级;2=较高优先级;3=高优先级;*/
|
||
strcpy((char*)list.Submit.ServiceID,Service_Id); //业务类型
|
||
strcpy((char*)list.Submit.FeeType , FeeType );/*00=免费,此时FixedFee和FeeCode无效;01=按条计信息费,此时FeeCode表示每条费用,FixedFee无效;02=按包月收取信息费,此时FeeCode无效,FixedFee表示包月费用;03=按封顶收取信息费*/
|
||
strcpy((char*)list.Submit.FeeCode , FeeCode );
|
||
strcpy((char*)list.Submit.FixedFee, "0" );
|
||
//取原地址类型并转换
|
||
strcpy( (char*)list.Submit.SrcTermID , SendNo );
|
||
//计费号码
|
||
//GetMobilePhoneTON( RecvNo , (char*)list.Submit.ChargeTermID );
|
||
//strcpy((char*)list.Submit.ChargeNumber , "000000000000000000000" );
|
||
|
||
list.Submit.DestTermIDCount = 1; /*短消息接收号码总数(≤100),用于SP实现群发短消息*/
|
||
|
||
strcpy((char*)list.Submit.DestTermID,RecvNo);
|
||
//GetMobilePhoneTON( RecvNo , (char*)list.Submit.DestTermID ); //目标号码
|
||
|
||
list.SubmitEx.TP_Pid = 0; /*协议类型,缺省为0。*/
|
||
list.SubmitEx.TP_udhi = (UCHAR)udhi; /*用于长短消息,表示UserData是否是结构,0,正常,1结构*/
|
||
|
||
|
||
//定时时间
|
||
if ( At_Time )
|
||
{
|
||
//040916165000032+
|
||
sprintf( (char*)list.Submit.AtTime , At_Time);
|
||
}
|
||
//超时时间
|
||
if ( ValId_Time )
|
||
{
|
||
//040916165000032+
|
||
sprintf( (char*)list.Submit.ValidTime , ValId_Time);
|
||
}
|
||
|
||
char * pMsgSrc= (char*)Msg;
|
||
char * pMsgOK = (char*)list.Msg;
|
||
long ludhiLen=0;
|
||
if ( list.SubmitEx.TP_udhi == 1 )
|
||
{
|
||
memcpy(pMsgOK,pMsgSrc,6); //原字节头
|
||
|
||
pMsgSrc = pMsgSrc+6;
|
||
pMsgOK = pMsgOK+6; //采用6位数的短信分隔
|
||
ludhiLen = 6;
|
||
|
||
list.SubmitEx.PkTotal = (UCHAR)(Msg[4]);
|
||
list.SubmitEx.PkNumber= (UCHAR)(Msg[5]);
|
||
}
|
||
|
||
if ( Msg_Fmt==0 && Msg_Length==0 )
|
||
{
|
||
//取编码方式
|
||
//if ( isChinese((char*)pMsgSrc,strlen((char*)pMsgSrc) ) )
|
||
if ( true )
|
||
{
|
||
if ( bHandFree )
|
||
list.Submit.MsgFormat = 0x18;//UCS2编码
|
||
else
|
||
list.Submit.MsgFormat = 8;//UCS2编码
|
||
|
||
unsigned short * pusSrc = (unsigned short *)pMsgSrc;
|
||
unsigned short * pusTag = (unsigned short *)pMsgOK;
|
||
long lSrcLen = wcslen((WCHAR*)pMsgSrc);
|
||
if ( lSrcLen>(70-ludhiLen/2) ) lSrcLen=(70-ludhiLen/2);
|
||
for ( int i=0 ; i<lSrcLen ; i++ )
|
||
{
|
||
pusTag[i] = htons(pusSrc[i]);
|
||
}
|
||
|
||
//wcscpy((WCHAR*)pMsgOK,(WCHAR*)pMsgSrc);
|
||
//((WCHAR*)pMsgOK)[70]=0;
|
||
|
||
list.Submit.MsgLength = lSrcLen*2+ludhiLen;
|
||
}
|
||
else
|
||
{
|
||
if ( bHandFree )
|
||
list.Submit.MsgFormat = 0x10;//7Bit编码
|
||
else
|
||
list.Submit.MsgFormat = 0;//7Bit编码
|
||
char szBuf[1024]={0};
|
||
UCS2ToAscii((WCHAR*)pMsgSrc,szBuf);
|
||
char * pMsg = (char*)szBuf;
|
||
pMsg[160]=0; //防止短信超界
|
||
list.Submit.MsgLength = (UCHAR)(strlen((char*)pMsg)+ludhiLen);
|
||
strcpy((char*)pMsgOK,(char*)pMsg);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
/*
|
||
list.Submit.MsgFormat = (unsigned char)Msg_Fmt;//hex编码
|
||
list.Submit.MsgLength = (unsigned char)(Msg_Length/2);
|
||
HexToAscii((const char*)pMsgSrc,(char*)pMsgOK);
|
||
//memcpy(list.Msg,Msg,Msg_Length);
|
||
*/
|
||
//list.SubmitEx.TP_udhi = 0;
|
||
//list.SubmitEx.PkTotal=2;
|
||
//list.SubmitEx.PkNumber=1;
|
||
|
||
list.Submit.MsgFormat = (unsigned char)Msg_Fmt;//hex编码
|
||
if ( list.Submit.MsgFormat == 4 ) //Hex编码
|
||
{
|
||
list.Submit.MsgLength = (unsigned char)(Msg_Length/2);
|
||
HexToAscii((const char*)pMsgSrc,(char*)pMsgOK);
|
||
//memcpy(list.Msg,Msg,Msg_Length);
|
||
}
|
||
else
|
||
{
|
||
unsigned short * pusSrc = (unsigned short *)pMsgSrc;
|
||
unsigned short * pusTag = (unsigned short *)pMsgOK;
|
||
long lSrcLen = (long)wcslen((WCHAR*)pMsgSrc);
|
||
if ( lSrcLen>(70-ludhiLen/2) ) lSrcLen=(70-ludhiLen/2);
|
||
for ( int i=0 ; i<lSrcLen ; i++ )
|
||
{
|
||
pusTag[i] = htons(pusSrc[i]);
|
||
}
|
||
|
||
//wcscpy((WCHAR*)pMsgOK,(WCHAR*)pMsgSrc);
|
||
//((WCHAR*)pMsgOK)[70]=0;
|
||
list.Submit.MsgLength = (UCHAR)(lSrcLen*2+ludhiLen);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
return SendSms(list); //加入发送列表
|
||
/*
|
||
//以下检测发送返回
|
||
DWORD dwTime = ::GetTickCount();
|
||
while ( ::GetTickCount()-dwTime < SMGP_SENDSMS_TIMEOUT + 5000 )
|
||
{
|
||
Smgp_Send_Resp resp={0};
|
||
if ( GetSendResp(resp) &&
|
||
resp.lSendID == m_lSendID)
|
||
{
|
||
sprintf( MsgID , "%I64u" , resp.lMsgID );
|
||
return resp.lResult; //发送成功,返回cmpp的返回参数
|
||
}
|
||
Sleep_Lu( 100 );
|
||
}
|
||
return -3; //发送超时
|
||
*/
|
||
}
|
||
|
||
unsigned char CSmgp::GetMobilePhoneTON(const char *pSrc, char *pOrg)
|
||
{
|
||
char szTemp[32]={0};
|
||
long lBegin = 0;
|
||
long lLen = strlen( pSrc );
|
||
unsigned lTON = 2; //2,国内号码,1,国际号码
|
||
if ( pSrc[0] == '+')
|
||
lBegin = 1; //去除"+"号
|
||
if ( pSrc[lBegin] == '0' && pSrc[lBegin+1] == '0' )
|
||
lBegin += 2; //去除"00"
|
||
|
||
strcpy( szTemp , pSrc+lBegin ); //复制去除"+"号与"00"的号码
|
||
if ( szTemp[0]=='8' && szTemp[1] =='6' ) //所有号码都要加86开头
|
||
{
|
||
strcpy( pOrg,szTemp);
|
||
}
|
||
else
|
||
{
|
||
#ifdef SMGP_3_0
|
||
sprintf( pOrg , "%s",szTemp);
|
||
#else
|
||
sprintf( pOrg , "86%s",szTemp);
|
||
#endif
|
||
}
|
||
|
||
// if ( pSrc[lBegin]=='8' && pSrc[lBegin+1] =='6' ) //因为86开头,号码为国际码
|
||
// lTON = 1; //国际编码
|
||
|
||
|
||
return lTON;
|
||
}
|
||
|
||
BOOL CSmgp::isChinese(const TCHAR *pSrc, long lSrcLen)
|
||
{
|
||
long lLen = lSrcLen;
|
||
if ( lLen <=0 )
|
||
lLen = _tcslen(pSrc);
|
||
|
||
#ifdef _UNICODE
|
||
for ( long i=0 ; i<lLen ; i++ )
|
||
{
|
||
if ( (BYTE)pSrc[i]>128 )
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
#else
|
||
char szBuf[4096];
|
||
long l = MultiByteToWideChar( CP_ACP,NULL,pSrc,lLen, (LPWSTR)szBuf , sizeof(szBuf) );
|
||
if ( l == lLen )
|
||
return false;
|
||
else
|
||
return true;
|
||
#endif
|
||
}
|
||
|
||
long CSmgp::UCS2ToAscii( const WCHAR * pUCS2 , char * pAscii)
|
||
{
|
||
long lLen = wcslen( pUCS2 );
|
||
long l = WideCharToMultiByte(CP_ACP,NULL,pUCS2,lLen, pAscii , 4096 ,NULL,NULL);
|
||
pAscii[l]=0;
|
||
return l;
|
||
}
|
||
long CSmgp::AsciiToUCS2(const char *pAscii, WCHAR *pUCS2)
|
||
{
|
||
long lLen = strlen( pAscii );
|
||
long l = MultiByteToWideChar( CP_ACP,NULL,pAscii,lLen, pUCS2 , 4096 );
|
||
pUCS2[l]=0;
|
||
return l;
|
||
// szName2[l*2]=0;
|
||
/*
|
||
AsciiToHex( szName2 , pUCS2 , l*2);
|
||
ExchangeUniCode( pUCS2 );
|
||
*/
|
||
}
|
||
|
||
void CSmgp::HexToAscii(const char *pszOrgRandom, char *pszDesRandom)
|
||
{
|
||
char Buf[4];
|
||
char *pDes = (char *)pszDesRandom;
|
||
char *pOrg = (char *)pszOrgRandom;
|
||
long lLen = strlen( pOrg);
|
||
long lTemp;
|
||
for( int i = 0; i < lLen/2; ++i )
|
||
{
|
||
memcpy( Buf , pOrg+i*2 , 2 );
|
||
Buf[2] = 0;
|
||
lTemp = CharHex(Buf[0])*16 + CharHex(Buf[1]);
|
||
pDes[i] = (char)lTemp;
|
||
}
|
||
pDes[i] = 0;
|
||
}
|
||
|
||
void CSmgp::ExchangeUniCode(char *pData)
|
||
{
|
||
char pEx[4];
|
||
long lLen = strlen( pData );
|
||
for ( int i = 0 ; i< lLen/4 ; i++ )
|
||
{
|
||
memcpy( pEx , pData+i*4 , 2 );
|
||
memcpy( pData+i*4 , pData+i*4+2 , 2 );
|
||
memcpy( pData+i*4+2 , pEx , 2 );
|
||
}
|
||
}
|
||
|
||
void CSmgp::AsciiToHex(const char *pszOrgRandom, TCHAR *pszDesRandom, long lLen)
|
||
{
|
||
#ifdef _UNICODE
|
||
TCHAR *p = (TCHAR *)pszDesRandom;
|
||
//long lLen = lstrlen( (char*)pszOrgRandom);
|
||
for( long i = 0; i < lLen; ++i )
|
||
{
|
||
_stprintf(p, _T("%02X"), (BYTE)pszOrgRandom[i] );
|
||
p += 2;
|
||
}
|
||
*p=0;
|
||
|
||
#else
|
||
char *p = (char *)pszDesRandom;
|
||
//long lLen = lstrlen( (char*)pszOrgRandom);
|
||
for( long i = 0; i < lLen; ++i )
|
||
{
|
||
sprintf(p, "%02X", (BYTE)pszOrgRandom[i] );
|
||
p += 2;
|
||
}
|
||
*p=0;
|
||
#endif
|
||
}
|
||
|
||
int CSmgp::CharHex(char ch)
|
||
{
|
||
//if( isdigit( ch ) ) return( atoi( &ch) );
|
||
if( ch >='0' && ch <= '9' ) return( atoi( &ch) );
|
||
else {
|
||
if( ch == 'a' || ch == 'A' ) return( 10 );
|
||
if( ch == 'b' || ch == 'B' ) return( 11 );
|
||
if( ch == 'c' || ch == 'C' ) return( 12 );
|
||
if( ch == 'd' || ch == 'D' ) return( 13 );
|
||
if( ch == 'e' || ch == 'E' ) return( 14 );
|
||
if( ch == 'f' || ch == 'F' ) return( 15 );
|
||
}
|
||
return( 0 );
|
||
}
|
||
|
||
BOOL CSmgp::GetStatus(Smgp_Deliver_List &deliver)
|
||
{
|
||
EnterCriticalSection(&m_CriStatusList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lStatusList ; i< SMGP_SMS_MAX_STATUS; i++ )
|
||
{
|
||
if ( m_pStatusList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lStatusList = i+1;
|
||
deliver = m_pStatusList[i];
|
||
m_pStatusList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lStatusList; i++ )
|
||
{
|
||
if ( m_pStatusList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lStatusList = i+1;
|
||
deliver = m_pStatusList[i];
|
||
m_pStatusList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
m_lStatusList = 0;
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::Status_Add(Smgp_Deliver_List Deliver)
|
||
{
|
||
EnterCriticalSection(&m_CriStatusList); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lStatusList ; i< SMGP_SMS_MAX_STATUS; i++ )
|
||
{
|
||
if ( m_pStatusList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pStatusList[i] = Deliver;
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lStatusList; i++ )
|
||
{
|
||
if ( m_pStatusList[i].lSendID == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pStatusList[i] = Deliver;
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriStatusList); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
long CSmgp::Smgp_Get(TCHAR *SendNo,TCHAR * RecvNo, BYTE *Msg, TCHAR *Msg_Fmt, TCHAR *Msg_Length,long & lLongMsgRand ,long & lLongMsgAll, long & lLongMsgIndex)
|
||
{
|
||
Smgp_Deliver_List deliver={0};
|
||
|
||
if ( GetDeliver(deliver) )
|
||
{
|
||
_tcsncpy( SendNo , CA2W((char*)deliver.deliver.SrcTermID ),21);
|
||
_tcsncpy( RecvNo , CA2W((char*)deliver.deliver.DestTermID ),21);
|
||
BYTE * pMsg = (BYTE *)deliver.szMsg;
|
||
long lMsgLen=deliver.deliver.MsgLength;
|
||
|
||
//if ( deliver.deliver.TP_udhi == 1 ) //判断是否长短信
|
||
//{
|
||
if ( pMsg[0]==5 && pMsg[1]==0 && pMsg[2]==3 )
|
||
{
|
||
lLongMsgRand = pMsg[3];
|
||
lLongMsgAll = pMsg[4];
|
||
lLongMsgIndex = pMsg[5];
|
||
|
||
pMsg = pMsg+6;
|
||
lMsgLen -=6;
|
||
}
|
||
if ( pMsg[0]==6 && pMsg[1]==8 && pMsg[2]==4 )
|
||
{
|
||
lLongMsgRand = pMsg[3]*100+pMsg[4];
|
||
lLongMsgAll = pMsg[5];
|
||
lLongMsgIndex = pMsg[6];
|
||
|
||
pMsg = pMsg+7;
|
||
lMsgLen -=7;
|
||
}
|
||
//}
|
||
|
||
if ( Msg_Fmt==NULL && Msg_Length==NULL ) //自动取内容
|
||
{
|
||
if (deliver.deliver.MsgFormat == 15 ) //gbk编码
|
||
{
|
||
_tcscpy((WCHAR*)Msg,CA2W((char*)pMsg));
|
||
}
|
||
else
|
||
{
|
||
if (deliver.deliver.MsgFormat & 8 ) //ucs2,要编码
|
||
{
|
||
unsigned short * pusSrc = (unsigned short *)pMsg;
|
||
unsigned short * pusTag = (unsigned short *)Msg;
|
||
long lSrcLen = lMsgLen/2;
|
||
for ( int i=0 ; i<lSrcLen ; i++ )
|
||
{
|
||
pusTag[i] = ntohs(pusSrc[i]);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (deliver.deliver.MsgFormat & 4 )// hex编码
|
||
{
|
||
_tcscpy((TCHAR*)Msg,_T("HEX16:"));
|
||
if ( deliver.deliver.MsgLength>70 )
|
||
deliver.deliver.MsgLength=70;
|
||
AsciiToHex((const char*)pMsg,((TCHAR*)Msg)+6,lMsgLen);
|
||
}
|
||
else
|
||
{
|
||
_tcscpy((WCHAR*)Msg,CA2W((char*)pMsg));
|
||
//AsciiToUCS2((const char*)pMsg , (TCHAR*)Msg);
|
||
//memcpy(Msg,pMsg,lMsgLen);
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
AsciiToHex((const char*)deliver.szMsg,(TCHAR*)Msg,deliver.deliver.MsgLength); //转换成Hex编码
|
||
//memcpy(Msg,deliver.szMsg,deliver.deliver.Msg_Length);
|
||
if ( Msg_Fmt )
|
||
_stprintf( Msg_Fmt , _T("%d") , deliver.deliver.MsgFormat );
|
||
if ( Msg_Length )
|
||
_stprintf( Msg_Length , _T("%d") , deliver.deliver.MsgFormat*2 );
|
||
return 0;
|
||
}
|
||
}
|
||
return -1; //没有信息
|
||
}
|
||
|
||
long CSmgp::Smgp_GetReport(TCHAR *No, TCHAR *Stat, TCHAR *Done_time, TCHAR *MsgID)
|
||
{
|
||
Smgp_Deliver_List deliver={0};
|
||
|
||
if ( GetStatus(deliver) )
|
||
{
|
||
_tcscpy( No , CA2W((char*)deliver.deliver.SrcTermID) );
|
||
//strcpy( Stat , (char*)deliver.status.Stat );
|
||
_tcsncpy( Stat , CA2W((char*)deliver.status.Stat) , 7 );
|
||
//strcpy( Done_time , (char*)deliver.status.Done_time );
|
||
_tcsncpy( Done_time , CA2W((char*)deliver.status.done_date) , 10 );
|
||
PrintMsgID(MsgID , deliver.status.MsgID );
|
||
return 0;
|
||
}
|
||
return -1; //没有信息
|
||
}
|
||
|
||
BOOL CSmgp::GetSmgpExchangeResp(SmgpExchangeResp &resp)
|
||
{
|
||
EnterCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lSmgpExchangeRespList ; i< SMGP_SMS_MAX_SMGPEXCHANGERESP; i++ )
|
||
{
|
||
if ( m_pSmgpExchangeResp[i].lLen != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lSmgpExchangeRespList = i+1;
|
||
resp = m_pSmgpExchangeResp[i];
|
||
m_pSmgpExchangeResp[i].lLen = 0;
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lSmgpExchangeRespList; i++ )
|
||
{
|
||
if ( m_pSmgpExchangeResp[i].lLen != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_lSmgpExchangeRespList = i+1;
|
||
resp = m_pSmgpExchangeResp[i];
|
||
m_pSmgpExchangeResp[i].lLen = 0;
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
m_lRespList = 0;
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::SmgpExchangeResp_Add(SmgpExchangeResp resp)
|
||
{
|
||
EnterCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
//从当前点到结尾
|
||
for ( int i=m_lSmgpExchangeRespList ; i< SMGP_SMS_MAX_SMGPEXCHANGERESP; i++ )
|
||
{
|
||
if ( m_pSmgpExchangeResp[i].lLen == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pSmgpExchangeResp[i] = resp;
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for ( i=0 ; i<m_lSmgpExchangeRespList; i++ )
|
||
{
|
||
if ( m_pSmgpExchangeResp[i].lLen == 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
m_pSmgpExchangeResp[i] = resp;
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriSmgpExchangeResp); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
void CSmgp::SetSmgpExchangeNum(CmppExchange * pSmgpExchange , long lSmgpExchange)
|
||
{
|
||
m_pSmgpExchange = pSmgpExchange;
|
||
m_lSmgpExchange = lSmgpExchange;
|
||
}
|
||
|
||
void CSmgp::SetSendSpeed(long lSpeed)
|
||
{
|
||
if (lSpeed <= 0)
|
||
{
|
||
m_lSendInterval = 0;
|
||
m_lSendIntervalCount = 1;
|
||
m_lSendIntervalIndex = 0;
|
||
}
|
||
else
|
||
{
|
||
double dwInterval = ((double)1000 / (double)lSpeed) * 0.9f;
|
||
m_lSendInterval = (long)(dwInterval + 0.5f);
|
||
if (m_lSendInterval >= 15)
|
||
{
|
||
m_lSendIntervalCount = 1;
|
||
m_lSendIntervalIndex = 0;
|
||
}
|
||
else
|
||
{
|
||
m_lSendInterval = 15;
|
||
m_lSendIntervalCount = (int)((double)m_lSendInterval / dwInterval + 0.5f);
|
||
if (m_lSendIntervalCount * dwInterval > m_lSendInterval)
|
||
m_lSendInterval = (int)(m_lSendIntervalCount * dwInterval + 0.5f);
|
||
}
|
||
}
|
||
}
|
||
|
||
long CSmgp::GetWFSms()
|
||
{
|
||
long lWF=0;
|
||
//从当前点到结尾
|
||
for ( int i=0 ; i< SMGP_SMS_MAX_LIST; i++ )
|
||
{
|
||
if ( m_pSendList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
lWF++;
|
||
}
|
||
}
|
||
return lWF;
|
||
}
|
||
|
||
TCHAR * CSmgp::PrintMsgID(TCHAR *pMsgID, UCHAR lMsgID[])
|
||
{
|
||
for ( int i=0 ; i<10 ; i++ )
|
||
{
|
||
_stprintf(pMsgID+i*2 , _T("%02X") , lMsgID[i]);
|
||
}
|
||
return pMsgID;
|
||
}
|
||
|
||
long CSmgp::GetReportSms()
|
||
{
|
||
long lReport=0;
|
||
//从当前点到结尾
|
||
for ( int i=0 ; i< SMGP_SMS_MAX_STATUS; i++ )
|
||
{
|
||
if ( m_pStatusList[i].lSendID != 0 ) //其等于0时,认为此位置为空
|
||
{
|
||
lReport++;
|
||
}
|
||
}
|
||
return lReport;
|
||
}
|
||
|
||
|
||
CString CSmgp::GetErrMsg()
|
||
{
|
||
EnterCriticalSection(&m_CriErrMsg); //防止冲突
|
||
|
||
CString strRet;
|
||
if ( m_strErrMsg.GetLength()>0 )
|
||
{
|
||
strRet = m_strErrMsg;
|
||
m_strErrMsg = CString(_T(""));
|
||
}
|
||
LeaveCriticalSection(&m_CriErrMsg); //防止冲突
|
||
return strRet;
|
||
}
|
||
|
||
void CSmgp::SetErrMsg(CString str)
|
||
{
|
||
EnterCriticalSection(&m_CriErrMsg); //防止冲突
|
||
|
||
m_strErrMsg = str;
|
||
|
||
LeaveCriticalSection(&m_CriErrMsg); //防止冲突
|
||
}
|
||
|
||
BOOL CSmgp::isConnectGateWay()
|
||
{
|
||
if ( m_bLoginSmgp == 0 )
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
|
||
BOOL CSmgp::Sms_Get(Smgp_Send_List*& pSms)
|
||
{
|
||
EnterCriticalSection(&m_CriSendSms); //防止冲突
|
||
//从当前点到结尾
|
||
int i = 0;
|
||
for (i = m_lSendList; i < SMGP_SMS_MAX_LIST; i++)
|
||
{
|
||
if (m_pSendList[i].lSendID != 0) //其等于0时,认为此位置为空
|
||
{
|
||
m_lSendList = i + 1;
|
||
pSms = &m_pSendList[i];
|
||
//m_pSendList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for (i = 0; i < m_lSendList; i++)
|
||
{
|
||
if (m_pSendList[i].lSendID != 0) //其等于0时,认为此位置为空
|
||
{
|
||
m_lSendList = i + 1;
|
||
pSms = &m_pSendList[i];
|
||
//m_pSendList[i].lSendID = 0;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
m_lSendList = 0;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return false;
|
||
}
|
||
|
||
BOOL CSmgp::Sms_Add(Smgp_Send_List sms)
|
||
{
|
||
EnterCriticalSection(&m_CriSendSms); //防止冲突
|
||
//从当前点到结尾
|
||
int i = 0;
|
||
for (i = m_lSendList; i < SMGP_SMS_MAX_LIST; i++)
|
||
{
|
||
if (m_pSendList[i].lSendID == 0) //其等于0时,认为此位置为空
|
||
{
|
||
m_pSendList[i] = sms;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
//从开头到当前点
|
||
for (i = 0; i < m_lSendList; i++)
|
||
{
|
||
if (m_pSendList[i].lSendID == 0) //其等于0时,认为此位置为空
|
||
{
|
||
m_pSendList[i] = sms;
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return true;
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_CriSendSms); //防止冲突
|
||
return false;
|
||
} |