123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- #include "stdafx.h"
- #include "LYFZReceiveMsgApp.h"
- #include "LYFZReceiveMsg.h"
- #include "ApplicationDlg.h"
- #include "ConnectThread.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CLYFZReceiveMsg *g_pWndServer;
- CStringArray g_conniparray;
- CStringArray g_ipnoallowarray;
- IMPLEMENT_DYNCREATE(CConnectThread, CWinThread)
- CConnectThread::CConnectThread()
- {
- m_nReceivedBytes = 0;
- m_nSentBytes = 0;
- m_nTimerID = 0;
- m_LastDataTransferTime = CTime::GetCurrentTime();
- }
- CConnectThread::~CConnectThread()
- {
- }
-
- extern BOOL AddConn(CDatabase *m_conndb);
- int FindArray(CStringArray *pArray, CString Str)
- {
- int count=0;
- for(int i=0; i<pArray->GetSize (); i++)
- {
- if(pArray->ElementAt (i)==Str)
- count++;
- }
- return count;
- }
- BOOL CConnectThread::InitInstance()
- {
- try
- {
- g_pWndServer->m_CriticalSection.Lock();
- g_pWndServer->m_ThreadList.AddTail(this);
- g_pWndServer->m_CriticalSection.Unlock();
- m_ConnectSocket.Attach(m_hSocket);
- m_ConnectSocket.m_pThread = this;
- CString strIPAddress;
- UINT nPort;
- m_ConnectSocket.GetPeerName(strIPAddress, nPort);
- m_strRemoteHost=strIPAddress;
- if(0)
- {
- int count=::FindArray(&g_conniparray, strIPAddress);
- if(count>20)
- {
- WriteLogin("连接超数ip:"+strIPAddress);
- PostThreadMessage(WM_QUIT,0,0);
- return 0;
- }
- g_conniparray.Add (strIPAddress);
- }
-
-
- g_pWndServer->SendMessage(WM_THREADSTART, (WPARAM)this, 0);
- if(AddConn(&m_conndb)==0)
- {
- WriteLogin("打开新数据库连接失败");
- PostThreadMessage(WM_QUIT,0,0);
- return 0;
- }
- m_conndb.SetQueryTimeout(5*60);
- m_ConnectSocket.m_pConndb=&m_conndb;
-
- if (g_pWndServer->CheckMaxUsers())
- {
-
- WriteLogin("连接超过最大许可");
- PostThreadMessage(WM_QUIT,0,0);
- }
- else
- if (!g_pWndServer->IsIPAddressAllowed(strIPAddress))
- {
-
- WriteLogin("非法IP地址");
- PostThreadMessage(WM_QUIT,0,0);
- }
- else
- {
-
-
-
- m_nTimerID = ::SetTimer(NULL, 0, 1000, TimerProc);
- }
- }
- catch(CException *e)
- {
- e->Delete();
- }
- return TRUE;
- }
-
- int CConnectThread::ExitInstance()
- {
- try
- {
- if(m_conndb.IsOpen())
- m_conndb.Close();
- g_pWndServer->m_CriticalSection.Lock();
-
- POSITION pos = g_pWndServer->m_ThreadList.Find(this);
- if(pos != NULL)
- {
- g_pWndServer->m_ThreadList.RemoveAt(pos);
- }
- g_pWndServer->m_CriticalSection.Unlock();
-
- g_pWndServer->SendMessage(WM_THREADCLOSE, (WPARAM)this, 0);
- }
- catch(CException *e)
- {
- g_pWndServer->m_CriticalSection.Unlock();
- e->Delete();
- }
- return CWinThread::ExitInstance();
- }
- BEGIN_MESSAGE_MAP(CConnectThread, CWinThread)
-
-
- #ifdef VC60
- ON_MESSAGE(WM_THREADMSG, OnThreadMessage)
- #else
- ON_THREAD_MESSAGE(WM_THREADMSG, OnThreadMessage)
- #endif
- END_MESSAGE_MAP()
-
- void CConnectThread::IncSentBytes(int nBytes)
- {
- m_LastDataTransferTime = CTime::GetCurrentTime();
- m_nSentBytes += nBytes;
-
- g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)0, (LPARAM)nBytes);
- }
-
- void CConnectThread::IncReceivedBytes(int nBytes)
- {
- m_LastDataTransferTime = CTime::GetCurrentTime();
- m_nReceivedBytes += nBytes;
-
- g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)1, (LPARAM)nBytes);
- }
-
- void CConnectThread::UpdateStatistic(int nType)
- {
-
- g_pWndServer->PostMessage(WM_THREADMSG, (WPARAM)2, (LPARAM)nType);
- }
-
- #ifdef VC60
- LRESULT CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
- {
- switch(wParam)
- {
- case 0:
-
- m_ConnectSocket.DestroyDataConnection();
- break;
- case 1:
- PostThreadMessage(WM_QUIT,0,0);
- break;
- default:
- break;
- }
- return 0L;
- }
- #else
- void CConnectThread::OnThreadMessage(WPARAM wParam, LPARAM lParam)
- {
- switch(wParam)
- {
- case 0:
-
- m_ConnectSocket.DestroyDataConnection();
- break;
- case 1:
- PostThreadMessage(WM_QUIT,0,0);
- break;
- default:
- break;
- }
-
- }
- #endif
-
- VOID CALLBACK CConnectThread::TimerProc(HWND hwnd, UINT uMsg, UINT uIDEvent, DWORD dwTime)
- {
- CConnectThread *pThread = (CConnectThread *)AfxGetThread();
- if (uIDEvent == pThread->m_nTimerID)
- {
- int nConnectionTimeout = g_pWndServer->GetTimeout();
-
- CTime time = pThread->m_LastDataTransferTime;
-
- time += CTimeSpan(0, 0, 0, 60);
- if (time < CTime::GetCurrentTime())
- {
-
- pThread->PostThreadMessage(WM_QUIT,0,0);
- }
- }
- }
|