Reputation: 91
Following code hangs forever when GetOverlappedResult gets called, I have not much experience in windows async IO operations, I implemented it as per my understanding. I have used it to access virtual network interface (by openvpn - TAP/TUN interface whose kernel driver is installed properly).
I found the place where it hangs, but I don't know the reason why it hangs ?
nread = 0;
memset(data_buffer, '\0', nread);
OVERLAPPED overlapped_read;
memset(&overlapped_read, 0, sizeof(overlapped_read));
overlapped_read.Offset = 0;
overlapped_read.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if ( ReadFile(fd, data_buffer, len, &nread, &overlapped_read) == false ) {
if (GetLastError() != ERROR_IO_PENDING) {
std::cerr << "ReadFile failed : " << GetLastError() << std::endl;
return false;
}
else {
DWORD dwRes = WaitForSingleObject(overlapped_read.hEvent, INFINITE);
if(dwRes == WAIT_OBJECT_0) {
if (!GetOverlappedResult(fd, &overlapped_read, &nread, FALSE)) {
std::cout << "GetOverlappedResult failed : ErrorCode = "
<< GetLastError() << std::endl;
CloseHandle(overlapped_read.hEvent);
return false;
}
else {
std:cout << "Read successfull." << std::endl;
}
}
else {
std::cout << "WaitForSingleObject failed : ErrorCode = "
<< GetLastError() << std::endl;
CloseHandle(overlapped_read.hEvent);
return false;
}
}
std::cout << "bytes read = " << nread << std::endl;
CloseHandle(overlapped_read.hEvent);
return TRUE;
}
Above code executed in an Infinite while loop inside a thread created using CreateThread API.
Do I have implemented async operation correctly ?
What could be the possible cases when GetOverlappedResult gets hangs ?
PS: Few ReadFile calls were successfull, it can read ethernet frame successfully, but hangs after approx. 15-20 calls.
Upvotes: 1
Views: 433