在分析了那么多IOCP相关api之后想把IOCP模型分析下,本人菜鸟一个,高手勿笑。
gh0st是单文档类型的程序框架。 文档类型的都是从theApp开始的。theApp是一个全局变量。 那我们就先看一下CGh0stApp这个类的初始化函数 BOOL CGh0stApp::InitInstance()
下面很大一部分是生成的框架。我给大家指出来,就没必要再看这些了
直到
if (!ProcessShellCommand(cmdInfo))
return FALSE;
都是框架。不去看。分析下面的。
((CMainFrame*) m_pMainWnd)->Activate(nPort, nMaxConnection);
这句是调用CMainFrame类的Activate函数。 m_pMainWnd是单文档类的主界面指针,也是框架类指针。就是CMainFrame类 ,接下来我们就去Activate函数里面看看 。
m_iocpServer = new CIOCPServer; /// 这里调用了IOCPserver构造函数
// 开启IOCP服务器, 初始化例程
if (m_iocpServer->Initialize(NotifyProc, this, 100000, nPort))
进入 Initialize 函数看下:
bool CIOCPServer::Initialize(NOTIFYPROC pNotifyProc, CMainFrame* pFrame, int nMaxConnections, int nPort)
{
创建套接字
m_socListen = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
// 创建事件 处理网络IO
m_hEvent = WSACreateEvent();
/// 在 m_socListen 套接字上接收 FD_ACCEPT 事件,关联事件 和套接字
int nRet = WSAEventSelect(m_socListen,m_hEvent,FD_ACCEPT);
// 绑定 套接字
nRet = bind(m_socListen, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
// Set the socket to listen
nRet = listen(m_socListen, SOMAXCONN);
/// 开启监听线程 ListenThreadProc
m_hThread = (HANDLE)_beginthreadex(NULL, 0, ListenThreadProc, (void*) this, 0, &dwThreadId);
if (m_hThread != INVALID_HANDLE_VALUE)
{
初始化完成端口
InitializeIOCP();
}
}
让我们看下 监听线程 ListenThreadProc 和 InitializeIOCP 函数都做了什么。
首先看监听线程:
unsigned CIOCPServer::ListenThreadProc(