123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- /****************************************************************/
- /* */
- /* DBServer.cpp */
- /* */
- /* Implementation of the CtheDBServer class. */
- /* This class is a part of the Date Server Application */
- /* */
- /* Programmed by LYFZ van der Meer */
- /* Copyright LYFZ Software Solutions 2002 */
- /* http://www.LYFZvandermeer.nl */
- /* */
- /* Last updated: 10 july 2002 */
- /* */
- /****************************************************************/
- #include "stdafx.h"
- #include "theDBServer.h"
- #include "MyLock.h"
- //extern int g_port;
- CtheDBServer *g_pWndServer=NULL;
- CtheDBServer::CtheDBServer()
- {
- m_nPort = g_dwTCPPort;
- m_nMaxUsers = 200;
- m_strWelcomeMessage = "Welcome to LYFZ Date Server";
- m_strGoodbyeMessage = "Bye";
- m_nTimeout = 30;
- m_bRunning = FALSE;
- m_hWnd = NULL;
- m_nConnectionCount = 0;
- // intialize statistics
- m_dwTotalSentBytes = 0;
- m_dwTotalReceivedBytes = 0;
- m_nTotalConnections = 0;
- m_nFilesDownloaded = 0;
- m_nFilesUploaded = 0;
- m_nFailedDownloads = 0;
- m_nFailedUploads = 0;
- m_nSecurityMode = 0;
- m_nStatisticsInterval = 0;
- // load users
- m_UserManager.Serialize(FALSE);
- // load security
- m_SecurityManager.Serialize(FALSE);
- }
- CtheDBServer::~CtheDBServer()
- {
- Stop();
- }
- BEGIN_MESSAGE_MAP(CtheDBServer, CWnd)
- //{{AFX_MSG_MAP(CtheDBServer)
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_THREADSTART, OnThreadStart)
- ON_MESSAGE(WM_THREADCLOSE, OnThreadClose)
- ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
- END_MESSAGE_MAP()
- /********************************************************************/
- /* */
- /* Function name : Start */
- /* Description : Start listining on port 21 and accept new */
- /* connections. */
- /* */
- /********************************************************************/
- //在21号端口启动侦听并准备接受新的连接
- BOOL CtheDBServer::Start()
- {
- if (m_bRunning)
- {
- WriteTextLog("服务已启动");
- return FALSE;
- }
- // 为消息发送创建一个新的窗口
- if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "Date Server Notification Sink", WS_POPUP, 0,0,0,0, NULL, 0))
- {
- AddTraceLine(0, "Failed to create notification window.");
- WriteTextLog("创建响应窗口失败");
- return FALSE;
- }
- // 创建一个新的套接字
- if (m_ListenSocket.Create(g_dwTCPPort)) // g_port==5678;
- {
- // 启动侦听
- if (m_ListenSocket.Listen())
- {
- g_pWndServer=this;
- // m_ListenSocket.m_pWndServer = this;
- m_bRunning = TRUE;
-
- // Jeff.定时器1 用于在主窗口显示接收返回等数值信息;
- SetTimer(1, m_nStatisticsInterval, NULL); // 定时器1;
- AddTraceLine(0, "Date Server started on port %d.", g_dwTCPPort);
- return TRUE;
- }
- }
- DWORD dwError = GetLastError();
- ErrorExit(CString("监听5678端口失败"), dwError);
- AddTraceLine(0, "Date Server failed to listen on port %d.", g_dwTCPPort);
- // 创建失败,销毁通知窗口;
- if (IsWindow(m_hWnd))
- DestroyWindow();
- m_hWnd = NULL;
- return FALSE;
- }
- /********************************************************************/
- /* */
- /* Function name : Stop */
- /* Description : Stop Date Server. */
- /* */
- /********************************************************************/
- //停止数据服务器
- extern CConnectThread *g_pThreadPt[200];
- void CtheDBServer::Stop()
- {
- if (!m_bRunning)
- return;
- // 停止统计计时器
- KillTimer(1);
- m_bRunning = FALSE;
- m_ListenSocket.Close();
- CConnectThread* pThread = NULL;
- //关闭所有线程
- do
- {
- m_CriticalSection.Lock();
- POSITION pos = m_ThreadList.GetHeadPosition();
- if (pos != NULL)
- {
- pThread = (CConnectThread *)m_ThreadList.GetAt(pos);
-
- m_CriticalSection.Unlock();
- // 保存线程
- int nThreadID = pThread->m_nThreadID;
- HANDLE hThread = pThread->m_hThread;
- AddTraceLine(0, "[%d] Shutting down thread...", nThreadID);
- // 通知线程停止
- pThread->SetThreadPriority(THREAD_PRIORITY_HIGHEST);
- pThread->PostThreadMessage(WM_QUIT,0,0);
- //等待线程终止
- if (WaitWithMessageLoop(hThread, 5000) == FALSE)
- {
- // 线程不能终止
- AddTraceLine(0, "[%d] Problem while killing thread.", nThreadID);
- //清除线程
- m_CriticalSection.Lock();
- POSITION rmPos = m_ThreadList.Find(pThread);
- if (rmPos != NULL)
- m_ThreadList.RemoveAt(rmPos);
- m_CriticalSection.Unlock();
- }
- else
- {
- AddTraceLine(0, "[%d] Thread successfully stopped.", nThreadID);
- }
- }
- else
- {
- m_CriticalSection.Unlock();
- pThread = NULL;
- }
- }
- while (pThread != NULL);
- //更新服务器状态
- AddTraceLine(0, "Date Server stopped.");
- for(int i=0; i<200; i++)
- {
- g_pThreadPt[i]=NULL;
- }
- if (IsWindow(m_hWnd))
- DestroyWindow();
- m_hWnd = NULL;
- }
- /********************************************************************/
- /* */
- /* Function name : IsActive */
- /* Description : Is Date Server active? */
- /* */
- /********************************************************************/
- BOOL CtheDBServer::IsActive()
- {
- return m_bRunning;
- }
- /********************************************************************/
- /* */
- /* Function name : SetMaxUsers */
- /* Description : Set maximum number of users */
- /* */
- /********************************************************************/
- void CtheDBServer::SetMaxUsers(int nValue)
- {
- // m_nMaxUsers = nValue;
- }
- /********************************************************************/
- /* */
- /* Function name : SetPort */
- /* Description : Set listening port for new connections */
- /* */
- /********************************************************************/
- void CtheDBServer::SetPort(int nValue)
- {
- // m_nPort = nValue;
- }
- /********************************************************************/
- /* */
- /* Function name : SetTimeout */
- /* Description : Set connection timeout */
- /* */
- /********************************************************************/
- void CtheDBServer::SetTimeout(int nValue)
- {
- // m_nTimeout = nValue;
- }
- /********************************************************************/
- /* */
- /* Function name : SetTimeout */
- /* Description : Set connection timeout */
- /* */
- /********************************************************************/
- void CtheDBServer::SetStatisticsInterval(int nValue)
- {
- m_nStatisticsInterval = nValue;
- if (m_nStatisticsInterval != 0)
- {
- KillTimer(1);
- SetTimer(1, nValue, NULL);
- }
- else
- {
- KillTimer(1);
- }
- }
- /********************************************************************/
- /* */
- /* Function name : SetWelcomeMessage */
- /* Description : Set welcome message */
- /* */
- /********************************************************************/
- void CtheDBServer::SetWelcomeMessage(LPCTSTR lpszText)
- {
- m_strWelcomeMessage = lpszText;
- }
- /********************************************************************/
- /* */
- /* Function name : SetGoodbyeMessage */
- /* Description : Set goodbye message */
- /* */
- /********************************************************************/
- void CtheDBServer::SetGoodbyeMessage(LPCTSTR lpszText)
- {
- m_strGoodbyeMessage = lpszText;
- }
- /********************************************************************/
- /* */
- /* Function name : Initialize */
- /* Description : Initialize event sink. */
- /* */
- /********************************************************************/
- void CtheDBServer::Initialize(CFTPEventSink *pEventSink)
- {
- m_pEventSink = pEventSink;
- }
- /********************************************************************/
- /* */
- /* Function name : AddTraceLine */
- /* Description : FTP status change. */
- /* */
- /********************************************************************/
- void CtheDBServer::AddTraceLine(int nType, LPCTSTR pstrFormat, ...)
- {
- CString str;
- // format and write the data we were given
- va_list args;
- va_start(args, pstrFormat);
- str.FormatV(pstrFormat, args);
- m_pEventSink->OnFTPStatusChange(nType, str);
- }
- /********************************************************************/
- /* */
- /* Function name : OnThreadStart */
- /* Description : Called when a new thread has started. */
- /* */
- /********************************************************************/
- LRESULT CtheDBServer::OnThreadStart(WPARAM wParam, LPARAM)
- {
- m_nConnectionCount++;
- m_nTotalConnections++;
- CConnectThread *pThread = (CConnectThread *)wParam;
-
- // pThread->m_ConnectSocket.GetPeerName(pThread->m_strRemoteHost, port);
- AddTraceLine(0, "[%d] Client connected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
- return TRUE;
- }
- /********************************************************************/
- /* */
- /* Function name : OnThreadClose */
- /* Description : Called when a thread is about to stop. */
- /* */
- /********************************************************************/
- LRESULT CtheDBServer::OnThreadClose(WPARAM wParam, LPARAM lParam)
- {
- m_nConnectionCount--;
- CConnectThread *pThread = (CConnectThread *)wParam;
- AddTraceLine(0, "[%d] Client disconnected from %s.", pThread->m_nThreadID, pThread->m_strRemoteHost);
-
- m_pEventSink->OnFTPUserDisconnected(pThread->m_nThreadID, pThread->m_ConnectSocket.m_strUserName);
- return TRUE;
- }
- /********************************************************************/
- /* */
- /* Function name : OnThreadMessage */
- /* Description : Message sent from connect connection. */
- /* */
- /********************************************************************/
- LRESULT CtheDBServer::OnThreadMessage(WPARAM wParam, LPARAM lParam)
- {
- switch(wParam)
- {
- case 0:
- m_dwTotalSentBytes += (int)lParam;
- break;
- case 1:
- m_dwTotalReceivedBytes += (int)lParam;
- break;
- case 2:
- switch(lParam)
- {
- case FTPSTAT_DOWNLOADSUCCEEDED:
- m_nFilesDownloaded++;
- break;
- case FTPSTAT_UPLOADSUCCEEDED:
- m_nFilesUploaded++;
- break;
- case FTPSTAT_DOWNLOADFAILED:
- m_nFailedDownloads++;
- break;
- case FTPSTAT_UPLOADFAILED:
- m_nFailedUploads++;
- break;
- }
- break;
- default:
- break;
- }
- return TRUE;
- }
- /********************************************************************/
- /* */
- /* Function name : CheckMaxUsers */
- /* Description : Reached maximum number of connections? */
- /* */
- /********************************************************************/
- BOOL CtheDBServer::CheckMaxUsers()
- {
- if (m_nConnectionCount > m_nMaxUsers)
- return TRUE;
- else
- return FALSE;
- }
- /********************************************************************/
- /* */
- /* Function name : OnTimer */
- /* Description : Update statictics. */
- /* */
- /********************************************************************/
- void CtheDBServer::OnTimer(UINT nIDEvent)
- {
- // update statictics ?
- if (nIDEvent == 1)
- {
- m_pEventSink->OnFTPSentBytesChange(m_dwTotalSentBytes);
- m_pEventSink->OnFTPReceivedBytesChange(m_dwTotalReceivedBytes);
- m_pEventSink->OnFTPStatisticChange(0, m_nTotalConnections);
- m_pEventSink->OnFTPStatisticChange(1, m_nConnectionCount);
- m_pEventSink->OnFTPStatisticChange(2, m_nFilesDownloaded);
- m_pEventSink->OnFTPStatisticChange(3, m_nFilesUploaded);
- m_pEventSink->OnFTPStatisticChange(4, m_nFailedDownloads);
- m_pEventSink->OnFTPStatisticChange(5, m_nFailedUploads);
- }
- CWnd::OnTimer(nIDEvent);
- }
- /********************************************************************/
- /* */
- /* Function name : SetSecurityMode */
- /* Description : Set security mode. */
- /* */
- /********************************************************************/
- void CtheDBServer::SetSecurityMode(BOOL bBlockSpecific)
- {
- m_nSecurityMode = bBlockSpecific ? 0 : 1;
- }
- int FindArray2(CStringArray *pArray, CString Str)
- {
- for(int i=0; i<pArray->GetSize (); i++)
- {
- if(pArray->ElementAt (i)==Str)
- return i;
- }
- return -1;
- }
- //extern CStringArray g_ipnoallowarray;
- /********************************************************************/
- /* */
- /* Function name : IsIPAddressAllowed */
- /* Description : Check (based on blockmode) if IP is allowed. */
- /* */
- /********************************************************************/
- BOOL CtheDBServer::IsIPAddressAllowed(LPCTSTR lpszIPAddress)
- {
- // MyLock lock("IsIPAddressAllowed");
- // if(::FindArray2 (&g_ipnoallowarray, lpszIPAddress)!=-1)return 0;
- return 1;
- /* if (m_nSecurityMode == 0)
- {
- return !m_SecurityManager.IsIPAddressBlocked(lpszIPAddress);
- }
- else
- {
- return m_SecurityManager.IsIPAddressNonBlocked(lpszIPAddress);
- }*/
- }
|