Chromium Code Reviews| Index: base/sync_socket_win.cc |
| =================================================================== |
| --- base/sync_socket_win.cc (revision 118668) |
| +++ base/sync_socket_win.cc (working copy) |
| @@ -3,12 +3,12 @@ |
| // 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" |
| +using base::win::ScopedHandle; |
|
darin (slow to review)
2012/01/24 18:59:04
nit: you could put this inside the "namespace base
tommi (sloooow) - chröme
2012/01/24 21:03:25
Done.
|
| + |
| namespace base { |
| namespace { |
| @@ -26,85 +26,143 @@ |
| const int kInBufferSize = 4096; |
| const int kDefaultTimeoutMilliSeconds = 1000; |
| -} // namespace |
| +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); |
| -const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
| + wchar_t name[kPipePathMax]; |
| + ScopedHandle handle_a; |
| + DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; |
| + if (overlapped) |
| + flags |= FILE_FLAG_OVERLAPPED; |
| -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; |
| - } |
| - |
| - wchar_t name[kPipePathMax]; |
| 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; |
| } |
| +// 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 len; |
| + // The following statement is for 64 bit portability. |
| + DWORD chunk = static_cast<DWORD>( |
| + ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| + // This is either the ReadFile or WriteFile call depending on whether |
| + // we're receiving or sending data. |
| + 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; |
| @@ -117,7 +175,7 @@ |
| DWORD len; |
| // The following statement is for 64 bit portability. |
| DWORD chunk = static_cast<DWORD>( |
|
darin (slow to review)
2012/01/24 18:59:04
Perhaps an inline helper function would be useful.
tommi (sloooow) - chröme
2012/01/24 21:03:25
Done.
|
| - ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| + ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| if (WriteFile(handle_, static_cast<const char*>(buffer) + count, |
| chunk, &len, NULL) == FALSE) { |
| return (0 < count) ? count : 0; |
| @@ -133,7 +191,7 @@ |
| while (count < length) { |
| DWORD len; |
| DWORD chunk = static_cast<DWORD>( |
| - ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| + ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| if (ReadFile(handle_, static_cast<char*>(buffer) + count, |
| chunk, &len, NULL) == FALSE) { |
| return (0 < count) ? count : 0; |
| @@ -149,4 +207,45 @@ |
| 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 |