[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sync_socket.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 6 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 8 | #include "base/threading/thread_restrictions.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 9 | #include "base/win/scoped_handle.h" |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 10 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 11 | namespace base { |
| 12 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 13 | using win::ScopedHandle; |
| 14 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 15 | namespace { |
[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 16 | // IMPORTANT: do not change how this name is generated because it will break |
| 17 | // in sandboxed scenarios as we might have by-name policies that allow pipe |
| 18 | // creation. Also keep the secure random number generation. |
| 19 | const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; |
| 20 | const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 21 | |
| 22 | // To avoid users sending negative message lengths to Send/Receive |
| 23 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 24 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 25 | |
| 26 | const int kOutBufferSize = 4096; |
| 27 | const int kInBufferSize = 4096; |
| 28 | const int kDefaultTimeoutMilliSeconds = 1000; |
| 29 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 30 | bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 31 | DCHECK_NE(socket_a, socket_b); |
| 32 | DCHECK_EQ(*socket_a, SyncSocket::kInvalidHandle); |
| 33 | DCHECK_EQ(*socket_b, SyncSocket::kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 34 | |
| 35 | wchar_t name[kPipePathMax]; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 36 | ScopedHandle handle_a; |
| 37 | DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; |
| 38 | if (overlapped) |
| 39 | flags |= FILE_FLAG_OVERLAPPED; |
| 40 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 41 | do { |
| 42 | unsigned int rnd_name; |
| 43 | if (rand_s(&rnd_name) != 0) |
| 44 | return false; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 45 | |
[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 46 | swprintf(name, kPipePathMax, |
| 47 | kPipeNameFormat, |
| 48 | GetCurrentProcessId(), |
| 49 | GetCurrentThreadId(), |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 50 | rnd_name); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 51 | |
| 52 | handle_a.Set(CreateNamedPipeW( |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 53 | name, |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 54 | flags, |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 55 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 56 | 1, |
| 57 | kOutBufferSize, |
| 58 | kInBufferSize, |
| 59 | kDefaultTimeoutMilliSeconds, |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 60 | NULL)); |
| 61 | } while (!handle_a.IsValid() && |
[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 62 | (GetLastError() == ERROR_PIPE_BUSY)); |
| 63 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 64 | if (!handle_a.IsValid()) { |
[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 65 | NOTREACHED(); |
| 66 | return false; |
| 67 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 68 | |
| 69 | // The SECURITY_ANONYMOUS flag means that the server side (handle_a) cannot |
| 70 | // impersonate the client (handle_b). This allows us not to care which side |
[email protected] | 774bdcc | 2012-01-19 03:44:38 | [diff] [blame] | 71 | // ends up in which side of a privilege boundary. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 72 | flags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS; |
| 73 | if (overlapped) |
| 74 | flags |= FILE_FLAG_OVERLAPPED; |
| 75 | |
| 76 | ScopedHandle handle_b(CreateFileW(name, |
| 77 | GENERIC_READ | GENERIC_WRITE, |
| 78 | 0, // no sharing. |
| 79 | NULL, // default security attributes. |
| 80 | OPEN_EXISTING, // opens existing pipe. |
| 81 | flags, |
| 82 | NULL)); // no template file. |
| 83 | if (!handle_b.IsValid()) { |
| 84 | DPLOG(ERROR) << "CreateFileW failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 85 | return false; |
| 86 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 87 | |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 88 | if (!ConnectNamedPipe(handle_a.Get(), NULL)) { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 89 | DWORD error = GetLastError(); |
| 90 | if (error != ERROR_PIPE_CONNECTED) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 91 | DPLOG(ERROR) << "ConnectNamedPipe failed"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 95 | |
| 96 | *socket_a = handle_a.Take(); |
| 97 | *socket_b = handle_b.Take(); |
| 98 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 102 | // Inline helper to avoid having the cast everywhere. |
| 103 | DWORD GetNextChunkSize(size_t current_pos, size_t max_size) { |
| 104 | // The following statement is for 64 bit portability. |
| 105 | return static_cast<DWORD>(((max_size - current_pos) <= UINT_MAX) ? |
| 106 | (max_size - current_pos) : UINT_MAX); |
| 107 | } |
| 108 | |
| 109 | // Template function that supports calling ReadFile or WriteFile in an |
| 110 | // overlapped fashion and waits for IO completion. The function also waits |
| 111 | // on an event that can be used to cancel the operation. If the operation |
| 112 | // is cancelled, the function returns and closes the relevant socket object. |
| 113 | template <typename BufferType, typename Function> |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 114 | size_t CancelableFileOperation(Function operation, |
| 115 | HANDLE file, |
| 116 | BufferType* buffer, |
| 117 | size_t length, |
| 118 | WaitableEvent* io_event, |
| 119 | WaitableEvent* cancel_event, |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 120 | CancelableSyncSocket* socket, |
| 121 | DWORD timeout_in_ms) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 122 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 123 | // The buffer must be byte size or the length check won't make much sense. |
| 124 | COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 125 | DCHECK_GT(length, 0u); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 126 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 127 | DCHECK_NE(file, SyncSocket::kInvalidHandle); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 128 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 129 | // Track the finish time so we can calculate the timeout as data is read. |
| 130 | TimeTicks current_time, finish_time; |
| 131 | if (timeout_in_ms != INFINITE) { |
| 132 | current_time = TimeTicks::Now(); |
| 133 | finish_time = |
| 134 | current_time + base::TimeDelta::FromMilliseconds(timeout_in_ms); |
| 135 | } |
| 136 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 137 | size_t count = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 138 | do { |
| 139 | // The OVERLAPPED structure will be modified by ReadFile or WriteFile. |
| 140 | OVERLAPPED ol = { 0 }; |
| 141 | ol.hEvent = io_event->handle(); |
| 142 | |
| 143 | const DWORD chunk = GetNextChunkSize(count, length); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 144 | // This is either the ReadFile or WriteFile call depending on whether |
| 145 | // we're receiving or sending data. |
[email protected] | 23bf698 | 2012-02-03 12:44:44 | [diff] [blame] | 146 | DWORD len = 0; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 147 | const BOOL operation_ok = operation( |
| 148 | file, static_cast<BufferType*>(buffer) + count, chunk, &len, &ol); |
| 149 | if (!operation_ok) { |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 150 | if (::GetLastError() == ERROR_IO_PENDING) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 151 | HANDLE events[] = { io_event->handle(), cancel_event->handle() }; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 152 | const int wait_result = WaitForMultipleObjects( |
viettrungluu | 805eabb | 2014-10-16 04:02:49 | [diff] [blame] | 153 | arraysize(events), events, FALSE, |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 154 | timeout_in_ms == INFINITE ? |
| 155 | timeout_in_ms : |
| 156 | static_cast<DWORD>( |
| 157 | (finish_time - current_time).InMilliseconds())); |
dalecurtis | 3bdbb01 | 2014-09-16 00:41:22 | [diff] [blame] | 158 | if (wait_result != WAIT_OBJECT_0 + 0) { |
| 159 | // CancelIo() doesn't synchronously cancel outstanding IO, only marks |
| 160 | // outstanding IO for cancellation. We must call GetOverlappedResult() |
| 161 | // below to ensure in flight writes complete before returning. |
[email protected] | 23bf698 | 2012-02-03 12:44:44 | [diff] [blame] | 162 | CancelIo(file); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 163 | } |
dalecurtis | 3bdbb01 | 2014-09-16 00:41:22 | [diff] [blame] | 164 | |
| 165 | // We set the |bWait| parameter to TRUE for GetOverlappedResult() to |
| 166 | // ensure writes are complete before returning. |
| 167 | if (!GetOverlappedResult(file, &ol, &len, TRUE)) |
| 168 | len = 0; |
| 169 | |
| 170 | if (wait_result == WAIT_OBJECT_0 + 1) { |
| 171 | DVLOG(1) << "Shutdown was signaled. Closing socket."; |
| 172 | socket->Close(); |
| 173 | return count; |
| 174 | } |
| 175 | |
| 176 | // Timeouts will be handled by the while() condition below since |
| 177 | // GetOverlappedResult() may complete successfully after CancelIo(). |
| 178 | DCHECK(wait_result == WAIT_OBJECT_0 + 0 || wait_result == WAIT_TIMEOUT); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 179 | } else { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 180 | break; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 181 | } |
| 182 | } |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 183 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 184 | count += len; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 185 | |
| 186 | // Quit the operation if we can't write/read anymore. |
| 187 | if (len != chunk) |
| 188 | break; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 189 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 190 | // Since TimeTicks::Now() is expensive, only bother updating the time if we |
| 191 | // have more work to do. |
| 192 | if (timeout_in_ms != INFINITE && count < length) |
| 193 | current_time = base::TimeTicks::Now(); |
| 194 | } while (count < length && |
| 195 | (timeout_in_ms == INFINITE || current_time < finish_time)); |
| 196 | |
| 197 | return count; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | } // namespace |
| 201 | |
[email protected] | 0f248ef | 2013-05-23 04:34:11 | [diff] [blame] | 202 | #if defined(COMPONENT_BUILD) |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 203 | const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
[email protected] | 0f248ef | 2013-05-23 04:34:11 | [diff] [blame] | 204 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 205 | |
| 206 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 207 | |
| 208 | SyncSocket::~SyncSocket() { |
| 209 | Close(); |
| 210 | } |
| 211 | |
| 212 | // static |
| 213 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 214 | return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false); |
| 215 | } |
| 216 | |
burnik | 3d67005 | 2014-09-08 06:58:22 | [diff] [blame] | 217 | // static |
| 218 | SyncSocket::Handle SyncSocket::UnwrapHandle( |
| 219 | const TransitDescriptor& descriptor) { |
| 220 | return descriptor; |
| 221 | } |
| 222 | |
| 223 | bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle, |
| 224 | TransitDescriptor* descriptor) { |
| 225 | DCHECK(descriptor); |
| 226 | if (!::DuplicateHandle(GetCurrentProcess(), handle(), peer_process_handle, |
| 227 | descriptor, 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 228 | DPLOG(ERROR) << "Cannot duplicate socket handle for peer process."; |
| 229 | return false; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 234 | bool SyncSocket::Close() { |
| 235 | if (handle_ == kInvalidHandle) |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 236 | return true; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 237 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 238 | const BOOL result = CloseHandle(handle_); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 239 | handle_ = kInvalidHandle; |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 240 | return result == TRUE; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 244 | ThreadRestrictions::AssertIOAllowed(); |
| 245 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 246 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 247 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 248 | size_t count = 0; |
| 249 | while (count < length) { |
| 250 | DWORD len; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 251 | DWORD chunk = GetNextChunkSize(count, length); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 252 | if (WriteFile(handle_, static_cast<const char*>(buffer) + count, |
| 253 | chunk, &len, NULL) == FALSE) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 254 | return count; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 255 | } |
| 256 | count += len; |
| 257 | } |
| 258 | return count; |
| 259 | } |
| 260 | |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 261 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, |
| 262 | size_t length, |
| 263 | TimeDelta timeout) { |
| 264 | NOTIMPLEMENTED(); |
| 265 | return 0; |
| 266 | } |
| 267 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 268 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 269 | ThreadRestrictions::AssertIOAllowed(); |
| 270 | DCHECK_GT(length, 0u); |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 271 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 272 | DCHECK_NE(handle_, kInvalidHandle); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 273 | size_t count = 0; |
| 274 | while (count < length) { |
| 275 | DWORD len; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 276 | DWORD chunk = GetNextChunkSize(count, length); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 277 | if (ReadFile(handle_, static_cast<char*>(buffer) + count, |
| 278 | chunk, &len, NULL) == FALSE) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 279 | return count; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 280 | } |
| 281 | count += len; |
| 282 | } |
| 283 | return count; |
| 284 | } |
| 285 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 286 | size_t SyncSocket::Peek() { |
| 287 | DWORD available = 0; |
| 288 | PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL); |
| 289 | return available; |
| 290 | } |
| 291 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 292 | CancelableSyncSocket::CancelableSyncSocket() |
| 293 | : shutdown_event_(true, false), file_operation_(true, false) { |
| 294 | } |
| 295 | |
| 296 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 297 | : SyncSocket(handle), shutdown_event_(true, false), |
| 298 | file_operation_(true, false) { |
| 299 | } |
| 300 | |
| 301 | bool CancelableSyncSocket::Shutdown() { |
| 302 | // This doesn't shut down the pipe immediately, but subsequent Receive or Send |
| 303 | // methods will fail straight away. |
| 304 | shutdown_event_.Signal(); |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | bool CancelableSyncSocket::Close() { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 309 | const bool result = SyncSocket::Close(); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 310 | shutdown_event_.Reset(); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 311 | return result; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 315 | static const DWORD kWaitTimeOutInMs = 500; |
| 316 | return CancelableFileOperation( |
| 317 | &WriteFile, handle_, reinterpret_cast<const char*>(buffer), |
| 318 | length, &file_operation_, &shutdown_event_, this, kWaitTimeOutInMs); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 322 | return CancelableFileOperation( |
| 323 | &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
| 324 | &file_operation_, &shutdown_event_, this, INFINITE); |
| 325 | } |
| 326 | |
| 327 | size_t CancelableSyncSocket::ReceiveWithTimeout(void* buffer, |
| 328 | size_t length, |
| 329 | TimeDelta timeout) { |
| 330 | return CancelableFileOperation( |
| 331 | &ReadFile, handle_, reinterpret_cast<char*>(buffer), length, |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 332 | &file_operation_, &shutdown_event_, this, |
| 333 | static_cast<DWORD>(timeout.InMilliseconds())); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // static |
| 337 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 338 | CancelableSyncSocket* socket_b) { |
| 339 | return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); |
| 340 | } |
| 341 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 342 | } // namespace base |