theDBServer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /****************************************************************/
  2. /* */
  3. /* DBServer.cpp */
  4. /* */
  5. /* Implementation of the CtheDBServer class. */
  6. /* This class is a part of the Date Server Application */
  7. /* */
  8. /* Programmed by LYFZ van der Meer */
  9. /* Copyright LYFZ Software Solutions 2002 */
  10. /* http://www.LYFZvandermeer.nl */
  11. /* */
  12. /* Last updated: 10 july 2002 */
  13. /* */
  14. /****************************************************************/
  15. #include "stdafx.h"
  16. #include "theDBServer.h"
  17. #include "MyLock.h"
  18. //extern int g_port;
  19. CtheDBServer *g_pWndServer=NULL;
  20. // extern void WriteLogin(CString str);
  21. CtheDBServer::CtheDBServer()
  22. {
  23. m_nPort = g_port;
  24. m_nMaxUsers = 200;
  25. m_strWelcomeMessage = "Welcome to LYFZ Date Server";
  26. m_strGoodbyeMessage = "Bye";
  27. m_nTimeout = 30;
  28. m_bRunning = FALSE;
  29. m_hWnd = NULL;
  30. m_nConnectionCount = 0;
  31. // intialize statistics
  32. m_dwTotalSentBytes = 0;
  33. m_dwTotalReceivedBytes = 0;
  34. m_nTotalConnections = 0;
  35. m_nFilesDownloaded = 0;
  36. m_nFilesUploaded = 0;
  37. m_nFailedDownloads = 0;
  38. m_nFailedUploads = 0;
  39. m_nSecurityMode = 0;
  40. m_nStatisticsInterval = 0;
  41. // load users
  42. m_UserManager.Serialize(FALSE);
  43. // load security
  44. m_SecurityManager.Serialize(FALSE);
  45. }
  46. CtheDBServer::~CtheDBServer()
  47. {
  48. Stop();
  49. }
  50. BEGIN_MESSAGE_MAP(CtheDBServer, CWnd)
  51. //{{AFX_MSG_MAP(CtheDBServer)
  52. ON_WM_TIMER()
  53. //}}AFX_MSG_MAP
  54. ON_MESSAGE(WM_THREADSTART, OnThreadStart)
  55. ON_MESSAGE(WM_THREADCLOSE, OnThreadClose)
  56. ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
  57. END_MESSAGE_MAP()
  58. /********************************************************************/
  59. /* */
  60. /* Function name : Start */
  61. /* Description : Start listining on port 21 and accept new */
  62. /* connections. */
  63. /* */
  64. /********************************************************************/
  65. //在21号端口启动侦听并准备接受新的连接
  66. BOOL CtheDBServer::Start()
  67. {
  68. if (m_bRunning)
  69. {
  70. WriteLogin("服务已启动");
  71. return FALSE;
  72. }
  73. // 为消息发送创建一个新的窗口
  74. if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "Date Server Notification Sink", WS_POPUP, 0,0,0,0, NULL, 0))
  75. {
  76. AddTraceLine(0, "Failed to create notification window.");
  77. WriteLogin("创建响应窗口失败");
  78. return FALSE;
  79. }
  80. // 创建一个新的套接字
  81. if (m_ListenSocket.Create(g_port)) // g_port==5678;
  82. {
  83. // 启动侦听
  84. if (m_ListenSocket.Listen())
  85. {
  86. g_pWndServer=this;
  87. // m_ListenSocket.m_pWndServer = this;
  88. m_bRunning = TRUE;
  89. // Jeff.定时器1 用于在主窗口显示接收返回等数值信息;
  90. SetTimer(1, m_nStatisticsInterval, NULL); // 定时器1;
  91. AddTraceLine(0, "Date Server started on port %d.", g_port);
  92. return TRUE;
  93. }
  94. }
  95. DWORD dwError = GetLastError();
  96. ErrorExit(CString("监听5678端口失败"), dwError);
  97. AddTraceLine(0, "Date Server failed to listen on port %d.", g_port);
  98. // 创建失败,销毁通知窗口;
  99. if (IsWindow(m_hWnd))
  100. DestroyWindow();
  101. m_hWnd = NULL;
  102. return FALSE;
  103. }
  104. /********************************************************************/
  105. /* */
  106. /* Function name : Stop */
  107. /* Description : Stop Date Server. */
  108. /* */
  109. /********************************************************************/
  110. //停止数据服务器
  111. extern CConnectThread *g_pThreadPt[200];
  112. void CtheDBServer::Stop()
  113. {
  114. if (!m_bRunning)
  115. return;
  116. // 停止统计计时器
  117. KillTimer(1);
  118. m_bRunning = FALSE;
  119. m_ListenSocket.Close();
  120. CConnectThread* pThread = NULL;
  121. //关闭所有线程
  122. do
  123. {
  124. m_CriticalSection.Lock();
  125. POSITION pos = m_ThreadList.GetHeadPosition();
  126. if (pos != NULL)
  127. {
  128. pThread = (CConnectThread *)m_ThreadList.GetAt(pos);
  129. m_CriticalSection.Unlock();
  130. // 保存线程
  131. int nThreadID = pThread->m_nThreadID;
  132. HANDLE hThread = pThread->m_hThread;
  133. AddTraceLine(0, "[%d] Shutting down thread...", nThreadID);
  134. // 通知线程停止
  135. pThread->SetThreadPriority(THREAD_PRIORITY_HIGHEST);
  136. pThread->PostThreadMessage(WM_QUIT,0,0);
  137. //等待线程终止
  138. if (WaitWithMessageLoop(hThread, 5000) == FALSE)
  139. {
  140. // 线程不能终止
  141. AddTraceLine(0, "[%d] Problem while killing thread.", nThreadID);
  142. //清除线程
  143. m_CriticalSection.Lock();
  144. POSITION rmPos = m_ThreadList.Find(pThread);
  145. if (rmPos != NULL)
  146. m_ThreadList.RemoveAt(rmPos);
  147. m_CriticalSection.Unlock();
  148. }
  149. else
  150. {
  151. AddTraceLine(0, "[%d] Thread successfully stopped.", nThreadID);
  152. }
  153. }
  154. else
  155. {
  156. m_CriticalSection.Unlock();
  157. pThread = NULL;
  158. }
  159. }
  160. while (pThread != NULL);
  161. //更新服务器状态
  162. AddTraceLine(0, "Date Server stopped.");
  163. for(int i=0; i<200; i++)
  164. {
  165. g_pThreadPt[i]=NULL;
  166. }
  167. if (IsWindow(m_hWnd))
  168. DestroyWindow();
  169. m_hWnd = NULL;
  170. }
  171. /********************************************************************/
  172. /* */
  173. /* Function name : IsActive */
  174. /* Description : Is Date Server active? */
  175. /* */
  176. /********************************************************************/
  177. BOOL CtheDBServer::IsActive()
  178. {
  179. return m_bRunning;
  180. }
  181. /********************************************************************/
  182. /* */
  183. /* Function name : SetMaxUsers */
  184. /* Description : Set maximum number of users */
  185. /* */
  186. /********************************************************************/
  187. void CtheDBServer::SetMaxUsers(int nValue)
  188. {
  189. // m_nMaxUsers = nValue;
  190. }
  191. /********************************************************************/
  192. /* */
  193. /* Function name : SetPort */
  194. /* Description : Set listening port for new connections */
  195. /* */
  196. /********************************************************************/
  197. void CtheDBServer::SetPort(int nValue)
  198. {
  199. // m_nPort = nValue;
  200. }
  201. /********************************************************************/
  202. /* */
  203. /* Function name : SetTimeout */
  204. /* Description : Set connection timeout */
  205. /* */
  206. /********************************************************************/
  207. void CtheDBServer::SetTimeout(int nValue)
  208. {
  209. // m_nTimeout = nValue;
  210. }
  211. /********************************************************************/
  212. /* */
  213. /* Function name : SetTimeout */
  214. /* Description : Set connection timeout */
  215. /* */
  216. /********************************************************************/
  217. void CtheDBServer::SetStatisticsInterval(int nValue)
  218. {
  219. m_nStatisticsInterval = nValue;
  220. if (m_nStatisticsInterval != 0)
  221. {
  222. KillTimer(1);
  223. SetTimer(1, nValue, NULL);
  224. }
  225. else
  226. {
  227. KillTimer(1);
  228. }
  229. }
  230. /********************************************************************/
  231. /* */
  232. /* Function name : SetWelcomeMessage */
  233. /* Description : Set welcome message */
  234. /* */
  235. /********************************************************************/
  236. void CtheDBServer::SetWelcomeMessage(LPCTSTR lpszText)
  237. {
  238. m_strWelcomeMessage = lpszText;
  239. }
  240. /********************************************************************/
  241. /* */
  242. /* Function name : SetGoodbyeMessage */
  243. /* Description : Set goodbye message */
  244. /* */
  245. /********************************************************************/
  246. void CtheDBServer::SetGoodbyeMessage(LPCTSTR lpszText)
  247. {
  248. m_strGoodbyeMessage = lpszText;
  249. }
  250. /********************************************************************/
  251. /* */
  252. /* Function name : Initialize */
  253. /* Description : Initialize event sink. */
  254. /* */
  255. /********************************************************************/
  256. void CtheDBServer::Initialize(CFTPEventSink *pEventSink)
  257. {
  258. m_pEventSink = pEventSink;
  259. }
  260. /********************************************************************/
  261. /* */
  262. /* Function name : AddTraceLine */
  263. /* Description : FTP status change. */
  264. /* */
  265. /********************************************************************/
  266. void CtheDBServer::AddTraceLine(int nType, LPCTSTR pstrFormat, ...)
  267. {
  268. CString str;
  269. // format and write the data we were given
  270. va_list args;
  271. va_start(args, pstrFormat);
  272. str.FormatV(pstrFormat, args);
  273. m_pEventSink->OnFTPStatusChange(nType, str);
  274. }
  275. /********************************************************************/
  276. /* */
  277. /* Function name : OnThreadStart */
  278. /* Description : Called when a new thread has started. */
  279. /* */
  280. /********************************************************************/
  281. LRESULT CtheDBServer::OnThreadStart(WPARAM wParam, LPARAM)
  282. {
  283. m_nConnectionCount++;
  284. m_nTotalConnections++;
  285. CConnectThread *pThread = (CConnectThread *)wParam;
  286. // pThread->m_ConnectSocket.GetPeerName(pThread->m_strRemoteHost, port);
  287. AddTraceLine(0, "[%d] Client connected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
  288. return TRUE;
  289. }
  290. /********************************************************************/
  291. /* */
  292. /* Function name : OnThreadClose */
  293. /* Description : Called when a thread is about to stop. */
  294. /* */
  295. /********************************************************************/
  296. LRESULT CtheDBServer::OnThreadClose(WPARAM wParam, LPARAM lParam)
  297. {
  298. m_nConnectionCount--;
  299. CConnectThread *pThread = (CConnectThread *)wParam;
  300. AddTraceLine(0, "[%d] Client disconnected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
  301. m_pEventSink->OnFTPUserDisconnected(pThread->m_nThreadID, pThread->m_ConnectSocket.m_strUserName);
  302. return TRUE;
  303. }
  304. /********************************************************************/
  305. /* */
  306. /* Function name : OnThreadMessage */
  307. /* Description : Message sent from connect connection. */
  308. /* */
  309. /********************************************************************/
  310. LRESULT CtheDBServer::OnThreadMessage(WPARAM wParam, LPARAM lParam)
  311. {
  312. switch(wParam)
  313. {
  314. case 0:
  315. m_dwTotalSentBytes += (int)lParam;
  316. break;
  317. case 1:
  318. m_dwTotalReceivedBytes += (int)lParam;
  319. break;
  320. case 2:
  321. switch(lParam)
  322. {
  323. case FTPSTAT_DOWNLOADSUCCEEDED:
  324. m_nFilesDownloaded++;
  325. break;
  326. case FTPSTAT_UPLOADSUCCEEDED:
  327. m_nFilesUploaded++;
  328. break;
  329. case FTPSTAT_DOWNLOADFAILED:
  330. m_nFailedDownloads++;
  331. break;
  332. case FTPSTAT_UPLOADFAILED:
  333. m_nFailedUploads++;
  334. break;
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. return TRUE;
  341. }
  342. /********************************************************************/
  343. /* */
  344. /* Function name : CheckMaxUsers */
  345. /* Description : Reached maximum number of connections? */
  346. /* */
  347. /********************************************************************/
  348. BOOL CtheDBServer::CheckMaxUsers()
  349. {
  350. if (m_nConnectionCount > m_nMaxUsers)
  351. return TRUE;
  352. else
  353. return FALSE;
  354. }
  355. /********************************************************************/
  356. /* */
  357. /* Function name : OnTimer */
  358. /* Description : Update statictics. */
  359. /* */
  360. /********************************************************************/
  361. void CtheDBServer::OnTimer(UINT nIDEvent)
  362. {
  363. // update statictics ?
  364. if (nIDEvent == 1)
  365. {
  366. m_pEventSink->OnFTPSentBytesChange(m_dwTotalSentBytes);
  367. m_pEventSink->OnFTPReceivedBytesChange(m_dwTotalReceivedBytes);
  368. m_pEventSink->OnFTPStatisticChange(0, m_nTotalConnections);
  369. m_pEventSink->OnFTPStatisticChange(1, m_nConnectionCount);
  370. m_pEventSink->OnFTPStatisticChange(2, m_nFilesDownloaded);
  371. m_pEventSink->OnFTPStatisticChange(3, m_nFilesUploaded);
  372. m_pEventSink->OnFTPStatisticChange(4, m_nFailedDownloads);
  373. m_pEventSink->OnFTPStatisticChange(5, m_nFailedUploads);
  374. }
  375. CWnd::OnTimer(nIDEvent);
  376. }
  377. /********************************************************************/
  378. /* */
  379. /* Function name : SetSecurityMode */
  380. /* Description : Set security mode. */
  381. /* */
  382. /********************************************************************/
  383. void CtheDBServer::SetSecurityMode(BOOL bBlockSpecific)
  384. {
  385. m_nSecurityMode = bBlockSpecific ? 0 : 1;
  386. }
  387. int FindArray2(CStringArray *pArray, CString Str)
  388. {
  389. for(int i=0; i<pArray->GetSize (); i++)
  390. {
  391. if(pArray->ElementAt (i)==Str)
  392. return i;
  393. }
  394. return -1;
  395. }
  396. //extern CStringArray g_ipnoallowarray;
  397. /********************************************************************/
  398. /* */
  399. /* Function name : IsIPAddressAllowed */
  400. /* Description : Check (based on blockmode) if IP is allowed. */
  401. /* */
  402. /********************************************************************/
  403. BOOL CtheDBServer::IsIPAddressAllowed(LPCTSTR lpszIPAddress)
  404. {
  405. // MyLock lock("IsIPAddressAllowed");
  406. // if(::FindArray2 (&g_ipnoallowarray, lpszIPAddress)!=-1)return 0;
  407. return 1;
  408. /* if (m_nSecurityMode == 0)
  409. {
  410. return !m_SecurityManager.IsIPAddressBlocked(lpszIPAddress);
  411. }
  412. else
  413. {
  414. return m_SecurityManager.IsIPAddressNonBlocked(lpszIPAddress);
  415. }*/
  416. }