ConnectThread.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /****************************************************************/
  2. /* */
  3. /* CONNECTTHREAD.CPP */
  4. /* */
  5. /* Implementation of the Connect Thread. */
  6. /* Created when a client logs on to the server and processes */
  7. /* 'Send' commando's. */
  8. /* */
  9. /* Programmed by LYFZ van der Meer */
  10. /* http://www.LYFZvandermeer.nl */
  11. /* */
  12. /* Last updated: 15 july 2002 */
  13. /* */
  14. /****************************************************************/
  15. //在ConnectThread.cpp文件中实现连接线程的创建,这个线程用来处理与客户端的连接。
  16. //当一个客户登录到服务器此线程将被创建,并处理"Send"命令。
  17. #include "stdafx.h"
  18. #include "LYFZIPReceiveApp.h"
  19. #include "LYFZIPReceive.h"
  20. #include "ApplicationDlg.h"
  21. #include "ConnectThread.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. extern CLYFZIPReceive *g_pWndServer;
  28. IMPLEMENT_DYNCREATE(CConnectThread, CWinThread)
  29. /********************************************************************/
  30. /* */
  31. /* Function name : CConnectThread::CConnectThread */
  32. /* Description : Constructor */
  33. /* */
  34. /********************************************************************/
  35. CConnectThread::CConnectThread()
  36. {
  37. m_nReceivedBytes = 0;
  38. m_nSentBytes = 0;
  39. m_nTimerID = 0;
  40. m_LastDataTransferTime = CTime::GetCurrentTime();
  41. }
  42. /********************************************************************/
  43. /* */
  44. /* Function name : CConnectThread::~CConnectThread */
  45. /* Description : Destructor */
  46. /* */
  47. /********************************************************************/
  48. CConnectThread::~CConnectThread()
  49. {
  50. }
  51. /********************************************************************/
  52. /* */
  53. /* Function name : InitInstance */
  54. /* Description : Perform tasks that must be completed when the */
  55. /* thread is first created. */
  56. /* */
  57. /********************************************************************/
  58. //初始化线程
  59. BOOL CConnectThread::InitInstance()
  60. {
  61. try
  62. {
  63. g_pWndServer->m_CriticalSection.Lock();
  64. g_pWndServer->m_ThreadList.AddTail(this);
  65. g_pWndServer->m_CriticalSection.Unlock();
  66. // 把CSocket对象与socket句柄绑定
  67. m_ConnectSocket.Attach(m_hSocket);
  68. CString strIPAddress;
  69. UINT nPort;
  70. m_ConnectSocket.GetPeerName(strIPAddress, nPort);
  71. m_strRemoteHost=strIPAddress;
  72. // 通知服务器一个新的连接到达
  73. g_pWndServer->SendMessage(WM_THREADSTART, (WPARAM)this, 0);
  74. if (g_pWndServer->CheckMaxUsers())
  75. {
  76. // m_ConnectSocket.SendResponse("421 Too many users are connected, please try again later.");
  77. PostThreadMessage(WM_QUIT,0,0);WriteLogin("连接超过最大许可");
  78. }
  79. else
  80. if (!g_pWndServer->IsIPAddressAllowed(strIPAddress))
  81. {
  82. // m_ConnectSocket.SendResponse("421 Access denied, IP address was rejected by the server.");
  83. PostThreadMessage(WM_QUIT,0,0);WriteLogin("非法IP地址");
  84. }
  85. else
  86. {
  87. // AfxMessageBox(strIPAddress);
  88. // 发送欢迎信息给客户端
  89. // m_ConnectSocket.SendResponse("220 %s", g_pWndServer->GetWelcomeMessage());
  90. m_nTimerID = ::SetTimer(NULL, 0, 1000, TimerProc);
  91. }
  92. }
  93. catch(CException *e)
  94. {
  95. e->Delete();
  96. }
  97. return TRUE;
  98. }
  99. /********************************************************************/
  100. /* */
  101. /* Function name : ExitInstance */
  102. /* Description : Perform clean-up when the thread terminates. */
  103. /* */
  104. /********************************************************************/
  105. //线程终止
  106. int CConnectThread::ExitInstance()
  107. {
  108. try
  109. {
  110. m_ConnectSocket.Close();
  111. }
  112. catch(...)
  113. {
  114. }
  115. try
  116. {
  117. g_pWndServer->m_CriticalSection.Lock();
  118. // 从链表中删除当前线程
  119. POSITION pos = g_pWndServer->m_ThreadList.Find(this);
  120. if(pos != NULL)
  121. {
  122. g_pWndServer->m_ThreadList.RemoveAt(pos);
  123. }
  124. g_pWndServer->m_CriticalSection.Unlock();
  125. //通知服务主循环
  126. g_pWndServer->SendMessage(WM_THREADCLOSE, (WPARAM)this, 0);
  127. }
  128. catch(CException *e)
  129. {
  130. g_pWndServer->m_CriticalSection.Unlock();
  131. e->Delete();
  132. }
  133. return CWinThread::ExitInstance();
  134. }
  135. BEGIN_MESSAGE_MAP(CConnectThread, CWinThread)
  136. //{{AFX_MSG_MAP(CConnectThread)
  137. //}}AFX_MSG_MAP
  138. ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
  139. END_MESSAGE_MAP()
  140. /********************************************************************/
  141. /* */
  142. /* Function name : IncSentBytes */
  143. /* Description : Increment number of bytes sent by the server. */
  144. /* */
  145. /********************************************************************/
  146. void CConnectThread::IncSentBytes(int nBytes)
  147. {
  148. m_LastDataTransferTime = CTime::GetCurrentTime();
  149. m_nSentBytes += nBytes;
  150. // notify server class
  151. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)0, (LPARAM)nBytes);
  152. }
  153. /********************************************************************/
  154. /* */
  155. /* Function name : IncReceivedBytes */
  156. /* Description : Increment number of bytes received by the server.*/
  157. /* */
  158. /********************************************************************/
  159. void CConnectThread::IncReceivedBytes(int nBytes)
  160. {
  161. m_LastDataTransferTime = CTime::GetCurrentTime();
  162. m_nReceivedBytes += nBytes;
  163. // notify server class
  164. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)1, (LPARAM)nBytes);
  165. }
  166. /********************************************************************/
  167. /* */
  168. /* Function name : UpdateStatistic */
  169. /* Description : Specific statistics has been changed. */
  170. /* */
  171. /********************************************************************/
  172. void CConnectThread::UpdateStatistic(int nType)
  173. {
  174. // notify server class
  175. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)2, (LPARAM)nType);
  176. }
  177. /********************************************************************/
  178. /* */
  179. /* Function name : OnThreadMessage */
  180. /* Description : Thread message received. */
  181. /* */
  182. /********************************************************************/
  183. LRESULT CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  184. {
  185. switch(wParam)
  186. {
  187. case 0: // destroy data socket
  188. // AfxMessageBox("destroy conn");
  189. break;
  190. case 1: // quit !
  191. PostThreadMessage(WM_QUIT,0,0);
  192. break;
  193. default:
  194. break;
  195. }
  196. return 0L;
  197. }
  198. /********************************************************************/
  199. /* */
  200. /* Function name : TimerProc */
  201. /* Description : Callback function for timer. */
  202. /* */
  203. /********************************************************************/
  204. VOID CALLBACK CConnectThread::TimerProc(HWND hwnd, UINT uMsg, UINT uIDEvent, DWORD dwTime)
  205. {
  206. CConnectThread *pThread = (CConnectThread *)AfxGetThread();
  207. if (uIDEvent == pThread->m_nTimerID)
  208. {
  209. int nConnectionTimeout = g_pWndServer->GetTimeout();
  210. // check for connection timeout
  211. CTime time = pThread->m_LastDataTransferTime;
  212. time += CTimeSpan(0, 0, 0, 60);
  213. if (time < CTime::GetCurrentTime())
  214. {
  215. // pThread->m_ConnectSocket.SendResponse("426 Connection timed out, aborting transfer");
  216. pThread->PostThreadMessage(WM_QUIT,0,0);
  217. }
  218. }
  219. }