AnyServerDoc.cpp
资源名称:(vc).rar [点击查看]
上传用户:wpp2016
上传日期:2010-02-01
资源大小:1250k
文件大小:5k
源码类别:
Telnet服务器
开发平台:
Visual C++
- // AnyServerDoc.cpp : implementation of the CAnyServerDoc class
- //
- #include "stdafx.h"
- #include "AnyServer.h"
- #include "ChatSock.h"
- #include "ChatLsnSock.h"
- #include "AnyServerDoc.h"
- #include "ChatClientView.h"
- #include "ChatView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerDoc
- IMPLEMENT_DYNCREATE(CAnyServerDoc, CDocument)
- BEGIN_MESSAGE_MAP(CAnyServerDoc, CDocument)
- //{{AFX_MSG_MAP(CAnyServerDoc)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerDoc construction/destruction
- CAnyServerDoc::CAnyServerDoc()
- {
- // TODO: add one-time construction code here
- g_pchatListen=NULL;
- m_pConnect=NULL;
- m_Client.Empty();
- m_Server.Empty();
- }
- CAnyServerDoc::~CAnyServerDoc()
- {
- }
- BOOL CAnyServerDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- SetTitle("万能服务");
- POSITION pos = GetFirstViewPosition();
- while (pos != NULL)
- {
- CView* pView = GetNextView(pos);
- if (pView->IsKindOf(RUNTIME_CLASS(CEditView)))
- {
- pView->EnableWindow(FALSE);
- }
- }
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerDoc serialization
- void CAnyServerDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- }
- else
- {
- // TODO: add loading code here
- }
- POSITION pos = GetFirstViewPosition();
- while (pos != NULL)
- {
- CView* pView = GetNextView(pos);
- if (pView->IsKindOf(RUNTIME_CLASS(CEditView)))
- ((CEditView*)pView)->SerializeRaw(ar);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerDoc diagnostics
- #ifdef _DEBUG
- void CAnyServerDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CAnyServerDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerDoc commands
- BOOL CAnyServerDoc::SaveModified()
- {
- // TODO: Add your specialized code here and/or call the base class
- BOOL bModified=IsModified();
- if (bModified)
- {
- if (AfxMessageBox("保存对话纪录?",MB_YESNO)==IDYES)
- {
- CFileDialog DlgSaveFile(FALSE,"TXT",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,".TXT");
- if (DlgSaveFile.DoModal()==IDOK)
- {
- CString szFileName=DlgSaveFile.GetPathName();
- try
- {
- CFile SaveMsg;
- CFileException*pError=new CFileException;
- SaveMsg.Open(szFileName,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate,pError);
- CArchive ar(&SaveMsg,CArchive::store);
- Serialize(ar);
- ar.Close();
- SaveMsg.Close();
- }
- catch(CFileException* pError)
- {
- AfxMessageBox("File Operation Error!");
- pError->Delete();
- return 0;
- }
- }
- SetModifiedFlag(FALSE);
- }
- }
- return TRUE;
- }
- void CAnyServerDoc::ProcessPendingAccept()
- {
- m_pConnect=new CChatSock(this);
- if (!(g_pchatListen->Accept(*m_pConnect)))
- {
- AfxMessageBox("Chat Accept Error!");
- delete m_pConnect;
- m_pConnect=NULL;
- }
- else
- {
- m_pConnect->init();
- }
- POSITION pos = GetFirstViewPosition();
- while (pos != NULL)
- {
- CView* pView = GetNextView(pos);
- if (pView->IsKindOf(RUNTIME_CLASS(CFormView)))
- {
- pView->SendMessage(WM_VALID,(WPARAM)TRUE,0);
- }
- if (pView->IsKindOf(RUNTIME_CLASS(CEditView)))
- {
- pView->EnableWindow(TRUE);
- if (pView->IsKindOf(RUNTIME_CLASS(CChatClientView)))
- pView->SetFocus();
- }
- }
- }
- void CAnyServerDoc::ProcessPendingRead(CChatSock *pSocket)
- {
- TRY
- {
- *(pSocket->m_pArchiveIn)>>m_Server;
- while (!pSocket->m_pArchiveIn->IsBufferEmpty());
- }
- CATCH(CFileException, e)
- {
- pSocket->m_pArchiveIn->Abort();
- AfxMessageBox("Error! Read Message!");
- }
- END_CATCH
- POSITION pos = GetFirstViewPosition();
- while (pos != NULL)
- {
- CView* pView = GetNextView(pos);
- if (pView->IsKindOf(RUNTIME_CLASS(CChatView)))
- {
- CChatView* pChatView=(CChatView*)pView;
- pChatView->GetEditCtrl().SetWindowText(m_Server);
- }
- if (pView->IsKindOf(RUNTIME_CLASS(CChatClientView)))
- {
- CChatClientView* pChatClientView=(CChatClientView*)pView;
- pChatClientView->SetFocus();
- }
- }
- }