⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nwiocpserver.cpp

📁 这是我做的一个iocp的服务器端的测试代码
💻 CPP
字号:
#include "StdAfx.h"
#include "NWIocpServer.h"  
#include "NWSendThread.h"
//NWIocpServer NWIocpServer::m_Instance;
NWIocpServer::NWIocpServer(void)
{
}

NWIocpServer::~NWIocpServer(void)
{
}

int NWIocpServer::InitServer(USHORT usServerPort, IMessageProcess* pfnMsgNotify)
{
	MYEXCEPTIONFUNC_START();
	m_usListenPort = usServerPort;
//	CSystemInfo lpSystemInfo; 
	
	iRetCode = CIOCompletionPort::GetInstance()->InitWorkerThreads(1 * 2 +2,pfnMsgNotify,this);
	if (iRetCode != 0)
	{
		throw_NWException("Init Worker Thread Error");
	}
	MYEXCEPTIONFUNC_END("NWIocpServer::InitServer()");
	//初始化消息处理线程
/*	pCMSGProcessThread procThread ; 
	for (int nIndex = 0 ; nIndex < MAX_MSGPROCESS_THREAD_CONUT ; nIndex ++)
	{
		procThread = new CMSGProcessThread ; 
		if (!procThread)
		{
			continue ;
		} 
		m_lsMSGProcessThreadList.AddTail(procThread); 
	} 
	NWIOBufferManager::GetInstance()->InitBufferList(m_hExitEvent);//*/
	return 0 ;
}

int NWIocpServer::UninitServer()
{
	CIOCompletionPort::GetInstance()->UninitWorkerThreads();

/*	pCMSGProcessThread procThread ; 
	NWIOBufferManager::GetInstance()->UninitBufferList();
	while (m_lsMSGProcessThreadList.GetCount() > 0)
	{
		m_lsMSGProcessThreadList.RemoveHead(procThread);
		if (procThread)
		{ 
			delete procThread;
			procThread = NULL;
		}
	}//*/
	return 0 ;
}

int NWIocpServer::StartServer()
{
	MYEXCEPTIONFUNC_START(); 

	iRetCode = CIOCompletionPort::GetInstance()->StartWorkerThreads() ;
	if (iRetCode  != 0 )
	{
		throw_NWException("StartWorkerThreads Error");
	}	
	iRetCode = StartupListen();
	if (iRetCode != 0)
	{
		throw_NWException("StartupListen Error");
	}

	NWSendThread::GetInstance()->SetSocket(m_pcSocket->GetSocket());

	iRetCode = NWSendThread::GetInstance()->Start();
	if (iRetCode != 0 )
	{
		throw_NWException("SendThread Start Error");
	}

	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::Startup()");
}
	//CSystemInfo lpSystemInfo; 
	//lpSystemInfo.dwNumberOfProcessors
	//启动工作线程

	//启动上层处理线程
/*    pCMSGProcessThread procThread ; 
    if (m_lsMSGProcessThreadList.GetCount() <= 0)
    {
		throw_NWException("m_lsMSGProcessThreadList.GetCount() <= 0");
    }
    if (m_lsMSGProcessThreadList.GotoHead() != NULL)
    {
		throw_NWException("m_lsMSGProcessThreadList.GotoHead() != NULL");
    }
	do 
	{
		procThread = m_lsMSGProcessThreadList.GetCur();
		if (procThread != NULL)
		{
			if (procThread->Start() != 0)
		    {
			    delete procThread;
		    }
		}
	} while (m_lsMSGProcessThreadList.GotoNext() == NULL);
	//*/	


int NWIocpServer::StopServer()
{
	MYEXCEPTIONFUNC_START();	
	iRetCode = NWSendThread::GetInstance()->Stop();
	if (iRetCode != 0)
	{
		throw_NWException(" NWSendThread Stop Error");
	}
	StopListen();
	iRetCode= CIOCompletionPort::GetInstance()->StopWorkerThreads();
    if (iRetCode != 0 )
    {
		throw_NWException("Stop Worker Threads Error");
    }
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::Cleanup()");
}
/*	pCMSGProcessThread procThread ; 
    if (m_lsMSGProcessThreadList.GetCount() <= 0)
    {
		throw_NWException("m_lsMSGProcessThreadList.GetCount() <= 0");
    }
    if (m_lsMSGProcessThreadList.GotoHead() != NULL)
    {
		throw_NWException("m_lsMSGProcessThreadList.GotoHead() != NULL");
    }
    do 
    {
		procThread = m_lsMSGProcessThreadList.GetCur();
		if (procThread != NULL)
		{
			procThread->Stop();

		}
    } while (m_lsMSGProcessThreadList.GotoNext() == NULL);
//*/

	//停止工作线程



int NWIocpServer::RegisterMsgFilter( IMessageProcess* pfnMsgNotify )
{
    MYEXCEPTIONFUNC_START();
	//CIOCompletionPort::GetInstance()->SetMSGProccessInterface(pfnMsgNotify);
	/*
	if (m_lsMSGProcessThreadList.GetCount() <= 0)
	{
        throw_NWException("");
	}
	if (m_lsMSGProcessThreadList.GotoHead() != NULL)
	{
		throw_NWException("");
	}

	pCMSGProcessThread procThread;
	do 
	{
		procThread = m_lsMSGProcessThreadList.GetCur() ;
		if (procThread == NULL)
		{
			Trace("procthread == NULL");
			continue ;
		}
		procThread->SetMsgProcess(pfnMsgNotify);

	} while (m_lsMSGProcessThreadList.GotoNext() == NULL);//*/
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("");
}

/************************************************
函数原型:PreparePackSink();
参数:	
返回值:
功能:		将上层应用的数据包转换成本模块的网络包
仅用于windows中。
************************************************/

int NWIocpServer::PreparePackSink()
{
	MYEXCEPTIONFUNC_START(); 
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::PreparePackSink()");
}

/************************************************
函数原型:PackDataToClient();
参数:	
返回值:
功能:		发送数据包,但是这里不等待,也不保证客户的接受
仅用于windows中。
************************************************/
int NWIocpServer::PackDataToClient( const CLIENTKEY &ulnClientID, const void * const pData, const size_t &datalength )
{
	MYEXCEPTIONFUNC_START(); 

	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::PackDataToClient()");
}

int NWIocpServer::SendPackToClient( const CLIENTKEY &ulnClientID )
{
	MYEXCEPTIONFUNC_START(); 
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::SendPackToClient()");
}

int NWIocpServer::SendData(const CLIENTKEY &ulnClientID, const void * pData, const size_t &datalength )
{
	MYEXCEPTIONFUNC_START(); 
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::SendData()");
}

int NWIocpServer::ShutdownClient(const CLIENTKEY ulClientID )
{
	MYEXCEPTIONFUNC_START(); 
	iRetCode = 0 ;
	MYEXCEPTIONFUNC_END("NWIocpServer::ShutdownClient()");
}
 
const char * NWIocpServer::GetClientInfo( const CLIENTKEY & ulnClientID )
{
	MYEXCEPTIONFUNC_START(); 
	iRetCode = 0 ;
	return "";
	MYEXCEPTIONFUNC_NOREV_END("NWIocpServer::GetClientInfo()");
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -