adb_io_completion.cpp revision dceaaa52cec11631c72cfea5fb74ee607602ecde
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** \file
18  This file consists of implementation of class AdbIOCompletion that
19  encapsulates a wrapper around OVERLAPPED Win32 structure returned
20  from asynchronous I/O requests.
21*/
22
23#include "stdafx.h"
24#include "adb_io_completion.h"
25
26AdbIOCompletion::AdbIOCompletion(AdbEndpointObject* parent_io_obj,
27                                 ULONG expected_trans_size,
28                                 HANDLE event_hndl)
29    : AdbObjectHandle(AdbObjectTypeIoCompletion),
30      expected_transfer_size_(expected_trans_size),
31      parent_io_object_(parent_io_obj) {
32  ATLASSERT(NULL != parent_io_obj);
33  parent_io_obj->AddRef();
34  ZeroMemory(&overlapped_, sizeof(overlapped_));
35  overlapped_.hEvent = event_hndl;
36}
37
38AdbIOCompletion::~AdbIOCompletion() {
39  parent_io_object_->Release();
40}
41
42bool AdbIOCompletion::GetOvelappedIoResult(LPOVERLAPPED ovl_data,
43                                           ULONG* bytes_transferred,
44                                           bool wait) {
45  if (NULL != bytes_transferred)
46    *bytes_transferred = 0;
47
48  if (!IsOpened()) {
49    SetLastError(ERROR_INVALID_HANDLE);
50    return false;
51  }
52
53  ULONG transfer;
54  bool ret = WinUsb_GetOverlappedResult(parent_io_object()->winusb_handle(),
55                                        overlapped(),
56                                        &transfer,
57                                        wait ? TRUE : FALSE) ? true : false;
58
59  // TODO: This is bizzare but I've seen it happening
60  // that GetOverlappedResult with wait set to true returns "prematurely",
61  // with wrong transferred bytes value and GetLastError reporting
62  // ERROR_IO_PENDING. So, lets give it an up to a 20 ms loop!
63  ULONG error = GetLastError();
64
65  if (wait && ret && (0 == transfer) && (0 != expected_transfer_size_) &&
66      ((ERROR_IO_INCOMPLETE == error) || (ERROR_IO_PENDING == error))) {
67    for (int trying = 0; trying < 10; trying++) {
68      Sleep(2);
69      ret = WinUsb_GetOverlappedResult(parent_io_object()->winusb_handle(),
70                                       overlapped(),
71                                       &transfer,
72                                       wait ? TRUE : FALSE) ? true : false;
73      error = GetLastError();
74      if (!ret || (0 != transfer) ||
75          ((ERROR_IO_INCOMPLETE != error) && (ERROR_IO_PENDING != error))) {
76        break;
77      }
78    }
79  }
80
81  if (NULL != ovl_data)
82    CopyMemory(ovl_data, overlapped(), sizeof(OVERLAPPED));
83
84  if (NULL != bytes_transferred)
85    *bytes_transferred = transfer;
86
87  return ret;
88}
89
90bool AdbIOCompletion::IsCompleted() {
91  SetLastError(NO_ERROR);
92  if (!IsOpened()) {
93    SetLastError(ERROR_INVALID_HANDLE);
94    return true;
95  }
96
97  return HasOverlappedIoCompleted(overlapped()) ? true : false;
98}
99