enumAsync.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "main.h"
  2. #include "./enumAsync.h"
  3. #include "./ifc_omservice.h"
  4. #include "./ifc_omserviceenum.h"
  5. #include "./ifc_wasabihelper.h"
  6. #include "./serviceList.h"
  7. EnumAsyncWrapper::EnumAsyncWrapper(ifc_omserviceenum *enumerator)
  8. : ref(1), enumerator(enumerator), userCallback(NULL), userData(NULL),
  9. completed(NULL), state(stateReady), resultCode(E_PENDING), serviceList(NULL)
  10. {
  11. if (NULL != enumerator)
  12. enumerator->AddRef();
  13. InitializeCriticalSection(&lock);
  14. }
  15. EnumAsyncWrapper::~EnumAsyncWrapper()
  16. {
  17. EnterCriticalSection(&lock);
  18. if (NULL != enumerator)
  19. enumerator->Release();
  20. if (NULL != completed)
  21. CloseHandle(completed);
  22. if (NULL != serviceList)
  23. serviceList->Release();
  24. LeaveCriticalSection(&lock);
  25. DeleteCriticalSection(&lock);
  26. }
  27. HRESULT EnumAsyncWrapper::CreateInstance(ifc_omserviceenum *enumerator, EnumAsyncWrapper **instance)
  28. {
  29. if (NULL == instance) return E_POINTER;
  30. *instance = NULL;
  31. if (NULL == enumerator)
  32. return E_INVALIDARG;
  33. *instance = new EnumAsyncWrapper(enumerator);
  34. if (NULL == *instance) return E_OUTOFMEMORY;
  35. return S_OK;
  36. }
  37. size_t EnumAsyncWrapper::AddRef()
  38. {
  39. return InterlockedIncrement((LONG*)&ref);
  40. }
  41. size_t EnumAsyncWrapper::Release()
  42. {
  43. if (0 == ref)
  44. return ref;
  45. LONG r = InterlockedDecrement((LONG*)&ref);
  46. if (0 == r)
  47. delete(this);
  48. return r;
  49. }
  50. int EnumAsyncWrapper::QueryInterface(GUID interface_guid, void **object)
  51. {
  52. if (NULL == object) return E_POINTER;
  53. if (IsEqualIID(interface_guid, IFC_OmStorageAsync))
  54. *object = static_cast<ifc_omstorageasync*>(this);
  55. else
  56. {
  57. *object = NULL;
  58. return E_NOINTERFACE;
  59. }
  60. if (NULL == *object)
  61. return E_UNEXPECTED;
  62. AddRef();
  63. return S_OK;
  64. }
  65. HRESULT EnumAsyncWrapper::GetState(UINT *state)
  66. {
  67. if (NULL == state)
  68. return E_POINTER;
  69. EnterCriticalSection(&lock);
  70. *state = this->state;
  71. LeaveCriticalSection(&lock);
  72. return S_OK;
  73. }
  74. HRESULT EnumAsyncWrapper::GetWaitHandle(HANDLE *handle)
  75. {
  76. if (NULL == handle)
  77. return E_POINTER;
  78. HRESULT hr = S_OK;
  79. EnterCriticalSection(&lock);
  80. if (NULL == completed)
  81. {
  82. completed = CreateEvent(NULL, TRUE, FALSE, NULL);
  83. if (NULL == completed)
  84. {
  85. *handle = NULL;
  86. DWORD error = GetLastError();
  87. hr = HRESULT_FROM_WIN32(error);
  88. }
  89. }
  90. if (SUCCEEDED(hr) && FALSE == DuplicateHandle(GetCurrentProcess(), completed,
  91. GetCurrentProcess(), handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
  92. {
  93. *handle = NULL;
  94. DWORD error = GetLastError();
  95. hr = HRESULT_FROM_WIN32(error);
  96. }
  97. LeaveCriticalSection(&lock);
  98. return hr;
  99. }
  100. HRESULT EnumAsyncWrapper::GetData(void **data)
  101. {
  102. if (NULL == data)
  103. return E_POINTER;
  104. EnterCriticalSection(&lock);
  105. *data = userData;
  106. LeaveCriticalSection(&lock);
  107. return S_OK;
  108. }
  109. HRESULT EnumAsyncWrapper::GetCallback(AsyncCallback *callback)
  110. {
  111. if (NULL == callback)
  112. return E_POINTER;
  113. EnterCriticalSection(&lock);
  114. *callback = userCallback;
  115. LeaveCriticalSection(&lock);
  116. return S_OK;
  117. }
  118. HRESULT EnumAsyncWrapper::SetData(void *data)
  119. {
  120. EnterCriticalSection(&lock);
  121. userData = data;
  122. LeaveCriticalSection(&lock);
  123. return S_OK;
  124. }
  125. HRESULT EnumAsyncWrapper::SetCallback(AsyncCallback callback)
  126. {
  127. EnterCriticalSection(&lock);
  128. userCallback = callback;
  129. LeaveCriticalSection(&lock);
  130. return S_OK;
  131. }
  132. HRESULT EnumAsyncWrapper::RequestAbort(BOOL fDrop)
  133. {
  134. HRESULT hr = S_FALSE;
  135. EnterCriticalSection(&lock);
  136. if (stateInitializing == state || stateReceiving == state)
  137. {
  138. state = stateAborting;
  139. if (FALSE != fDrop)
  140. {
  141. userCallback = NULL;
  142. userData = NULL;
  143. }
  144. }
  145. LeaveCriticalSection(&lock);
  146. return hr;
  147. }
  148. static int EnumAsyncWrapper_ThreadFunc(HANDLE handle, void *user_data, intptr_t id)
  149. {
  150. EnumAsyncWrapper *instance = (EnumAsyncWrapper*)user_data;
  151. if (NULL != instance) instance->Enumerate();
  152. return 0;
  153. }
  154. HRESULT EnumAsyncWrapper::BeginEnumerate()
  155. {
  156. ifc_wasabihelper *wasabi = NULL;
  157. HRESULT hr;
  158. EnterCriticalSection(&lock);
  159. if (stateReady != state && stateCompleted != state)
  160. {
  161. hr = E_PENDING;
  162. }
  163. else
  164. {
  165. state = stateInitializing;
  166. if (NULL != serviceList)
  167. {
  168. serviceList->Release();
  169. serviceList = NULL;
  170. }
  171. hr = Plugin_GetWasabiHelper(&wasabi);
  172. if (SUCCEEDED(hr) && wasabi != NULL)
  173. {
  174. api_threadpool *threadpool;
  175. hr = wasabi->GetThreadpoolApi(&threadpool);
  176. if (SUCCEEDED(hr))
  177. {
  178. if (0 != threadpool->RunFunction(NULL, EnumAsyncWrapper_ThreadFunc, this, 0, 0))
  179. {
  180. hr = E_FAIL;
  181. }
  182. else
  183. {
  184. AddRef();
  185. }
  186. threadpool->Release();
  187. }
  188. else
  189. {
  190. hr = E_NOINTERFACE;
  191. }
  192. wasabi->Release();
  193. }
  194. if (FAILED(hr))
  195. state = stateCompleted;
  196. }
  197. LeaveCriticalSection(&lock);
  198. return hr;
  199. }
  200. HRESULT EnumAsyncWrapper::Enumerate()
  201. {
  202. EnterCriticalSection(&lock);
  203. state = stateReceiving;
  204. resultCode = OmServiceList::CreateInstance(&serviceList);
  205. LeaveCriticalSection(&lock);
  206. if (SUCCEEDED(resultCode))
  207. {
  208. if (NULL == enumerator)
  209. {
  210. resultCode = E_UNEXPECTED;
  211. }
  212. else
  213. {
  214. ifc_omservice *service;
  215. while(S_OK == enumerator->Next(1, &service, NULL))
  216. {
  217. if (stateAborting == state)
  218. {
  219. resultCode = E_ABORT;
  220. break;
  221. }
  222. if (NULL != service)
  223. {
  224. serviceList->Add(service);
  225. service->Release();
  226. }
  227. }
  228. }
  229. }
  230. EnterCriticalSection(&lock);
  231. state = stateCompleted;
  232. HANDLE event = completed;
  233. LeaveCriticalSection(&lock);
  234. if (NULL != event)
  235. {
  236. SetEvent(event);
  237. }
  238. EnterCriticalSection(&lock);
  239. AsyncCallback cb = userCallback;
  240. LeaveCriticalSection(&lock);
  241. if (NULL != cb)
  242. {
  243. cb(this);
  244. }
  245. Release();
  246. return resultCode;
  247. }
  248. HRESULT EnumAsyncWrapper::GetResultCode()
  249. {
  250. return resultCode;
  251. }
  252. HRESULT EnumAsyncWrapper::GetServiceList(ifc_omserviceenum **list)
  253. {
  254. if (NULL == list)
  255. return E_POINTER;
  256. if (NULL == serviceList)
  257. {
  258. *list = NULL;
  259. return E_UNEXPECTED;
  260. }
  261. EnterCriticalSection(&lock);
  262. *list = serviceList;
  263. serviceList->AddRef();
  264. LeaveCriticalSection(&lock);
  265. return S_OK;
  266. }
  267. #define CBCLASS EnumAsyncWrapper
  268. START_DISPATCH;
  269. CB(ADDREF, AddRef)
  270. CB(RELEASE, Release)
  271. CB(QUERYINTERFACE, QueryInterface)
  272. CB(API_GETSTATE, GetState)
  273. CB(API_GETWAITHANDLE, GetWaitHandle)
  274. CB(API_GETDATA, GetData)
  275. END_DISPATCH;
  276. #undef CBCLASS