CCHelper.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:4k
源码类别:

网格计算

开发平台:

Visual C++

  1. // CCHelper.cpp: implementation of the CCCHelper class.
  2. #include "stdafx.h"
  3. #include "Mainfrm.h"
  4. #include "trfAgent.h"
  5. #include "CCHelper.h"
  6. #include "register.h"
  7. //////////////////////////////////////////////////////////////////////
  8. CCCHelper::CCCHelper()
  9. {
  10. this->m_pchatSvrThread = NULL;
  11. m_pfileSenderSvr = NULL;
  12. m_pfileRecvorSvr = NULL;
  13. }
  14. CCCHelper::~CCCHelper()
  15. {
  16. this->Stop();
  17. }
  18. //start the cc helper functions.
  19. bool CCCHelper::Start(void)
  20. {
  21. return this->Start_FileTransSvr() &&
  22. this->Start_P2PChatSvr();
  23. }
  24. //stop the cc helper functions.
  25. void CCCHelper::Stop(void)
  26. {
  27. this->Stop_P2PChatSvr();
  28. this->Stop_FileTransSvr();
  29. }
  30. //start function of file sending server.
  31. bool CCCHelper::Start_FileSendSvr(void)
  32. {
  33. m_pfileSenderSvr = new CFileSenderThread();
  34. return true;
  35. }
  36. //stop function of file sending server.
  37. void CCCHelper::Stop_FileSendSvr(void)
  38. {
  39. if(NULL != this->m_pfileSenderSvr)
  40. {
  41. this->m_pfileSenderSvr->Stop();
  42. this->m_pfileSenderSvr->Destroy();
  43. _DELETE(this->m_pfileSenderSvr);
  44. }
  45. }
  46. //start function of file transfering.
  47. bool CCCHelper::Start_FileTransSvr(void)
  48. {
  49. //open the register for reading service's parameters.
  50. CRegister regkey;
  51. if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY))
  52. {
  53. return false;
  54. }
  55. CString savedir = regkey.Read_String_Value(REG_SRECVFLESDIR);
  56. DWORD dwport = regkey.Read_DWord_Value(REG_NTRFTRANSPORT);
  57. regkey.Close();
  58. this->Stop_FileTransSvr();
  59. this->m_pfileRecvorSvr = new CFileReceiverThread();
  60. this->m_pfileRecvorSvr->SetRecedFileSaveDir(savedir.GetBuffer(0));
  61. assert(NULL != theApp.GetMainWnd());
  62. HWND hwnd = theApp.GetMainWnd()->GetSafeHwnd();
  63. return this->m_pfileRecvorSvr->Create(false, dwport, hwnd);
  64. }
  65. //stop function of file transfering.
  66. void CCCHelper::Stop_FileTransSvr(void)
  67. {
  68. if(NULL != this->m_pfileRecvorSvr)
  69. {
  70. this->m_pfileRecvorSvr->Destroy();
  71. _DELETE(this->m_pfileRecvorSvr);
  72. }
  73. }
  74. //start function of peer to peer chating.
  75. bool CCCHelper::Start_P2PChatSvr(void)
  76. {
  77. CRegister regkey;
  78. DWORD dwUDPort,
  79. dwHeadportrait;
  80. //read the UDP server listening port.
  81. if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY)) return false;
  82. dwUDPort = regkey.Read_DWord_Value(REG_NCHATUDPPORT);
  83. //determine the UDP port whether is valid.
  84. if(dwUDPort < 1000 || dwUDPort > 65535) dwUDPort = 5558;
  85. dwHeadportrait = regkey.Read_DWord_Value(REG_NHEADBMPINDEX);
  86. regkey.Close();
  87. //stop the P2P chating service.
  88. this->Stop_P2PChatSvr();
  89. //create the chating service.
  90. CMainFrame *pMainFrm = (CMainFrame*)::AfxGetMainWnd();
  91. ASSERT(NULL != pMainFrm);
  92. //////////////////////////////////////////////////////////////////////////
  93. memset(&m_cpgResponse, 0x0, sizeof(m_cpgResponse));
  94. sprintf(m_cpgResponse.pktype, "%*d", SIZE_PKTYPE, CPT_RESPONSE);
  95. sprintf(m_cpgResponse.agentid, "%*d", SIZE_AGENTID, theApp.m_oCSHelper.GetClientID());
  96. sprintf(m_cpgResponse.headportrait, "%*d", SIZE_HADPTRT, dwHeadportrait);
  97. //////////////////////////////////////////////////////////////////////////
  98. this->m_pchatSvrThread = new CUDPServerThread;
  99. this->m_pchatSvrThread->SetMsgWnd(pMainFrm->m_wndChatBar, UWM_CHATINGMSG);
  100. this->m_pchatSvrThread->SetResponsePackage((char*)&m_cpgResponse, sizeof(m_cpgResponse));
  101. if(!this->m_pchatSvrThread->Create(dwUDPort)) return false;
  102. //Sleep(500);
  103. //::SendMessage(pMainFrm->m_wndChatBar.GetSafeHwnd(), UWM_CHATSTARTLOGON, 0, 0);
  104. return true;
  105. }
  106. //stop function of peer to peer chating.
  107. void CCCHelper::Stop_P2PChatSvr(void)
  108. {
  109. if(this->m_pchatSvrThread)
  110. {
  111. this->m_pchatSvrThread->Stop();
  112. _DELETE(this->m_pchatSvrThread);
  113. }
  114. }
  115. //get received file save directory.
  116. LPCSTR CCCHelper::GetRecvedFileSaveDir()
  117. {
  118. if(NULL == this->m_pfileRecvorSvr) return NULL;
  119. return this->m_pfileRecvorSvr->GetRecedFileSaveDir();
  120. }
  121. //get member pointer.
  122. CFileReceiverThread *CCCHelper::GetFileRecverThread()
  123. {
  124. return this->m_pfileRecvorSvr;
  125. }
  126. //get member pointer.
  127. CFileSenderThread *CCCHelper::GetFileSenderThread()
  128. {
  129. return this->m_pfileSenderSvr;
  130. }