1
0

discoveryMonitor.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "main.h"
  2. #include "./discoveryMonitor.h"
  3. DiscoveryMonitor::DiscoveryMonitor()
  4. {
  5. InitializeCriticalSection(&lock);
  6. }
  7. DiscoveryMonitor::~DiscoveryMonitor()
  8. {
  9. DeleteCriticalSection(&lock);
  10. }
  11. void DiscoveryMonitor::Lock()
  12. {
  13. EnterCriticalSection(&lock);
  14. }
  15. void DiscoveryMonitor::Unlock()
  16. {
  17. LeaveCriticalSection(&lock);
  18. }
  19. BOOL DiscoveryMonitor::Register(ifc_deviceprovider *provider)
  20. {
  21. Lock();
  22. size_t index = activityList.size();
  23. while(index--)
  24. {
  25. ActiveDiscovery *entry = &activityList[index];
  26. if ((intptr_t)provider == entry->providerId)
  27. {
  28. entry->ref++;
  29. Unlock();
  30. return FALSE;
  31. }
  32. }
  33. ActiveDiscovery record;
  34. record.providerId = (intptr_t)provider;
  35. record.ref = 1;
  36. activityList.push_back(record);
  37. index = activityList.size();
  38. Unlock();
  39. return (1 == index);
  40. }
  41. BOOL DiscoveryMonitor::Unregister(ifc_deviceprovider *provider)
  42. {
  43. Lock();
  44. size_t index = activityList.size();
  45. while(index--)
  46. {
  47. ActiveDiscovery *entry = &activityList[index];
  48. if ((intptr_t)provider == entry->providerId)
  49. {
  50. if (1 == entry->ref)
  51. {
  52. activityList.erase(activityList.begin() + index);
  53. index = activityList.size();
  54. Unlock();
  55. return (0 == index);
  56. }
  57. entry->ref--;
  58. break;
  59. }
  60. }
  61. Unlock();
  62. return FALSE;
  63. }
  64. BOOL DiscoveryMonitor::IsActive()
  65. {
  66. size_t count;
  67. Lock();
  68. count = activityList.size();
  69. Unlock();
  70. return (0 != count);
  71. }
  72. BOOL DiscoveryMonitor::Reset()
  73. {
  74. size_t count;
  75. Lock();
  76. count = activityList.size();
  77. activityList.clear();
  78. Unlock();
  79. return (0 != count);
  80. }