Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
3.6 KiB

  1. #include "precomp.h"
  2. PXFER_LIST
  3. CreateXferList(
  4. VOID
  5. )
  6. {
  7. PXFER_LIST XferList;
  8. XferList=(PXFER_LIST)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*XferList));
  9. if (XferList == NULL) {
  10. return NULL;
  11. }
  12. ZeroMemory(XferList,sizeof(XferList));
  13. XferList->CloseEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  14. if (XferList->CloseEvent == NULL) {
  15. HeapFree(GetProcessHeap(),0,XferList);
  16. return NULL;
  17. }
  18. _try {
  19. InitializeCriticalSection(
  20. &XferList->Lock
  21. );
  22. } _except(EXCEPTION_EXECUTE_HANDLER) {
  23. CloseHandle(XferList->CloseEvent);
  24. HeapFree(GetProcessHeap(),0,XferList);
  25. return NULL;
  26. }
  27. return XferList;
  28. }
  29. VOID
  30. DeleteXferList(
  31. PXFER_LIST XferList
  32. )
  33. {
  34. ULONG i;
  35. EnterCriticalSection(&XferList->Lock);
  36. XferList->Closing=TRUE;
  37. for (i=0; i<MAX_TRANSFERS; i++) {
  38. if (XferList->List[i] != NULL) {
  39. XferList->List[i]->StopListening();
  40. }
  41. }
  42. if (XferList->Transfers == 0 ) {
  43. SetEvent(XferList->CloseEvent);
  44. }
  45. LeaveCriticalSection(&XferList->Lock);
  46. WaitForSingleObject(XferList->CloseEvent,INFINITE);
  47. CloseHandle(XferList->CloseEvent);
  48. DeleteCriticalSection(
  49. &XferList->Lock
  50. );
  51. HeapFree(GetProcessHeap(),0,XferList);
  52. return;
  53. }
  54. BOOL
  55. AddTransferToList(
  56. PXFER_LIST XferList,
  57. FILE_TRANSFER* FileTransfer
  58. )
  59. {
  60. ULONG i;
  61. BOOL bReturn=FALSE;
  62. EnterCriticalSection(&XferList->Lock);
  63. if (!XferList->Closing) {
  64. for (i=0; i<MAX_TRANSFERS; i++) {
  65. if (XferList->List[i] == NULL) {
  66. XferList->List[i]=FileTransfer;
  67. InterlockedIncrement(&XferList->Transfers);
  68. bReturn=TRUE;
  69. break;
  70. }
  71. }
  72. }
  73. LeaveCriticalSection(&XferList->Lock);
  74. return bReturn;
  75. }
  76. BOOL
  77. RemoveTransferFromList(
  78. PXFER_LIST XferList,
  79. FILE_TRANSFER* FileTransfer
  80. )
  81. {
  82. ULONG i;
  83. BOOL bReturn=FALSE;
  84. LONG Count;
  85. EnterCriticalSection(&XferList->Lock);
  86. for (i=0; i<MAX_TRANSFERS; i++) {
  87. if (XferList->List[i] == FileTransfer) {
  88. XferList->List[i]=NULL;
  89. Count=InterlockedDecrement(&XferList->Transfers);
  90. bReturn=TRUE;
  91. break;
  92. }
  93. }
  94. if ((Count == 0) && (XferList->Closing)) {
  95. SetEvent(XferList->CloseEvent);
  96. }
  97. LeaveCriticalSection(&XferList->Lock);
  98. return TRUE;
  99. }
  100. BOOL
  101. AreThereActiveTransfers(
  102. PXFER_LIST XferList
  103. )
  104. {
  105. ULONG i;
  106. BOOL bReturn=FALSE;
  107. EnterCriticalSection(&XferList->Lock);
  108. for (i=0; i<MAX_TRANSFERS; i++) {
  109. if (XferList->List[i] != NULL) {
  110. if (XferList->List[i]->IsActive()) {
  111. bReturn=TRUE;
  112. break;
  113. }
  114. }
  115. }
  116. LeaveCriticalSection(&XferList->Lock);
  117. return bReturn;
  118. }
  119. FILE_TRANSFER*
  120. TransferFromCookie(
  121. PXFER_LIST XferList,
  122. __int64 Cookie
  123. )
  124. {
  125. ULONG i;
  126. BOOL bReturn=FALSE;
  127. FILE_TRANSFER* Transfer=NULL;
  128. EnterCriticalSection(&XferList->Lock);
  129. for (i=0; i<MAX_TRANSFERS; i++) {
  130. if (XferList->List[i] != NULL) {
  131. if (XferList->List[i]->GetCookie() == Cookie) {
  132. Transfer=XferList->List[i];
  133. break;
  134. }
  135. }
  136. }
  137. #if DBG
  138. if (Transfer == NULL) {
  139. DbgPrint("IRMON: TransferFromCookie: could not find cookie\n");
  140. }
  141. #endif
  142. LeaveCriticalSection(&XferList->Lock);
  143. return Transfer;
  144. }