NetCenter.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:2k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // NetCenter.cpp: implementation of the CNetCenter class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "NetCenter.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CNetCenter::CNetCenter()
  10. {
  11. }
  12. CNetCenter::~CNetCenter()
  13. {
  14. }
  15. BOOL CNetCenter::Initialize()
  16. {
  17. AUTOLOCKWRITE(m_lockAccess);
  18. OnBuildup();
  19. return TRUE;
  20. }
  21. BOOL CNetCenter::Uninitialize()
  22. {
  23. AUTOLOCKWRITE(m_lockAccess);
  24. for (CLIENTSET::iterator it = m_setClient.begin(); it != m_setClient.end(); it++)
  25. {
  26. CNetClient* pClient = (*it);
  27. assert(pClient);
  28. if (pClient)
  29. pClient->Shutdown();
  30. }
  31. m_setClient.clear();
  32. OnClearup();
  33. return TRUE;
  34. }
  35. void CNetCenter::_NotifyServerEventCreate(CNetClient* pClient)
  36. {
  37. AUTOLOCKWRITE(m_lockAccess);
  38. OnServerEventCreate(pClient);
  39. m_setClient.insert(pClient);
  40. }
  41. void CNetCenter::_NotifyServerEventClose(CNetClient* pClient)
  42. {
  43. AUTOLOCKWRITE(m_lockAccess);
  44. m_setClient.erase(pClient);
  45. OnServerEventClose(pClient);
  46. }
  47. size_t CNetCenter::GetClientCount() 
  48. {
  49. AUTOLOCKREAD(m_lockAccess);
  50. return m_setClient.size();
  51. }
  52. BOOL CNetCenter::BroadPackage(const void* pData, size_t size)
  53. {
  54. AUTOLOCKREAD(m_lockAccess);
  55. for (CLIENTSET::iterator it = m_setClient.begin(); it != m_setClient.end(); it++)
  56. {
  57. CNetClient* pNetClient = (*it);
  58. assert(pNetClient);
  59. if (pNetClient)
  60. pNetClient->SendPackage(pData, size);
  61. }
  62. return TRUE;
  63. }
  64. BOOL CNetCenter::Route()
  65. {
  66. AUTOLOCKREAD(m_lockAccess);
  67. for (CLIENTSET::iterator it = m_setClient.begin(); it != m_setClient.end(); it++)
  68. {
  69. CNetClient* pNetClient = *it;
  70. pNetClient->Route();
  71. }
  72. return TRUE;
  73. }