theDBServer.cpp 13 KB

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