深入分析驴子系列(1)2011-04-02 博客园 wangok一直在看驴子的代码,网上进行深入分析的文章不多,也许 这和驴子的代码量太大, 代码质量不高也许有关系。但更多的也许是不想分享,舍不得分享。其实,它本身就是开 源的 不分享人家慢慢看也能看懂 。由于时间关系 我会陆续把分析的文章帖上来,与各 位网友分享,也希望大家拍砖 进行讨论 也把你的心得分享出来。。系列分析文章如果没 有特别注明 以easy mule 0.47为准CListenSocket 类 的作用就是 监听 等待客户 端的socket 到来 并维护到来的套接字 把accept进来的套接字 加入到list 中// 该函数的作用是创建本地的listensocket, 是否Accept 连接是由 winsock消息驱动 bool CListenSocket::StartListening() { bListening = true; // Creating the socket with SO_REUSEADDR may solve LowID issues if emule was restarted // quickly or started after a crash, but(!) it will also create another problem. If the // socket is already used by some other application (e.g. a 2nd emule), we though bind // to that socket leading to the situation that 2 applications are listening at the same // port! if (!Create (thePrefs.GetPort(), SOCK_STREAM, FD_ACCEPT, thePrefs.GetBindAddrA(), FALSE/*bReuseAddr*/)) return false; if (!Listen()) return false; m_port = thePrefs.GetPort(); return true; } // 该函数改名为RestartAccept 更合适 // 它的本意 是重新开始接受连接 // 为什么需要重新开始接受连接呢,原因是连接数过多情况 下, // 会暂时StopListeing (bListening 设置为false;) , // 连接数少 的情况下会重新开始接受连接RestartListening; // 应该注意的是:这个暂停和 重新开始并不实际关闭/打开本地listen端口,只是设置一个bool标志 void CListenSocket::ReStartListening() { bListening = true; ASSERT( m_nPendingConnections >= 0 ); if (m_nPendingConnections > 0) { m_nPendingConnections--; OnAccept(0); } }