home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / chatsrvr / srvrdoc.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  295 lines

  1. // srvrdoc.cpp : implementation of the CServerDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chatsrvr.h"
  15.  
  16. #include "srvrdoc.h"
  17. #include "srvrvw.h"
  18.  
  19. #include "msg.h"
  20. #include "dialogs.h"
  21.  
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CServerDoc
  29.  
  30. IMPLEMENT_DYNCREATE(CServerDoc, CDocument)
  31.  
  32. BEGIN_MESSAGE_MAP(CServerDoc, CDocument)
  33.     //{{AFX_MSG_MAP(CServerDoc)
  34.     //}}AFX_MSG_MAP
  35.     ON_UPDATE_COMMAND_UI(ID_MESSAGES, OnUpdateMessages)
  36.     ON_UPDATE_COMMAND_UI(ID_CONNECTIONS, OnUpdateConnections)
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CServerDoc construction/destruction
  41.  
  42. CServerDoc::CServerDoc()
  43. {
  44.     m_pSocket = NULL;
  45. }
  46.  
  47. CServerDoc::~CServerDoc()
  48. {
  49.     delete m_pSocket;
  50. }
  51.  
  52. BOOL CServerDoc::OnNewDocument()
  53. {
  54.     if (!CDocument::OnNewDocument())
  55.         return FALSE;
  56.  
  57.     CDiscussionDlg Dialog;
  58.  
  59.     if (Dialog.DoModal() == IDOK)
  60.     {
  61.         m_pSocket = new CListeningSocket(this);
  62.         if (m_pSocket->Create(Dialog.m_nPort+700))
  63.         {
  64.             if (m_pSocket->Listen())
  65.                 return TRUE;
  66.         }
  67.     }
  68.     return FALSE;
  69. }
  70.  
  71. void CServerDoc::DeleteContents()
  72. {
  73.     delete m_pSocket;
  74.     m_pSocket = NULL;
  75.  
  76.     CString temp;
  77.     if (temp.LoadString(IDS_SERVERSHUTDOWN))
  78.         m_msgList.AddTail(temp);
  79.  
  80.     while(!m_connectionList.IsEmpty())
  81.     {
  82.         CClientSocket* pSocket = (CClientSocket*)m_connectionList.RemoveHead();
  83.         CMsg* pMsg = AssembleMsg(pSocket);
  84.         pMsg->m_bClose = TRUE;
  85.  
  86.         SendMsg(pSocket, pMsg);
  87.  
  88.         if (!pSocket->IsAborted())
  89.         {
  90.             pSocket->ShutDown();
  91.  
  92.             BYTE Buffer[50];
  93.  
  94.             while (pSocket->Receive(Buffer,50) > 0);
  95.  
  96.             delete pSocket;
  97.         }
  98.     }
  99.  
  100.     m_msgList.RemoveAll();
  101.  
  102.     if (!m_viewList.IsEmpty())
  103.         ((CEditView*)m_viewList.GetHead())->SetWindowText(_T(""));
  104.  
  105.     CDocument::DeleteContents();
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CServerDoc serialization
  110.  
  111. void CServerDoc::Serialize(CArchive& ar)
  112. {
  113.     if (ar.IsStoring())
  114.     {
  115.         // CEditView contains an edit control which handles all serialization
  116.         ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
  117.     }
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CServerDoc diagnostics
  122.  
  123. #ifdef _DEBUG
  124. void CServerDoc::AssertValid() const
  125. {
  126.     CDocument::AssertValid();
  127. }
  128.  
  129. void CServerDoc::Dump(CDumpContext& dc) const
  130. {
  131.     CDocument::Dump(dc);
  132. }
  133. #endif //_DEBUG
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CServerDoc Operations
  137.  
  138. void CServerDoc::UpdateClients()
  139. {
  140.     for(POSITION pos = m_connectionList.GetHeadPosition(); pos != NULL;)
  141.     {
  142.         CClientSocket* pSocket = (CClientSocket*)m_connectionList.GetNext(pos);
  143.         CMsg* pMsg = AssembleMsg(pSocket);
  144.  
  145.         if (pMsg != NULL)
  146.             SendMsg(pSocket, pMsg);
  147.     }
  148. }
  149.  
  150. void CServerDoc::ProcessPendingAccept()
  151. {
  152.     CClientSocket* pSocket = new CClientSocket(this);
  153.  
  154.     if (m_pSocket->Accept(*pSocket))
  155.     {
  156.         pSocket->Init();
  157.         m_connectionList.AddTail(pSocket);
  158.     }
  159.     else
  160.         delete pSocket;
  161. }
  162.  
  163. void CServerDoc::ProcessPendingRead(CClientSocket* pSocket)
  164. {
  165.     do
  166.     {
  167.         CMsg* pMsg = ReadMsg(pSocket);
  168.  
  169.         if (pMsg->m_bClose)
  170.         {
  171.             CloseSocket(pSocket);
  172.             break;
  173.         }
  174.     }
  175.     while (!pSocket->m_pArchiveIn->IsBufferEmpty());
  176.  
  177.     UpdateClients();
  178. }
  179.  
  180. CMsg* CServerDoc::AssembleMsg(CClientSocket* pSocket)
  181. {
  182.     static CMsg msg;
  183.  
  184.     msg.Init();
  185.  
  186.     if (pSocket->m_nMsgCount >= m_msgList.GetCount())
  187.         return NULL;
  188.  
  189.     for (POSITION pos1 = m_msgList.FindIndex(pSocket->m_nMsgCount); pos1 != NULL;)
  190.     {
  191.         CString temp = m_msgList.GetNext(pos1);
  192.         msg.m_msgList.AddTail(temp);
  193.     }
  194.     pSocket->m_nMsgCount = m_msgList.GetCount();
  195.     return &msg;
  196. }
  197.  
  198. CMsg* CServerDoc::ReadMsg(CClientSocket* pSocket)
  199. {
  200.     static CMsg msg;
  201.  
  202.     TRY
  203.     {
  204.         pSocket->ReceiveMsg(&msg);
  205.  
  206.         Message(msg.m_strText);
  207.  
  208.         m_msgList.AddTail(msg.m_strText);
  209.     }
  210.     CATCH(CFileException, e)
  211.     {
  212.         CString strTemp;
  213.         if (strTemp.LoadString(IDS_READERROR))
  214.             Message(strTemp);
  215.  
  216.         msg.m_bClose = TRUE;
  217.         pSocket->Abort();
  218.     }
  219.     END_CATCH
  220.  
  221.     return &msg;
  222. }
  223.  
  224. void CServerDoc::SendMsg(CClientSocket* pSocket, CMsg* pMsg)
  225. {
  226.     TRY
  227.     {
  228.         pSocket->SendMsg(pMsg);
  229.     }
  230.     CATCH(CFileException, e)
  231.     {
  232.         pSocket->Abort();
  233.  
  234.         CString strTemp;
  235.         if (strTemp.LoadString(IDS_SENDERROR))
  236.             Message(strTemp);
  237.     }
  238.     END_CATCH
  239. }
  240.  
  241. void CServerDoc::CloseSocket(CClientSocket* pSocket)
  242. {
  243.     pSocket->Close();
  244.  
  245.     POSITION pos,temp;
  246.     for(pos = m_connectionList.GetHeadPosition(); pos != NULL;)
  247.     {
  248.         temp = pos;
  249.         CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
  250.         if (pSock == pSocket)
  251.         {
  252.             m_connectionList.RemoveAt(temp);
  253.             break;
  254.         }
  255.     }
  256.  
  257.     delete pSocket;
  258. }
  259.  
  260. void CServerDoc::Message(LPCTSTR lpszMessage)
  261. {
  262.     ((CServerView*)m_viewList.GetHead())->Message(lpszMessage);
  263. }
  264.  
  265. /////////////////////////////////////////////////////////////////////////////
  266. // CServerDoc Handlers
  267.  
  268. void CServerDoc::OnUpdateMessages(CCmdUI* pCmdUI)
  269. {
  270.     pCmdUI->Enable(TRUE);
  271.  
  272.     CString strFmt;
  273.     if (strFmt.LoadString(IDS_MESSAGESFMT))
  274.     {
  275.         CString strTemp;
  276.         wsprintf(strTemp.GetBuffer(50),strFmt,m_msgList.GetCount());
  277.         strTemp.ReleaseBuffer();
  278.         pCmdUI->SetText(strTemp);
  279.     }
  280. }
  281.  
  282. void CServerDoc::OnUpdateConnections(CCmdUI* pCmdUI)
  283. {
  284.     pCmdUI->Enable(TRUE);
  285.  
  286.     CString strFmt;
  287.     if (strFmt.LoadString(IDS_CONNECTIONSFMT))
  288.     {
  289.         CString strTemp;
  290.         wsprintf(strTemp.GetBuffer(50),strFmt,m_connectionList.GetCount());
  291.         strTemp.ReleaseBuffer();
  292.         pCmdUI->SetText(strTemp);
  293.     }
  294. }
  295.