📄 chatterdoc.cpp
字号:
// ChatterDoc.cpp : CChatterDoc 类的实现
//
#include "stdafx.h"
#include "Chatter.h"
#include "ChatterDoc.h"
#include "ChatSocket.h"
#include "ChatterView.h"
#include "ConnectDialog.h"
#include "Msg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChatterDoc
IMPLEMENT_DYNCREATE(CChatterDoc, CDocument)
BEGIN_MESSAGE_MAP(CChatterDoc, CDocument)
END_MESSAGE_MAP()
// CChatterDoc 构造/销毁
CChatterDoc::CChatterDoc()
{
// TODO:在此添加一次性构造代码
m_bAutoChat = FALSE;
m_pSocket = NULL;
m_pFile = NULL;
m_pArchiveIn = NULL;
m_pArchiveOut = NULL;
}
CChatterDoc::~CChatterDoc()
{
}
BOOL CChatterDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
CConnectDialog dlg;
dlg.m_ClientName=m_ClientName;
dlg.m_ServerIP=_T("");
dlg.m_nPort=0;
while(TRUE)
{
if (IDOK != dlg.DoModal())
return FALSE;
if (ConnectSocket(dlg.m_ClientName, dlg.m_ServerIP, dlg.m_nPort))
return TRUE;
if (AfxMessageBox(IDS_CHANGEADDRESS,MB_YESNO) == IDNO)
return FALSE;
}
}
// CChatterDoc 序列化
void CChatterDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
CChatterView* pChatView = DYNAMIC_DOWNCAST(CChatterView, pView);
if (pChatView != NULL)
pChatView->SerializeRaw(ar);
}
}
}
// CChatterDoc 诊断
#ifdef _DEBUG
void CChatterDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CChatterDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
// CChatterDoc 命令
void CChatterDoc::DeleteContents()
{
// TODO: 在此添加专用代码和/或调用基类
m_bAutoChat = FALSE;
if ((m_pSocket != NULL) && (m_pFile != NULL) && (m_pArchiveOut != NULL))
{
CMsg msg;
CString strTemp;
if (strTemp.LoadString(IDS_DISCONNECT))
{
msg.m_bClose = TRUE;
msg.m_strText = m_ClientName + strTemp;
msg.Serialize(*m_pArchiveOut);
m_pArchiveOut->Flush();
}
}
delete m_pArchiveOut;
m_pArchiveOut = NULL;
delete m_pArchiveIn;
m_pArchiveIn = NULL;
delete m_pFile;
m_pFile = NULL;
if (m_pSocket != NULL)
{
BYTE Buffer[50];
m_pSocket->ShutDown();
while(m_pSocket->Receive(Buffer,50) > 0);
}
delete m_pSocket;
m_pSocket = NULL;
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
if (pView->IsKindOf(RUNTIME_CLASS(CChatterView)))
{
CChatterView* pChatView = (CChatterView*)pView;
pChatView->GetEditCtrl().SetWindowText(_T(""));
}
}
CDocument::DeleteContents();
}
BOOL CChatterDoc::ConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, UINT nPort)
{
m_ClientName = lpszHandle;
m_pSocket = new CChatSocket(this);
if (!m_pSocket->Create())
{
delete m_pSocket;
m_pSocket = NULL;
AfxMessageBox(IDS_CREATEFAILED);
return FALSE;
}
while (!m_pSocket->Connect(lpszAddress, nPort + 700))
{
if (AfxMessageBox(IDS_RETRYCONNECT,MB_YESNO) == IDNO)
{
delete m_pSocket;
m_pSocket = NULL;
return FALSE;
}
}
m_pFile = new CSocketFile(m_pSocket);
m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
CString strTemp;
if (strTemp.LoadString(IDS_CONNECT))
SendMsg(strTemp);
return TRUE;
}
void CChatterDoc::ProcessPendingRead()
{
do
{
ReceiveMsg();
if (m_pSocket == NULL)
return;
}
while(!m_pArchiveIn->IsBufferEmpty());
}
void CChatterDoc::SendMsg(CString& strText)
{
if (m_pArchiveOut != NULL)
{
CMsg msg;
msg.m_strText = m_ClientName + _T(": ") + strText;
try
{
msg.Serialize(*m_pArchiveOut);
m_pArchiveOut->Flush();
}
catch(CFileException e)
{
m_bAutoChat = FALSE;
m_pArchiveOut->Abort();
delete m_pArchiveOut;
m_pArchiveOut = NULL;
CString strTemp;
if (strTemp.LoadString(IDS_SERVERRESET))
DisplayMsg(strTemp);
}
}
}
void CChatterDoc::ReceiveMsg()
{
CMsg msg;
try
{
msg.Serialize(*m_pArchiveIn);
while(!msg.m_msgList.IsEmpty())
{
CString temp = msg.m_msgList.RemoveHead();
DisplayMsg(temp);
}
}
catch(CFileException e)
{
m_bAutoChat = FALSE;
msg.m_bClose = TRUE;
m_pArchiveOut->Abort();
CString strTemp;
if (strTemp.LoadString(IDS_SERVERRESET))
DisplayMsg(strTemp);
if (strTemp.LoadString(IDS_CONNECTIONCLOSED))
DisplayMsg(strTemp);
}
if (msg.m_bClose)
{
delete m_pArchiveIn;
m_pArchiveIn = NULL;
delete m_pArchiveOut;
m_pArchiveOut = NULL;
delete m_pFile;
m_pFile = NULL;
delete m_pSocket;
m_pSocket = NULL;
}
}
void CChatterDoc::DisplayMsg(LPCTSTR lpszText)
{
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
{
CView* pView = GetNextView(pos);
CChatterView* pChatView = DYNAMIC_DOWNCAST(CChatterView, pView);
if (pChatView != NULL)
pChatView->Message(lpszText);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -