异步调用InternetSendRequestEx为什么失败?(含代码)

unalone 2006-03-06 11:35:00
有如下几个类:
// 网络异步请求状态事件接收器接口
struct IInternetStatusEventHandler;

// 基本异步请求Context
struct TAsyncRequestContext;

// 用于异步读取数据的Context
struct TAsyncReadRequestContext;
// 用于异步发送数据的Context
struct TAsyncPostRequestContext;

class CAsyncInternetHadle;
class CAsyncInternetFile;
class CAsyncInternetConnection;
class CAsyncInternetSession;

CAsyncInternetSession、CAsyncInternetConnection以及CAsyncInternetFile分别使用异步操作打开句柄,句柄立即返回,并且没有触发INTERNET_STATUS_REQUEST_COMPLETE事件,然后调用CAsyncInternetFile的SendRequest方法,内部创建线程来调用SendRequestEx方法,以此来向调用者报告进度。代码如下:

TAsyncPostRequestContext *SendRequest(const StdString::TStringType &strPostContent)
{
ATLASSERT(m_hHandle);
TAsyncPostRequestContext *pContext = new TAsyncPostRequestContext(this);
pContext->RequestSourceID = AsyncHttpFileRequestHandlerID;
pContext->PostContent = strPostContent;

unsigned tid = 0;
CloseHandle((HANDLE)_beginthreadex(NULL,0,ThreadPostData,pContext,0,&tid));

return pContext;
}


static unsigned __stdcall ThreadPostData(void *pVoid)
{
TAsyncPostRequestContext *pContext = static_cast<TAsyncPostRequestContext*>(pVoid);

CAsyncHttpFile *pThis = static_cast<CAsyncHttpFile*>(pContext->handler);
ATLASSERT(pThis != NULL);
ATLASSERT(pThis->m_hHandle != NULL);

DWORD dwContentLength = pContext->PostContent.length() * sizeof(TCHAR);

INTERNET_BUFFERS BufferIn;

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwContentLength; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

// 初始一个请求
if(!HttpSendRequestEx( pThis->m_hHandle, &BufferIn, NULL, 0, 0)) {
DWORD dwErrorCode = GetLastError();
if (dwErrorCode != ERROR_IO_PENDING) {
pContext->EndRequest(HRESULT_FROM_WIN32(GetLastError()));
return -1;
}
}

// 循环发送数据
BOOL bResult = TRUE;
long lBlockCount = (long)dwContentLength / 1024;
HRESULT hr = S_OK;
DWORD dwTimeBeforePost = timeGetTime();
DWORD dwTimeAfterPost = 0;
DWORD dwBytesWritten = 0;
for(long lBlockIndex = 0; lBlockIndex < lBlockCount && bResult; lBlockIndex++) {
// 检查是否需要终止
if (pContext->NeedCancel()) {
hr = E_ABORT;
bResult = FALSE;
break;
}

if ((dwBytesWritten = pThis->Write(pContext->PostContent.data() + (lBlockIndex * 1024), 1024)) != -1) {
dwTimeAfterPost = timeGetTime();

pContext->posted_size += dwBytesWritten;

// 输出状态信息
__raise pContext->StatusChanged(pContext,
StdString::FormatString(_T("已经发送: %ul bytes"),pContext->posted_size));

// 输出已传送量以及速度信息
__raise pContext->DataPosted(pContext,
(double)pContext->posted_size / ((dwTimeAfterPost - dwTimeBeforePost) / 1000),
lBlockIndex == lBlockCount);
} else {
bResult = FALSE;
}
}

if(!bResult && SUCCEEDED(hr)) {
// 有错误,输出错误信息
DWORD dwErrorCode = GetLastError();
__raise pContext->StatusChanged(pContext,StdString::FormatString(_T("Error on InternetWriteFile %lu")
,dwErrorCode));
pContext->EndRequest(HRESULT_FROM_WIN32(dwErrorCode));
return -1;
}

if(!HttpEndRequest(pThis->m_hHandle, NULL, 0, 0)) {
// 有错误,输出错误信息
DWORD dwErrorCode = GetLastError();
__raise pContext->StatusChanged(pContext,StdString::FormatString(_T("Error on HttpEndRequest %lu")
,dwErrorCode));
pContext->EndRequest(HRESULT_FROM_WIN32(dwErrorCode));
return -1;
}

// 结束请求
pContext->EndRequest(hr);

return 0;
}

问题是,之前所有创建句柄的方法均立即返回句柄,并且没有触发INTERNET_STATUS_REQUEST_COMPLETE(有INTERNET_STATUS_HANDLE_CREATED事件),然后在调用InternetSendRequestEx时返回ERROR_IO_PENDING,如果忽略,则下面的Write方法(内部调用InternetWriteFile函数)就会失败,这是怎么回事?真的有未结束的异步调用么?
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
unalone 2006-06-29
  • 打赏
  • 举报
回复
还是没人关注么……
tong20037 2006-06-29
  • 打赏
  • 举报
回复
关注了~不会,帮你顶

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