Leaked source code of windows server 2003
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.

234 lines
4.0 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 (bReturn) {
  95. //
  96. // found the transfer in the list
  97. //
  98. if ((Count == 0) && (XferList->Closing)) {
  99. SetEvent(XferList->CloseEvent);
  100. }
  101. } else {
  102. //
  103. // where did the transfer go?
  104. //
  105. ASSERT(0);
  106. }
  107. LeaveCriticalSection(&XferList->Lock);
  108. return TRUE;
  109. }
  110. BOOL
  111. AreThereActiveTransfers(
  112. PXFER_LIST XferList
  113. )
  114. {
  115. ULONG i;
  116. BOOL bReturn=FALSE;
  117. EnterCriticalSection(&XferList->Lock);
  118. for (i=0; i<MAX_TRANSFERS; i++) {
  119. if (XferList->List[i] != NULL) {
  120. if (XferList->List[i]->IsActive()) {
  121. bReturn=TRUE;
  122. break;
  123. }
  124. }
  125. }
  126. LeaveCriticalSection(&XferList->Lock);
  127. return bReturn;
  128. }
  129. FILE_TRANSFER*
  130. TransferFromCookie(
  131. PXFER_LIST XferList,
  132. __int64 Cookie
  133. )
  134. {
  135. ULONG i;
  136. BOOL bReturn=FALSE;
  137. FILE_TRANSFER* Transfer=NULL;
  138. EnterCriticalSection(&XferList->Lock);
  139. for (i=0; i<MAX_TRANSFERS; i++) {
  140. if (XferList->List[i] != NULL) {
  141. if (XferList->List[i]->GetCookie() == Cookie) {
  142. Transfer=XferList->List[i];
  143. break;
  144. }
  145. }
  146. }
  147. #if DBG
  148. if (Transfer == NULL) {
  149. DbgPrint("IRMON: TransferFromCookie: could not find cookie\n");
  150. }
  151. #endif
  152. LeaveCriticalSection(&XferList->Lock);
  153. return Transfer;
  154. }