?? gatewayproxyweb.cpp
字號(hào):
/////////////////////////////////////////////////////////////////////
// Project : GatewayProxy
// File: GatewayProxyWeb.c
// web module of this project
// 2005/03/27
/////////////////////////////////////////////////////////////////////
//---------------- Inculde ------------------
#include <AEESSL.h>
#include "GatewayProxyWeb.h"
#include "AppsViewer.h"
//-------------------------------------------
#define HTTP_PROXY_SERVER "http://192.168.1.9:8080/jmsn/servlet/Proxy" //通過(guò)Http服務(wù)器代理
#define WEB_RETRYCOUNT 1
CGatewayProxyWeb::CGatewayProxyWeb(CAppsViewer *pApp)
{
//初始化類指針
m_pShell = NULL;
m_pWeb = new CWeb();
m_pWebResp = new CWebResp();
m_pSourceUtil = new CSourceUtil();
m_pSource = new CSource();
m_pApp = pApp;
//初始化緩沖區(qū)
m_pbRecvBuff = NULL; //接收數(shù)據(jù)緩沖區(qū)
m_nRecvSize = 0;
m_nRecvMaxSize = 0;
m_pszSendBuff = NULL; //發(fā)送緩沖區(qū)
m_pszUrl = NULL; //連接的Url
m_bBusy = FALSE;
//初始化回調(diào)
CALLBACK_Init(&m_cbWebResp, GotWebResp, this);
CALLBACK_Init(&m_cbRead, ReadSource, this);
}
CGatewayProxyWeb::~CGatewayProxyWeb(void)
{
//清除Web的回調(diào)函數(shù)調(diào)用
CALLBACK_Cancel(&m_cbWebResp);
CALLBACK_Cancel(&m_cbRead);
//
if(m_pWeb)
delete m_pWeb;
//
if(m_pWebResp)
delete m_pWebResp;
//
if(m_pSourceUtil)
delete m_pSourceUtil;
//
if(m_pSource)
delete m_pSource;
//清除Timer
if(m_pShell)
m_pShell->CancelTimer(NULL, this);
//釋放緩沖區(qū)
FREEIF(m_pbRecvBuff);
FREEIF(m_pszSendBuff);
FREEIF(m_pszUrl);
}
//-------------------------------------------------------------------
// Writer class operator new
//-------------------------------------------------------------------
void* CGatewayProxyWeb::operator new(unsigned int size)
{
return MALLOC(size);
}
//-------------------------------------------------------------------
// Writer class operator delete
//-------------------------------------------------------------------
void CGatewayProxyWeb::operator delete(void * ptr)
{
FREE(ptr);
}
// 創(chuàng)建WebAction對(duì)象
boolean CGatewayProxyWeb::Create(CShell *pShell, CWebUtil *pWebUtil)
{
//指針判斷
if(!pShell || !pWebUtil || !m_pWeb || !m_pWebResp || !m_pSourceUtil || !m_pSource)
return FALSE;
//
m_pShell = pShell;
m_pWebUtil = pWebUtil;
//創(chuàng)建SourceUtil實(shí)例
if(!m_pSourceUtil->GetHandle() && pShell->CreateInstance(AEECLSID_SOURCEUTIL, m_pSourceUtil) != SUCCESS)
return FALSE;
//
m_nRecvSize = 0;
return TRUE;
}
// 創(chuàng)建WebAction中的CWeb對(duì)象
boolean CGatewayProxyWeb::CreateWeb()
{
if(!m_pShell || !m_pWeb)
return FALSE;
//刪除以前的句柄
if(m_pWeb->GetHandle())
m_pWeb->Release();
//創(chuàng)建Web句柄
if(m_pShell->CreateInstance(AEECLSID_WEB, m_pWeb) != SUCCESS)
return FALSE;
//
return TRUE;
}
// 釋放網(wǎng)絡(luò)連接
void CGatewayProxyWeb::ReleaseWeb()
{
//清除Web的回調(diào)函數(shù)調(diào)用
CALLBACK_Cancel(&m_cbWebResp);
CALLBACK_Cancel(&m_cbRead);
//釋放WebResp
if(m_pWebResp->GetHandle())
m_pWebResp->Release();
//釋放Web
if(m_pWeb->GetHandle())
m_pWeb->Release();
//清除定時(shí)器
m_pShell->CancelTimer(NULL, this);
}
// 設(shè)置Web連接的Url
boolean CGatewayProxyWeb::SetUrl(const char * pszUrl)
{
#ifdef _DEBUG
if(!pszUrl)
{
DBGPRINTF("CGatewayProxyWeb::SetUrl param error");
return FALSE;
}
#endif
//重新分配保存URL
FREE(m_pszUrl);
m_pszUrl = STRDUP(pszUrl);
if(!m_pszUrl)
return FALSE;
//是否需要重新組織URL
#ifdef PROXY_WITH_HTTP
return ReArrangeUrl();
#else
return TRUE;
#endif
}
// 進(jìn)行Web連接
boolean CGatewayProxyWeb::WebConnect(const char * pszWeb, char *pszSendBuff)
{
//設(shè)置發(fā)送緩沖區(qū)
FREEIF(m_pszSendBuff);
m_pszSendBuff = pszSendBuff;
//制作Url
if(!SetUrl(pszWeb))
return FALSE;
//初始化發(fā)送次數(shù)
m_nSendCount = WEB_RETRYCOUNT;
//調(diào)用內(nèi)部函數(shù)
return HttpWebConnect();
}
// 進(jìn)行安全Web連接
boolean CGatewayProxyWeb::SSLWebConnect(const char *pszUrl, char *pszSendBuff)
{
//設(shè)置發(fā)送緩沖區(qū)
FREEIF(m_pszSendBuff);
m_pszSendBuff = pszSendBuff;
//設(shè)置Url
if(!SetUrl(pszUrl))
return FALSE;
//初始化發(fā)送次數(shù)
m_nSendCount = WEB_RETRYCOUNT;
#ifdef _DEBUG
DBGPRINTF("Https,Url : %s", m_pszUrl);
// OutputDebug(m_pszSendBuff, TRUE);
#endif
//調(diào)用內(nèi)部函數(shù)
return SSLHttpWebConnect();
}
// 內(nèi)部的普通HttpWeb連接
boolean CGatewayProxyWeb::HttpWebConnect()
{
//組織參數(shù)
WebOpt wo[3];
int nIndex = 0;
//Header回調(diào)函數(shù)參數(shù)指針
//wo[nIndex].nId = WEBOPT_HANDLERDATA;
//wo[nIndex++].pVal = this;
//
//wo[nIndex].nId = WEBOPT_USERAGENT;
//wo[nIndex++].pVal = NULL;
//Method
wo[nIndex].nId = WEBOPT_METHOD;
wo[nIndex++].pVal = (void *)"GET";
wo[nIndex].nId = WEBOPT_HEADER;
wo[nIndex++].pVal = (void*)"X-Method: GET\r\n";
//發(fā)送的數(shù)據(jù)長(zhǎng)度
// wo[nIndex].nId = WEBOPT_CONTENTLENGTH;
// wo[nIndex++].pVal = (void *)STRLEN(m_pszSendBuff);
//釋放原來(lái)的ISource
if(m_pSource->GetHandle())
m_pSource->Release();
//創(chuàng)建ISource接口
// if(m_pSourceUtil->SourceFromMemory(m_pszSendBuff, STRLEN(m_pszSendBuff), NULL, NULL, m_pSource) != SUCCESS)
// return FALSE;
//發(fā)送的數(shù)據(jù)
// wo[nIndex].nId = WEBOPT_BODY;
// wo[nIndex++].pVal = m_pSource->GetHandle();
//結(jié)束
wo[nIndex].nId = WEBOPT_END;
//獲取Web回應(yīng)
GetResponse(wo);
return TRUE;
}
// 內(nèi)部的安全HttpWeb連接
boolean CGatewayProxyWeb::SSLHttpWebConnect()
{
//組織參數(shù)
WebOpt wo[4];
int nIndex = 0;
//SSL參數(shù)
wo[nIndex].nId = WEBOPT_SSL_TRUST_MODE;
wo[nIndex++].pVal = (void*)SSL_TRUST_MODE_IGNORE;
/*
//Header回調(diào)函數(shù)參數(shù)指針
wo[nIndex].nId = WEBOPT_HANDLERDATA;
wo[nIndex++].pVal = this;
//
wo[nIndex].nId = WEBOPT_USERAGENT;
wo[nIndex++].pVal = NULL;
*/
//Method
wo[nIndex].nId = WEBOPT_METHOD;
wo[nIndex++].pVal = (void *)"GET";
wo[nIndex].nId = WEBOPT_HEADER;
wo[nIndex++].pVal = (void*)"X-Method: GET\r\n";
//發(fā)送的數(shù)據(jù)長(zhǎng)度
// wo[nIndex].nId = WEBOPT_CONTENTLENGTH;
// wo[nIndex++].pVal = (void *)STRLEN(m_pszSendBuff);
//釋放原來(lái)的ISource
// if(m_pSource->GetHandle())
// m_pSource->Release();
//創(chuàng)建ISource接口
// if(m_pSourceUtil->SourceFromMemory(m_pszSendBuff, STRLEN(m_pszSendBuff), NULL, NULL, m_pSource) != SUCCESS)
// return FALSE;
//發(fā)送的數(shù)據(jù)
// wo[nIndex].nId = WEBOPT_BODY;
// wo[nIndex++].pVal = m_pSource->GetHandle();
//結(jié)束
wo[nIndex].nId = WEBOPT_END;
//獲取Web回應(yīng)
GetResponse(wo);
return TRUE;
}
// 獲取Web回應(yīng)
void CGatewayProxyWeb::GetResponse(WebOpt *pWebOpt)
{
//釋放原來(lái)的WebResp
if(m_pWebResp->GetHandle())
m_pWebResp->Release();
//
// AECHAR szTemp[200], szTitle[] = {'t', 0};
// STRTOWSTR(m_pszUrl, szTemp, 100);
// m_pShell->MessageBoxText(szTitle, szTemp);
// return;
//
m_pWeb->GetResponseV(m_pWebResp, &m_cbWebResp, m_pszUrl, pWebOpt);
//發(fā)送次數(shù)減去1
m_nSendCount -= 1;
//設(shè)置當(dāng)前繁忙
m_bBusy = TRUE;
//
m_nError = SUCCESS;
//釋放接收緩沖區(qū)
FREEIF(m_pbRecvBuff);
m_nHeaderSize = 0;
}
//處理Web接收
void CGatewayProxyWeb::OnWebResp()
{
#ifdef _DEBUG
DBGPRINTF("Web response");
if(m_pszHeaderBuff && STRLEN(m_pszHeaderBuff) > 0)
DBGPRINTF(m_pszHeaderBuff);
#endif
//內(nèi)存不足?
if(m_nError != SUCCESS)
{
EndWeb(EVT_APP_WEBERROR, m_nError);
return;
}
//
WebRespInfo* pWebRespInfo = m_pWebResp->GetInfo();
ISource* pISource = pWebRespInfo->pisMessage;
if(!pISource || pWebRespInfo->lContentLength == -1)
{
//重發(fā)了WEB_RETRYCOUNT次?
if(m_nSendCount > 0)
{
//延時(shí)1s重發(fā)
int nReturn = m_pShell->SetTimer((WEB_RETRYCOUNT - m_nSendCount) * 1000, (PFNNOTIFY)ReSend, this);
if(nReturn != SUCCESS)
EndWeb(EVT_APP_WEBERROR, nReturn);
else
{
#ifdef _DEBUG
DBGPRINTF("Web Error %x, Resend!", WEB_ERROR_MAP(pWebRespInfo->nCode));
#endif
}
return;
}
//重發(fā)了WEB_RETRYCOUNT次,還錯(cuò)誤,提示用戶
EndWeb(EVT_APP_WEBERROR, WEB_ERROR_MAP(pWebRespInfo->nCode));
return;
}
//分配接收緩沖區(qū)內(nèi)存
if(!m_pbRecvBuff)
{
m_pbRecvBuff = (byte *)MALLOC(pWebRespInfo->lContentLength + 1);
if(!m_pbRecvBuff)
{
//清除Web的回調(diào)函數(shù)調(diào)用
EndWeb(EVT_APP_WEBERROR, ENOMEMORY);
return;
}
m_nRecvMaxSize = uint16(pWebRespInfo->lContentLength + 1);
m_nRecvSize = 0;
}
//讀取數(shù)據(jù)
OnReadSource();
}
// 讀取Web返回的數(shù)據(jù)
void CGatewayProxyWeb::OnReadSource()
{
WebRespInfo* pWebRespInfo = m_pWebResp->GetInfo();
ISource* pISource = pWebRespInfo->pisMessage;
// 讀數(shù)據(jù)
int nByteCount = ISOURCE_Read(pISource, (char*)(m_pbRecvBuff + m_nRecvSize), pWebRespInfo->lContentLength - m_nRecvSize);
//根據(jù)返回值進(jìn)行處理
switch (nByteCount)
{
case ISOURCE_WAIT: // 繼續(xù)讀?
ISOURCE_Readable(pISource, &m_cbRead);
return;
case ISOURCE_ERROR: // 錯(cuò)誤?
#ifdef _DEBUG
DBGPRINTF("ISource Read Error!");
#endif
EndWeb(EVT_APP_WEBERROR, EFAILED);
return;
case ISOURCE_END:
HandleRecv();
return;
default:
//接收字節(jié)數(shù)
m_nRecvSize = m_nRecvSize + (uint16)nByteCount;
//已經(jīng)接收結(jié)束?
if(m_nRecvSize == pWebRespInfo->lContentLength)
HandleRecv();
else //繼續(xù)讀
ISOURCE_Readable(pISource, &m_cbRead);
return;
}
}
// 處理接收到的數(shù)據(jù)
void CGatewayProxyWeb::HandleRecv()
{
WebRespInfo* pWebRespInfo = m_pWebResp->GetInfo(); //Web Response Info
//釋放內(nèi)存(URL)
FREEIF(m_pszUrl);
//釋放內(nèi)存(Send Buffer)
FREEIF(m_pszSendBuff);
//成功?
if(pWebRespInfo->nCode == 200)
EndWeb(EVT_APP_WEBSUCCEED, m_nRecvSize, (uint32)m_pbRecvBuff);
else
EndWeb(EVT_APP_WEBERROR, WEB_ERROR_MAP(pWebRespInfo->nCode), pWebRespInfo->nCode);
}
// 重發(fā)數(shù)據(jù)
void CGatewayProxyWeb::OnReSend()
{
boolean bReturn;
bReturn = HttpWebConnect();
if(bReturn != TRUE)
EndWeb(EVT_APP_WEBERROR, ENOMEMORY);
}
// 結(jié)束Web調(diào)用,返回結(jié)果
void CGatewayProxyWeb::EndWeb(AEEEvent evt, uint32 dwParam1, uint32 dwParam2)
{
//清除Web的回調(diào)函數(shù)調(diào)用
CALLBACK_Cancel(&m_cbWebResp);
CALLBACK_Cancel(&m_cbRead);
//釋放WebResp
if(m_pWebResp->GetHandle())
m_pWebResp->Release();
//
m_bBusy = FALSE;
//返回
if(m_pApp)
m_pApp->OnWebEnd(evt, (uint16)dwParam1, dwParam2);
}
// 復(fù)制接收到的數(shù)據(jù)到指定緩沖區(qū),如果緩沖區(qū)太小,則擴(kuò)大緩沖區(qū)
boolean CGatewayProxyWeb::CopyRecv(char *&pszRecvBuff, int &nOrigDataSize, int &nBuffSize)
{
//模塊變量判斷
if(!m_pbRecvBuff || m_nRecvSize == 0)
return TRUE;
//是否需要重新分配內(nèi)存?
if(nOrigDataSize + m_nRecvSize >= nBuffSize)
{
int nNewSize = nOrigDataSize + m_nRecvSize + 1;
char *pszTemp = (char *)REALLOC(pszRecvBuff, nNewSize);
if(!pszTemp)
return FALSE;
//設(shè)置新的內(nèi)存
pszRecvBuff = pszTemp;
nBuffSize = nNewSize;
}
//復(fù)制收到的數(shù)據(jù)
MEMCPY(pszRecvBuff + nOrigDataSize, m_pbRecvBuff, m_nRecvSize);
nOrigDataSize += m_nRecvSize;
//釋放Web接收
FREEIF(m_pbRecvBuff);
m_nRecvSize = 0;
return TRUE;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -