home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / chatter / chatdoc.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  6.2 KB  |  315 lines

  1. // chatdoc.cpp : implementation of the CChatDoc 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 "chatter.h"
  15.  
  16. #include "chatsock.h"
  17. #include "chatdoc.h"
  18. #include "chatvw.h"
  19. #include "setupdlg.h"
  20. #include "msg.h"
  21.  
  22. #ifdef _WIN32
  23. #ifndef _UNICODE
  24. #include <strstrea.h>
  25. #endif
  26. #endif
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CChatDoc
  35.  
  36. IMPLEMENT_DYNCREATE(CChatDoc, CDocument)
  37.  
  38. BEGIN_MESSAGE_MAP(CChatDoc, CDocument)
  39.     //{{AFX_MSG_MAP(CChatDoc)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CChatDoc construction/destruction
  45.  
  46. CChatDoc::CChatDoc()
  47. {
  48.     m_bAutoChat = FALSE;
  49.     m_pSocket = NULL;
  50.     m_pFile = NULL;
  51.     m_pArchiveIn = NULL;
  52.     m_pArchiveOut = NULL;
  53. }
  54.  
  55. CChatDoc::~CChatDoc()
  56. {
  57. }
  58.  
  59. BOOL CChatDoc::OnNewDocument()
  60. {
  61.     if (!CDocument::OnNewDocument())
  62.         return FALSE;
  63.  
  64. #ifdef _WIN32
  65. #ifndef _UNICODE
  66.     if (AfxGetApp()->m_lpCmdLine[0] != '\0')
  67.     {
  68.         TCHAR strHandle[128];
  69.         TCHAR strServer[128];
  70.         int nChannel;
  71.  
  72.         istrstream(AfxGetApp()->m_lpCmdLine) >> strHandle >> strServer >> nChannel;
  73.         return ConnectSocket(strHandle, strServer, nChannel);
  74.     }
  75.     else
  76. #endif
  77. #endif
  78.     {
  79.         CSetupDlg Dialog;
  80.  
  81.         Dialog.m_strHandle=m_strHandle;
  82.         Dialog.m_strServer=_T("");
  83.         Dialog.m_nChannel=0;
  84.  
  85.         while(TRUE)
  86.         {
  87.             if (IDOK != Dialog.DoModal())
  88.                 return FALSE;
  89.  
  90.             if (ConnectSocket(Dialog.m_strHandle, Dialog.m_strServer, Dialog.m_nChannel))
  91.                 return TRUE;
  92.  
  93.             if (AfxMessageBox(IDS_CHANGEADDRESS,MB_YESNO) == IDNO)
  94.                 return FALSE;
  95.         }
  96.     }
  97. }
  98.  
  99. void CChatDoc::DeleteContents()
  100. {
  101.     m_bAutoChat = FALSE;
  102.  
  103.     if ((m_pSocket != NULL) && (m_pFile != NULL) && (m_pArchiveOut != NULL))
  104.     {
  105.         CMsg msg;
  106.         CString strTemp;
  107.  
  108.         if (strTemp.LoadString(IDS_DISCONNECT))
  109.         {
  110.             msg.m_bClose = TRUE;
  111.             msg.m_strText = m_strHandle + strTemp;
  112.             msg.Serialize(*m_pArchiveOut);
  113.             m_pArchiveOut->Flush();
  114.         }
  115.     }
  116.  
  117.     delete m_pArchiveOut;
  118.     m_pArchiveOut = NULL;
  119.     delete m_pArchiveIn;
  120.     m_pArchiveIn = NULL;
  121.     delete m_pFile;
  122.     m_pFile = NULL;
  123.  
  124.     if (m_pSocket != NULL)
  125.     {
  126.         BYTE Buffer[50];
  127.         m_pSocket->ShutDown();
  128.  
  129.         while(m_pSocket->Receive(Buffer,50) > 0);
  130.     }
  131.  
  132.     delete m_pSocket;
  133.     m_pSocket = NULL;
  134.  
  135.     for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  136.     {
  137.         CView* pView = GetNextView(pos);
  138.  
  139.         if (pView->IsKindOf(RUNTIME_CLASS(CChatView)))
  140.         {
  141.             CChatView* pChatView = (CChatView*)pView;
  142.             pChatView->GetEditCtrl().SetWindowText(_T(""));
  143.         }
  144.     }
  145.     CDocument::DeleteContents();
  146. }
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CChatDoc Operations
  150.  
  151. BOOL CChatDoc::ConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, UINT nPort)
  152. {
  153.     m_strHandle = lpszHandle;
  154.  
  155.     m_pSocket = new CChatSocket(this);
  156.  
  157.     if (!m_pSocket->Create())
  158.     {
  159.         delete m_pSocket;
  160.         m_pSocket = NULL;
  161.         AfxMessageBox(IDS_CREATEFAILED);
  162.         return FALSE;
  163.     }
  164.  
  165.     while (!m_pSocket->Connect(lpszAddress, nPort + 700))
  166.     {
  167.         if (AfxMessageBox(IDS_RETRYCONNECT,MB_YESNO) == IDNO)
  168.         {
  169.             delete m_pSocket;
  170.             m_pSocket = NULL;
  171.             return FALSE;
  172.         }
  173.     }
  174.  
  175.     m_pFile = new CSocketFile(m_pSocket);
  176.     m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
  177.     m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
  178.  
  179.     CString strTemp;
  180.     if (strTemp.LoadString(IDS_CONNECT))
  181.         SendMsg(strTemp);
  182.  
  183.     return TRUE;
  184. }
  185.  
  186. void CChatDoc::ProcessPendingRead()
  187. {
  188.     do
  189.     {
  190.         ReceiveMsg();
  191.         if (m_pSocket == NULL)
  192.             return;
  193.     }
  194.     while(!m_pArchiveIn->IsBufferEmpty());
  195. }
  196.  
  197. void CChatDoc::SendMsg(CString& strText)
  198. {
  199.     if (m_pArchiveOut != NULL)
  200.     {
  201.         CMsg msg;
  202.  
  203.         msg.m_strText = m_strHandle + _T(": ") + strText;
  204.  
  205.         TRY
  206.         {
  207.             msg.Serialize(*m_pArchiveOut);
  208.             m_pArchiveOut->Flush();
  209.         }
  210.         CATCH(CFileException, e)
  211.         {
  212.             m_bAutoChat = FALSE;
  213.             m_pArchiveOut->Abort();
  214.             delete m_pArchiveOut;
  215.             m_pArchiveOut = NULL;
  216.  
  217.             CString strTemp;
  218.             if (strTemp.LoadString(IDS_SERVERRESET))
  219.                 DisplayMsg(strTemp);
  220.         }
  221.         END_CATCH
  222.     }
  223. }
  224.  
  225. void CChatDoc::ReceiveMsg()
  226. {
  227.     CMsg msg;
  228.  
  229.     TRY
  230.     {
  231.         msg.Serialize(*m_pArchiveIn);
  232.  
  233.         while(!msg.m_msgList.IsEmpty())
  234.         {
  235.             CString temp = msg.m_msgList.RemoveHead();
  236.             DisplayMsg(temp);
  237.         }
  238.  
  239.     }
  240.     CATCH(CFileException, e)
  241.     {
  242.         m_bAutoChat = FALSE;
  243.         msg.m_bClose = TRUE;
  244.         m_pArchiveOut->Abort();
  245.  
  246.         CString strTemp;
  247.         if (strTemp.LoadString(IDS_SERVERRESET))
  248.             DisplayMsg(strTemp);
  249.         if (strTemp.LoadString(IDS_CONNECTIONCLOSED))
  250.             DisplayMsg(strTemp);
  251.     }
  252.     END_CATCH
  253.  
  254.     if (msg.m_bClose)
  255.     {
  256.         delete m_pArchiveIn;
  257.         m_pArchiveIn = NULL;
  258.         delete m_pArchiveOut;
  259.         m_pArchiveOut = NULL;
  260.         delete m_pFile;
  261.         m_pFile = NULL;
  262.         delete m_pSocket;
  263.         m_pSocket = NULL;
  264.     }
  265. }
  266.  
  267. void CChatDoc::DisplayMsg(LPCTSTR lpszText)
  268. {
  269.  
  270.     for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  271.     {
  272.         CView* pView = GetNextView(pos);
  273.         CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
  274.  
  275.         if (pChatView != NULL)
  276.             pChatView->Message(lpszText);
  277.     }
  278. }
  279.  
  280. /////////////////////////////////////////////////////////////////////////////
  281. // CChatDoc serialization
  282.  
  283. void CChatDoc::Serialize(CArchive& ar)
  284. {
  285.     if (ar.IsStoring())
  286.     {
  287.         for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  288.         {
  289.             CView* pView = GetNextView(pos);
  290.             CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
  291.  
  292.             if (pChatView != NULL)
  293.                 pChatView->SerializeRaw(ar);
  294.         }
  295.     }
  296. }
  297.  
  298. /////////////////////////////////////////////////////////////////////////////
  299. // CChatDoc diagnostics
  300.  
  301. #ifdef _DEBUG
  302. void CChatDoc::AssertValid() const
  303. {
  304.     CDocument::AssertValid();
  305. }
  306.  
  307. void CChatDoc::Dump(CDumpContext& dc) const
  308. {
  309.     CDocument::Dump(dc);
  310. }
  311. #endif //_DEBUG
  312.  
  313. /////////////////////////////////////////////////////////////////////////////
  314. // CChatDoc commands
  315.