@@ -13,9 +13,7 @@ ServerNamedPipe::~ServerNamedPipe()
13
13
{
14
14
if (isValid ())
15
15
{
16
- LOG_verbose << mName << " Endpoint server flush" ;
17
16
FlushFileBuffers (mPipeHandle );
18
- LOG_verbose << mName << " Endpoint server disconnect" ;
19
17
DisconnectNamedPipe (mPipeHandle );
20
18
}
21
19
}
@@ -25,58 +23,53 @@ void ServerWin32::operator()()
25
23
serverListeningLoop ();
26
24
}
27
25
28
- std::error_code ServerWin32::waitForClient (HANDLE hPipe, OVERLAPPED* overlapped)
26
+ std::error_code ServerWin32::waitForClient (HANDLE hPipe, WinOverlapped& overlapped)
29
27
{
30
28
assert (hPipe != INVALID_HANDLE_VALUE);
31
- assert (overlapped);
29
+ assert (overlapped. isValid () );
32
30
33
31
// Wait for the client to connect asynchronous; if it succeeds,
34
32
// the function returns a nonzero value.
35
33
// If the function returns zero,
36
- // GetLastError returns ERROR_IO_PENDING , the IO is connected
37
- // GetLastError returns ERROR_PIPE_CONNECTED , the IO is pending
38
- bool success = ConnectNamedPipe (hPipe, overlapped);
34
+ // GetLastError returns ERROR_PIPE_CONNECTED , the IO is connected
35
+ // GetLastError returns ERROR_IO_PENDING , the IO is pending
36
+ bool success = ConnectNamedPipe (hPipe, overlapped. data () );
39
37
if (success)
40
38
{
41
39
LOG_verbose << " Client connected" ;
42
40
return OK;
43
41
}
44
42
45
- if (!success && GetLastError () == ERROR_PIPE_CONNECTED)
43
+ if (GetLastError () == ERROR_PIPE_CONNECTED)
46
44
{
47
45
LOG_verbose << " Client connected" ;
48
46
return OK;
49
47
}
50
48
51
- if (!success && GetLastError () != ERROR_IO_PENDING)
49
+ if (GetLastError () != ERROR_IO_PENDING)
52
50
{
53
51
LOG_verbose << " Client couldn't connect, error=" << GetLastError () << " " << mega::winErrorMessage (GetLastError ());
54
52
return std::make_error_code (std::errc::not_connected);
55
53
}
56
54
57
- // IO_PENDING
55
+ // Wait
56
+ if (auto [error, errorText] = overlapped.waitForCompletion (mWaitMs ); error)
57
+ {
58
+ LOG_verbose << " Client " << errorText;
59
+ return error;
60
+ }
61
+
62
+ // Get result
58
63
DWORD numberOfBytesTransferred = 0 ;
59
- if (GetOverlappedResultEx (
60
- hPipe,
61
- overlapped,
62
- &numberOfBytesTransferred,
63
- mWaitMs ,
64
- false ))
64
+ if (GetOverlappedResult (hPipe, overlapped.data (), &numberOfBytesTransferred, false /* bWait*/ ))
65
65
{
66
66
LOG_verbose << " Client connected" ;
67
67
return OK;
68
68
}
69
69
70
- if (GetLastError () == WAIT_TIMEOUT)
71
- {
72
- LOG_verbose << " Wait client connecting Timeout" ;
73
- return std::make_error_code (std::errc::timed_out);
74
- }
75
- else
76
- {
77
- LOG_verbose << " Client couldn't connect, error=" << GetLastError () << " " << mega::winErrorMessage (GetLastError ());
78
- return std::make_error_code (std::errc::not_connected);
79
- }
70
+ LOG_verbose << " Client couldn't connect, error=" << GetLastError () << " "
71
+ << mega::winErrorMessage (GetLastError ());
72
+ return std::make_error_code (std::errc::not_connected);
80
73
}
81
74
82
75
void ServerWin32::serverListeningLoop ()
@@ -87,15 +80,15 @@ void ServerWin32::serverListeningLoop()
87
80
return ;
88
81
}
89
82
83
+ LOG_verbose << " server awaiting client connection" ;
84
+
90
85
const auto fullPipeName = win_utils::toFullPipeName (mPipeName );
91
86
92
87
// first instance to prevent two processes create the same pipe
93
88
DWORD firstInstance = FILE_FLAG_FIRST_PIPE_INSTANCE;
94
89
const DWORD BUFSIZE = 512 ;
95
90
for (;;)
96
91
{
97
- LOG_verbose << " server awaiting client connection" ;
98
-
99
92
auto hPipe = CreateNamedPipe (
100
93
fullPipeName.c_str (), // pipe name
101
94
PIPE_ACCESS_DUPLEX | // read/write access
@@ -120,7 +113,7 @@ void ServerWin32::serverListeningLoop()
120
113
firstInstance = 0 ;
121
114
122
115
bool stopRunning = false ;
123
- auto err_code = waitForClient (hPipe, overlapped. data () );
116
+ auto err_code = waitForClient (hPipe, overlapped);
124
117
if (err_code)
125
118
{
126
119
// if has timeout and expires, we'll stop running
0 commit comments