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 / clntsock.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  2KB  |  102 lines

  1. // clntsock.cpp : implementation of the CClientSocket 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 "clntsock.h"
  15. #include "srvrdoc.h"
  16. #include "msg.h"
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CClientSocket Construction
  20.  
  21. CClientSocket::CClientSocket(CServerDoc* pDoc)
  22. {
  23.     m_pDoc = pDoc;
  24.     m_nMsgCount = 0;
  25.     m_pFile = NULL;
  26.     m_pArchiveIn = NULL;
  27.     m_pArchiveOut = NULL;
  28. }
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CClientSocket Operations
  32.  
  33. void CClientSocket::Init()
  34. {
  35.     m_pFile = new CSocketFile(this);
  36.     m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
  37.     m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
  38. }
  39.  
  40. void CClientSocket::Abort()
  41. {
  42.     if (m_pArchiveOut != NULL)
  43.     {
  44.         m_pArchiveOut->Abort();
  45.         delete m_pArchiveOut;
  46.         m_pArchiveOut = NULL;
  47.     }
  48. }
  49.  
  50. void CClientSocket::SendMsg(CMsg* pMsg)
  51. {
  52.     if (m_pArchiveOut != NULL)
  53.     {
  54.         pMsg->Serialize(*m_pArchiveOut);
  55.         m_pArchiveOut->Flush();
  56.     }
  57. }
  58.  
  59. void CClientSocket::ReceiveMsg(CMsg* pMsg)
  60. {
  61.     pMsg->Serialize(*m_pArchiveIn);
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CClientSocket Overridable callbacks
  66.  
  67. void CClientSocket::OnReceive(int nErrorCode)
  68. {
  69.     CSocket::OnReceive(nErrorCode);
  70.  
  71.     m_pDoc->ProcessPendingRead(this);
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CSocket Implementation
  76.  
  77. CClientSocket::~CClientSocket()
  78. {
  79.     if (m_pArchiveOut != NULL)
  80.         delete m_pArchiveOut;
  81.  
  82.     if (m_pArchiveIn != NULL)
  83.         delete m_pArchiveIn;
  84.  
  85.     if (m_pFile != NULL)
  86.         delete m_pFile;
  87. }
  88.  
  89. #ifdef _DEBUG
  90. void CClientSocket::AssertValid() const
  91. {
  92.     CSocket::AssertValid();
  93. }
  94.  
  95. void CClientSocket::Dump(CDumpContext& dc) const
  96. {
  97.     CSocket::Dump(dc);
  98. }
  99. #endif //_DEBUG
  100.  
  101. IMPLEMENT_DYNAMIC(CClientSocket, CSocket)
  102.