090828项目进展:学习emule源码4,CAsyncSocketEx
来源:互联网 发布:淘宝教学 编辑:程序博客网 时间:2024/05/06 02:16
/*CAsyncSocketEx by Tim Kosse (Tim.Kosse@gmx.de)
Version 1.2 (2003-03-28)
--------------------------------------------------------
Introduction:
-------------
CAsyncSocketEx is a replacement for the MFC class CAsyncSocket.
This class was written because CAsyncSocket is not the fastest WinSock
wrapper and it's very hard to add new functionality to CAsyncSocket
derived classes. This class offers the same functionality as CAsyncSocket.
Also, CAsyncSocketEx offers some enhancements which were not possible with
CAsyncSocket without some tricks.
How do I use it?
----------------
Basically exactly like CAsyncSocket.
To use CAsyncSocketEx, just replace all occurrences of CAsyncSocket in your
code with CAsyncSocketEx. If you did not enhance CAsyncSocket yourself in
any way, you won't have to change anything else in your code.
Why is CAsyncSocketEx faster?
-----------------------------
CAsyncSocketEx is slightly faster when dispatching notification event messages.
First have a look at the way CAsyncSocket works. For each thread that uses
CAsyncSocket, a window is created. CAsyncSocket calls WSAAsyncSelect with
the handle of that window. Until here, CAsyncSocketEx works the same way.
But CAsyncSocket uses only one window message (WM_SOCKET_NOTIFY) for all
sockets within one thread. When the window receive WM_SOCKET_NOTIFY, wParam
contains the socket handle and the window looks up an CAsyncSocket instance
using a map. CAsyncSocketEx works differently. It's helper window uses a
wide range of different window messages (WM_USER through 0xBFFF) and passes
a different message to WSAAsyncSelect for each socket. When a message in
the specified range is received, CAsyncSocketEx looks up the pointer to a
CAsyncSocketEx instance in an Array using the index of message - WM_USER.
As you can see, CAsyncSocketEx uses the helper window in a more efficient
way, as it don't have to use the slow maps 避免每次都进行匹配to lookup it's own instance.
Still, speed increase is not very much, but it may be noticeable when using
a lot of sockets at the same time.在大量sockets存在的情况下会有明显的效率提升
Please note that the changes do not affect the raw data throughput rate,
CAsyncSocketEx only dispatches the notification messages faster.
What else does CAsyncSocketEx offer?
------------------------------------
CAsyncSocketEx offers a flexible layer system. One example is the proxy layer.
Just create an instance of the proxy layer, configure it and add it to the layer
chain of your CAsyncSocketEx instance. After that, you can connect through
proxies.
Benefit: You don't have to change much to use the layer system.
Another layer that is currently in development is the SSL layer to establish
SSL encrypted connections.
BOOL CAsyncSocketEx::Connect(LPCSTR lpszHostAddress, UINT nHostPort)
{
ASSERT( lpszHostAddress != NULL );
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Connect(lpszHostAddress, nHostPort);
else
#endif //NOLAYERS
{
SOCKADDR_IN sockAddr = {0};
sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);
if (sockAddr.sin_addr.s_addr == INADDR_NONE)
{
m_pAsyncGetHostByNameBuffer = new char[MAXGETHOSTSTRUCT];
m_nAsyncGetHostByNamePort = nHostPort;
m_hAsyncGetHostByNameHandle = WSAAsyncGetHostByName(GetHelperWindowHandle(), WM_SOCKETEX_GETHOST, lpszHostAddress, m_pAsyncGetHostByNameBuffer, MAXGETHOSTSTRUCT);
if (!m_hAsyncGetHostByNameHandle)
return FALSE;
WSASetLastError(WSAEWOULDBLOCK);
return TRUE;
}
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons((u_short)nHostPort);
return CAsyncSocketEx::Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}
}
BOOL CAsyncSocketEx::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
#ifndef NOLAYERS
if (m_pFirstLayer)
return m_pFirstLayer->Connect(lpSockAddr, nSockAddrLen);
else
#endif //NOLAYERS
return connect(m_SocketData.hSocket, lpSockAddr, nSockAddrLen) != SOCKET_ERROR;
是对Windows提供类的再次封装
}
- 090828项目进展:学习emule源码4,CAsyncSocketEx
- 090822项目进展:学习emule源码1,
- 090823项目进展:学习emule源码2,文件管理类
- 090824项目进展:学习emule源码3,CAICHHashTree
- eMule中的 CAsyncSocketEx 类
- 090818项目进展:了解emule基础知识
- 090819项目进展:了解emule基础知识,猜想实现的方向
- together项目进展报告4
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 项目进展
- 函数类型有什么用?
- verilog中wire与reg类型的区别
- dwr 保存数据对象
- 当寂寞遭遇激情后,床上留下了什么
- C语言中可变参数的用法
- 090828项目进展:学习emule源码4,CAsyncSocketEx
- 关于instance
- 学车潮席卷上海驾校 三因素助“好教练”独占“花魁”
- 最近静不下来,要折腾,开始感觉时间又不够用了。其中一个就是想要把英语提升上去 幸好在我实施之前看到了这篇文章
- Davinci调试环境的搭建(三)
- 告别Windows与Linux双系统启动的常见错误
- 使用memcached进行内存缓存
- 晕死,居然发不了那么多文字(公司真NB)---获得—学得差异假设(The Acquisition-Learning Hypothesis)
- flex知识点1