|
// DBServer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <DbgHelp.h>
#include "ACE\ACE.h"
#pragma comment(lib, "DbgHelp.lib")
#include "ace/Asynch_Acceptor.h"
#include "ace/Proactor.h"
#include "ace/WIN32_Proactor.h"
#ifndef SAFE_DELETE
#define SAFE_DELETE(x) if( (x) != NULL ) { delete (x); (x) = NULL; }
#endif
class ProConnectAcceptor : public ACE_Asynch_Acceptor<ACE_Service_Handler>
{
public:
virtual ACE_Service_Handler* make_handler (void)
{
//return new My_Service_Handler();
return 0;
}
};
class CAceProactor : public ACE_Task<ACE_MT_SYNCH>
{
public:
void Init(unsigned int nThreadCount)
{
m_u4ThreadCount = nThreadCount;
}
ACE_Proactor* GetProactor();
bool Stop()
{
ACE_Proactor::instance()->proactor_end_event_loop();
return true;
}
virtual int open(void *args = 0)
{
activate(THR_NEW_LWP | THR_BOUND | THR_JOINABLE, m_u4ThreadCount);
return 0;
}
virtual int svc(void);
unsigned int m_u4ThreadCount; //处理的线程总数
};
int CAceProactor::svc()
{
ACE_Proactor::instance()->proactor_run_event_loop();
return 0;
}
ACE_Proactor* CAceProactor::GetProactor()
{
return ACE_Proactor::instance();
}
class MyPServer : public ACE_Task<ACE_MT_SYNCH>
{
public:
int start_server(const char* szIp, UINT nPort, UINT nMaxConnectNum);
virtual INT End();
private:
void CleanUp();
void stop_server();
virtual int svc(void);
private:
ProConnectAcceptor *m_Acceptor;
CAceProactor *m_AceProactor;
};
int MyPServer::start_server(const char* szIp, UINT nPort, UINT nMaxConnectNum)
{
int nRet=0;
ACE_INET_Addr m_listenAddr;
//Proactor & Acceptor
m_AceProactor = new CAceProactor();
m_AceProactor->Init(ACE_OS::num_processors() * 2);
m_Acceptor = new ProConnectAcceptor();
m_listenAddr.set(nPort,szIp);
//start logger
unsigned int nMark = 0;
msg_queue()->high_water_mark(10000);
msg_queue()->low_water_mark(10000);
if(activate(THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED , 1) != -1)
{
nRet = m_Acceptor->open(m_listenAddr, 0, 1,ACE_DEFAULT_ASYNCH_BACKLOG, 1, m_AceProactor->GetProactor());
m_AceProactor->open();
}
return nRet;
}
INT MyPServer::End()
{
m_Acceptor->cancel();
stop_server();
wait();
CleanUp();
return 0;
}
void MyPServer::CleanUp()
{
SAFE_DELETE(m_Acceptor);
if (m_AceProactor)
{
m_AceProactor->Stop();
m_AceProactor->wait();
SAFE_DELETE(m_AceProactor);
}
}
void MyPServer::stop_server(void)
{
ACE_Message_Block* mb = NULL;
mb = new ACE_Message_Block(0, ACE_Message_Block::MB_STOP);
this->putq(mb);
}
int MyPServer::svc (void)
{
ACE_Message_Block *mb=NULL;
ACE_Time_Value xtime;
int nCount=0;
while(true)
{
xtime=ACE_OS::gettimeofday()+ACE_Time_Value(3, 0);
nCount = getq(mb,&xtime);
if((nCount != -1) && (mb->msg_type() == ACE_Message_Block::MB_STOP))
{
delete mb;
break;
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
//检测内存泄漏
#if defined(WIN32) && defined(_DEBUG)
int tmpDbgFlag;
tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(tmpDbgFlag);
#endif //WIN32 & _DEBUG
ACE::init();
MyPServer aa;
aa.start_server("127.0.0.1", 5264, 3);
///!!!!!!!在这个用客户端连接一次, 退出的时候就报泄漏
Sleep(10*1000);
aa.End();
ACE::fini();
return 0;
}
////当服务器接受过连接后,退出时就会报内存泄漏, 请大家帮帮忙,查一查,谢谢! |
|