FXSend/MMS_mm7_Ex/IMm7MMSServiceEx.cpp
2025-02-28 17:05:50 +08:00

628 lines
15 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// IMm7MMSServiceEx.cpp: implementation of the IMm7MMSServiceEx class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IMm7MMSServiceEx.h"
#include "..\FXSend.h"
#include "..\FXSendDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace IMm7MMS {
IMm7MMSServiceEx::IMm7MMSServiceEx()
{
m_pDlg = NULL;
m_hThread = NULL;
m_bStart = false;
m_pSQL = NULL;
m_Critical = NULL;
m_lSQLCount=0;
m_bCopy = false;
}
IMm7MMSServiceEx::~IMm7MMSServiceEx()
{
Web_Exit();
}
IMm7MMSServiceEx *IMm7MMSServiceEx::copy2()
{
IMm7MMSServiceEx *dup = new IMm7MMSServiceEx();
dup->destroy(); //笨办法因为copy会复制一份soap原来的soap资源需要先释放
soap_copy_context(dup, this);
//复制部分参数
dup->m_pDlg = this->m_pDlg;
dup->m_strSQL_IP = this->m_strSQL_IP;
dup->m_strSQL_User = this->m_strSQL_User;
dup->m_strSQL_Passwd = this->m_strSQL_Passwd;
dup->m_strSQL_DB = this->m_strSQL_DB;
dup->m_strSQL_Provider = this->m_strSQL_Provider;
dup->m_pSQL = this->m_pSQL;
dup->m_lSQLCount = this->m_lSQLCount;
dup->m_Critical = this->m_Critical;
dup->m_bCopy = true; //特殊,标识是复制的类
//解决中文乱码问题
//soap_set_mode(dup,SOAP_C_UTFSTRING);
//dup->mode|=SOAP_C_UTFSTRING;
return dup;
}
long IMm7MMSServiceEx::Web_Init(TCHAR * strIP, long lPort)
{
char * pIP = NULL;
char szIP[64]={0};
if ( strIP && _tcslen(strIP)>8 )
{
strcpy(szIP , CW2A(strIP));
pIP = szIP;
}
if ( !soap_valid_socket(bind(pIP,lPort,100)) ) //100为同时处理的数量
{
//soap_print_fault(&soap, stderr);
return -1;
}
//解决中文乱码问题
soap_set_mode(this,SOAP_C_UTFSTRING);
this->mode|=SOAP_C_UTFSTRING;
/*
m_Critical = new CRITICAL_SECTION;
InitializeCriticalSection(m_Critical);
m_lSQLCount = 10;
m_pSQL = new SQL_Pool[m_lSQLCount];
for ( int i=0 ; i<m_lSQLCount ; i++ )
{
m_pSQL[i].bUse = false;
m_pSQL[i].dwBeginUse = 0;
m_pSQL[i].dwEndUse = 0;
m_pSQL[i].lRandID = 0;
//m_pSQL[i].spDoc = NULL;
}
*/
this->accept_timeout = 1000; //accept time out
this->connect_timeout = 1000; //accept time out
this->recv_timeout = 5000;
this->send_timeout = 5000;
this->fget = Web_Get;
this->fpost = Web_post;
DWORD ID=0;
m_hThread = CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)Process_AcceptT , (LPVOID)this , 0 , &ID );
m_bStart = true;
return 0;
}
UINT IMm7MMSServiceEx::Process_AcceptT(LPVOID sParam)
{
IMm7MMSServiceEx * pCtcc = (IMm7MMSServiceEx *)sParam;
while(pCtcc->m_bStart)
{
SOAP_SOCKET s = pCtcc->accept();
if (soap_valid_socket(s))
{
//fprintf(stderr,"Accepted connection from IP= %d.%d.%d.%d socket = %d ",
// ((ServerSoap.ip)>>24)&&0xFF,((ServerSoap.ip)>>16)&0xFF,((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF,(ServerSoap.socket));
IMm7MMSServiceEx * pNewCtcc = (IMm7MMSServiceEx *)pCtcc->copy2();
if ( pNewCtcc )
{
pNewCtcc->socket = s;
QueueUserWorkItem(Process_Req, (PVOID)pNewCtcc, WT_EXECUTEDEFAULT); //WT_EXECUTELONGFUNCTION
}
}
else
{
//接收连接出错
}
}
return 0;
}
DWORD WINAPI IMm7MMSServiceEx::Process_Req(LPVOID lpParam)
{
IMm7MMSServiceEx * pCtcc = (IMm7MMSServiceEx *)lpParam;
pCtcc->serve(); //处理数据
soap_destroy(pCtcc);
soap_end(pCtcc);
soap_done(pCtcc);
delete pCtcc;
return 0;
}
IMm7MMSServiceEx::SQL_Pool * IMm7MMSServiceEx::GetBankSQL()
{
if ( !m_pSQL )
return NULL;
for ( int j=0 ; j<10 ; j++ )
{
EnterCriticalSection(m_Critical);
for ( int i=0 ; i<m_lSQLCount ; i++ )
{
if ( !m_pSQL[i].bUse && m_pSQL[i].lRandID==0 &&
m_pSQL[i].adoConnection.isOK() )
{
m_pSQL[i].bUse = true;
m_pSQL[i].lRandID = rand();
m_pSQL[i].dwBeginUse = GetTickCount();
//if ( !m_pSQL[i].spDoc )
// m_pSQL[i].spDoc.CoCreateInstance(__uuidof(DOMDocument40));
if ( ConnectSQLServer(&m_pSQL[i]) ) //连接数据库
{
LeaveCriticalSection(m_Critical);
return &m_pSQL[i];
}
}
}
LeaveCriticalSection(m_Critical);
Sleep(500);
}
return NULL;
}
BOOL IMm7MMSServiceEx::ConnectSQLServer(IMm7MMSServiceEx::SQL_Pool *pSql)
{
if ( !pSql )
return false;
//if ( !pSql->spDoc )
//{
// pSql->spDoc.CoCreateInstance(__uuidof(DOMDocument40));
//}
if( pSql->adoConnection.IsOpen() )
{
return true;
}
for ( int i=0 ; i<3; i++ ) //重试3次看能否连上数据库
{
BOOL b = pSql->adoConnection.ConnectSQLServer2(m_strSQL_IP,m_strSQL_DB,m_strSQL_User,m_strSQL_Passwd, m_strSQL_Provider);
if ( b )
{
pSql->adoConnection.SetCommandTimeout(7200); //设置连接超时时间
pSql->adoConnection.SetCursorLocation(adUseClient); //设置为本地游标类型
return true;
}
Sleep(2000);
}
return false;
}
void IMm7MMSServiceEx::SetSQLParam(CDialog * pDlg,CString strIP, CString strUser, CString strPasswd, CString strDB,CString strProvider)
{
m_pDlg = pDlg;
m_strSQL_IP =strIP;
m_strSQL_User=strUser;
m_strSQL_Passwd=strPasswd;
m_strSQL_DB=strDB;
m_strSQL_Provider = strProvider;
}
long IMm7MMSServiceEx::Web_Exit()
{
if ( m_bCopy ) //如果只是复制的,不用释放资源。
return true;
m_bStart = false;
if ( m_hThread != INVALID_HANDLE_VALUE)
{
soap_destroy(this);
soap_end(this);
soap_done(this);
WaitForSingleObject(m_hThread , 10000 ); //等待10秒
m_hThread = INVALID_HANDLE_VALUE;
}
if ( m_pSQL )
{
EnterCriticalSection(m_Critical);
for (int i=0 ; i<m_lSQLCount ; i++ )
{
m_pSQL[i].adoConnection.Close();
//if ( m_pSQL[i].spDoc )
//{
// m_pSQL[i].spDoc.Release();
// m_pSQL[i].spDoc = NULL;
//}
}
delete []m_pSQL;
m_pSQL=NULL;
LeaveCriticalSection(m_Critical);
}
if ( m_Critical )
{
DeleteCriticalSection(m_Critical);
delete m_Critical;
m_Critical = NULL;
}
return true;
}
int IMm7MMSServiceEx::Web_post(struct soap *soap, const char *endpoint, const char *host, int port,const char *path, const char *action, size_t count)
{
return Web_Get( soap ); // 简单处理直接调用http_get
}
int IMm7MMSServiceEx::Web_Get(struct soap* soap)
{
// 请求WSDL时传送相应文件
// 获取请求的wsdl文件名
CString fielPath(soap->path);
fielPath.MakeUpper();
CStringA strRet;
strRet = " <HTML> MobsetApi WebServices! </HTML> ";
if ( fielPath.Find(_T("WSDL"))>0 )
{
TCHAR RootDirectory[_MAX_PATH]={0};
GetCurrentPath(RootDirectory);
CString strFileName = CString(RootDirectory)+CString(_T("\\IMm7MMSService.wsdl"));
CFile file;
if ( file.Open(strFileName , CFile::modeRead) )
{
char * p = new char[file.GetLength()];
memset(p , 0 , file.GetLength());
file.Read(p , file.GetLength());
CStringA str(p,file.GetLength());
file.Close();
strRet = str;
//return 404;
}
}
soap_response(soap, SOAP_HTML);
soap_send(soap, strRet );
soap_end_send(soap);
return SOAP_OK;
}
void IMm7MMSServiceEx::GetCurrentPath(TCHAR *pPath)
{
try
{
::GetModuleFileName( NULL , pPath , 512);
int j=lstrlen(pPath);
for ( ; j>0 ; j-- )
{
if (pPath[j]=='\\' )
break;
}
pPath[j+1]=0;
}
catch(...)
{
}
}
int mm7SoapBindingService::submitReq(ns1__submitReqType *ns1__SubmitReq, ns1__submitRspType *ns1__SubmitRsp)
{
return 0;
}
int mm7SoapBindingService::deliverReq(ns1__deliverReqType *ns1__DeliverReq, ns1__deliverRspType *ns1__DeliverRsp)
{
return 0;
}
int mm7SoapBindingService::deliveryReportReq(ns1__deliveryReportReqType *ns1__DeliveryReportReq, ns1__genericResponseType *ns1__DeliveryReportRsp)
{
return 0;
}
int IMm7MMSServiceEx::submitReq(ns1__submitReqType *ns1__SubmitReq, ns1__submitRspType *ns1__SubmitRsp)
{
return 0;
}
BOOL IMm7MMSServiceEx::InsertMms(FZ_Send & mms,RecvSms_MmsAttach & mmsAttach,soap_multipart::iterator attachment)
{
try
{
BYTE * pData = (BYTE*)(*attachment).ptr;
long lSize = (*attachment).size;
long lEncoding = (*attachment).encoding;
CString strType = ((*attachment).type?(*attachment).type:"");
CString strID = ((*attachment).id?(*attachment).id:"");
CString strLocation = ((*attachment).location?(*attachment).location:"");
CString strDescription = ((*attachment).description?(*attachment).description:"");
//复制附件数据
mmsAttach.lSize = lSize;
mmsAttach.pData = new BYTE[lSize];
memcpy(mmsAttach.pData , pData , lSize );
//复件附件参数
CString strName=strLocation;
if ( strName.Find(_T("."))<0 && strID.GetLength()>0)
strName = strID;
strName.Replace(_T(">") , _T("") );
strName.Replace(_T("<") , _T("") );
_tcsncpy(mmsAttach.szID , strID,sizeof(mmsAttach.szID)/sizeof(TCHAR)-1 );
_tcsncpy(mmsAttach.szName , strName,sizeof(mmsAttach.szName)/sizeof(TCHAR)-1 );
_tcsncpy(mmsAttach.szType , strType,sizeof(mmsAttach.szType)/sizeof(TCHAR)-1 );
_tcsncpy(mmsAttach.szLocation , strLocation,sizeof(mmsAttach.szLocation)/sizeof(TCHAR)-1 );
mms.lmmSize += lSize;
return true;
}
catch(...)
{
}
return false;
}
int IMm7MMSServiceEx::deliverReq(ns1__deliverReqType *ns1__DeliverReq, ns1__deliverRspType *ns1__DeliverRsp)
{
try
{
ns1__DeliverRsp->MM7Version = ns1__versionType__6_x002e3_x002e0;
ns1__DeliverRsp->Status = soap_new_ns1__responseStatusType(this,-1);
ns1__DeliverRsp->Status->StatusCode = L"1000";
ns1__DeliverRsp->Status->StatusText = L"处理成功";
CString strID;
if ( ns1__DeliverReq->LinkedID )
strID=ns1__DeliverReq->LinkedID->c_str();
CString strMobile;
if (ns1__DeliverReq->Sender && ns1__DeliverReq->Sender->__item)
strMobile=ns1__DeliverReq->Sender->__item;
CString strRecvNum;
try
{
if ( ns1__DeliverReq->Recipients && ns1__DeliverReq->Recipients->__size_recipientsType_sequence>0 )
{
for ( int i=0 ; i<ns1__DeliverReq->Recipients->__size_recipientsType_sequence ; i++ )
{
__ns1__recipientsType_sequence * pSeq = &ns1__DeliverReq->Recipients->__recipientsType_sequence[i];
if ( pSeq && pSeq->union_recipientsType.To )
{
if ( pSeq->union_recipientsType.To->__size_multiAddressType_sequence >0 )
{
strRecvNum = pSeq->union_recipientsType.To->__multiAddressType_sequence->union_multiAddressType.Number->__item.c_str();
if ( strRecvNum.GetLength()>3 )
break;
}
}
}
}
}
catch(...)
{
}
CString strSubject;
if ( ns1__DeliverReq->Subject )
strSubject = ns1__DeliverReq->Subject->c_str();
/*
const CStringA utf8 = CW2A(unicode1, CP_UTF8);
ASSERT(utf8.GetLength() > unicode1.GetLength());
const CStringW unicode2 = CA2W(utf8, CP_UTF8);
ASSERT(unicode1 == unicode2);
*/
//CFXSendDlg::ConvertUtf8ToGBK(strSubject); //转换编码
FZ_Send mms={0};
_tcscpy(mms.szSendNum , strMobile);
_tcscpy(mms.Mobile[0].szMobile, strRecvNum);
_tcscpy(mms.szMsg , strSubject );
mms.lMmsAttach = 20;//默认最大20个附件
/*
//找出附件数量
for (soap_multipart::iterator attachment = ns1__DeliverReq->soap->mime.begin(); attachment != ns1__DeliverReq->soap->mime.end(); ++attachment)
{
BYTE * pData = (BYTE*)(*attachment).ptr;
long lSize = (*attachment).size;
if ( pData>0 && lSize>0 )
{
mms.lMmsAttach ++;
}
}
*/
mms.pMmsAttach = new RecvSms_MmsAttach[mms.lMmsAttach];
memset(mms.pMmsAttach , 0 , sizeof(RecvSms_MmsAttach)*mms.lMmsAttach );
long lIndex = 0;
for (soap_multipart::iterator attachment = ns1__DeliverReq->soap->mime.begin(); attachment != ns1__DeliverReq->soap->mime.end(); ++attachment)
{
BYTE * pData = (BYTE*)(*attachment).ptr;
long lSize = (*attachment).size;
if ( pData>0 && lSize>0 )
{
BOOL bSubAttach=false;
//以下为小于65K数据的读取与分析Mime方法如果要读取大于65K的数据构想的方法如下
//1、新建frecv方法。//size_t (*frecv)(struct soap*, char*, size_t);
//2、替换psoap->frecv方法
//3、在frecv方法中使用pdata读取数据
if ( lSize<SOAP_BUFLEN)
{
soap * psoap2 = this;
//soap * psoap = SOAP_NEW(soap);
//soap * psoap = soap_copy(this);
soap * psoap = new soap;
//memset(psoap,0,sizeof(soap));
psoap->bufidx=0;
psoap->buflen = lSize;
psoap->mode|= SOAP_ENC_MIME;
memcpy(psoap->buf,pData,lSize);
soap_getmime(psoap);
psoap->mime.list = psoap->mime.first;
psoap->mime.first = NULL;
psoap->mime.last = NULL;
psoap->mime.boundary = NULL;
/*
if (psoap->xlist)
{ struct soap_multipart *content;
for (content = psoap->mime.list; content; content = content->next)
soap_resolve_attachment(psoap, content);
}
*/
for (soap_multipart::iterator attachment21 = psoap->mime.begin(); attachment21 != psoap->mime.end(); ++attachment21)
{
InsertMms(mms,mms.pMmsAttach[lIndex],attachment21);
bSubAttach = true;
lIndex ++;
if (lIndex>=mms.lMmsAttach ) //超过容量
{
break;
}
}
soap_destroy(psoap);
soap_end(psoap);
soap_done(psoap);
delete psoap;
}
if ( !bSubAttach)
{
lIndex ++;
InsertMms(mms,mms.pMmsAttach[lIndex],attachment);
}
if (lIndex>=mms.lMmsAttach ) //超过容量
{
break;
}
}
}
mms.lMmsAttach = lIndex; //最后实际数量
if ( m_pDlg )
((CFXSendDlg*)m_pDlg)->Public_RecvSms_Add(mms); //加入返回列表
}
catch(...)
{
}
//Sleep(10000);
return 0;
}
int IMm7MMSServiceEx::deliveryReportReq(ns1__deliveryReportReqType *ns1__DeliveryReportReq, ns1__genericResponseType *ns1__DeliveryReportRsp)
{
try
{
ns1__DeliveryReportRsp->MM7Version = ns1__versionType__6_x002e3_x002e0;
ns1__DeliveryReportRsp->Status = soap_new_ns1__responseStatusType(this,-1);
ns1__DeliveryReportRsp->Status->StatusCode = L"1000";
ns1__DeliveryReportRsp->Status->StatusText = L"处理成功";
CString strID;
strID = ns1__DeliveryReportReq->MessageID.c_str();
CString strMobile;
if ( ns1__DeliveryReportReq->Recipient->__size_multiAddressType_sequence>0 && ns1__DeliveryReportReq->Recipient->__multiAddressType_sequence)
{
//strMobile = ns1__DeliveryReportReq->Recipient->__multiAddressType_sequence->union_multiAddressType.Number->__item.c_str();
__ns1__multiAddressType_sequence * pSeq = &ns1__DeliveryReportReq->Recipient->__multiAddressType_sequence[0];
if ( pSeq && pSeq->union_multiAddressType.Number )
{
strMobile = pSeq->union_multiAddressType.Number->__item.c_str();
}
}
CString strStatus=_T("unknow");
switch(ns1__DeliveryReportReq->MMStatus)
{
case ns1__mmDeliveryStatusType__Expired:
strStatus=_T("Expired");
break;
case ns1__mmDeliveryStatusType__Retrieved:
strStatus=_T("Retrieved");
break;
case ns1__mmDeliveryStatusType__Indeterminate:
strStatus=_T("Indeterminate");
break;
case ns1__mmDeliveryStatusType__Forwarded:
strStatus=_T("Forwarded");
break;
}
MT_Report report2={0};
_tcscpy(report2.szMsgID , strID );
_tcscpy(report2.szMobile , strMobile );
_tcscpy(report2.szStatus , strStatus );
if ( strStatus == CString(_T("Retrieved")) || strStatus.GetLength()<=0 )
{
report2.lReportOK = 1;
}
else
{
report2.lReportOK = 0;
}
if ( m_pDlg )
((CFXSendDlg*)m_pDlg)->Public_Report_Add(report2); //加入返回列表
}
catch(...)
{
}
return 0;
}
};