Chromium Code Reviews| Index: chrome/common/profiling/memlog_sender_pipe_win.cc |
| diff --git a/chrome/common/profiling/memlog_sender_pipe_win.cc b/chrome/common/profiling/memlog_sender_pipe_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ef0a323e0680fe4a05f3f27eba23233da8b9f0ed |
| --- /dev/null |
| +++ b/chrome/common/profiling/memlog_sender_pipe_win.cc |
| @@ -0,0 +1,51 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/common/profiling/memlog_sender_pipe_win.h" |
| + |
| +#include "base/logging.h" |
| +#include "chrome/common/profiling/memlog_stream.h" |
| + |
| +namespace profiling { |
| + |
| +MemlogSenderPipe::MemlogSenderPipe(const base::string16& pipe_id) |
| + : pipe_id_(pipe_id), handle_(INVALID_HANDLE_VALUE) {} |
| + |
| +MemlogSenderPipe::~MemlogSenderPipe() { |
| + if (handle_ != INVALID_HANDLE_VALUE) |
| + ::CloseHandle(handle_); |
| +} |
| + |
| +bool MemlogSenderPipe::Connect() { |
| + DCHECK(handle_ == INVALID_HANDLE_VALUE); |
| + |
| + base::string16 pipe_name = kWindowsPipePrefix; |
| + pipe_name.append(pipe_id_); |
| + |
| + do { |
| + if (!::WaitNamedPipe(pipe_name.c_str(), NMPWAIT_WAIT_FOREVER)) { |
| + // Since it will wait "forever", the only time WaitNamedPipe should fail |
| + // is if the pipe doesn't exist. |
| + return false; |
| + } |
| + handle_ = ::CreateFile(pipe_name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
|
Boris Vidolov
2017/06/15 22:25:45
Is it a bidirectional pipe? These are not fun, as
brettw
2017/06/15 22:53:10
It is but I don't need that. I changed it to a sin
|
| + NULL, OPEN_EXISTING, 0, NULL); |
| + // Need to loop since there is a race condition waiting for a connection to |
| + // be available and actually connecting to it. |
| + } while (handle_ == INVALID_HANDLE_VALUE); |
|
Boris Vidolov
2017/06/15 22:25:45
Consider timeout. Maybe die after 30 seconds. The
brettw
2017/06/15 22:53:10
Done.
|
| + |
| + return true; |
| +} |
| + |
| +bool MemlogSenderPipe::Send(const void* data, size_t sz) { |
|
awong
2017/06/15 21:32:46
Can we check fail that sz fits in a DWORD?
brettw
2017/06/15 22:12:21
You asking me this made me realize that we can't c
|
| + // The pipe uses a blocking wait mode (it doesn't specify PIPE_NOWAIT) so |
| + // WriteFile should do only complete writes. |
| + DWORD bytes_written = 0; |
| + if (!::WriteFile(handle_, data, static_cast<DWORD>(sz), &bytes_written, NULL)) |
| + return false; |
| + DCHECK(bytes_written == static_cast<DWORD>(sz)); |
|
awong
2017/06/15 21:32:46
DCHECK_EQ
|
| + return true; |
| +} |
| + |
| +} // namespace profiling |