?? chatserverdoc.cpp
字號:
// ChatServerDoc.cpp : implementation of the CChatServerDoc class
//
#include "stdafx.h"
#include "ChatServer.h"
#include "ChatServerDoc.h"//添加的代碼
#include "ChatServerView.h"//添加的代碼
#include "Msg.h"//添加的代碼
#include "SetupDlg.h"//添加的代碼
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChatServerDoc
IMPLEMENT_DYNCREATE(CChatServerDoc, CDocument)
BEGIN_MESSAGE_MAP(CChatServerDoc, CDocument)
//{{AFX_MSG_MAP(CChatServerDoc)
// 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
ON_UPDATE_COMMAND_UI(ID_STATEMENT_NUM, OnUpdateMessages)//添加的代碼
ON_UPDATE_COMMAND_UI(ID_PEOPLE_NUM, OnUpdateConnections)//添加的代碼
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatServerDoc construction/destruction
CChatServerDoc::CChatServerDoc()
{
// TODO: add one-time construction code here
m_pSocket = NULL;
}
CChatServerDoc::~CChatServerDoc()
{
delete m_pSocket;
}
BOOL CChatServerDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
CSetupDlg Dialog;
if (Dialog.DoModal() == IDOK)
{
m_pSocket=new CListenSocket(this);
if (m_pSocket->Create(Dialog.m_nPort))
{
if (m_pSocket->Listen())
return TRUE;
}
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CChatServerDoc serialization
void CChatServerDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// CEditView contains an edit control which handles all serialization
((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
}
/////////////////////////////////////////////////////////////////////////////
// CChatServerDoc diagnostics
#ifdef _DEBUG
void CChatServerDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CChatServerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChatServerDoc commands
void CChatServerDoc::DeleteContents()
{
delete m_pSocket;
m_pSocket = NULL; //釋放監聽Socket
CString temp;
if (temp.LoadString(IDS_SERVERSHUTDOWN))
m_msgList.AddTail(temp); //顯示關閉了的信息于窗口中
while(!m_connectionList.IsEmpty())
{
CClientSocket* pSocket = (CClientSocket*)m_connectionList.RemoveHead();
CMsg* pMsg = AssembleMsg(pSocket); //釋放每一個客戶接收Socket
pMsg->m_bClose = TRUE; //同時發送給每一個客戶相應的信息
//可以發送信息給客戶,使其進行關閉操作
SendMsg(pSocket, pMsg);
if (!pSocket->IsAborted())
{
pSocket->ShutDown();
BYTE Buffer[50];
while (pSocket->Receive(Buffer,50) > 0);
delete pSocket;
}
}
m_msgList.RemoveAll();
if (!m_viewList.IsEmpty())
((CEditView*)m_viewList.GetHead())->SetWindowText(_T(""));//在窗口上清空
CDocument::DeleteContents();
}
void CChatServerDoc::UpdateClients()
{
for(POSITION pos = m_connectionList.GetHeadPosition(); pos != NULL;)
{
CClientSocket* pSocket = (CClientSocket*)m_connectionList.GetNext(pos);
CMsg* pMsg = AssembleMsg(pSocket);
if (pMsg != NULL)
SendMsg(pSocket, pMsg);
}
}
void CChatServerDoc::ProcessPendingAccept()
{
CClientSocket* pSocket = new CClientSocket(this);
if (m_pSocket->Accept(*pSocket))
{
pSocket->Init();
m_connectionList.AddTail(pSocket);
}
else
delete pSocket;
}
void CChatServerDoc::ProcessPendingRead(CClientSocket* pSocket)
{
do
{
CMsg* pMsg = ReadMsg(pSocket);
if (pMsg->m_bClose)
{
CloseSocket(pSocket);
break;
}
}
while (!pSocket->m_pArchiveIn->IsBufferEmpty());
UpdateClients();
}
CMsg* CChatServerDoc::AssembleMsg(CClientSocket* pSocket)//注意1:這個函數
{ //的作用是給每個客戶組裝信息結構
static CMsg msg; //從而為了下面的發送
msg.Init();
if (pSocket->m_nMsgCount >= m_msgList.GetCount())//接收前的信息總數應該小于接收
return NULL; //一個或幾個(幾個是由于有多個客戶在發送信息)
//新信息之后的信息總數
//在這里每個客戶接收Socket的m_nMsgCount
//都保存了一個時刻的服務器端的信息總數
for (POSITION pos1 = m_msgList.FindIndex(pSocket->m_nMsgCount); pos1 != NULL;)
{
CString temp = m_msgList.GetNext(pos1);
msg.m_msgList.AddTail(temp); //注意:需要理解的是放了哪些串在msg的里面了
} //:是比接收前多出的那幾個信息串
pSocket->m_nMsgCount = m_msgList.GetCount();
return &msg;
}
CMsg* CChatServerDoc::ReadMsg(CClientSocket* pSocket)
{
static CMsg msg;
TRY
{
pSocket->ReceiveMsg(&msg);//注意3:接收的是Msg結構,
//包括了它的三個成員變量
//而從客戶端的發送函數可以看出,最新的串沒有加入到信息List中。
Message(msg.m_strText);//所以在這里只是處理這個最新的串,
//方式可以從下面的語句看出:把它放到信息List的最后。
m_msgList.AddTail(msg.m_strText);//注意這個信息List是服務器端應用程序的
} //的信息List,它包括了所有客戶的信息
CATCH(CFileException, e)
{
CString strTemp;
if (strTemp.LoadString(IDS_READERROR))
Message(strTemp);
msg.m_bClose = TRUE;
pSocket->Abort();
}
END_CATCH
return &msg;
}
void CChatServerDoc::SendMsg(CClientSocket* pSocket, CMsg* pMsg)
{
TRY
{
pSocket->SendMsg(pMsg);
}
CATCH(CFileException, e)
{
pSocket->Abort();
CString strTemp;
if (strTemp.LoadString(IDS_SENDERROR))
Message(strTemp);
}
END_CATCH
}
void CChatServerDoc::CloseSocket(CClientSocket* pSocket)
{
pSocket->Close();
POSITION pos,temp;
for(pos = m_connectionList.GetHeadPosition(); pos != NULL;)
{
temp = pos;
CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
if (pSock == pSocket)
{
m_connectionList.RemoveAt(temp);
break;
}
}
delete pSocket;
}
void CChatServerDoc::Message(LPCTSTR lpszMessage)
{
((CChatServerView*)m_viewList.GetHead())->Message(lpszMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CServerDoc Handlers
void CChatServerDoc::OnUpdateMessages(CCmdUI* pCmdUI)//在狀態欄中顯示有多少的語句已經交流了
{
pCmdUI->Enable(TRUE);
CString strFmt;
if (strFmt.LoadString(ID_STATEMENT_NUM_FORMAT))
{
CString strTemp;
wsprintf(strTemp.GetBuffer(50),strFmt,m_msgList.GetCount());
strTemp.ReleaseBuffer();
pCmdUI->SetText(strTemp);
}
}
void CChatServerDoc::OnUpdateConnections(CCmdUI* pCmdUI)//在狀態欄中顯示有多少人在線
{
pCmdUI->Enable(TRUE);
CString strFmt;
if (strFmt.LoadString(ID_PEOPLE_NUM_FORMAT))
{
CString strTemp;
wsprintf(strTemp.GetBuffer(50),strFmt,m_connectionList.GetCount());
strTemp.ReleaseBuffer();
pCmdUI->SetText(strTemp);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -