Remove base/sync_socket*
Change-Id: I0cca2cba1fd51a0d5ca5eb90cf4c01d32f7b6338
Reviewed-on: https://gn-review.googlesource.com/1520
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
diff --git a/base/sync_socket.h b/base/sync_socket.h
deleted file mode 100644
index 9d582da..0000000
--- a/base/sync_socket.h
+++ /dev/null
@@ -1,161 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef BASE_SYNC_SOCKET_H_
-#define BASE_SYNC_SOCKET_H_
-
-// A socket abstraction used for sending and receiving plain
-// data. Because the receiving is blocking, they can be used to perform
-// rudimentary cross-process synchronization with low latency.
-
-#include <stddef.h>
-
-#include "base/base_export.h"
-#include "base/compiler_specific.h"
-#include "base/macros.h"
-#include "base/process/process_handle.h"
-#include "base/synchronization/waitable_event.h"
-#include "base/time/time.h"
-#include "build_config.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#endif
-#include <sys/types.h>
-
-#if defined(OS_POSIX) || defined(OS_FUCHSIA)
-#include "base/file_descriptor_posix.h"
-#endif
-
-namespace base {
-
-class BASE_EXPORT SyncSocket {
- public:
-#if defined(OS_WIN)
- typedef HANDLE Handle;
- typedef Handle TransitDescriptor;
-#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- typedef int Handle;
- typedef FileDescriptor TransitDescriptor;
-#endif
- static const Handle kInvalidHandle;
-
- SyncSocket();
-
- // Creates a SyncSocket from a Handle. Used in transport.
- explicit SyncSocket(Handle handle) : handle_(handle) {}
- virtual ~SyncSocket();
-
- // Initializes and connects a pair of sockets.
- // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
- // return, the sockets will both be valid and connected.
- static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
-
- // Returns |Handle| wrapped in a |TransitDescriptor|.
- static Handle UnwrapHandle(const TransitDescriptor& descriptor);
-
- // Prepares a |TransitDescriptor| which wraps |Handle| used for transit.
- // This is used to prepare the underlying shared resource before passing back
- // the handle to be used by the peer process.
- bool PrepareTransitDescriptor(ProcessHandle peer_process_handle,
- TransitDescriptor* descriptor);
-
- // Closes the SyncSocket. Returns true on success, false on failure.
- virtual bool Close();
-
- // Sends the message to the remote peer of the SyncSocket.
- // Note it is not safe to send messages from the same socket handle by
- // multiple threads simultaneously.
- // buffer is a pointer to the data to send.
- // length is the length of the data to send (must be non-zero).
- // Returns the number of bytes sent, or 0 upon failure.
- virtual size_t Send(const void* buffer, size_t length);
-
- // Receives a message from an SyncSocket.
- // buffer is a pointer to the buffer to receive data.
- // length is the number of bytes of data to receive (must be non-zero).
- // Returns the number of bytes received, or 0 upon failure.
- virtual size_t Receive(void* buffer, size_t length);
-
- // Same as Receive() but only blocks for data until |timeout| has elapsed or
- // |buffer| |length| is exhausted. Currently only timeouts less than one
- // second are allowed. Return the amount of data read.
- virtual size_t ReceiveWithTimeout(void* buffer,
- size_t length,
- TimeDelta timeout);
-
- // Returns the number of bytes available. If non-zero, Receive() will not
- // not block when called.
- virtual size_t Peek();
-
- // Extracts the contained handle. Used for transferring between
- // processes.
- Handle handle() const { return handle_; }
-
- // Extracts and takes ownership of the contained handle.
- Handle Release();
-
- protected:
- Handle handle_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(SyncSocket);
-};
-
-// Derives from SyncSocket and adds support for shutting down the socket from
-// another thread while a blocking Receive or Send is being done from the
-// thread that owns the socket.
-class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
- public:
- CancelableSyncSocket();
- explicit CancelableSyncSocket(Handle handle);
- ~CancelableSyncSocket() override = default;
-
- // Initializes a pair of cancelable sockets. See documentation for
- // SyncSocket::CreatePair for more details.
- static bool CreatePair(CancelableSyncSocket* socket_a,
- CancelableSyncSocket* socket_b);
-
- // A way to shut down a socket even if another thread is currently performing
- // a blocking Receive or Send.
- bool Shutdown();
-
-#if defined(OS_WIN)
- // Since the Linux and Mac implementations actually use a socket, shutting
- // them down from another thread is pretty simple - we can just call
- // shutdown(). However, the Windows implementation relies on named pipes
- // and there isn't a way to cancel a blocking synchronous Read that is
- // supported on <Vista. So, for Windows only, we override these
- // SyncSocket methods in order to support shutting down the 'socket'.
- bool Close() override;
- size_t Receive(void* buffer, size_t length) override;
- size_t ReceiveWithTimeout(void* buffer,
- size_t length,
- TimeDelta timeout) override;
-#endif
-
- // Send() is overridden to catch cases where the remote end is not responding
- // and we fill the local socket buffer. When the buffer is full, this
- // implementation of Send() will not block indefinitely as
- // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
- // Note that the socket will not be closed in this case.
- size_t Send(const void* buffer, size_t length) override;
-
- private:
-#if defined(OS_WIN)
- WaitableEvent shutdown_event_;
- WaitableEvent file_operation_;
-#endif
- DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
-};
-
-#if defined(OS_WIN) && !defined(COMPONENT_BUILD)
-// TODO(cpu): remove this once chrome is split in two dlls.
-__declspec(selectany)
- const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
-#endif
-
-} // namespace base
-
-#endif // BASE_SYNC_SOCKET_H_
diff --git a/base/sync_socket_nacl.cc b/base/sync_socket_nacl.cc
deleted file mode 100644
index 19a20be..0000000
--- a/base/sync_socket_nacl.cc
+++ /dev/null
@@ -1,105 +0,0 @@
-// Copyright (c) 2012 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 "base/sync_socket.h"
-
-#include <errno.h>
-#include <limits.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <sys/types.h>
-
-#include "base/logging.h"
-
-namespace base {
-
-const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
-
-SyncSocket::SyncSocket() : handle_(kInvalidHandle) {
-}
-
-SyncSocket::~SyncSocket() {
- Close();
-}
-
-// static
-bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
- return false;
-}
-
-// static
-SyncSocket::Handle SyncSocket::UnwrapHandle(
- const SyncSocket::TransitDescriptor& descriptor) {
- // TODO(xians): Still unclear how NaCl uses SyncSocket.
- // See http://crbug.com/409656
- NOTIMPLEMENTED();
- return SyncSocket::kInvalidHandle;
-}
-
-bool SyncSocket::PrepareTransitDescriptor(
- ProcessHandle peer_process_handle,
- SyncSocket::TransitDescriptor* descriptor) {
- // TODO(xians): Still unclear how NaCl uses SyncSocket.
- // See http://crbug.com/409656
- NOTIMPLEMENTED();
- return false;
-}
-
-bool SyncSocket::Close() {
- if (handle_ != kInvalidHandle) {
- if (close(handle_) < 0)
- DPLOG(ERROR) << "close";
- handle_ = kInvalidHandle;
- }
- return true;
-}
-
-size_t SyncSocket::Send(const void* buffer, size_t length) {
- const ssize_t bytes_written = write(handle_, buffer, length);
- return bytes_written > 0 ? bytes_written : 0;
-}
-
-size_t SyncSocket::Receive(void* buffer, size_t length) {
- const ssize_t bytes_read = read(handle_, buffer, length);
- return bytes_read > 0 ? bytes_read : 0;
-}
-
-size_t SyncSocket::ReceiveWithTimeout(void* buffer, size_t length, TimeDelta) {
- NOTIMPLEMENTED();
- return 0;
-}
-
-size_t SyncSocket::Peek() {
- NOTIMPLEMENTED();
- return 0;
-}
-
-SyncSocket::Handle SyncSocket::Release() {
- Handle r = handle_;
- handle_ = kInvalidHandle;
- return r;
-}
-
-CancelableSyncSocket::CancelableSyncSocket() {
-}
-
-CancelableSyncSocket::CancelableSyncSocket(Handle handle)
- : SyncSocket(handle) {
-}
-
-size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
- return SyncSocket::Send(buffer, length);
-}
-
-bool CancelableSyncSocket::Shutdown() {
- return SyncSocket::Close();
-}
-
-// static
-bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
- CancelableSyncSocket* socket_b) {
- return SyncSocket::CreatePair(socket_a, socket_b);
-}
-
-} // namespace base
diff --git a/base/sync_socket_posix.cc b/base/sync_socket_posix.cc
deleted file mode 100644
index 816bb73..0000000
--- a/base/sync_socket_posix.cc
+++ /dev/null
@@ -1,253 +0,0 @@
-// Copyright (c) 2012 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 "base/sync_socket.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <poll.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-
-#if defined(OS_SOLARIS)
-#include <sys/filio.h>
-#endif
-
-#include "base/files/file_util.h"
-#include "base/logging.h"
-#include "base/threading/thread_restrictions.h"
-#include "build_config.h"
-
-namespace base {
-
-namespace {
-// To avoid users sending negative message lengths to Send/Receive
-// we clamp message lengths, which are size_t, to no more than INT_MAX.
-const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
-
-// Writes |length| of |buffer| into |handle|. Returns the number of bytes
-// written or zero on error. |length| must be greater than 0.
-size_t SendHelper(SyncSocket::Handle handle,
- const void* buffer,
- size_t length) {
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle, SyncSocket::kInvalidHandle);
- const char* charbuffer = static_cast<const char*>(buffer);
- return WriteFileDescriptor(handle, charbuffer, length)
- ? static_cast<size_t>(length)
- : 0;
-}
-
-bool CloseHandle(SyncSocket::Handle handle) {
- if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) {
- DPLOG(ERROR) << "close";
- return false;
- }
-
- return true;
-}
-
-} // namespace
-
-const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
-
-SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
-
-SyncSocket::~SyncSocket() {
- Close();
-}
-
-// static
-bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
- DCHECK_NE(socket_a, socket_b);
- DCHECK_EQ(socket_a->handle_, kInvalidHandle);
- DCHECK_EQ(socket_b->handle_, kInvalidHandle);
-
-#if defined(OS_MACOSX)
- int nosigpipe = 1;
-#endif // defined(OS_MACOSX)
-
- Handle handles[2] = { kInvalidHandle, kInvalidHandle };
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
- CloseHandle(handles[0]);
- CloseHandle(handles[1]);
- return false;
- }
-
-#if defined(OS_MACOSX)
- // On OSX an attempt to read or write to a closed socket may generate a
- // SIGPIPE rather than returning -1. setsockopt will shut this off.
- if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
- &nosigpipe, sizeof nosigpipe) ||
- 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
- &nosigpipe, sizeof nosigpipe)) {
- CloseHandle(handles[0]);
- CloseHandle(handles[1]);
- return false;
- }
-#endif
-
- // Copy the handles out for successful return.
- socket_a->handle_ = handles[0];
- socket_b->handle_ = handles[1];
-
- return true;
-}
-
-// static
-SyncSocket::Handle SyncSocket::UnwrapHandle(
- const TransitDescriptor& descriptor) {
- return descriptor.fd;
-}
-
-bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle,
- TransitDescriptor* descriptor) {
- descriptor->fd = handle();
- descriptor->auto_close = false;
- return descriptor->fd != kInvalidHandle;
-}
-
-bool SyncSocket::Close() {
- const bool retval = CloseHandle(handle_);
- handle_ = kInvalidHandle;
- return retval;
-}
-
-size_t SyncSocket::Send(const void* buffer, size_t length) {
- AssertBlockingAllowed();
- return SendHelper(handle_, buffer, length);
-}
-
-size_t SyncSocket::Receive(void* buffer, size_t length) {
- AssertBlockingAllowed();
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle_, kInvalidHandle);
- char* charbuffer = static_cast<char*>(buffer);
- if (ReadFromFD(handle_, charbuffer, length))
- return length;
- return 0;
-}
-
-size_t SyncSocket::ReceiveWithTimeout(void* buffer,
- size_t length,
- TimeDelta timeout) {
- AssertBlockingAllowed();
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle_, kInvalidHandle);
-
- // Only timeouts greater than zero and less than one second are allowed.
- DCHECK_GT(timeout.InMicroseconds(), 0);
- DCHECK_LT(timeout.InMicroseconds(),
- TimeDelta::FromSeconds(1).InMicroseconds());
-
- // Track the start time so we can reduce the timeout as data is read.
- TimeTicks start_time = TimeTicks::Now();
- const TimeTicks finish_time = start_time + timeout;
-
- struct pollfd pollfd;
- pollfd.fd = handle_;
- pollfd.events = POLLIN;
- pollfd.revents = 0;
-
- size_t bytes_read_total = 0;
- while (bytes_read_total < length) {
- const TimeDelta this_timeout = finish_time - TimeTicks::Now();
- const int timeout_ms =
- static_cast<int>(this_timeout.InMillisecondsRoundedUp());
- if (timeout_ms <= 0)
- break;
- const int poll_result = poll(&pollfd, 1, timeout_ms);
- // Handle EINTR manually since we need to update the timeout value.
- if (poll_result == -1 && errno == EINTR)
- continue;
- // Return if other type of error or a timeout.
- if (poll_result <= 0)
- return bytes_read_total;
-
- // poll() only tells us that data is ready for reading, not how much. We
- // must Peek() for the amount ready for reading to avoid blocking.
- // At hang up (POLLHUP), the write end has been closed and there might still
- // be data to be read.
- // No special handling is needed for error (POLLERR); we can let any of the
- // following operations fail and handle it there.
- DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents;
- const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
-
- // There may be zero bytes to read if the socket at the other end closed.
- if (!bytes_to_read)
- return bytes_read_total;
-
- const size_t bytes_received =
- Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
- bytes_read_total += bytes_received;
- if (bytes_received != bytes_to_read)
- return bytes_read_total;
- }
-
- return bytes_read_total;
-}
-
-size_t SyncSocket::Peek() {
- DCHECK_NE(handle_, kInvalidHandle);
- int number_chars = 0;
- if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
- // If there is an error in ioctl, signal that the channel would block.
- return 0;
- }
- DCHECK_GE(number_chars, 0);
- return number_chars;
-}
-
-SyncSocket::Handle SyncSocket::Release() {
- Handle r = handle_;
- handle_ = kInvalidHandle;
- return r;
-}
-
-CancelableSyncSocket::CancelableSyncSocket() = default;
-CancelableSyncSocket::CancelableSyncSocket(Handle handle)
- : SyncSocket(handle) {
-}
-
-bool CancelableSyncSocket::Shutdown() {
- DCHECK_NE(handle_, kInvalidHandle);
- return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0;
-}
-
-size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle_, kInvalidHandle);
-
- const int flags = fcntl(handle_, F_GETFL);
- if (flags != -1 && (flags & O_NONBLOCK) == 0) {
- // Set the socket to non-blocking mode for sending if its original mode
- // is blocking.
- fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
- }
-
- const size_t len = SendHelper(handle_, buffer, length);
-
- if (flags != -1 && (flags & O_NONBLOCK) == 0) {
- // Restore the original flags.
- fcntl(handle_, F_SETFL, flags);
- }
-
- return len;
-}
-
-// static
-bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
- CancelableSyncSocket* socket_b) {
- return SyncSocket::CreatePair(socket_a, socket_b);
-}
-
-} // namespace base
diff --git a/base/sync_socket_win.cc b/base/sync_socket_win.cc
deleted file mode 100644
index 905d0a2..0000000
--- a/base/sync_socket_win.cc
+++ /dev/null
@@ -1,356 +0,0 @@
-// Copyright (c) 2012 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 "base/sync_socket.h"
-
-#include <limits.h>
-#include <stddef.h>
-
-#include "base/logging.h"
-#include "base/macros.h"
-#include "base/rand_util.h"
-#include "base/threading/thread_restrictions.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
-// creation. Also keep the secure random number generation.
-const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu";
-const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1;
-
-// To avoid users sending negative message lengths to Send/Receive
-// we clamp message lengths, which are size_t, to no more than INT_MAX.
-const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
-
-const int kOutBufferSize = 4096;
-const int kInBufferSize = 4096;
-const int kDefaultTimeoutMilliSeconds = 1000;
-
-bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) {
- DCHECK_NE(socket_a, socket_b);
- DCHECK_EQ(*socket_a, SyncSocket::kInvalidHandle);
- DCHECK_EQ(*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 long rnd_name;
- RandBytes(&rnd_name, sizeof(rnd_name));
-
- swprintf(name, kPipePathMax,
- kPipeNameFormat,
- GetCurrentProcessId(),
- GetCurrentThreadId(),
- rnd_name);
-
- handle_a.Set(CreateNamedPipeW(
- name,
- flags,
- PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
- 1,
- kOutBufferSize,
- kInBufferSize,
- kDefaultTimeoutMilliSeconds,
- NULL));
- } while (!handle_a.IsValid() &&
- (GetLastError() == ERROR_PIPE_BUSY));
-
- if (!handle_a.IsValid()) {
- NOTREACHED();
- return false;
- }
-
- // 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.
- 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(handle_a.Get(), NULL)) {
- DWORD error = GetLastError();
- if (error != ERROR_PIPE_CONNECTED) {
- DPLOG(ERROR) << "ConnectNamedPipe failed";
- return false;
- }
- }
-
- *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,
- WaitableEvent* io_event,
- WaitableEvent* cancel_event,
- CancelableSyncSocket* socket,
- DWORD timeout_in_ms) {
- AssertBlockingAllowed();
- // The buffer must be byte size or the length check won't make much sense.
- static_assert(sizeof(buffer[0]) == sizeof(char), "incorrect buffer type");
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(file, SyncSocket::kInvalidHandle);
-
- // Track the finish time so we can calculate the timeout as data is read.
- TimeTicks current_time, finish_time;
- if (timeout_in_ms != INFINITE) {
- current_time = TimeTicks::Now();
- finish_time =
- current_time + base::TimeDelta::FromMilliseconds(timeout_in_ms);
- }
-
- size_t count = 0;
- do {
- // The OVERLAPPED structure will be modified by ReadFile or WriteFile.
- OVERLAPPED ol = { 0 };
- ol.hEvent = io_event->handle();
-
- const DWORD chunk = GetNextChunkSize(count, length);
- // This is either the ReadFile or WriteFile call depending on whether
- // we're receiving or sending data.
- DWORD len = 0;
- const BOOL operation_ok = operation(
- file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol);
- if (!operation_ok) {
- if (::GetLastError() == ERROR_IO_PENDING) {
- HANDLE events[] = { io_event->handle(), cancel_event->handle() };
- const int wait_result = WaitForMultipleObjects(
- arraysize(events), events, FALSE,
- timeout_in_ms == INFINITE ?
- timeout_in_ms :
- static_cast<DWORD>(
- (finish_time - current_time).InMilliseconds()));
- if (wait_result != WAIT_OBJECT_0 + 0) {
- // CancelIo() doesn't synchronously cancel outstanding IO, only marks
- // outstanding IO for cancellation. We must call GetOverlappedResult()
- // below to ensure in flight writes complete before returning.
- CancelIo(file);
- }
-
- // We set the |bWait| parameter to TRUE for GetOverlappedResult() to
- // ensure writes are complete before returning.
- if (!GetOverlappedResult(file, &ol, &len, TRUE))
- len = 0;
-
- if (wait_result == WAIT_OBJECT_0 + 1) {
- DVLOG(1) << "Shutdown was signaled. Closing socket.";
- socket->Close();
- return count;
- }
-
- // Timeouts will be handled by the while() condition below since
- // GetOverlappedResult() may complete successfully after CancelIo().
- DCHECK(wait_result == WAIT_OBJECT_0 + 0 || wait_result == WAIT_TIMEOUT);
- } else {
- break;
- }
- }
-
- count += len;
-
- // Quit the operation if we can't write/read anymore.
- if (len != chunk)
- break;
-
- // Since TimeTicks::Now() is expensive, only bother updating the time if we
- // have more work to do.
- if (timeout_in_ms != INFINITE && count < length)
- current_time = base::TimeTicks::Now();
- } while (count < length &&
- (timeout_in_ms == INFINITE || current_time < finish_time));
-
- return count;
-}
-
-} // namespace
-
-#if defined(COMPONENT_BUILD)
-const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
-#endif
-
-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);
-}
-
-// static
-SyncSocket::Handle SyncSocket::UnwrapHandle(
- const TransitDescriptor& descriptor) {
- return descriptor;
-}
-
-bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle,
- TransitDescriptor* descriptor) {
- DCHECK(descriptor);
- if (!::DuplicateHandle(GetCurrentProcess(), handle(), peer_process_handle,
- descriptor, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
- DPLOG(ERROR) << "Cannot duplicate socket handle for peer process.";
- return false;
- }
- return true;
-}
-
-bool SyncSocket::Close() {
- if (handle_ == kInvalidHandle)
- return true;
-
- const BOOL result = CloseHandle(handle_);
- handle_ = kInvalidHandle;
- return result == TRUE;
-}
-
-size_t SyncSocket::Send(const void* buffer, size_t length) {
- AssertBlockingAllowed();
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle_, kInvalidHandle);
- size_t count = 0;
- while (count < length) {
- DWORD len;
- DWORD chunk = GetNextChunkSize(count, length);
- if (::WriteFile(handle_, static_cast<const char*>(buffer) + count, chunk,
- &len, NULL) == FALSE) {
- return count;
- }
- count += len;
- }
- return count;
-}
-
-size_t SyncSocket::ReceiveWithTimeout(void* buffer,
- size_t length,
- TimeDelta timeout) {
- NOTIMPLEMENTED();
- return 0;
-}
-
-size_t SyncSocket::Receive(void* buffer, size_t length) {
- AssertBlockingAllowed();
- DCHECK_GT(length, 0u);
- DCHECK_LE(length, kMaxMessageLength);
- DCHECK_NE(handle_, kInvalidHandle);
- size_t count = 0;
- while (count < length) {
- DWORD len;
- DWORD chunk = GetNextChunkSize(count, length);
- if (::ReadFile(handle_, static_cast<char*>(buffer) + count, chunk, &len,
- NULL) == FALSE) {
- return count;
- }
- count += len;
- }
- return count;
-}
-
-size_t SyncSocket::Peek() {
- DWORD available = 0;
- PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL);
- return available;
-}
-
-SyncSocket::Handle SyncSocket::Release() {
- Handle r = handle_;
- handle_ = kInvalidHandle;
- return r;
-}
-
-CancelableSyncSocket::CancelableSyncSocket()
- : shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED),
- file_operation_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED) {}
-
-CancelableSyncSocket::CancelableSyncSocket(Handle handle)
- : SyncSocket(handle),
- shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED),
- file_operation_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED) {}
-
-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() {
- const bool result = SyncSocket::Close();
- shutdown_event_.Reset();
- return result;
-}
-
-size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
- static const DWORD kWaitTimeOutInMs = 500;
- return CancelableFileOperation(
- &::WriteFile, handle_, reinterpret_cast<const char*>(buffer), length,
- &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs);
-}
-
-size_t CancelableSyncSocket::Receive(void* buffer, size_t length) {
- return CancelableFileOperation(
- &::ReadFile, handle_, reinterpret_cast<char*>(buffer), length,
- &file_operation_, &shutdown_event_, this, INFINITE);
-}
-
-size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer,
- size_t length,
- TimeDelta timeout) {
- return CancelableFileOperation(&::ReadFile, handle_,
- reinterpret_cast<char*>(buffer), length,
- &file_operation_, &shutdown_event_, this,
- static_cast<DWORD>(timeout.InMilliseconds()));
-}
-
-// static
-bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
- CancelableSyncSocket* socket_b) {
- return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true);
-}
-
-} // namespace base
diff --git a/build/gen.py b/build/gen.py
index 9e2ab8f..412fa9e 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -713,7 +713,6 @@
'base/profiler/win32_stack_frame_unwinder.cc',
'base/rand_util_win.cc',
'base/strings/sys_string_conversions_win.cc',
- 'base/sync_socket_win.cc',
'base/synchronization/condition_variable_win.cc',
'base/synchronization/lock_impl_win.cc',
'base/synchronization/waitable_event_watcher_win.cc',