123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "StdAfx.h"
- #include "scan_common.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- UINT ServiceThread(LPVOID p){
- IService * service =(IService*)p;
- return service->Run0();
- }
- // 启动服务
- BOOL IService::Start( void )
- {
- EnterCriticalSection(&m_state_lock);
- if(m_serviceState == stoped){
- if(m_serviceThread ==NULL){
- m_command = command_start;
- m_serviceState = starting;
- m_serviceThread =AfxBeginThread(ServiceThread,this);
- if(m_serviceThread != NULL){
- m_command = command_none;
- LeaveCriticalSection(&m_state_lock);
- return TRUE;
- } else{
- m_serviceState = stoped;
- m_command = command_none;
- }
- }
- }
- LeaveCriticalSection(&m_state_lock);
- return FALSE;
- }
- // 停止服务
- BOOL IService::Stop( void )
- {
- EnterCriticalSection(&m_state_lock);
- if(m_serviceState != stoping&&m_serviceState!=stoped){
- m_command = command_stop;
- LeaveCriticalSection(&m_state_lock);
- return TRUE;
- }
- LeaveCriticalSection(&m_state_lock);
- return FALSE;
- }
- // 暂停服务
- BOOL IService::Pause( void )
- {
- EnterCriticalSection(&m_state_lock);
- if(SupportPause()&& m_serviceState == running){
- m_command = command_pause;
- LeaveCriticalSection(&m_state_lock);
- return TRUE;
- }
- LeaveCriticalSection(&m_state_lock);
- return FALSE;
- }
- // 恢复服务
- BOOL IService::Resume( void )
- {
- EnterCriticalSection(&m_state_lock);
- if(SupportPause()&& m_serviceState == paused){
- m_command = command_resume;
- LeaveCriticalSection(&m_state_lock);
- return TRUE;
- }
- LeaveCriticalSection(&m_state_lock);
- return FALSE;
- }
- // 是否支持暂停
- BOOL IService::SupportPause( void )
- {
- return FALSE;
- }
- // 服务执行方法
- int IService::Run(void)
- {
- BOOL run =TRUE;
- m_serviceState =OnStarting();
- do
- {
- switch(m_serviceState){
- case running:
- m_serviceState=OnRunning();
- break;
- case pausing:
- m_serviceState=OnPausing();
- break;
- case paused:
- m_serviceState=OnPaused();
- break;
- case resuming:
- m_serviceState=OnResuming();
- break;
- case stoping:
- m_serviceState=OnStoping();
- break;
- default:
- run = FALSE;
- break;
- }
- } while (run);
- return 0;
- }
- IService::IService( void )
- {
- m_serviceState = stoped;
- m_command = command_none;
- m_serviceThread =NULL;
- InitializeCriticalSection(&m_state_lock);
- }
- IService::~IService( void )
- {
- DeleteCriticalSection(&m_state_lock);
- }
- //获取服务当前状态
- ServiceState IService::GetServiceSate( void )
- {
- return m_serviceState;
- }
- UINT IService::Run0()
- {
- int ret = Run();
- m_serviceThread =NULL;
- m_serviceState = stoped;
- return ret;
- }
- inline BOOL IService::SetServiceSate( ServiceState newState,ServiceState oldState )
- {
- EnterCriticalSection(&m_state_lock);
- if( m_serviceState == oldState){
- m_serviceState = newState;
- LeaveCriticalSection(&m_state_lock);
- return TRUE;
- }
- LeaveCriticalSection(&m_state_lock);
- return FALSE;
- }
- ServiceState IService::OnStarting( void )
- {
- return running;
- }
- ServiceState IService::OnRunning( void )
- {
- if(m_command == command_pause) return pausing;
- if(m_command == command_stop) return stoping;
- return running;
- }
- ServiceState IService::OnPausing( void )
- {
- return paused;
- }
- ServiceState IService::OnPaused( void )
- {
- if(m_command == command_resume) return resuming;
- if(m_command == command_stop) return stoping;
- Sleep(10);
- return paused;
- }
- ServiceState IService::OnResuming( void )
- {
- return running;
- }
- ServiceState IService::OnStoping( void )
- {
- return stoped;
- }
|