-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwoliocbufsend.cpp
More file actions
67 lines (53 loc) · 1.42 KB
/
Copy pathwoliocbufsend.cpp
File metadata and controls
67 lines (53 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define WIN32_LEAN_AND_MEAN
#include <wio.h>
#ifdef _MSC_VER
#pragma comment(lib, "kernel32")
#endif
//-- wio.h definitions
//--------------- SafeSend(), LineRecv(), BufRecv() and BufSend() functions are
//--------------- used for I/O on overlapped streams such as sockets, comm
//--------------- devices or named pipes.
EXTERN_C BOOL BufSend(HANDLE h, const void *pBuf, DWORD dwBufSize,
DWORD dwTimeout)
{
WOverlappedIOC ol;
if (!ol)
return FALSE;
return ol.BufSend(h, pBuf, dwBufSize, dwTimeout);
}
/// Write fixed number of bytes.
BOOL
WOverlappedIOC::BufSend(HANDLE h, const void *pBuf, DWORD dwBufSize,
DWORD dwTimeout)
{
// First try non-overlapped. If ERROR_INVALID_PARAMETER, try overlapped.
DWORD dwDone = 0;
for (const void *ptr = pBuf; ; )
{
DWORD dwWriteLen;
if (!WriteFile(h, ptr, dwBufSize-dwDone, &dwWriteLen, NULL))
break;
if (dwWriteLen == 0)
return dwDone == dwBufSize;
dwDone += dwWriteLen;
*(LPBYTE*)&ptr += dwWriteLen;
if (dwDone >= dwBufSize)
return dwDone == dwBufSize;
}
dwDone = 0;
for (const void *ptr = pBuf; dwDone < dwBufSize; )
{
if (!Write(h, ptr, dwBufSize-dwDone))
break;
if (!Wait(dwTimeout))
break;
DWORD dwWriteLen;
if (!GetResult(&dwWriteLen))
break;
if (dwWriteLen == 0)
break;
dwDone += dwWriteLen;
*(CONST BYTE**)&ptr += dwWriteLen;
}
return dwDone == dwBufSize;
}