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提供类的再次封装
}

原创粉丝点击