339 lines
8.2 KiB
C++
339 lines
8.2 KiB
C++
|
|
#include "StdAfx.h"
|
|||
|
|
#include "ServiceUnits.h"
|
|||
|
|
#include "ControlPacket.h"
|
|||
|
|
|
|||
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|
|||
|
|
//<2F><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
|
|||
|
|
CServiceUnits * CServiceUnits::g_pServiceUnits=NULL; //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
|
|
|||
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|
|||
|
|
BEGIN_MESSAGE_MAP(CServiceUnits, CWnd)
|
|||
|
|
ON_MESSAGE(WM_UICONTROL_REQUEST,OnUIControlRequest)
|
|||
|
|
END_MESSAGE_MAP()
|
|||
|
|
|
|||
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>캯<EFBFBD><ECBAAF>
|
|||
|
|
CServiceUnits::CServiceUnits()
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
|
|||
|
|
m_ServiceStatus=ServiceStatus_Stop;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ýӿ<C3BD>
|
|||
|
|
m_pIServiceUnitsSink=NULL;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>
|
|||
|
|
ASSERT(g_pServiceUnits==NULL);
|
|||
|
|
if (g_pServiceUnits==NULL) g_pServiceUnits=this;
|
|||
|
|
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
CServiceUnits::~CServiceUnits()
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
bool CServiceUnits::StartService()
|
|||
|
|
{
|
|||
|
|
//Ч<><D0A7>״̬
|
|||
|
|
ASSERT(m_ServiceStatus==ServiceStatus_Stop);
|
|||
|
|
if (m_ServiceStatus!=ServiceStatus_Stop) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>״̬
|
|||
|
|
SetServiceStatus(ServiceStatus_Config);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_hWnd==NULL)
|
|||
|
|
{
|
|||
|
|
CRect rcCreate(0,0,0,0);
|
|||
|
|
Create(NULL,NULL,WS_CHILD,rcCreate,AfxGetMainWnd(),100);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
|
|||
|
|
if (InitializeService()==false)
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
|
|||
|
|
if (StartKernelService()==false)
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><>ȡ<EFBFBD>б<EFBFBD>
|
|||
|
|
SendControlPacket(CT_LOAD_DB_GAME_LIST,NULL,0);
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
|||
|
|
bool CServiceUnits::ConcludeService()
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
|
|||
|
|
SetServiceStatus(ServiceStatus_Stop);
|
|||
|
|
|
|||
|
|
//ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TimerEngine.GetInterface()!=NULL) m_TimerEngine->ConcludeService();
|
|||
|
|
if (m_AttemperEngine.GetInterface()!=NULL) m_AttemperEngine->ConcludeService();
|
|||
|
|
if (m_DataBaseEngine.GetInterface()!=NULL) m_DataBaseEngine->ConcludeService();
|
|||
|
|
if (m_TCPNetworkEngine.GetInterface()!=NULL) m_TCPNetworkEngine->ConcludeService();
|
|||
|
|
if (m_TCPSocketService.GetInterface()!=NULL) m_TCPSocketService->ConcludeService();
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ýӿ<C3BD>
|
|||
|
|
bool CServiceUnits::SetServiceUnitsSink(IServiceUnitsSink * pIServiceUnitsSink)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
|
|||
|
|
m_pIServiceUnitsSink=pIServiceUnitsSink;
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Ͷ<><CDB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
bool CServiceUnits::PostControlRequest(WORD wIdentifier, VOID * pData, WORD wDataSize)
|
|||
|
|
{
|
|||
|
|
//״̬<D7B4>ж<EFBFBD>
|
|||
|
|
ASSERT(IsWindow(m_hWnd));
|
|||
|
|
if (IsWindow(m_hWnd)==FALSE) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
CWHDataLocker DataLocker(m_CriticalSection);
|
|||
|
|
if (m_DataQueue.InsertData(wIdentifier,pData,wDataSize)==false) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
PostMessage(WM_UICONTROL_REQUEST,wIdentifier,wDataSize);
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
bool CServiceUnits::InitializeService()
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>
|
|||
|
|
m_InitParameter.LoadInitParameter();
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if ((m_TimerEngine.GetInterface()==NULL)&&(m_TimerEngine.CreateInstance()==false)) return false;
|
|||
|
|
if ((m_AttemperEngine.GetInterface()==NULL)&&(m_AttemperEngine.CreateInstance()==false)) return false;
|
|||
|
|
if ((m_DataBaseEngine.GetInterface()==NULL)&&(m_DataBaseEngine.CreateInstance()==false)) return false;
|
|||
|
|
if ((m_TCPNetworkEngine.GetInterface()==NULL)&&(m_TCPNetworkEngine.CreateInstance()==false)) return false;
|
|||
|
|
if ((m_TCPSocketService.GetInterface()==NULL)&&(m_TCPSocketService.CreateInstance()==false)) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
|
IUnknownEx * pIAttemperEngine=m_AttemperEngine.GetInterface();
|
|||
|
|
IUnknownEx * pITCPNetworkEngine=m_TCPNetworkEngine.GetInterface();
|
|||
|
|
IUnknownEx * pIAttemperEngineSink=QUERY_OBJECT_INTERFACE(m_AttemperEngineSink,IUnknownEx);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
IUnknownEx * pIDataBaseEngineSink[CountArray(m_DataBaseEngineSink)];
|
|||
|
|
for (WORD i=0;i<CountArray(pIDataBaseEngineSink);i++) pIDataBaseEngineSink[i]=QUERY_OBJECT_INTERFACE(m_DataBaseEngineSink[i],IUnknownEx);
|
|||
|
|
|
|||
|
|
//<2F>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TimerEngine->SetTimerEngineEvent(pIAttemperEngine)==false) return false;
|
|||
|
|
if (m_AttemperEngine->SetNetworkEngine(pITCPNetworkEngine)==false) return false;
|
|||
|
|
if (m_AttemperEngine->SetAttemperEngineSink(pIAttemperEngineSink)==false) return false;
|
|||
|
|
if (m_TCPNetworkEngine->SetTCPNetworkEngineEvent(pIAttemperEngine)==false) return false;
|
|||
|
|
if (m_DataBaseEngine->SetDataBaseEngineSink(pIDataBaseEngineSink,CountArray(pIDataBaseEngineSink))==false) return false;
|
|||
|
|
|
|||
|
|
//Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TCPSocketService->SetServiceID(NETWORK_CORRESPOND)==false) return false;
|
|||
|
|
if (m_TCPSocketService->SetTCPSocketEvent(pIAttemperEngine)==false) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>Ȼص<C8BB>
|
|||
|
|
m_AttemperEngineSink.m_pInitParameter=&m_InitParameter;
|
|||
|
|
m_AttemperEngineSink.m_pITimerEngine=m_TimerEngine.GetInterface();
|
|||
|
|
m_AttemperEngineSink.m_pIDataBaseEngine=m_DataBaseEngine.GetInterface();
|
|||
|
|
m_AttemperEngineSink.m_pITCPNetworkEngine=m_TCPNetworkEngine.GetInterface();
|
|||
|
|
m_AttemperEngineSink.m_pITCPSocketService=m_TCPSocketService.GetInterface();
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ݿ<EFBFBD><DDBF>ص<EFBFBD>
|
|||
|
|
for (INT i=0;i<CountArray(m_DataBaseEngineSink);i++)
|
|||
|
|
{
|
|||
|
|
m_DataBaseEngineSink[i].m_pInitParameter=&m_InitParameter;
|
|||
|
|
m_DataBaseEngineSink[i].m_pIDataBaseEngineEvent=QUERY_OBJECT_PTR_INTERFACE(pIAttemperEngine,IDataBaseEngineEvent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
WORD wMaxConnect=m_InitParameter.m_wMaxConnect;
|
|||
|
|
WORD wServicePort=m_InitParameter.m_wServicePort;
|
|||
|
|
if (m_TCPNetworkEngine->SetServiceParameter(wServicePort,wMaxConnect,NULL)==false) return false;
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
|
|||
|
|
bool CServiceUnits::StartKernelService()
|
|||
|
|
{
|
|||
|
|
//ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TimerEngine->StartService()==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_AttemperEngine->StartService()==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_DataBaseEngine->StartService()==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TCPSocketService->StartService()==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
bool CServiceUnits::StartNetworkService()
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (m_TCPNetworkEngine->StartService()==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>״̬
|
|||
|
|
bool CServiceUnits::SetServiceStatus(enServiceStatus ServiceStatus)
|
|||
|
|
{
|
|||
|
|
//״̬<D7B4>ж<EFBFBD>
|
|||
|
|
if (m_ServiceStatus!=ServiceStatus)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>֪ͨ
|
|||
|
|
if ((m_ServiceStatus!=ServiceStatus_Service)&&(ServiceStatus==ServiceStatus_Stop))
|
|||
|
|
{
|
|||
|
|
LPCTSTR pszString=TEXT("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>");
|
|||
|
|
CTraceService::TraceString(pszString,TraceLevel_Exception);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
|
|||
|
|
m_ServiceStatus=ServiceStatus;
|
|||
|
|
|
|||
|
|
//״̬֪ͨ
|
|||
|
|
ASSERT(m_pIServiceUnitsSink!=NULL);
|
|||
|
|
if (m_pIServiceUnitsSink!=NULL) m_pIServiceUnitsSink->OnServiceUnitsStatus(m_ServiceStatus);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD>
|
|||
|
|
bool CServiceUnits::SendControlPacket(WORD wControlID, VOID * pData, WORD wDataSize)
|
|||
|
|
{
|
|||
|
|
//״̬Ч<CCAC><D0A7>
|
|||
|
|
ASSERT(m_AttemperEngine.GetInterface()!=NULL);
|
|||
|
|
if (m_AttemperEngine.GetInterface()==NULL) return false;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD>
|
|||
|
|
m_AttemperEngine->OnEventControl(wControlID,pData,wDataSize);
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
LRESULT CServiceUnits::OnUIControlRequest(WPARAM wParam, LPARAM lParam)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
tagDataHead DataHead;
|
|||
|
|
BYTE cbBuffer[MAX_ASYNCHRONISM_DATA];
|
|||
|
|
|
|||
|
|
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
|
CWHDataLocker DataLocker(m_CriticalSection);
|
|||
|
|
if (m_DataQueue.DistillData(DataHead,cbBuffer,sizeof(cbBuffer))==false)
|
|||
|
|
{
|
|||
|
|
ASSERT(FALSE);
|
|||
|
|
return NULL;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
|
|||
|
|
switch (DataHead.wIdentifier)
|
|||
|
|
{
|
|||
|
|
case UI_LOAD_DB_LIST_RESULT: //<2F>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
//Ч<><D0A7><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
ASSERT(DataHead.wDataSize==sizeof(CP_ControlResult));
|
|||
|
|
if (DataHead.wDataSize!=sizeof(CP_ControlResult)) return 0;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
CP_ControlResult * pControlResult=(CP_ControlResult *)cbBuffer;
|
|||
|
|
|
|||
|
|
//ʧ<>ܴ<EFBFBD><DCB4><EFBFBD>
|
|||
|
|
if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_FAILURE))
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_SUCCESS))
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>
|
|||
|
|
SendControlPacket(CT_CONNECT_CORRESPOND,NULL,0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
case UI_CORRESPOND_RESULT: //Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
//Ч<><D0A7><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
ASSERT(DataHead.wDataSize==sizeof(CP_ControlResult));
|
|||
|
|
if (DataHead.wDataSize!=sizeof(CP_ControlResult)) return 0;
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
CP_ControlResult * pControlResult=(CP_ControlResult *)cbBuffer;
|
|||
|
|
|
|||
|
|
//ʧ<>ܴ<EFBFBD><DCB4><EFBFBD>
|
|||
|
|
if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_FAILURE))
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_SUCCESS))
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (StartNetworkService()==false)
|
|||
|
|
{
|
|||
|
|
ConcludeService();
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>״̬
|
|||
|
|
SetServiceStatus(ServiceStatus_Service);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//////////////////////////////////////////////////////////////////////////////////
|