test_utils_win.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2014 The Chromium Authors. All rights reserved. 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 "mojo/common/test/test_utils.h" 6 7#include <windows.h> 8 9#include "mojo/system/embedder/platform_handle.h" 10 11namespace mojo { 12namespace test { 13 14bool BlockingWrite(const embedder::PlatformHandle& handle, 15 const void* buffer, 16 size_t bytes_to_write, 17 size_t* bytes_written) { 18 OVERLAPPED overlapped = { 0 }; 19 DWORD bytes_written_dword = 0; 20 21 if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write), 22 &bytes_written_dword, &overlapped)) { 23 if (GetLastError() != ERROR_IO_PENDING || 24 !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword, 25 TRUE)) { 26 return false; 27 } 28 } 29 30 *bytes_written = bytes_written_dword; 31 return true; 32} 33 34bool BlockingRead(const embedder::PlatformHandle& handle, 35 void* buffer, 36 size_t buffer_size, 37 size_t* bytes_read) { 38 OVERLAPPED overlapped = { 0 }; 39 DWORD bytes_read_dword = 0; 40 41 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), 42 &bytes_read_dword, &overlapped)) { 43 if (GetLastError() != ERROR_IO_PENDING || 44 !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, 45 TRUE)) { 46 return false; 47 } 48 } 49 50 *bytes_read = bytes_read_dword; 51 return true; 52} 53 54bool NonBlockingRead(const embedder::PlatformHandle& handle, 55 void* buffer, 56 size_t buffer_size, 57 size_t* bytes_read) { 58 OVERLAPPED overlapped = { 0 }; 59 DWORD bytes_read_dword = 0; 60 61 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), 62 &bytes_read_dword, &overlapped)) { 63 if (GetLastError() != ERROR_IO_PENDING) 64 return false; 65 66 CancelIo(handle.handle); 67 68 if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, 69 TRUE)) { 70 *bytes_read = 0; 71 return true; 72 } 73 } 74 75 *bytes_read = bytes_read_dword; 76 return true; 77} 78 79} // namespace test 80} // namespace mojo 81