有看过ACE_win32_proactor实现的代码的兄弟么?
有看过ACE_win32_proactor实现的代码的兄弟么小生有个问题
ACE_WIN32_Proactor::handle_events (unsigned long milli_seconds)
{
OVERLAPPED *overlapped = 0;
u_long bytes_transferred = 0;
ULONG completion_key = 0;
// Get the next asynchronous operation that completes
BOOL result = ::GetQueuedCompletionStatus (this->completion_port_,
&bytes_transferred,
&completion_key,
&overlapped,
milli_seconds);
// Narrow the result.
ACE_WIN32_Asynch_Result *asynch_result = (ACE_WIN32_Asynch_Result *) overlapped;
u_long result_err = asynch_result->error ();
// Call completion hook
asynch_result->complete (bytes_transferred,
error ? 0 : 1,
(void *) completion_key,
error);
}
overlapped 指向::GetQueuedCompletionStatus 分配的一快内存,这应该是个WIN32 SDK的结构体
但是下面还有一句: ACE_WIN32_Asynch_Result *asynch_result = (ACE_WIN32_Asynch_Result *) overlapped;
ACE中把这个overlapped强转成了一个ACE_WIN32_Asynch_Result指针
然后居然还call这个指针指的对象的函数
u_long result_err = asynch_result->error ();
// Call completion hook
asynch_result->complete (bytes_transferred,
error ? 0 : 1,
(void *) completion_key,
error);
这是怎么回事情啊! class ACE_Export ACE_WIN32_Asynch_Result : public virtual ACE_Asynch_Result_Impl,
public OVERLAPPED
看代码就知道这个类是由OVERLAPPED派生的.. 楼上:我问的是GetQueuedCompletionStatus 第4个参数,它是自己定义的么?
It is a Pointer to a variable that receives the address of the OVERLAPPED structure!!!!!!!!!!!!!!!!!!!!!!!!!
GetQueuedCompletionStatus返回的时候,会new一块内存,这快内存的大小是sizeof(OVERLAPPED)!!!!
然后把这块内存的首地址赋给这个排老4的参数。
好,如果你对原文中变量overlapped指的是一块大小为sizeof(OVERLAPPED)的内存没有异议的话,请继续看:
索性我们就说overlapped指的是个OVERLAPPED对象吧。:)
GetQueuedCompletionStatus返回后,就是下面这行:
// Narrow the result.
ACE_WIN32_Asynch_Result *asynch_result = (ACE_WIN32_Asynch_Result *) overlapped;
没错 ACE_WIN32_Asynch_Result 是派生自OVERLAPPED。
class ACE_Export ACE_WIN32_Asynch_Result : public virtual ACE_Asynch_Result_Impl,
public OVERLAPPED
我见过基类指针指向子类对象,但是很少见子类指针指向基类对象(这样通常会造成access violation)
这里它出现的用意何在!!???
下面这句也在ACE的代码里出现了,不会crash么?有谁知道?
asynch_result->complete (bytes_transferred, error ? 0 : 1,(void *) completion_key,error); 好了这个问题可以结束了!
我的概念错误:
GetQueuedCompletionStatus返回的时候,会new一块内存,这快内存的大小是sizeof(OVERLAPPED)!!!!
上面那句话根本就不对。
原文中变量overlapped指的是一块预先new的内存,这个块内存是个ACE_WIN32_Asynch_Accept_Result
(ACE_WIN32_Asynch_Connect_Result, ACE_WIN32_Asynch_Read_Stream, ACE_WIN32_Asynch_Write_Stream)对象
页:
[1]