scan_common.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "StdAfx.h"
  2. #include "scan_common.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. UINT ServiceThread(LPVOID p){
  9. IService * service =(IService*)p;
  10. return service->Run0();
  11. }
  12. // 启动服务
  13. BOOL IService::Start( void )
  14. {
  15. EnterCriticalSection(&m_state_lock);
  16. if(m_serviceState == stoped){
  17. if(m_serviceThread ==NULL){
  18. m_command = command_start;
  19. m_serviceState = starting;
  20. m_serviceThread =AfxBeginThread(ServiceThread,this);
  21. if(m_serviceThread != NULL){
  22. m_command = command_none;
  23. LeaveCriticalSection(&m_state_lock);
  24. return TRUE;
  25. } else{
  26. m_serviceState = stoped;
  27. m_command = command_none;
  28. }
  29. }
  30. }
  31. LeaveCriticalSection(&m_state_lock);
  32. return FALSE;
  33. }
  34. // 停止服务
  35. BOOL IService::Stop( void )
  36. {
  37. EnterCriticalSection(&m_state_lock);
  38. if(m_serviceState != stoping&&m_serviceState!=stoped){
  39. m_command = command_stop;
  40. LeaveCriticalSection(&m_state_lock);
  41. return TRUE;
  42. }
  43. LeaveCriticalSection(&m_state_lock);
  44. return FALSE;
  45. }
  46. // 暂停服务
  47. BOOL IService::Pause( void )
  48. {
  49. EnterCriticalSection(&m_state_lock);
  50. if(SupportPause()&& m_serviceState == running){
  51. m_command = command_pause;
  52. LeaveCriticalSection(&m_state_lock);
  53. return TRUE;
  54. }
  55. LeaveCriticalSection(&m_state_lock);
  56. return FALSE;
  57. }
  58. // 恢复服务
  59. BOOL IService::Resume( void )
  60. {
  61. EnterCriticalSection(&m_state_lock);
  62. if(SupportPause()&& m_serviceState == paused){
  63. m_command = command_resume;
  64. LeaveCriticalSection(&m_state_lock);
  65. return TRUE;
  66. }
  67. LeaveCriticalSection(&m_state_lock);
  68. return FALSE;
  69. }
  70. // 是否支持暂停
  71. BOOL IService::SupportPause( void )
  72. {
  73. return FALSE;
  74. }
  75. // 服务执行方法
  76. int IService::Run(void)
  77. {
  78. BOOL run =TRUE;
  79. m_serviceState =OnStarting();
  80. do
  81. {
  82. switch(m_serviceState){
  83. case running:
  84. m_serviceState=OnRunning();
  85. break;
  86. case pausing:
  87. m_serviceState=OnPausing();
  88. break;
  89. case paused:
  90. m_serviceState=OnPaused();
  91. break;
  92. case resuming:
  93. m_serviceState=OnResuming();
  94. break;
  95. case stoping:
  96. m_serviceState=OnStoping();
  97. break;
  98. default:
  99. run = FALSE;
  100. break;
  101. }
  102. } while (run);
  103. return 0;
  104. }
  105. IService::IService( void )
  106. {
  107. m_serviceState = stoped;
  108. m_command = command_none;
  109. m_serviceThread =NULL;
  110. InitializeCriticalSection(&m_state_lock);
  111. }
  112. IService::~IService( void )
  113. {
  114. DeleteCriticalSection(&m_state_lock);
  115. }
  116. //获取服务当前状态
  117. ServiceState IService::GetServiceSate( void )
  118. {
  119. return m_serviceState;
  120. }
  121. UINT IService::Run0()
  122. {
  123. int ret = Run();
  124. m_serviceThread =NULL;
  125. m_serviceState = stoped;
  126. return ret;
  127. }
  128. inline BOOL IService::SetServiceSate( ServiceState newState,ServiceState oldState )
  129. {
  130. EnterCriticalSection(&m_state_lock);
  131. if( m_serviceState == oldState){
  132. m_serviceState = newState;
  133. LeaveCriticalSection(&m_state_lock);
  134. return TRUE;
  135. }
  136. LeaveCriticalSection(&m_state_lock);
  137. return FALSE;
  138. }
  139. ServiceState IService::OnStarting( void )
  140. {
  141. return running;
  142. }
  143. ServiceState IService::OnRunning( void )
  144. {
  145. if(m_command == command_pause) return pausing;
  146. if(m_command == command_stop) return stoping;
  147. return running;
  148. }
  149. ServiceState IService::OnPausing( void )
  150. {
  151. return paused;
  152. }
  153. ServiceState IService::OnPaused( void )
  154. {
  155. if(m_command == command_resume) return resuming;
  156. if(m_command == command_stop) return stoping;
  157. Sleep(10);
  158. return paused;
  159. }
  160. ServiceState IService::OnResuming( void )
  161. {
  162. return running;
  163. }
  164. ServiceState IService::OnStoping( void )
  165. {
  166. return stoped;
  167. }