PendingRequestCollection.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace SMBLibrary.Win32
  11. {
  12. internal class PendingRequestCollection
  13. {
  14. private Dictionary<IntPtr, List<PendingRequest>> m_handleToNotifyChangeRequests = new Dictionary<IntPtr, List<PendingRequest>>();
  15. public void Add(PendingRequest request)
  16. {
  17. lock (m_handleToNotifyChangeRequests)
  18. {
  19. List<PendingRequest> pendingRequests;
  20. bool containsKey = m_handleToNotifyChangeRequests.TryGetValue(request.FileHandle, out pendingRequests);
  21. if (containsKey)
  22. {
  23. pendingRequests.Add(request);
  24. }
  25. else
  26. {
  27. pendingRequests = new List<PendingRequest>();
  28. pendingRequests.Add(request);
  29. m_handleToNotifyChangeRequests.Add(request.FileHandle, pendingRequests);
  30. }
  31. }
  32. }
  33. public void Remove(IntPtr handle, uint threadID)
  34. {
  35. lock (m_handleToNotifyChangeRequests)
  36. {
  37. List<PendingRequest> pendingRequests;
  38. bool containsKey = m_handleToNotifyChangeRequests.TryGetValue(handle, out pendingRequests);
  39. if (containsKey)
  40. {
  41. for (int index = 0; index < pendingRequests.Count; index++)
  42. {
  43. if (pendingRequests[index].ThreadID == threadID)
  44. {
  45. pendingRequests.RemoveAt(index);
  46. index--;
  47. }
  48. }
  49. if (pendingRequests.Count == 0)
  50. {
  51. m_handleToNotifyChangeRequests.Remove(handle);
  52. }
  53. }
  54. }
  55. }
  56. public List<PendingRequest> GetRequestsByHandle(IntPtr handle)
  57. {
  58. List<PendingRequest> pendingRequests;
  59. bool containsKey = m_handleToNotifyChangeRequests.TryGetValue((IntPtr)handle, out pendingRequests);
  60. if (containsKey)
  61. {
  62. return new List<PendingRequest>(pendingRequests);
  63. }
  64. return new List<PendingRequest>();
  65. }
  66. }
  67. }