📄 gatewayproxyweb.cpp
字号:
/////////////////////////////////////////////////////////////////////
// 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" //通过Http服务器代理
#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;
//初始化缓冲区
m_pbRecvBuff = NULL; //接收数据缓冲区
m_nRecvSize = 0;
m_nRecvMaxSize = 0;
m_pszSendBuff = NULL; //发送缓冲区
m_pszUrl = NULL; //连接的Url
m_bBusy = FALSE;
//初始化回调
CALLBACK_Init(&m_cbWebResp, GotWebResp, this);
CALLBACK_Init(&m_cbRead, ReadSource, this);
}
CGatewayProxyWeb::~CGatewayProxyWeb(void)
{
//清除Web的回调函数调用
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);
//释放缓冲区
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);
}
// 创建WebAction对象
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;
//创建SourceUtil实例
if(!m_pSourceUtil->GetHandle() && pShell->CreateInstance(AEECLSID_SOURCEUTIL, m_pSourceUtil) != SUCCESS)
return FALSE;
//
m_nRecvSize = 0;
return TRUE;
}
// 创建WebAction中的CWeb对象
boolean CGatewayProxyWeb::CreateWeb()
{
if(!m_pShell || !m_pWeb)
return FALSE;
//删除以前的句柄
if(m_pWeb->GetHandle())
m_pWeb->Release();
//创建Web句柄
if(m_pShell->CreateInstance(AEECLSID_WEB, m_pWeb) != SUCCESS)
return FALSE;
//
return TRUE;
}
// 释放网络连接
void CGatewayProxyWeb::ReleaseWeb()
{
//清除Web的回调函数调用
CALLBACK_Cancel(&m_cbWebResp);
CALLBACK_Cancel(&m_cbRead);
//释放WebResp
if(m_pWebResp->GetHandle())
m_pWebResp->Release();
//释放Web
if(m_pWeb->GetHandle())
m_pWeb->Release();
//清除定时器
m_pShell->CancelTimer(NULL, this);
}
// 设置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
}
// 进行Web连接
boolean CGatewayProxyWeb::WebConnect(const char * pszWeb, char *pszSendBuff)
{
//设置发送缓冲区
FREEIF(m_pszSendBuff);
m_pszSendBuff = pszSendBuff;
//制作Url
if(!SetUrl(pszWeb))
return FALSE;
//初始化发送次数
m_nSendCount = WEB_RETRYCOUNT;
//调用内部函数
return HttpWebConnect();
}
// 进行安全Web连接
boolean CGatewayProxyWeb::SSLWebConnect(const char *pszUrl, char *pszSendBuff)
{
//设置发送缓冲区
FREEIF(m_pszSendBuff);
m_pszSendBuff = pszSendBuff;
//设置Url
if(!SetUrl(pszUrl))
return FALSE;
//初始化发送次数
m_nSendCount = WEB_RETRYCOUNT;
#ifdef _DEBUG
DBGPRINTF("Https,Url : %s", m_pszUrl);
// OutputDebug(m_pszSendBuff, TRUE);
#endif
//调用内部函数
return SSLHttpWebConnect();
}
// 内部的普通HttpWeb连接
boolean CGatewayProxyWeb::HttpWebConnect()
{
//组织参数
WebOpt wo[3];
int nIndex = 0;
//Header回调函数参数指针
//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";
//发送的数据长度
// wo[nIndex].nId = WEBOPT_CONTENTLENGTH;
// wo[nIndex++].pVal = (void *)STRLEN(m_pszSendBuff);
//释放原来的ISource
if(m_pSource->GetHandle())
m_pSource->Release();
//创建ISource接口
// if(m_pSourceUtil->SourceFromMemory(m_pszSendBuff, STRLEN(m_pszSendBuff), NULL, NULL, m_pSource) != SUCCESS)
// return FALSE;
//发送的数据
// wo[nIndex].nId = WEBOPT_BODY;
// wo[nIndex++].pVal = m_pSource->GetHandle();
//结束
wo[nIndex].nId = WEBOPT_END;
//获取Web回应
GetResponse(wo);
return TRUE;
}
// 内部的安全HttpWeb连接
boolean CGatewayProxyWeb::SSLHttpWebConnect()
{
//组织参数
WebOpt wo[4];
int nIndex = 0;
//SSL参数
wo[nIndex].nId = WEBOPT_SSL_TRUST_MODE;
wo[nIndex++].pVal = (void*)SSL_TRUST_MODE_IGNORE;
/*
//Header回调函数参数指针
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";
//发送的数据长度
// wo[nIndex].nId = WEBOPT_CONTENTLENGTH;
// wo[nIndex++].pVal = (void *)STRLEN(m_pszSendBuff);
//释放原来的ISource
// if(m_pSource->GetHandle())
// m_pSource->Release();
//创建ISource接口
// if(m_pSourceUtil->SourceFromMemory(m_pszSendBuff, STRLEN(m_pszSendBuff), NULL, NULL, m_pSource) != SUCCESS)
// return FALSE;
//发送的数据
// wo[nIndex].nId = WEBOPT_BODY;
// wo[nIndex++].pVal = m_pSource->GetHandle();
//结束
wo[nIndex].nId = WEBOPT_END;
//获取Web回应
GetResponse(wo);
return TRUE;
}
// 获取Web回应
void CGatewayProxyWeb::GetResponse(WebOpt *pWebOpt)
{
//释放原来的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);
//发送次数减去1
m_nSendCount -= 1;
//设置当前繁忙
m_bBusy = TRUE;
//
m_nError = SUCCESS;
//释放接收缓冲区
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
//内存不足?
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)
{
//重发了WEB_RETRYCOUNT次?
if(m_nSendCount > 0)
{
//延时1s重发
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;
}
//重发了WEB_RETRYCOUNT次,还错误,提示用户
EndWeb(EVT_APP_WEBERROR, WEB_ERROR_MAP(pWebRespInfo->nCode));
return;
}
//分配接收缓冲区内存
if(!m_pbRecvBuff)
{
m_pbRecvBuff = (byte *)MALLOC(pWebRespInfo->lContentLength + 1);
if(!m_pbRecvBuff)
{
//清除Web的回调函数调用
EndWeb(EVT_APP_WEBERROR, ENOMEMORY);
return;
}
m_nRecvMaxSize = uint16(pWebRespInfo->lContentLength + 1);
m_nRecvSize = 0;
}
//读取数据
OnReadSource();
}
// 读取Web返回的数据
void CGatewayProxyWeb::OnReadSource()
{
WebRespInfo* pWebRespInfo = m_pWebResp->GetInfo();
ISource* pISource = pWebRespInfo->pisMessage;
// 读数据
int nByteCount = ISOURCE_Read(pISource, (char*)(m_pbRecvBuff + m_nRecvSize), pWebRespInfo->lContentLength - m_nRecvSize);
//根据返回值进行处理
switch (nByteCount)
{
case ISOURCE_WAIT: // 继续读?
ISOURCE_Readable(pISource, &m_cbRead);
return;
case ISOURCE_ERROR: // 错误?
#ifdef _DEBUG
DBGPRINTF("ISource Read Error!");
#endif
EndWeb(EVT_APP_WEBERROR, EFAILED);
return;
case ISOURCE_END:
HandleRecv();
return;
default:
//接收字节数
m_nRecvSize = m_nRecvSize + (uint16)nByteCount;
//已经接收结束?
if(m_nRecvSize == pWebRespInfo->lContentLength)
HandleRecv();
else //继续读
ISOURCE_Readable(pISource, &m_cbRead);
return;
}
}
// 处理接收到的数据
void CGatewayProxyWeb::HandleRecv()
{
WebRespInfo* pWebRespInfo = m_pWebResp->GetInfo(); //Web Response Info
//释放内存(URL)
FREEIF(m_pszUrl);
//释放内存(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);
}
// 重发数据
void CGatewayProxyWeb::OnReSend()
{
boolean bReturn;
bReturn = HttpWebConnect();
if(bReturn != TRUE)
EndWeb(EVT_APP_WEBERROR, ENOMEMORY);
}
// 结束Web调用,返回结果
void CGatewayProxyWeb::EndWeb(AEEEvent evt, uint32 dwParam1, uint32 dwParam2)
{
//清除Web的回调函数调用
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);
}
// 复制接收到的数据到指定缓冲区,如果缓冲区太小,则扩大缓冲区
boolean CGatewayProxyWeb::CopyRecv(char *&pszRecvBuff, int &nOrigDataSize, int &nBuffSize)
{
//模块变量判断
if(!m_pbRecvBuff || m_nRecvSize == 0)
return TRUE;
//是否需要重新分配内存?
if(nOrigDataSize + m_nRecvSize >= nBuffSize)
{
int nNewSize = nOrigDataSize + m_nRecvSize + 1;
char *pszTemp = (char *)REALLOC(pszRecvBuff, nNewSize);
if(!pszTemp)
return FALSE;
//设置新的内存
pszRecvBuff = pszTemp;
nBuffSize = nNewSize;
}
//复制收到的数据
MEMCPY(pszRecvBuff + nOrigDataSize, m_pbRecvBuff, m_nRecvSize);
nOrigDataSize += m_nRecvSize;
//释放Web接收
FREEIF(m_pbRecvBuff);
m_nRecvSize = 0;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -