ServiceManager.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "ServiceManager.h"
  10. #include <api.h>
  11. #include <api/service/waservicefactory.h>
  12. #include "../nu/AutoLock.h"
  13. #include <api/syscb/callbacks/syscb.h>
  14. #include <api/syscb/callbacks/svccb.h>
  15. using namespace Nullsoft::Utility;
  16. struct Counter
  17. {
  18. Counter( waServiceFactory *_owner, void *_ptr ) : ptr( _ptr ), owner( _owner ) {}
  19. void *ptr;
  20. waServiceFactory *owner;
  21. };
  22. ServiceManager::ServiceManager() : serviceGuard(512)
  23. {}
  24. int ServiceManager::service_register( waServiceFactory *svc )
  25. {
  26. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_register" ) );
  27. FOURCC service_type = svc->getServiceType();
  28. // add the service to the master list
  29. services.push_back( svc );
  30. // add it to the by-type lookup
  31. ServiceList *&type_list = services_by_type[ service_type ];
  32. if ( !type_list )
  33. type_list = new ServiceList;
  34. type_list->push_back( svc );
  35. // send notifications
  36. svc->serviceNotify( SvcNotify::ONREGISTERED );
  37. WASABI_API_SYSCB->syscb_issueCallback( SysCallback::SERVICE, SvcCallback::ONREGISTER, (intptr_t)service_type, reinterpret_cast<intptr_t>( svc ) );
  38. return 1;
  39. }
  40. int ServiceManager::service_deregister( waServiceFactory *svc )
  41. {
  42. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_deregister" ) );
  43. FOURCC service_type = svc->getServiceType();
  44. // remove it from the master list
  45. //services.eraseObject(svc);
  46. auto it = std::find( services.begin(), services.end(), svc );
  47. if ( it != services.end() )
  48. services.erase( it );
  49. // and from the type lookup map
  50. ServiceList *type_list = services_by_type[ service_type ];
  51. if ( type_list )
  52. {
  53. //type_list->eraseObject(svc);
  54. auto it = std::find( type_list->begin(), type_list->end(), svc );
  55. if ( it != type_list->end() )
  56. type_list->erase( it );
  57. }
  58. WASABI_API_SYSCB->syscb_issueCallback( SysCallback::SERVICE, SvcCallback::ONDEREGISTER, (intptr_t)service_type, reinterpret_cast<intptr_t>( svc ) );
  59. svc->serviceNotify( SvcNotify::ONDEREGISTERED );
  60. return 1;
  61. }
  62. size_t ServiceManager::service_getNumServices( FOURCC svc_type )
  63. {
  64. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_getNumServices" ) );
  65. if ( svc_type )
  66. {
  67. ServiceList *type_list = services_by_type[ svc_type ];
  68. if ( type_list )
  69. return type_list->size();
  70. else
  71. return 0;
  72. }
  73. else
  74. return services.size();
  75. }
  76. waServiceFactory *ServiceManager::service_enumService( FOURCC svc_type, size_t n )
  77. {
  78. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_enumService" ) );
  79. ServiceList *type_list = 0;
  80. if ( svc_type )
  81. type_list = services_by_type[ svc_type ];
  82. else
  83. type_list = &services;
  84. if ( type_list && n < type_list->size() )
  85. return type_list->at( n );
  86. else
  87. return 0;
  88. }
  89. waServiceFactory *ServiceManager::service_getServiceByGuid( GUID guid )
  90. {
  91. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_getServiceByGuid" ) );
  92. for ( waServiceFactory *l_service : services )
  93. {
  94. if ( l_service->getGuid() == guid )
  95. return l_service;
  96. }
  97. return NULL;
  98. }
  99. int ServiceManager::service_lock( waServiceFactory *owner, void *svcptr )
  100. {
  101. if ( owner == NULL || svcptr == NULL )
  102. return 0;
  103. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_lock" ) );
  104. locks.push_back( new Counter( owner, svcptr ) );
  105. return 1;
  106. }
  107. int ServiceManager::service_clientLock( void *svcptr )
  108. {
  109. return 1;
  110. }
  111. int ServiceManager::service_release( void *svcptr )
  112. {
  113. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_release" ) );
  114. for ( size_t i = 0; i != locks.size(); i++ )
  115. {
  116. if ( locks[ i ]->ptr == svcptr )
  117. {
  118. void *ptr = locks[ i ]->ptr;
  119. waServiceFactory *owner = locks[ i ]->owner;
  120. delete locks[ i ];
  121. locks.erase( locks.begin() + i );
  122. owner->releaseInterface( ptr );
  123. return 1;
  124. }
  125. }
  126. return 0;
  127. }
  128. const char *ServiceManager::service_getTypeName( FOURCC svc_type )
  129. {
  130. return NULL;
  131. }
  132. int ServiceManager::service_unlock( void *svcptr )
  133. {
  134. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_unlock" ) );
  135. for ( size_t i = 0; i != locks.size(); i++ )
  136. {
  137. if ( locks[ i ]->ptr == svcptr )
  138. {
  139. delete locks[ i ];
  140. locks.erase( locks.begin() + i );
  141. return 1;
  142. }
  143. }
  144. return 0;
  145. }
  146. int ServiceManager::service_isvalid( FOURCC svctype, waServiceFactory *service )
  147. {
  148. AutoLock lock( serviceGuard LOCKNAME( "ServiceManager::service_isvalid" ) );
  149. //return !!services.contains(service);
  150. return ( services.end() != std::find( services.begin(), services.end(), service ) );
  151. }
  152. int ServiceManager::service_compactDuplicates( waServiceFactory *me )
  153. {
  154. // first, find 'me'
  155. GUID guid = me->getGuid();
  156. size_t me_index = 0;
  157. //bool found = services.findItem(me, &me_index);
  158. //if (!found)
  159. // return 1;
  160. if ( services.end() == std::find( services.begin(), services.end(), me ) )
  161. return 1;
  162. ServiceList *type_list = services_by_type[ me->getServiceType() ];
  163. // go in reverse order because service to compact is likely at the end
  164. size_t n = services.size();
  165. while ( n-- ) // n is guaranteed to be >0 because otherwise the 'find' loop above would have failed
  166. {
  167. waServiceFactory *n_service = services[ n ];
  168. if ( n != me_index && n_service != me && n_service->getGuid() == guid )
  169. {
  170. services[ me_index ] = n_service;
  171. services.erase( services.begin() + n );
  172. // fix up our by-type cache
  173. if ( type_list )
  174. {
  175. size_t me_index = 0;
  176. size_t n_index = 0;
  177. //----------------------------------------------------
  178. // Replaced with code below this commented code
  179. //if (type_list->findItem_reverse(n_service, &n_index) && type_list->findItem(me, &me_index))
  180. //{
  181. // type_list->at(me_index) = n_service;
  182. // type_list->eraseindex(n_index);
  183. //}
  184. bool foundService = false;
  185. for ( int idx = type_list->size() - 1; idx >= 0; idx-- )
  186. {
  187. if ( type_list->at( idx ) == n_service )
  188. {
  189. foundService = true;
  190. n_index = idx;
  191. break;
  192. }
  193. }
  194. bool foundMe = false;
  195. for ( int idx2 = 0; idx2 < type_list->size(); idx2++ )
  196. {
  197. if ( type_list->at( idx2 ) == me )
  198. {
  199. foundMe = true;
  200. me_index = idx2;
  201. break;
  202. }
  203. }
  204. if ( foundService && foundMe )
  205. {
  206. type_list->at( me_index ) = n_service;
  207. type_list->erase( type_list->begin() + n_index );
  208. }
  209. //--------------------------------------------------
  210. }
  211. me->serviceNotify( SvcNotify::ONDEREGISTERED );
  212. return 0;
  213. }
  214. }
  215. return 1;
  216. }
  217. #ifdef CBCLASS
  218. #undef CBCLASS
  219. #endif
  220. #define CBCLASS ServiceManager
  221. START_DISPATCH;
  222. CB( API_SERVICE_SERVICE_REGISTER, service_register );
  223. CB( API_SERVICE_SERVICE_DEREGISTER, service_deregister );
  224. CB( API_SERVICE_SERVICE_GETNUMSERVICES, service_getNumServices );
  225. CB( API_SERVICE_SERVICE_ENUMSERVICE, service_enumService );
  226. CB( API_SERVICE_SERVICE_GETSERVICEBYGUID, service_getServiceByGuid );
  227. CB( API_SERVICE_SERVICE_LOCK, service_lock );
  228. CB( API_SERVICE_SERVICE_CLIENTLOCK, service_clientLock );
  229. CB( API_SERVICE_SERVICE_RELEASE, service_release );
  230. CB( API_SERVICE_SERVICE_GETTYPENAME, service_getTypeName );
  231. CB( API_SERVICE_SERVICE_UNLOCK, service_unlock );
  232. CB( API_SERVICE_ISVALID, service_isvalid );
  233. CB( API_SERVICE_COMPACT_DUPLICATES, service_compactDuplicates );
  234. END_DISPATCH;
  235. #undef CBCLASS