ConnectThread.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "LYFZReceiveMsgApp.h"
  19. #include "LYFZReceiveMsg.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 CLYFZReceiveMsg *g_pWndServer;
  28. CStringArray g_conniparray;
  29. CStringArray g_ipnoallowarray;
  30. IMPLEMENT_DYNCREATE(CConnectThread, CWinThread)
  31. /********************************************************************/
  32. /* */
  33. /* Function name : CConnectThread::CConnectThread */
  34. /* Description : Constructor */
  35. /* */
  36. /********************************************************************/
  37. CConnectThread::CConnectThread()
  38. {
  39. m_nReceivedBytes = 0;
  40. m_nSentBytes = 0;
  41. m_nTimerID = 0;
  42. m_LastDataTransferTime = CTime::GetCurrentTime();
  43. }
  44. /********************************************************************/
  45. /* */
  46. /* Function name : CConnectThread::~CConnectThread */
  47. /* Description : Destructor */
  48. /* */
  49. /********************************************************************/
  50. CConnectThread::~CConnectThread()
  51. {
  52. }
  53. /********************************************************************/
  54. /* */
  55. /* Function name : InitInstance */
  56. /* Description : Perform tasks that must be completed when the */
  57. /* thread is first created. */
  58. /* */
  59. /********************************************************************/
  60. //初始化线程
  61. extern BOOL AddConn(CDatabase *m_conndb);
  62. int FindArray(CStringArray *pArray, CString Str)
  63. {
  64. int count=0;
  65. for(int i=0; i<pArray->GetSize (); i++)
  66. {
  67. if(pArray->ElementAt (i)==Str)
  68. count++;
  69. }
  70. return count;
  71. }
  72. BOOL CConnectThread::InitInstance()
  73. {
  74. try
  75. {
  76. g_pWndServer->m_CriticalSection.Lock();
  77. g_pWndServer->m_ThreadList.AddTail(this);
  78. g_pWndServer->m_CriticalSection.Unlock();
  79. m_ConnectSocket.Attach(m_hSocket);
  80. m_ConnectSocket.m_pThread = this;
  81. CString strIPAddress;
  82. UINT nPort;
  83. m_ConnectSocket.GetPeerName(strIPAddress, nPort);
  84. m_strRemoteHost=strIPAddress;
  85. if(0)
  86. {
  87. int count=::FindArray(&g_conniparray, strIPAddress);
  88. if(count>20)
  89. {
  90. WriteLogin("连接超数ip:"+strIPAddress);
  91. PostThreadMessage(WM_QUIT,0,0);
  92. return 0;
  93. }
  94. g_conniparray.Add (strIPAddress);
  95. }
  96. // 把CSocket对象与socket句柄绑定
  97. // 通知服务器一个新的连接到达
  98. g_pWndServer->SendMessage(WM_THREADSTART, (WPARAM)this, 0);
  99. if(AddConn(&m_conndb)==0)
  100. {
  101. WriteLogin("打开新数据库连接失败");
  102. PostThreadMessage(WM_QUIT,0,0);
  103. return 0;
  104. }
  105. m_conndb.SetQueryTimeout(5*60);
  106. m_ConnectSocket.m_pConndb=&m_conndb;
  107. // m_ConnectSocket.m_pConndb=&g_db;
  108. if (g_pWndServer->CheckMaxUsers())
  109. {
  110. // m_ConnectSocket.SendResponse("421 Too many users are connected, please try again later.");
  111. WriteLogin("连接超过最大许可");
  112. PostThreadMessage(WM_QUIT,0,0);
  113. }
  114. else
  115. if (!g_pWndServer->IsIPAddressAllowed(strIPAddress))
  116. {
  117. // m_ConnectSocket.SendResponse("421 Access denied, IP address was rejected by the server.");
  118. WriteLogin("非法IP地址");
  119. PostThreadMessage(WM_QUIT,0,0);
  120. }
  121. else
  122. {
  123. // AfxMessageBox(strIPAddress);
  124. // 发送欢迎信息给客户端
  125. // m_ConnectSocket.SendResponse("220 %s", g_pWndServer->GetWelcomeMessage());
  126. m_nTimerID = ::SetTimer(NULL, 0, 1000, TimerProc);
  127. }
  128. }
  129. catch(CException *e)
  130. {
  131. e->Delete();
  132. }
  133. return TRUE;
  134. }
  135. /********************************************************************/
  136. /* */
  137. /* Function name : ExitInstance */
  138. /* Description : Perform clean-up when the thread terminates. */
  139. /* */
  140. /********************************************************************/
  141. //线程终止
  142. int CConnectThread::ExitInstance()
  143. {
  144. try
  145. {
  146. if(m_conndb.IsOpen())
  147. m_conndb.Close();
  148. g_pWndServer->m_CriticalSection.Lock();
  149. // 从链表中删除当前线程
  150. POSITION pos = g_pWndServer->m_ThreadList.Find(this);
  151. if(pos != NULL)
  152. {
  153. g_pWndServer->m_ThreadList.RemoveAt(pos);
  154. }
  155. g_pWndServer->m_CriticalSection.Unlock();
  156. //通知服务主循环
  157. g_pWndServer->SendMessage(WM_THREADCLOSE, (WPARAM)this, 0);
  158. }
  159. catch(CException *e)
  160. {
  161. g_pWndServer->m_CriticalSection.Unlock();
  162. e->Delete();
  163. }
  164. return CWinThread::ExitInstance();
  165. }
  166. BEGIN_MESSAGE_MAP(CConnectThread, CWinThread)
  167. //{{AFX_MSG_MAP(CConnectThread)
  168. //}}AFX_MSG_MAP
  169. #ifdef VC60
  170. ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
  171. #else
  172. ON_THREAD_MESSAGE(WM_THREADMSG, OnThreadMessage)
  173. #endif
  174. END_MESSAGE_MAP()
  175. /********************************************************************/
  176. /* */
  177. /* Function name : IncSentBytes */
  178. /* Description : Increment number of bytes sent by the server. */
  179. /* */
  180. /********************************************************************/
  181. void CConnectThread::IncSentBytes(int nBytes)
  182. {
  183. m_LastDataTransferTime = CTime::GetCurrentTime();
  184. m_nSentBytes += nBytes;
  185. // notify server class
  186. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)0, (LPARAM)nBytes);
  187. }
  188. /********************************************************************/
  189. /* */
  190. /* Function name : IncReceivedBytes */
  191. /* Description : Increment number of bytes received by the server.*/
  192. /* */
  193. /********************************************************************/
  194. void CConnectThread::IncReceivedBytes(int nBytes)
  195. {
  196. m_LastDataTransferTime = CTime::GetCurrentTime();
  197. m_nReceivedBytes += nBytes;
  198. // notify server class
  199. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)1, (LPARAM)nBytes);
  200. }
  201. /********************************************************************/
  202. /* */
  203. /* Function name : UpdateStatistic */
  204. /* Description : Specific statistics has been changed. */
  205. /* */
  206. /********************************************************************/
  207. void CConnectThread::UpdateStatistic(int nType)
  208. {
  209. // notify server class
  210. g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)2, (LPARAM)nType);
  211. }
  212. /********************************************************************/
  213. /* */
  214. /* Function name : OnThreadMessage */
  215. /* Description : Thread message received. */
  216. /* */
  217. /********************************************************************/
  218. #ifdef VC60
  219. LRESULT CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  220. {
  221. switch(wParam)
  222. {
  223. case 0: // destroy data socket
  224. // AfxMessageBox("destroy conn");
  225. m_ConnectSocket.DestroyDataConnection();
  226. break;
  227. case 1: // quit !
  228. PostThreadMessage(WM_QUIT,0,0);
  229. break;
  230. default:
  231. break;
  232. }
  233. return 0L;
  234. }
  235. #else
  236. void CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  237. {
  238. switch(wParam)
  239. {
  240. case 0: // destroy data socket
  241. // AfxMessageBox("destroy conn");
  242. m_ConnectSocket.DestroyDataConnection();
  243. break;
  244. case 1: // quit !
  245. PostThreadMessage(WM_QUIT,0,0);
  246. break;
  247. default:
  248. break;
  249. }
  250. //return 0L;
  251. }
  252. #endif
  253. /********************************************************************/
  254. /* */
  255. /* Function name : TimerProc */
  256. /* Description : Callback function for timer. */
  257. /* */
  258. /********************************************************************/
  259. VOID CALLBACK CConnectThread::TimerProc(HWND hwnd, UINT uMsg, UINT uIDEvent, DWORD dwTime)
  260. {
  261. CConnectThread *pThread = (CConnectThread *)AfxGetThread();
  262. if (uIDEvent == pThread->m_nTimerID)
  263. {
  264. int nConnectionTimeout = g_pWndServer->GetTimeout();
  265. // check for connection timeout
  266. CTime time = pThread->m_LastDataTransferTime;
  267. // time += CTimeSpan(0, 0, nConnectionTimeout, 0);
  268. time += CTimeSpan(0, 0, 0, 60);
  269. if (time < CTime::GetCurrentTime())
  270. {
  271. // pThread->m_ConnectSocket.SendResponse("426 Connection timed out, aborting transfer");
  272. pThread->PostThreadMessage(WM_QUIT,0,0);
  273. }
  274. }
  275. }