Skip to content

Commit a014e4c

Browse files
committed
Merge branch 'fix/SDK-4573_Replace_GetOverlappedResultEx_to_support_Windows_7' into 'release/v8.0.0'
SDK-4573 Replace GetOverlappedResultEx to support Windows 7 (v8.0.0) See merge request sdk/sdk!6035
2 parents 0f08698 + 672b44e commit a014e4c

File tree

6 files changed

+79
-56
lines changed

6 files changed

+79
-56
lines changed

include/mega/win32/gfx/worker/comms.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,17 @@ class WinOverlapped final
6363

6464
OVERLAPPED* data();
6565

66-
bool isValid() const { return mOverlapped.hEvent != NULL; };
66+
bool isValid() const
67+
{
68+
return mOverlapped.hEvent != NULL;
69+
};
70+
71+
// Return an error code and error string on error
72+
std::pair<std::error_code, std::string> waitForCompletion(DWORD mWaitMs);
6773

6874
private:
6975
OVERLAPPED mOverlapped;
7076
};
7177

7278
} // namespace
73-
}
79+
}

src/gfx/worker/client.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ bool GfxClient::runHello(const std::string& text)
3636
auto response = sendAndReceive<CommandHelloResponse>(endpoint.get(), command);
3737
if (response)
3838
{
39-
LOG_verbose << "GfxClient gets hello response: " << response->Text;
4039
return true;
4140
}
4241
else

src/win32/gfx/worker/comms.cpp

+48-21
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ WinOverlapped::WinOverlapped()
2424
{
2525
mOverlapped.Offset = 0;
2626
mOverlapped.OffsetHigh = 0;
27-
mOverlapped.hEvent = CreateEvent(
28-
NULL, // default security attribute
29-
TRUE, // manual-reset event
30-
TRUE, // initial state = signaled
31-
NULL); // unnamed event object
27+
mOverlapped.hEvent = CreateEvent(NULL, // default security attribute
28+
TRUE, // manual-reset event
29+
FALSE, // initial state = non signaled
30+
NULL); // unnamed event object
3231

3332
if (mOverlapped.hEvent == NULL)
3433
{
@@ -49,6 +48,30 @@ OVERLAPPED* WinOverlapped::data()
4948
return &mOverlapped;
5049
}
5150

51+
std::pair<std::error_code, std::string> WinOverlapped::waitForCompletion(DWORD waitMs)
52+
{
53+
const auto waitResult = WaitForSingleObject(mOverlapped.hEvent, waitMs);
54+
switch (waitResult)
55+
{
56+
case WAIT_OBJECT_0:
57+
return {std::error_code{}, ""};
58+
case WAIT_TIMEOUT:
59+
return {std::make_error_code(std::errc::timed_out),
60+
"wait timeout: " + std::to_string(waitMs)};
61+
case WAIT_ABANDONED:
62+
return {std::make_error_code(std::errc::not_connected), "wait abandoned"};
63+
case WAIT_FAILED:
64+
{
65+
const auto lastError = GetLastError();
66+
return {std::make_error_code(std::errc::not_connected),
67+
"wait failed error " + std::to_string(lastError) + " " +
68+
mega::winErrorMessage(lastError)};
69+
}
70+
default:
71+
return {std::make_error_code(std::errc::not_connected), "wait error"};
72+
}
73+
}
74+
5275
NamedPipe::NamedPipe(NamedPipe&& other)
5376
{
5477
this->mPipeHandle = other.mPipeHandle;
@@ -61,7 +84,6 @@ NamedPipe::~NamedPipe()
6184
if (mPipeHandle != INVALID_HANDLE_VALUE)
6285
{
6386
CloseHandle(mPipeHandle);
64-
LOG_verbose << "endpoint " << mName << "_" << mPipeHandle << " closed";
6587
}
6688
}
6789

@@ -108,7 +130,7 @@ bool NamedPipe::doOverlappedOperation(std::function<bool(OVERLAPPED*)> op,
108130
return false;
109131
}
110132

111-
WinOverlapped overlapped;
133+
WinOverlapped overlapped{};
112134
if (!overlapped.isValid())
113135
{
114136
return false;
@@ -117,34 +139,39 @@ bool NamedPipe::doOverlappedOperation(std::function<bool(OVERLAPPED*)> op,
117139
// Call Op.
118140
if (op(overlapped.data()))
119141
{
120-
return true;
142+
return true; // Completed
121143
}
122-
123-
// Error
124-
auto lastError = GetLastError();
125-
if (lastError!= ERROR_IO_PENDING)
144+
else if (const auto lastError = GetLastError();
145+
lastError != ERROR_IO_PENDING) // Fail with other errors
126146
{
127-
LOG_err << mName << ": " << opStr << " pipe failed. error=" << lastError << " " << mega::winErrorMessage(lastError);
147+
LOG_err << mName << ": " << opStr << " pipe failed. error=" << lastError << " "
148+
<< mega::winErrorMessage(lastError);
128149
return false;
129150
}
130151

131-
// Wait op to complete. Negative timeout is infinite
152+
// Wait
132153
DWORD waitTimeout = timeout.count() < 0 ? INFINITE : static_cast<DWORD>(timeout.count());
154+
if (const auto& [error, errorText] = overlapped.waitForCompletion(waitTimeout); error)
155+
{
156+
LOG_verbose << mName << ": " << opStr << " " << errorText;
157+
return false;
158+
}
159+
160+
// Get result
133161
DWORD numberOfBytesTransferred = 0;
134-
bool success = GetOverlappedResultEx(mPipeHandle,
135-
overlapped.data(),
136-
&numberOfBytesTransferred,
137-
waitTimeout,
138-
false);
162+
bool success = GetOverlappedResult(mPipeHandle,
163+
overlapped.data(),
164+
&numberOfBytesTransferred,
165+
false /*bWait*/);
139166

140167
if (!success)
141168
{
142-
lastError = GetLastError();
169+
const auto lastError = GetLastError();
143170
LOG_err << mName << ": " << opStr << " pipe fail to complete error=" << lastError << " " << mega::winErrorMessage(lastError);
144171
return false;
145172
}
146173

147-
// IO completed
174+
// Completed
148175
return true;
149176
}
150177

src/win32/gfx/worker/comms_client.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ std::pair<CommError, HANDLE> GfxCommunicationsClient::doConnect(LPCTSTR pipeName
6161
}
6262
}
6363

64-
LOG_verbose << "Connected Handle:" << hPipe << " error: " << static_cast<int>(error);
65-
6664
return {error, hPipe};
6765
}
6866

