在CServer的Create()中,创建对象CListenSocket来监听21端口,来看看具体的代码实现:
CListenSocket *pListenSocket = new CListenSocket(this, ssl);
...
if (!pListenSocket->Create(nPort, SOCK_STREAM, FD_ACCEPT, NULL) || !pListenSocket->Listen())
...
基本上分三步:
1、new CListenSocket:没有什么特别的,基本就是初始化成员变量
2、Create
注:在所有的代码中,先不看大量的if (m_pFirstLayer)这种代码,这是CAsyncSocketExLayer的机制。
Create其实调用的是父类CAsyncSocketEx的create()方法,这个方法中第一件事情就是建立m_spAsyncSocketExThreadDataList链、分发线程t_AsyncSocketExThreadData以及CAsyncSocketEx之间的关系,CAsyncSocketEx的create()方法首先调用InitAsyncSocketExInstance(),下面是CAsyncSocketEx::InitAsyncSocketExInstance()代码片断:
DWORD id=GetCurrentThreadId();
...
//Get thread specific data
if (m_spAsyncSocketExThreadDataList) // 这个链已经建立了
{
t_AsyncSocketExThreadDataList *pList=m_spAsyncSocketExThreadDataList;
while (pList) // 遍历链
{
ASSERT(pList->pThreadData);
ASSERT(pList->pThreadData->nInstanceCount>0);
if (pList->pThreadData->nThreadId==id) // 对当前线程已经有分发线程了,就把当前socket的分发由这个分发线程来代理
{
m_pLocalAsyncSocketExThreadData=pList->pThreadData;
m_pLocalAsyncSocketExThreadData->nInstanceCount++; // 多了这一个socket
break;
}
pList=pList->pNext;
}
//Current thread yet has no sockets
if (!pList) // 当前线程还没有分发线程,则创建一个
{
//Initialize data for current thread
pList=new t_AsyncSocketExThreadDataList;
pList->pNext=m_spAsyncSocketExThreadDataList;
m_spAsyncSocketExThreadDataList=pList;
m_pLocalAsyncSocketExThreadData=new t_AsyncSocketExThreadData;
m_pLocalAsyncSocketExThreadData->nInstanceCount=1; // 只挂了当前的socket
m_pLocalAsyncSocketExThreadData->nThreadId=id; // 这个分发线程的threadID
m_pLocalAsyncSocketExThreadData->m_pHelperWindow=new CAsyncSocketExHelperWindow(m_pLocalAsyncSocketExThreadData); // 为这个分发线程创建CAsyncSocketExHelperWindow
m_spAsyncSocketExThreadDataList->pThreadData=m_pLocalAsyncSocketExThreadData;
}
}
else // 如果分发线程链还没有创建,则创建一个
{ //No thread has instances of CAsyncSocketEx; Initialize data
m_spAsyncSocketExThreadDataList=new t_AsyncSocketExThreadDataList;
m_spAsyncSocketExThreadDataList->pNext=0;
m_pLocalAsyncSocketExThreadData=new t_AsyncSocketExThreadData; // 第一个分发线程
m_pLocalAsyncSocketExThreadData->nInstanceCount=1; // 只挂了当前的socket
m_pLocalAsyncSocketExThreadData->nThreadId=id; // 这个分发线程的threadID
m_pLocalAsyncSocketExThreadData->m_pHelperWindow=new CAsyncSocketExHelperWindow(m_pLocalAsyncSocketExThreadData); // 为这个分发线程创建CAsyncSocketExHelperWindow
m_spAsyncSocketExThreadDataList->pThreadData=m_pLocalAsyncSocketExThreadData;
...
}
下面看一个创建CAsyncSocketExHelperWindow的过程:
CAsyncSocketExHelperWindow(CAsyncSocketEx::t_AsyncSocketExThreadData* pThreadData)
{
// m_pAsyncSocketExWindowData是一个t_AsyncSocketExWindowData数组,
// 数组的每一个元素代表了一个CAsyncSocketEx,即要服务的socket
//Initialize data
m_pAsyncSocketExWindowData = new t_AsyncSocketExWindowData[512]; //Reserve space for 512 active sockets
memset(m_pAsyncSocketExWindowData, 0, 512*sizeof(t_AsyncSocketExWindowData));
m_nWindowDataSize=512; // 当前数组的大小,这是可自动扩充的,不过最大不能超过一个最大值
m_nSocketCount=0; // 当前数组中CAsyncSocketEx的数量
m_nWindowDataPos=0; // 如果要加一个新的CAsyncSocketEx进来,加到数组的哪个位置
m_pThreadData = pThreadData; // 这个CAsyncSocketExHelperWindow对应的分发线程,两者一一对应的
// 下面创建一个标准的窗口,不过并不显示出来
//Create window
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof wndclass;
wndclass.style=0;
wndclass.lpfnWndProc=WindowProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=GetModuleHandle(0);
wndclass.hIcon=0;
wndclass.hCursor=0;
wndclass.hbrBackground=0;
wndclass.lpszMenuName=0;
wndclass.lpszClassName=_T("CAsyncSocketEx Helper Window");
wndclass.hIconSm=0;
RegisterClassEx(&wndclass);
m_hWnd=CreateWindow(_T("CAsyncSocketEx Helper Window"), _T("CAsyncSocketEx Helper Window"), 0, 0, 0, 0, 0, 0, 0, 0, GetModuleHandle(0));
ASSERT(m_hWnd);
SetWindowLongPtr(m_hWnd, GWL_USERDATA, (LONG)this);
};
在调用完InitAsyncSocketExInstance()之后,CAsyncSocketEx的create()方法然后:
SOCKET hSocket = socket(m_SocketData.nFamily, nSocketType, 0); // 这是真正的socket api,建立一个socket
if (hSocket == INVALID_SOCKET)
return FALSE;
m_SocketData.hSocket = hSocket;
AttachHandle(hSocket); // 将当前创建的socket加到分发线程管理中,这样可以让分发线程来负责这个socket的消息
AttachHandle()调用了CAsyncSocketExHelperWindow的AddSocket方法:
BOOL AddSocket(CAsyncSocketEx *pSocket, int &nSocketIndex)
{
...
//Search for free slot
// 从m_nWindowDataPos开始搜索, 共搜m_nWindowDataSize个位置
// 由于是下面的模运算i%m_nWindowDataSize,因此到达数组尾时,从重从绕回来,即收遍数组的每一个位置
for (int i=m_nWindowDataPos;i<(m_nWindowDataSize+m_nWindowDataPos);i++)
{
// 注意模运算
if (!m_pAsyncSocketExWindowData[i%m_nWindowDataSize].m_pSocket) // 这个位置是空的
{
m_pAsyncSocketExWindowData[i%m_nWindowDataSize].m_pSocket=pSocket;
nSocketIndex=i%m_nWindowDataSize; // 在list中的pos
m_nWindowDataPos=(i+1)%m_nWindowDataSize; // 以后从下一个搜索位置开台
m_nSocketCount++;
return TRUE;
}
}
...
}
即在CAsyncSocketExHelperWindow管理的socket数组中,加上这次的这个CAsyncSocketEx。
CAsyncSocketEx的create()方法然后:
if (!AsyncSelect(lEvent))
{
Close();
return FALSE;
}
这里AsyncSelect()方法里调用了windows socket api: WSAAsyncSelect(),这个方法可以让windows在CAsyncSocketEx指定的socket上,当socket事件accept, read, write等发生时,发送消息到CAsyncSocketExHelperWindow中的窗口hWnd,然后CAsyncSocketExHelperWindow再通过回调函数WindowProc将消息发回到负责处理这个消息的CAsyncSocketEx上(这部分下面再详细分析)。
继续CAsyncSocketEx的create()方法:
if (!Bind(nSocketPort, lpszSocketAddress))
{
Close();
return FALSE;
}
Bind()实际上调用了socket api: bind()方法,实现了local address和socket的绑定。
3、Listen
create完以后,就是listen,这个比较简单,直接调用了socket api: listen(),在指定地址、端口进行监听。
程序运行到这里,核心的类已经初始化完成了,下面分析当socket有活动时,消息是如何从CAsyncSocketExHelperWindo分发到CAsyncSocketEx的。