📄 network.cpp
字号:
// NetWork.cpp: implementation of the CNetWork class.
//
//////////////////////////////////////////////////////////////////////
#include "NetWork.h"
#include "wk_BaseFunc.h"
#include "Object.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNetWork::CNetWork()
{
}
CNetWork::~CNetWork()
{
}
void CNetWork::Init(AEEApplet * pApplet, AEEDeviceInfo *pDevice)
{
m_pApplet = pApplet;
m_pDevice = pDevice;
SETAEERECT(&m_rcScreen, 0, 0, m_pDevice->cxScreen,m_pDevice->cyScreen);
m_pObject = NULL;
m_pIWeb = NULL;
m_pIWebResp = NULL;
m_pIWebOpts = NULL;
m_pIPeek = NULL;
m_pIStatic = NULL;
m_BodyBuffer = NULL;
m_BodySize = 0;
m_BodyAllocSize = 0;
m_TotalSize = 0;
m_bFirst = TRUE;
LoadTextMessageSingleByte(m_pApplet->m_pIShell, IDS_URL_REAL, &m_szUrl, RES_FILE);
m_szError = NULL;
m_nTimeout = 0;
m_nPic = 0;
m_bReady = FALSE;
}
void CNetWork::Release()
{
WebCleanup();
CALLBACK_Cancel(&m_cb);
ReleaseObj((void * *) &m_pIStatic);
if (m_szUrl) FREEIF(m_szUrl);
if (m_szError) FREEIF(m_szError);
}
void CNetWork::WebCleanup()
{
CALLBACK_Cancel(&m_Callback);
ReleaseObj((void * *) &m_pIWeb);
ReleaseObj((void * *) &m_pIWebResp);
ReleaseObj((void * *) &m_pIWebOpts);
ReleaseObj((void * *) &m_pIPeek);
m_pIWeb = NULL;
m_pIWebResp = NULL;
m_pIWebOpts = NULL;
m_pIPeek = NULL;
if (m_BodyBuffer) FREEIF(m_BodyBuffer);
m_BodySize = 0;
m_BodyAllocSize = 0;
}
void CNetWork::PostData(char * szPostData, long nBufferLength, void * pObject)
{
int i = -1;
m_pObject = (CObject *)pObject;
if (m_pIStatic)
ReleaseObj((void * *) &m_pIStatic);
/*
if (ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_STATIC, (void * *)(&m_pIStatic)) == SUCCESS)
{
AEERect rcStatic;
IImage * pImage;
AEEImageInfo info;
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_DOWN1);
rcStatic = m_rcScreen;
if(pImage)
{
IIMAGE_GetInfo(pImage, &info);
rcStatic.x = 0;
rcStatic.y = (m_pDevice->cyScreen+info.cy)/2;
rcStatic.dy = m_rcScreen.dy-rcStatic.y;
IIMAGE_Release(pImage);
pImage = NULL;
}
ISTATIC_SetRect(m_pIStatic, &rcStatic);
ISTATIC_SetProperties(m_pIStatic, ST_CENTERTITLE / *| ST_NOSCROLL* / | ST_MIDDLETEXT | ST_CENTERTEXT ); //ST_ASCII |
}
*/
// init buffer
m_BodySize = 0;
m_TotalSize = 0;
if (m_BodyBuffer)
FREEIF(m_BodyBuffer);
m_BodyBuffer = NULL;
if (szPostData)
{
// to create a IPeek to hold the response data
ISourceUtil * pisu;
i = ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_SOURCEUTIL, (void * *) & pisu);
if (SUCCESS == i)
{
i = ISOURCEUTIL_PeekFromMemory(pisu, szPostData, nBufferLength, 0, 0, &m_pIPeek);
ISOURCEUTIL_Release(pisu);
}
pisu = NULL;
}
// Create an instance of IWeb
i = ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_WEB, (void * *) (&m_pIWeb));
if (i != SUCCESS)
{
// if connection failed
WebCleanup();
return;
}
else
{
m_BodyBuffer = NULL;
m_BodySize = 0;
m_BodyAllocSize = 0;
CALLBACK_Init(&m_Callback, (PFNNOTIFY)WebReadCallBack, this);
i = ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_SSLROOTCERTS, (void * *) (&m_pIWebOpts));
if (m_pIPeek)
{ // with post data
IWEB_GetResponse(m_pIWeb, (m_pIWeb, &m_pIWebResp, &m_Callback, m_szUrl,
WEBOPT_DEFAULTS, m_pIWebOpts, // the SSL Support
WEBOPT_HANDLERDATA, this, // the data
WEBOPT_BODY, m_pIPeek, // the post data stream ptr
WEBOPT_CONTENTLENGTH, nBufferLength, // content length
WEBOPT_METHOD, "POST", // post method
WEBOPT_HEADER, "X-Method: POST\r\nContent-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n", // content type
WEBOPT_STATUSHANDLER, WebStatusNotification, // status handler
WEBOPT_HEADERHANDLER, WebHeaderHandler, // header handler
WEBOPT_END));
}
else
{ // without post data
}
return;
}
}
void CNetWork::WebReadCallBack(void * cxt)
{
CNetWork * pMe = (CNetWork *) cxt;
AEEApplet * pApplet = pMe->m_pApplet;
// WebRespInfo * pWebRespInfo;
// int size;
if (pMe->m_pIWebResp == NULL)
{
IDISPLAY_ClearScreen(pApplet->m_pIDisplay);
ShowResString(pMe->m_pApplet->m_pIShell, &pMe->m_pIStatic, &pMe->m_rcScreen, NULL, IDS_ERR_UNKNOWN);
pMe->WebCleanup();
return;
}
WebReadCB(pMe);
}
void CNetWork::WebReadCB(void * pi)
{
CNetWork * pMe = (CNetWork*) pi;
AEEApplet * pApplet = pMe->m_pApplet;
WebRespInfo* pWebRespInfo;
int ByteCount;
char buf[1024];
if(!pMe->m_pIWebResp) return;
pWebRespInfo = IWEBRESP_GetInfo(pMe->m_pIWebResp);
if (pWebRespInfo && WEB_ERROR_SUCCEEDED(pWebRespInfo->nCode))
{
pMe->m_TotalSize = pWebRespInfo->lContentLength;
ISource* pISource = pWebRespInfo->pisMessage;
ByteCount = ISOURCE_Read(pISource, (char*)buf, sizeof(buf));
// Read() return values fall into four categories of interest:
//
// 1. ISOURCE_WAIT: This means data is not yet available
// 2. ISOURCE_ERROR: An eror occurred
// 3. ISOURCE_END: No more data
// 4. >0 : The actual data that was read
//
switch (ByteCount)
{
case ISOURCE_WAIT:
CALLBACK_Init(&pMe->m_cb, WebReadCB, pi);
ISOURCE_Readable(pISource, &pMe->m_cb);
return;
case ISOURCE_ERROR:
// Connection hosed.
pMe->CancelConnectCallback();
ShowResString(pMe->m_pApplet->m_pIShell, &pMe->m_pIStatic, &pMe->m_rcScreen, NULL, IDS_NETERRO);
CALLBACK_Init(&pMe->m_cb, WebReadCB, pi);
ISOURCE_Readable(pISource, &pMe->m_cb);
return;
case ISOURCE_END:
pMe->CancelConnectCallback();
pMe->m_bReady = TRUE;
// No more data, display the results
break;
default:
if (ByteCount)
{
if (pMe->m_BodySize + ByteCount > pMe->m_BodyAllocSize)
{
const int NewSize = pMe->m_BodyAllocSize + 1024;
char* NewBuf = (char*)REALLOC(pMe->m_BodyBuffer, NewSize);
if (NewBuf)
{
pMe->m_BodyBuffer = NewBuf;
pMe->m_BodyAllocSize = NewSize;
}
}
if (pMe->m_BodySize + ByteCount <= pMe->m_BodyAllocSize)
{
MEMCPY(pMe->m_BodyBuffer + pMe->m_BodySize, buf, ByteCount);
pMe->m_BodySize += ByteCount;
}
}
CALLBACK_Init(&pMe->m_cb, WebReadCB, pi);
ISOURCE_Readable(pISource, &pMe->m_cb);
return;
}
// GET the data
if ( pMe->m_bReady )
{
if (pMe->m_pObject)
{
pMe->m_pObject->Construct(pMe->m_BodyBuffer, pMe->m_BodySize);
FREEIF(pMe->m_pObject->m_szPostData);
}
pMe->m_bFirst=FALSE;
}
}
pMe->WebCleanup();
}
void CNetWork::WebStatusNotification(void * pNotifyData, WebStatus ws, void * p)
{
CNetWork * pMe = (CNetWork *) pNotifyData;
AEEApplet * pApplet = pMe->m_pApplet;
int16 nResID = 0;
pMe->m_nTimeout=0;
(void)p;
switch (ws) {
case WEBS_CANCELLED: // cancelled by CALLBACK_Cancel
nResID = IDS_WEBCANCEL;
break;
case WEBS_STARTING:
nResID = IDS_WAIT;
break;
case WEBS_GETHOSTBYNAME: // looking up hostname in URL
nResID = IDS_GETHOST;
break;
case WEBS_CONNECT: // connecting to host
nResID = IDS_CONNECT;
break;
case WEBS_SENDREQUEST: // sending request data
nResID = IDS_SEND;
break;
case WEBS_READRESPONSE: // receiving response data
nResID = IDS_RECEIVE;
break;
case WEBS_GOTREDIRECT: // handling HTTP redirect
nResID = IDS_WEBS_GOTREDIRECT;
break;
case WEBS_CACHEHIT: // document found in cache
nResID = IDS_WEBS_CACHEHIT;
break;
}
if (nResID)
{
AECHAR * szBuf = NULL;
IDISPLAY_ClearScreen(pApplet->m_pIDisplay);
if(nResID != IDS_WEBCANCEL)
{
pMe->ConnectCallback(pMe);
/*// RGBVAL clr = IDISPLAY_SetColor(pMe->m_pApplet->m_pIDisplay, CLR_USER_BACKGROUND, RGB_BLACK);
LoadTextMessage(pApplet->m_pIShell, nResID, & szBuf, RES_FILE);
PrintString2(pMe->m_pIStatic, NULL, szBuf, FALSE);
// IDISPLAY_SetColor(pMe->m_pApplet->m_pIDisplay, CLR_USER_BACKGROUND, clr);
if (szBuf) FREEIF(szBuf); */
}
else
{
pMe->CancelConnectCallback();
}
}
}
void CNetWork::WebHeaderHandler(void * pNotifyData, const char * pszName, GetLine * pglVal)
{
CNetWork * pMe = (CNetWork *) pNotifyData;
AEEApplet * pApplet = pMe->m_pApplet;
// Handle HSE Error Codes, but don't display right now.
if (0 == STRICMP(pszName, "x-errorcode"))
{
// contains error code
short int nResID; // the resource id
int code = ATOI(pglVal->psz); // the error code
nResID = IDS_ERR_UNKNOWN;
if (pMe->m_szError ) FREEIF(pMe->m_szError);
LoadTextMessage(pApplet->m_pIShell, nResID, & pMe->m_szError, RES_FILE); // the error msg
}
}
void CNetWork::ConnectCallback(CNetWork *pMe)
{
ISHELL_SetTimer(pMe->m_pApplet->m_pIShell, 500, (PFNNOTIFY)(ConnectCallback), pMe);
pMe->m_nTimeout++;
if(pMe->m_nTimeout>NET_TIMEOUT)
{
pMe->CancelConnectCallback();
pMe->WebCleanup();
ShowResString(pMe->m_pApplet->m_pIShell, &pMe->m_pIStatic, &pMe->m_rcScreen, NULL, IDS_NETERRO);
}
else
pMe->ShowConnect();
}
void CNetWork::ShowConnect()
{
IImage * pImage = NULL;
AEEImageInfo info;
/* // 背景图片
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_BACK);
if(pImage)
{
IIMAGE_GetInfo(pImage, &info);
IIMAGE_Draw(pImage,(m_pDevice->cxScreen-info.cx)/2,(m_pDevice->cyScreen-info.cy)/2);
IIMAGE_Release(pImage);
pImage = NULL;
}
*/
// 连接信息
if(m_nPic < 1)
{
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_DOWN1);
}
else if(m_nPic < 2)
{
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_DOWN2);
}
else if(m_nPic < 3)
{
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_DOWN3);
}
else if(m_nPic < 4)
{
pImage = ISHELL_LoadResImage(m_pApplet->m_pIShell, RES_FILE, BM_DOWN4);
}
m_nPic++;
if (m_nPic >= 4)
m_nPic = 0;
if(pImage)
{
IDISPLAY_FillRect(m_pApplet->m_pIDisplay, &m_rcScreen, MAKE_BACK_COLOR);
IIMAGE_GetInfo(pImage, &info);
IIMAGE_Draw(pImage,(m_pDevice->cxScreen-info.cx)/2,(m_pDevice->cyScreen-info.cy)/2);
IIMAGE_Release(pImage);
pImage = NULL;
}
if(m_TotalSize==0)
{
IDISPLAY_Update(m_pApplet->m_pIDisplay);
return;
}
//progress
AEERect rect;
AEERect rcProgress;
rect.x = (m_pDevice->cxScreen-100)/2;
rect.y = (m_pDevice->cyScreen+info.cy)/2+10;
rect.dx = 100;
rect.dy = 10;
rcProgress.x = rect.x;
rcProgress.y = rect.y;
rcProgress.dx = (short)(100*m_BodySize/m_TotalSize);
rcProgress.dy = 10;
IDISPLAY_DrawRect(m_pApplet->m_pIDisplay, &rect, MAKE_RGB(0,0,0),MAKE_RGB(255,255,255), IDF_RECT_FILL);
IDISPLAY_DrawRect(m_pApplet->m_pIDisplay, &rcProgress, MAKE_RGB(0,0,255),MAKE_RGB(0,0,255), IDF_RECT_FILL);
IDISPLAY_Update(m_pApplet->m_pIDisplay);
}
void CNetWork::CancelConnectCallback()
{
ISHELL_CancelTimer(m_pApplet->m_pIShell, (PFNNOTIFY)(ConnectCallback), this);
}
boolean CNetWork::KeyHandle(AEEEvent evt, uint16 wParam, uint32 dwParam)
{
boolean status = FALSE;
switch(evt)
{
//Key handler
case EVT_KEY:
//case EVT_KEY_PRESS:
if (wParam == AVK_CLR)
{
CancelConnectCallback();
WebCleanup();
m_pObject=NULL;
status = TRUE;
if(m_pIStatic)
ReleaseObj((void * *) &m_pIStatic);
m_pIStatic=NULL;
m_nTimeout=0;
}
}
return status;
}
void CNetWork::Resume()
{
if(m_pIStatic)
{
ISTATIC_SetActive(m_pIStatic,TRUE);
ISTATIC_Redraw(m_pIStatic);
}
}
void CNetWork::Suspend()
{
if(m_pIStatic)
ISTATIC_SetActive(m_pIStatic,FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -