关于ace中ACE_Event_Handler中handle_input的一点疑惑...
以下是CFileEventHandler类的handle_input代码: 接收文件内容。
int CClientEventHandler::handle_input(ACE_HANDLE fd)
{
reactor()->suspend_handler(this);
char buf[1024] = {0};
int iFactRecvBytes = 0;
int iCompleteBytes = 0;
int iTotalBytes = 0;
int iSectionBytes = 1024;
struct FILE_INFO
{
char name[MAX_PATH]; //文件名称
int length; //文件长度(字节数)
}m_FileInfo;
std::ofstream file(".\\desc.txt", std::ios_base::trunc);
m_ClientStream.recv(buf, sizeof(FILE_INFO) ); //接收文件相关信息结构体
memcpy(&m_FileInfo, buf, sizeof(FILE_INFO) );
printf("Received File Name is: %s, size bytes is: %d\n", m_FileInfo.name, m_FileInfo.length);
printf("File context is below:\n");
iTotalBytes = m_FileInfo.length;
for (; iCompleteBytes<iTotalBytes; iCompleteBytes += iFactRecvBytes)
{
if (iTotalBytes - iCompleteBytes < iSectionBytes)
{
iFactRecvBytes = m_ClientStream.recv(buf, iTotalBytes - iCompleteBytes);
}
else
{
iFactRecvBytes = m_ClientStream.recv(buf, iSectionBytes);
}
/*
iFactRecvBytes = m_ClientStream.recv(buf, iSectionBytes);
*/
file.write(buf, iFactRecvBytes);
std::cout << buf;
}
file.close( );
reactor()->resume_handler(this);
return 0;
}
经调试,可以收到客户端发过来的数据,但是,在运行时,程序好像根本无法中止,终始不断地显示所收到的内容。我想了好久,handle_input是在当套按字上有数据接收时调用的,如果正在执行该函数时(刚读取完套接字的数据,此时套接字又发送数据过来),handle_input函数应该会重入执行。 所以,为了避免这个问题,我在该函数的一开始就挂起了事件处理: reactor()->suspend_handler(this); 在接收完毕后, reactor()->resume_handler(this); 但是,为什么程序不能正确运行,请各位看看。