MainFrm.cpp
上传用户:hysujiao87
上传日期:2007-12-02
资源大小:156k
文件大小:6k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "QQServer.h"
- #include "MainFrm.h"
- #include "QQServerDoc.h"
- #include "QQServerView.h"
- #include "UserListDlg.h"
- #include <shlwapi.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CEvent g_eventTimer;
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_NET_START, OnNetStart)
- ON_UPDATE_COMMAND_UI(ID_NET_START, OnUpdateNetStart)
- ON_COMMAND(ID_NET_FINISH, OnNetFinish)
- ON_UPDATE_COMMAND_UI(ID_NET_FINISH, OnUpdateNetFinish)
- ON_WM_CLOSE()
- ON_WM_TIMER()
- ON_MESSAGE(WM_SERVICE_NOTIFY, OnServiceNotify)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_VIEW_USER, OnViewUser)
- ON_WM_DESTROY()
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_MOUSE_POSITION,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_FLOATING, CRect(0, 0, 0, 0)) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this, WS_CHILD | WS_VISIBLE |
- CBRS_BOTTOM) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- void CMainFrame::OnNetStart()
- {
- // TODO: Add your command handler code here
- GetActiveDocument()->DeleteContents();
- try{
- CFile loadFile(getDataPath(), CFile::modeRead | CFile::typeBinary);
- CArchive ar(&loadFile, CArchive::load);
- GetActiveDocument()->Serialize(ar);
- ar.Close();
- loadFile.Close();
- }
- catch(CArchiveException *ae)
- {
- ae->ReportError();
- ae->Delete();
- return ;
- }
- catch(CFileException *fe)
- {
- if (fe->m_cause != CFileException::fileNotFound)
- {
- fe->ReportError();
- fe->Delete();
- return ;
- }
- fe->Delete();
- }
- CQQServerDoc *doc = (CQQServerDoc*)GetActiveDocument();
- _service.startListen(&doc->_userList);
- SetTimer(100, 500, NULL);
- showMessage("QQ server is running.");
- }
- void CMainFrame::showMessage(LPCTSTR msg)
- {
- CQQServerView *view = (CQQServerView*)GetActiveView();
- view->showMessage(msg);
- }
- void CMainFrame::OnUpdateNetStart(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- if (_service.isListening() == FALSE)
- pCmdUI->Enable(TRUE);
- else
- pCmdUI->Enable(FALSE);
- }
- void CMainFrame::OnNetFinish()
- {
- // TODO: Add your command handler code here
- if (_service.isListening() == FALSE)
- return ;
- KillTimer(100);
- _service.stopListen();
- CQQServerDoc* pDoc = (CQQServerDoc*)GetActiveDocument();
- for(INT_PTR i = 0; i < pDoc->_userList.getCount(); i++)
- {
- CUserData* userData = pDoc->_userList.getUserByIndex(i);
- userData->_online = FALSE;
- }
- try{
- CFile saveFile(getDataPath(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
- CArchive ar(&saveFile, CArchive::store);
- GetActiveDocument()->Serialize(ar);
- ar.Close();
- saveFile.Close();
- }
- catch(CArchiveException *ae)
- {
- ae->ReportError();
- ae->Delete();
- }
- catch(CFileException *fe)
- {
- fe->ReportError();
- fe->Delete();
- }
- showMessage("QQ server is finished.");
- }
- void CMainFrame::OnUpdateNetFinish(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- if( _service.isListening() != TRUE)
- pCmdUI->Enable(FALSE);
- else
- pCmdUI->Enable(TRUE);
- }
- CString CMainFrame::getDataPath()
- {
- TCHAR savePath[_MAX_PATH + 5];
- ::GetModuleFileName(AfxGetInstanceHandle(), savePath, _MAX_PATH + 5);
- ::PathRemoveFileSpec(savePath);
- _tcscat(savePath, _T("\userdata.dat"));
- return CString(savePath);
- }
- void CMainFrame::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- CFrameWnd::OnClose();
- }
- void CMainFrame::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- switch(nIDEvent)
- {
- case 100:
- {
- CQQServerDoc* pDoc = (CQQServerDoc*)GetActiveDocument();
- for(INT_PTR i = 0; i < pDoc->_userList.getCount(); i++)
- {
- CUserData *userData = pDoc->_userList.getUserByIndex(i);
- _ASSERTE(userData != NULL);
- if (userData == NULL)
- return ;
- if (userData->_online == TRUE)
- {
- if( (GetTickCount() - userData->_lastReport) >= 10000 )
- {
- userData->_online = FALSE;
- userData->_lastReport = 0;
- ::ZeroMemory(&userData->_sockAddr, sizeof(sockaddr_in));
- }
- }
- }
- break;
- }
- }
- CFrameWnd::OnTimer(nIDEvent);
- }
- LRESULT CMainFrame::OnServiceNotify(WPARAM wParam, LPARAM lParam)
- {
- TCHAR *message = (TCHAR*)wParam;
- _ASSERTE(message != NULL);
- if (message == NULL)
- return 1;
- showMessage(message);
- delete[] message;
- return 0;
- }
- void CMainFrame::OnViewUser()
- {
- // TODO: Add your command handler code here
- CUserListDlg dlg;
- CQQServerDoc *doc = (CQQServerDoc*)GetActiveDocument();
- dlg.setUserList(&doc->_userList);
- dlg.DoModal();
- }
- void CMainFrame::OnDestroy()
- {
- CFrameWnd::OnDestroy();
- // TODO: Add your message handler code here
- OnNetFinish();
- }