// IagwMmsServiceEx.cpp: implementation of the IagwMmsServiceEx class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IagwMmsServiceEx.h"
#include "..\FXSend.h"
#include "..\FXSendDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include "../../../public/CMarkup/Markup.h"
namespace IAGWMMS {
IagwMmsServiceEx::IagwMmsServiceEx()
{
m_pDlg = NULL;
m_hThread = NULL;
m_bStart = false;
m_pSQL = NULL;
m_Critical = NULL;
m_lSQLCount=0;
m_bCopy = false;
}
IagwMmsServiceEx::~IagwMmsServiceEx()
{
Web_Exit();
}
int capServiceSoapBindingService::ExecuteInvoke(std::wstring ns1__requsetXml, std::wstring &ns1__ExecuteInvokeReturn)
{
return 0;
}
int capServiceSoapBindingService::EncodeString(std::wstring ns1__decode, std::wstring &ns1__EncodeStringReturn)
{
return 0;
}
int IagwMmsServiceEx::ExecuteInvoke(std::wstring ns1__requsetXml, std::wstring &ns1__ExecuteInvokeReturn)
{
try
{
if ( ns1__requsetXml.c_str())
{
CString strReqXml = ns1__requsetXml.c_str();
CMarkup xml(strReqXml);
if ( xml.FindElem(_T("/Package/ParamItems")) && xml.IntoElem())
{
CString SendAddress,MsgId,ReturnType,Status,Subject,AccessCode;
while ( xml.FindElem( _T("ParamItem") ) )
{
//xml.IntoElem();
CString ParamID,ParamValue;
if ( xml.FindChildElem( _T("ParamID") ) )
{
xml.IntoElem();
ParamID = xml.GetData();
xml.OutOfElem();
xml.ResetChildPos();
}
if ( xml.FindChildElem( _T("ParamValue") ) )
{
xml.IntoElem();
ParamValue = xml.GetData();
xml.OutOfElem();
xml.ResetChildPos();
}
if ( ParamID == CString(_T("SendAddress")))
{
SendAddress = ParamValue;
SendAddress.MakeLower();
SendAddress.Replace(_T("tel:") , _T(""));
}
if ( ParamID == CString(_T("MsgId")))
{
MsgId = ParamValue;
}
if ( ParamID == CString(_T("ReturnType")))
{
ReturnType = ParamValue;
}
if ( ParamID == CString(_T("Status")))
{
Status = ParamValue;
}
if ( ParamID == CString(_T("Subject")))
{
Subject = ParamValue;
}
if ( ParamID == CString(_T("AccessCode")))
{
AccessCode = ParamValue;
}
}
//处理状态报告
if (ReturnType == CString(_T("Receipt")) ) //是状态报告
{
MT_Report report2={0};
//FZ_Send send={0};
//send.lReSendCount = -1234; //特殊情况
_tcscpy(report2.szMsgID , MsgId );
_tcscpy(report2.szMobile , SendAddress );
_tcscpy(report2.szStatus , Status );
//if ( (Status==CString(_T("DeliveryImpossible")) || Status == CString(_T("DELIVRD")) ) || Status.GetLength()<=0 )
if ( (Status==CString(_T("DeliveredToTerminal")) || Status == CString(_T("DELIVRD")) ) || Status.GetLength()<=0 )
{
report2.lReportOK = 1;
}
else
{
report2.lReportOK = 0;
}
if ( m_pDlg )
((CFXSendDlg*)m_pDlg)->Public_Report_Add(report2); //加入返回列表
}
else
{
//不是状态报告的话,可能会是接收到上行彩信,要继续判断
if ( Subject.GetLength()>=0 || AccessCode.GetLength()>=0 )
{
//处理上行彩信
FZ_Send mms={0};
_tcscpy(mms.szSendNum , SendAddress);
_tcscpy(mms.Mobile[0].szMobile, AccessCode);
_tcscpy(mms.szMsg , Subject );
mms.lMmsAttach = 0;
//找出附件数量
for (soap_multipart::iterator attachment = mime.begin(); attachment != 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 attachment2 = mime.begin(); attachment2 != mime.end(); ++attachment2)
{
BYTE * pData = (BYTE*)(*attachment2).ptr;
long lSize = (*attachment2).size;
long lEncoding = (*attachment2).encoding;
CString strType = ((*attachment2).type?(*attachment2).type:"");
CString strID = ((*attachment2).id?(*attachment2).id:"");
CString strLocation = ((*attachment2).location?(*attachment2).location:"");
CString strDescription = ((*attachment2).description?(*attachment2).description:"");
if ( pData>0 && lSize>0 )
{
//复制附件数据
mms.pMmsAttach[lIndex].lSize = lSize;
mms.pMmsAttach[lIndex].pData = new BYTE[lSize];
memcpy(mms.pMmsAttach[lIndex].pData , pData , lSize );
//复件附件参数
CString strName=strLocation;
if ( strName.Find(_T("."))<0 && strID.GetLength()>0)
strName = strID;
strName.Replace(_T(">") , _T("") );
strName.Replace(_T("<") , _T("") );
_tcscpy(mms.pMmsAttach[lIndex].szID , strID );
_tcscpy(mms.pMmsAttach[lIndex].szName , strName );
_tcscpy(mms.pMmsAttach[lIndex].szType , strType );
_tcscpy(mms.pMmsAttach[lIndex].szLocation , strLocation );
mms.lmmSize += lSize;
lIndex ++;
if (lIndex>=mms.lMmsAttach ) //超过容量
{
break;
}
}
}
mms.lMmsAttach = lIndex; //最后实际数量
if ( m_pDlg )
((CFXSendDlg*)m_pDlg)->Public_RecvSms_Add(mms); //加入返回列表
}
}
}
CString strRet=_T("")\
_T("")\
_T("%s")\
_T("%s")\
_T("0000")\
_T("Sucess")\
_T("3")\
_T("");
CString TimeStamp;
SYSTEMTIME t;GetSystemTime(&t);
TimeStamp.Format(_T("%04d%02d%02d%02d%02d%02d%03d"),t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute,t.wSecond,t.wMilliseconds);
CString strRet2;
strRet2.Format(strRet,TimeStamp,TimeStamp);
ns1__ExecuteInvokeReturn = strRet2;
}
}
catch(...)
{
}
return 0;
}
int IagwMmsServiceEx::EncodeString(std::wstring ns1__decode, std::wstring &ns1__EncodeStringReturn)
{
return 0;
}
IagwMmsServiceEx *IagwMmsServiceEx::copy2()
{
IagwMmsServiceEx *dup = new IagwMmsServiceEx();
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 IagwMmsServiceEx::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 ; iaccept_timeout = 1000; //accept time out
this->connect_timeout = 1000; //accept time out
this->recv_timeout = 5000;
this->send_timeout = 5000;
DWORD ID=0;
m_hThread = CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)Process_AcceptT , (LPVOID)this , 0 , &ID );
m_bStart = true;
return 0;
}
UINT IagwMmsServiceEx::Process_AcceptT(LPVOID sParam)
{
IagwMmsServiceEx * pCtcc = (IagwMmsServiceEx *)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));
IagwMmsServiceEx * pNewCtcc = (IagwMmsServiceEx *)pCtcc->copy2();
if ( pNewCtcc )
{
pNewCtcc->socket = s;
QueueUserWorkItem(Process_Req, (PVOID)pNewCtcc, WT_EXECUTEDEFAULT); //WT_EXECUTELONGFUNCTION
}
}
else
{
//接收连接出错
}
}
return 0;
}
DWORD WINAPI IagwMmsServiceEx::Process_Req(LPVOID lpParam)
{
IagwMmsServiceEx * pCtcc = (IagwMmsServiceEx *)lpParam;
pCtcc->serve(); //处理数据
soap_destroy(pCtcc);
soap_end(pCtcc);
soap_done(pCtcc);
delete pCtcc;
return 0;
}
IagwMmsServiceEx::SQL_Pool * IagwMmsServiceEx::GetBankSQL()
{
if ( !m_pSQL )
return NULL;
for ( int j=0 ; j<10 ; j++ )
{
EnterCriticalSection(m_Critical);
for ( int i=0 ; ispDoc )
//{
// 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 IagwMmsServiceEx::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 IagwMmsServiceEx::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 ; ipath);
fielPath.MakeUpper();
CStringA strRet;
strRet = " MobsetApi WebServices! ";
if ( fielPath.Find(_T("WSDL"))>0 )
{
TCHAR RootDirectory[_MAX_PATH]={0};
GetCurrentPath(RootDirectory);
CString strFileName = CString(RootDirectory)+CString(_T("\\IagwMmsService.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 IagwMmsServiceEx::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(...)
{
}
}
};