OpenVPN TAP I / O操作永远阻止

时间:2014-10-14 17:52:46

标签: c++ readfile msdn openvpn createthread

以下代码在GetOverlappedResult被调用时永远挂起,我对Windows异步IO操作没有太多经验,我按照我的理解实现了它。我用它来访问虚拟网络接口(通过openvpn - TAP / TUN接口,其内核驱动程序安装正确)。

我找到了它挂起的地方,但我不知道它挂起的原因?

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;
}

上面的代码在使用CreateThread API创建的线程内的无限while循环中执行。

我是否正确实施了异步操作?

当GetOverlappedResult挂起时可能出现什么情况?

PS:很少有ReadFile调用成功,它可以成功读取以太网帧,但在大约后挂起。 15-20个电话。

0 个答案:

没有答案