tools/gfxworker/src/win32/server.cpp

+22-29
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ ServerNamedPipe::~ServerNamedPipe()
1313
{
1414
if (isValid())
1515
{
16-
LOG_verbose << mName << "Endpoint server flush";
1716
FlushFileBuffers(mPipeHandle);
18-
LOG_verbose << mName << "Endpoint server disconnect";
1917
DisconnectNamedPipe(mPipeHandle);
2018
}
2119
}
@@ -25,58 +23,53 @@ void ServerWin32::operator()()
2523
serverListeningLoop();
2624
}
2725

28-
std::error_code ServerWin32::waitForClient(HANDLE hPipe, OVERLAPPED* overlapped)
26+
std::error_code ServerWin32::waitForClient(HANDLE hPipe, WinOverlapped& overlapped)
2927
{
3028
assert(hPipe != INVALID_HANDLE_VALUE);
31-
assert(overlapped);
29+
assert(overlapped.isValid());
3230

3331
// Wait for the client to connect asynchronous; if it succeeds,
3432
// the function returns a nonzero value.
3533
// 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());
3937
if (success)
4038
{
4139
LOG_verbose << "Client connected";
4240
return OK;
4341
}
4442

45-
if (!success && GetLastError() == ERROR_PIPE_CONNECTED)
43+
if (GetLastError() == ERROR_PIPE_CONNECTED)
4644
{
4745
LOG_verbose << "Client connected";
4846
return OK;
4947
}
5048

51-
if (!success && GetLastError() != ERROR_IO_PENDING)
49+
if (GetLastError() != ERROR_IO_PENDING)
5250
{
5351
LOG_verbose << "Client couldn't connect, error=" << GetLastError() << " " << mega::winErrorMessage(GetLastError());
5452
return std::make_error_code(std::errc::not_connected);
5553
}
5654

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
5863
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*/))
6565
{
6666
LOG_verbose << "Client connected";
6767
return OK;
6868
}
6969

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);
8073
}
8174

8275
void ServerWin32::serverListeningLoop()
@@ -87,15 +80,15 @@ void ServerWin32::serverListeningLoop()
8780
return;
8881
}
8982

83+
LOG_verbose << "server awaiting client connection";
84+
9085
const auto fullPipeName = win_utils::toFullPipeName(mPipeName);
9186

9287
// first instance to prevent two processes create the same pipe
9388
DWORD firstInstance = FILE_FLAG_FIRST_PIPE_INSTANCE;
9489
const DWORD BUFSIZE = 512;
9590
for (;;)
9691
{
97-
LOG_verbose << "server awaiting client connection";
98-
9992
auto hPipe = CreateNamedPipe(
10093
fullPipeName.c_str(), // pipe name
10194
PIPE_ACCESS_DUPLEX | // read/write access
@@ -120,7 +113,7 @@ void ServerWin32::serverListeningLoop()
120113
firstInstance = 0;
121114

122115
bool stopRunning = false;
123-
auto err_code = waitForClient(hPipe, overlapped.data());
116+
auto err_code = waitForClient(hPipe, overlapped);
124117
if (err_code)
125118
{
126119
// if has timeout and expires, we'll stop running

tools/gfxworker/src/win32/server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ServerWin32
4949

5050
void serverListeningLoop();
5151

52-
std::error_code waitForClient(HANDLE hPipe, OVERLAPPED* overlapped);
52+
std::error_code waitForClient(HANDLE hPipe, WinOverlapped& overlapped);
5353

5454
std::unique_ptr<RequestProcessor> mRequestProcessor;
5555

0 commit comments

Comments
 (0)