ConnectThread.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "CPhotoFTPReceiveApp.h"
  19. #include "CPhotoFTPReceive.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. IMPLEMENT_DYNCREATE(CConnectThread, CWinThread)
  28. /********************************************************************/
  29. /* */
  30. /* Function name : CConnectThread::CConnectThread */
  31. /* Description : Constructor */
  32. /* */
  33. /********************************************************************/
  34. CConnectThread::CConnectThread()
  35. {
  36. m_nReceivedBytes = 0;
  37. m_nSentBytes = 0;
  38. m_nTimerID = 0;
  39. m_LastDataTransferTime = CTime::GetCurrentTime();
  40. }
  41. /********************************************************************/
  42. /* */
  43. /* Function name : CConnectThread::~CConnectThread */
  44. /* Description : Destructor */
  45. /* */
  46. /********************************************************************/
  47. CConnectThread::~CConnectThread()
  48. {
  49. }
  50. /********************************************************************/
  51. /* */
  52. /* Function name : InitInstance */
  53. /* Description : Perform tasks that must be completed when the */
  54. /* thread is first created. */
  55. /* */
  56. /********************************************************************/
  57. //初始化线程
  58. BOOL CConnectThread::InitInstance()
  59. {
  60. TRACE("CConnectThread初始化线程\n");
  61. try
  62. {
  63. // 把CSocket对象与socket句柄绑定
  64. m_ConnectSocket.Attach(m_hSocket);
  65. m_ConnectSocket.m_pThread = this;
  66. CString strIPAddress;
  67. UINT nPort;
  68. m_ConnectSocket.GetPeerName(strIPAddress, nPort);
  69. // 通知服务器一个新的连接到达
  70. m_pWndServer->SendMessage(WM_THREADSTART, (WPARAM)this, 0);
  71. if (((CCPhotoFTPReceive *)m_pWndServer)->CheckMaxUsers())
  72. {
  73. m_ConnectSocket.SendResponse("421 Too many users are connected, please try again later.");
  74. PostThreadMessage(WM_QUIT,0,0);
  75. }
  76. else if (!((CCPhotoFTPReceive *)m_pWndServer)->IsIPAddressAllowed(strIPAddress))
  77. {
  78. m_ConnectSocket.SendResponse("421 Access denied, IP address was rejected by the server.");
  79. PostThreadMessage(WM_QUIT,0,0);
  80. }
  81. else
  82. {
  83. // 发送欢迎信息给客户端
  84. m_ConnectSocket.SendResponse("220 %s", ((CCPhotoFTPReceive *)m_pWndServer)->GetWelcomeMessage());
  85. m_nTimerID = ::SetTimer(NULL, 0, 1000, TimerProc);
  86. }
  87. }
  88. catch(CException *e)
  89. {
  90. e->Delete();
  91. }
  92. return TRUE;
  93. }
  94. /********************************************************************/
  95. /* */
  96. /* Function name : ExitInstance */
  97. /* Description : Perform clean-up when the thread terminates. */
  98. /* */
  99. /********************************************************************/
  100. //线程终止
  101. int CConnectThread::ExitInstance()
  102. {
  103. CCPhotoFTPReceive *pWnd = (CCPhotoFTPReceive *)m_pWndServer;
  104. try
  105. {
  106. pWnd->m_CriticalSection.Lock();
  107. // 从链表中删除当前线程
  108. POSITION pos = pWnd->m_ThreadList.Find(this);
  109. if(pos != NULL)
  110. {
  111. pWnd->m_ThreadList.RemoveAt(pos);
  112. }
  113. pWnd->m_CriticalSection.Unlock();
  114. //通知服务主循环
  115. pWnd->SendMessage(WM_THREADCLOSE, (WPARAM)this, 0);
  116. }
  117. catch(CException *e)
  118. {
  119. pWnd->m_CriticalSection.Unlock();
  120. e->Delete();
  121. }
  122. return CWinThread::ExitInstance();
  123. }
  124. BEGIN_MESSAGE_MAP(CConnectThread, CWinThread)
  125. //{{AFX_MSG_MAP(CConnectThread)
  126. //}}AFX_MSG_MAP
  127. //ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
  128. ON_THREAD_MESSAGE(WM_THREADMSG, OnThreadMessage)
  129. END_MESSAGE_MAP()
  130. /********************************************************************/
  131. /* */
  132. /* Function name : IncSentBytes */
  133. /* Description : Increment number of bytes sent by the server. */
  134. /* */
  135. /********************************************************************/
  136. void CConnectThread::IncSentBytes(int nBytes)
  137. {
  138. m_LastDataTransferTime = CTime::GetCurrentTime();
  139. m_nSentBytes += nBytes;
  140. // notify server class
  141. m_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)0, (LPARAM)nBytes);
  142. }
  143. /********************************************************************/
  144. /* */
  145. /* Function name : IncReceivedBytes */
  146. /* Description : Increment number of bytes received by the server.*/
  147. /* */
  148. /********************************************************************/
  149. void CConnectThread::IncReceivedBytes(int nBytes)
  150. {
  151. m_LastDataTransferTime = CTime::GetCurrentTime();
  152. m_nReceivedBytes += nBytes;
  153. // notify server class
  154. m_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)1, (LPARAM)nBytes);
  155. }
  156. /********************************************************************/
  157. /* */
  158. /* Function name : UpdateStatistic */
  159. /* Description : Specific statistics has been changed. */
  160. /* */
  161. /********************************************************************/
  162. void CConnectThread::UpdateStatistic(int nType)
  163. {
  164. // notify server class
  165. m_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)2, (LPARAM)nType);
  166. }
  167. /********************************************************************/
  168. /* */
  169. /* Function name : OnThreadMessage */
  170. /* Description : Thread message received. */
  171. /* */
  172. /********************************************************************/
  173. //LRESULT CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  174. void CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  175. {
  176. switch(wParam)
  177. {
  178. case 0: // destroy data socket
  179. // AfxMessageBox("destroy conn");
  180. m_ConnectSocket.DestroyDataConnection();
  181. break;
  182. case 1: // quit !
  183. PostThreadMessage(WM_QUIT,0,0);
  184. break;
  185. default:
  186. break;
  187. }
  188. //return 0L;
  189. }
  190. /********************************************************************/
  191. /* */
  192. /* Function name : TimerProc */
  193. /* Description : Callback function for timer. */
  194. /* */
  195. /********************************************************************/
  196. VOID CALLBACK CConnectThread::TimerProc(HWND hwnd, UINT uMsg, UINT uIDEvent, DWORD dwTime)
  197. {
  198. CConnectThread *pThread = (CConnectThread *)AfxGetThread();
  199. if (uIDEvent == pThread->m_nTimerID)
  200. {
  201. int nConnectionTimeout = ((CCPhotoFTPReceive *)pThread->m_pWndServer)->GetTimeout();
  202. // check for connection timeout
  203. CTime time = pThread->m_LastDataTransferTime;
  204. time += CTimeSpan(0, 0, nConnectionTimeout, 0);
  205. if (time < CTime::GetCurrentTime())
  206. {
  207. pThread->m_ConnectSocket.SendResponse("426 Connection timed out, aborting transfer");
  208. pThread->PostThreadMessage(WM_QUIT,0,0);
  209. }
  210. }
  211. }