Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(729)

Unified Diff: base/sync_socket_win.cc

Issue 8965053: Implement support for a cancelable SyncSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed micro-nit Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/sync_socket_posix.cc ('k') | content/browser/renderer_host/media/audio_input_sync_writer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/sync_socket_win.cc
diff --git a/base/sync_socket_win.cc b/base/sync_socket_win.cc
index 032e04f7d6b0b940d01fce7672670c666fc6783d..359790c4f1001ed52a5e059776b92d149ac0d8ec 100644
--- a/base/sync_socket_win.cc
+++ b/base/sync_socket_win.cc
@@ -3,14 +3,14 @@
// found in the LICENSE file.
#include "base/sync_socket.h"
-#include <limits.h>
-#include <stdio.h>
-#include <windows.h>
-#include <sys/types.h>
+
#include "base/logging.h"
+#include "base/win/scoped_handle.h"
namespace base {
+using win::ScopedHandle;
+
namespace {
// IMPORTANT: do not change how this name is generated because it will break
// in sandboxed scenarios as we might have by-name policies that allow pipe
@@ -26,85 +26,148 @@ const int kOutBufferSize = 4096;
const int kInBufferSize = 4096;
const int kDefaultTimeoutMilliSeconds = 1000;
-} // namespace
-
-const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
-
-bool SyncSocket::CreatePair(SyncSocket* pair[2]) {
- Handle handles[2];
- SyncSocket* tmp_sockets[2];
-
- // Create the two SyncSocket objects first to avoid ugly cleanup issues.
- tmp_sockets[0] = new SyncSocket(kInvalidHandle);
- if (tmp_sockets[0] == NULL) {
- return false;
- }
- tmp_sockets[1] = new SyncSocket(kInvalidHandle);
- if (tmp_sockets[1] == NULL) {
- delete tmp_sockets[0];
- return false;
- }
+bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) {
+ DCHECK(socket_a != socket_b);
+ DCHECK(*socket_a == SyncSocket::kInvalidHandle);
+ DCHECK(*socket_b == SyncSocket::kInvalidHandle);
wchar_t name[kPipePathMax];
+ ScopedHandle handle_a;
+ DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE;
+ if (overlapped)
+ flags |= FILE_FLAG_OVERLAPPED;
+
do {
unsigned int rnd_name;
if (rand_s(&rnd_name) != 0)
return false;
+
swprintf(name, kPipePathMax,
kPipeNameFormat,
GetCurrentProcessId(),
GetCurrentThreadId(),
rnd_name);
- handles[0] = CreateNamedPipeW(
+
+ handle_a.Set(CreateNamedPipeW(
name,
- PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
+ flags,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
1,
kOutBufferSize,
kInBufferSize,
kDefaultTimeoutMilliSeconds,
- NULL);
- } while ((handles[0] == INVALID_HANDLE_VALUE) &&
+ NULL));
+ } while (!handle_a.IsValid() &&
(GetLastError() == ERROR_PIPE_BUSY));
- if (handles[0] == INVALID_HANDLE_VALUE) {
+ if (!handle_a.IsValid()) {
NOTREACHED();
return false;
}
- // The SECURITY_ANONYMOUS flag means that the server side (pair[0]) cannot
- // impersonate the client (pair[1]). This allows us not to care which side
+
+ // The SECURITY_ANONYMOUS flag means that the server side (handle_a) cannot
+ // impersonate the client (handle_b). This allows us not to care which side
// ends up in which side of a privilege boundary.
- handles[1] = CreateFileW(name,
- GENERIC_READ | GENERIC_WRITE,
- 0, // no sharing.
- NULL, // default security attributes.
- OPEN_EXISTING, // opens existing pipe.
- SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS,
- NULL); // no template file.
- if (handles[1] == INVALID_HANDLE_VALUE) {
- CloseHandle(handles[0]);
+ flags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS;
+ if (overlapped)
+ flags |= FILE_FLAG_OVERLAPPED;
+
+ ScopedHandle handle_b(CreateFileW(name,
+ GENERIC_READ | GENERIC_WRITE,
+ 0, // no sharing.
+ NULL, // default security attributes.
+ OPEN_EXISTING, // opens existing pipe.
+ flags,
+ NULL)); // no template file.
+ if (!handle_b.IsValid()) {
+ DPLOG(ERROR) << "CreateFileW failed";
return false;
}
- if (ConnectNamedPipe(handles[0], NULL) == FALSE) {
+
+ if (!ConnectNamedPipe(handle_a, NULL)) {
DWORD error = GetLastError();
if (error != ERROR_PIPE_CONNECTED) {
- CloseHandle(handles[0]);
- CloseHandle(handles[1]);
+ DPLOG(ERROR) << "ConnectNamedPipe failed";
return false;
}
}
- // Copy the handles out for successful return.
- tmp_sockets[0]->handle_ = handles[0];
- pair[0] = tmp_sockets[0];
- tmp_sockets[1]->handle_ = handles[1];
- pair[1] = tmp_sockets[1];
+
+ *socket_a = handle_a.Take();
+ *socket_b = handle_b.Take();
+
return true;
}
+// Inline helper to avoid having the cast everywhere.
+DWORD GetNextChunkSize(size_t current_pos, size_t max_size) {
+ // The following statement is for 64 bit portability.
+ return static_cast<DWORD>(((max_size - current_pos) <= UINT_MAX) ?
+ (max_size - current_pos) : UINT_MAX);
+}
+
+// Template function that supports calling ReadFile or WriteFile in an
+// overlapped fashion and waits for IO completion. The function also waits
+// on an event that can be used to cancel the operation. If the operation
+// is cancelled, the function returns and closes the relevant socket object.
+template <typename BufferType, typename Function>
+size_t CancelableFileOperation(Function operation, HANDLE file,
+ BufferType* buffer, size_t length,
+ base::WaitableEvent* io_event,
+ base::WaitableEvent* cancel_event,
+ CancelableSyncSocket* socket) {
+ // The buffer must be byte size or the length check won't make much sense.
+ COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type);
+ DCHECK_LE(length, kMaxMessageLength);
+
+ OVERLAPPED ol = {0};
+ ol.hEvent = io_event->handle();
+ size_t count = 0;
+ while (count < length) {
+ DWORD chunk = GetNextChunkSize(count, length);
+ // This is either the ReadFile or WriteFile call depending on whether
+ // we're receiving or sending data.
+ DWORD len;
+ BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk,
+ &len, &ol);
+ if (!ok) {
+ if (::GetLastError() == ERROR_IO_PENDING) {
+ base::WaitableEvent* events[] = { io_event, cancel_event };
+ size_t signaled = WaitableEvent::WaitMany(events, arraysize(events));
+ if (signaled == 1) {
+ VLOG(1) << "Shutdown was signaled. Closing socket.";
+ socket->Close();
+ break;
+ } else {
+ GetOverlappedResult(file, &ol, &len, TRUE);
+ }
+ } else {
+ return (0 < count) ? count : 0;
+ }
+ }
+ count += len;
+ }
+ return count;
+}
+
+} // namespace
+
+const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
+
+SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
+
+SyncSocket::~SyncSocket() {
+ Close();
+}
+
+// static
+bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
+ return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false);
+}
+
bool SyncSocket::Close() {
- if (handle_ == kInvalidHandle) {
+ if (handle_ == kInvalidHandle)
return false;
- }
+
BOOL retval = CloseHandle(handle_);
handle_ = kInvalidHandle;
return retval ? true : false;
@@ -115,9 +178,7 @@ size_t SyncSocket::Send(const void* buffer, size_t length) {
size_t count = 0;
while (count < length) {
DWORD len;
- // The following statement is for 64 bit portability.
- DWORD chunk = static_cast<DWORD>(
- ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
+ DWORD chunk = GetNextChunkSize(count, length);
if (WriteFile(handle_, static_cast<const char*>(buffer) + count,
chunk, &len, NULL) == FALSE) {
return (0 < count) ? count : 0;
@@ -132,8 +193,7 @@ size_t SyncSocket::Receive(void* buffer, size_t length) {
size_t count = 0;
while (count < length) {
DWORD len;
- DWORD chunk = static_cast<DWORD>(
- ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
+ DWORD chunk = GetNextChunkSize(count, length);
if (ReadFile(handle_, static_cast<char*>(buffer) + count,
chunk, &len, NULL) == FALSE) {
return (0 < count) ? count : 0;
@@ -149,4 +209,45 @@ size_t SyncSocket::Peek() {
return available;
}
+CancelableSyncSocket::CancelableSyncSocket()
+ : shutdown_event_(true, false), file_operation_(true, false) {
+}
+
+CancelableSyncSocket::CancelableSyncSocket(Handle handle)
+ : SyncSocket(handle), shutdown_event_(true, false),
+ file_operation_(true, false) {
+}
+
+bool CancelableSyncSocket::Shutdown() {
+ // This doesn't shut down the pipe immediately, but subsequent Receive or Send
+ // methods will fail straight away.
+ shutdown_event_.Signal();
+ return true;
+}
+
+bool CancelableSyncSocket::Close() {
+ bool ret = SyncSocket::Close();
+ shutdown_event_.Reset();
+ return ret;
+}
+
+size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
+ return CancelableFileOperation(&WriteFile, handle_,
+ reinterpret_cast<const char*>(buffer), length, &file_operation_,
+ &shutdown_event_, this);
+}
+
+size_t CancelableSyncSocket::Receive(void* buffer, size_t length) {
+ return CancelableFileOperation(&ReadFile, handle_,
+ reinterpret_cast<char*>(buffer), length, &file_operation_,
+ &shutdown_event_, this);
+}
+
+// static
+bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
+ CancelableSyncSocket* socket_b) {
+ return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true);
+}
+
+
} // namespace base
« no previous file with comments | « base/sync_socket_posix.cc ('k') | content/browser/renderer_host/media/audio_input_sync_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698