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

模拟服务器

开发平台:

C/C++

  1. /********************************************************************
  2. created: 2003/08/22
  3. file base: SmartClient
  4. file ext: h
  5. author: liupeng
  6. purpose:
  7. *********************************************************************/
  8. #ifndef __INCLUDE_SMARTCLIENT__
  9. #define __INCLUDE_SMARTCLIENT__
  10. #pragma once
  11. #include "IClient.h"
  12. #include "RainbowInterface.h"
  13. #include "Library.h"
  14. #include "Buffer.h"
  15. #include "tstring.h"
  16. #include "CriticalSection.h"
  17. #include <list>
  18. class CSmartClient
  19. {
  20. public:
  21. CSmartClient();
  22. virtual ~CSmartClient();
  23. bool Open( const char * const pAddressToConnectServer, 
  24. unsigned short usPortToConnectServer, 
  25. HWND hwndContain,
  26. const char *pName,
  27. const char *pPwd,
  28. bool bReLink = true );
  29. void Close();
  30. bool Valid() { return ( NULL != m_pClient ); };
  31. bool Send( const void * const pData, const size_t &datalength );
  32. const void *Recv( size_t &datalength );
  33. static DWORD WINAPI WorkingThreadFunction( void *pV );
  34. HWND m_hwndContain;
  35. protected:
  36. bool CreateConnectThread();
  37. IClient* _Connect();
  38. void _Verify(IClient* pClient, bool bDisconnect);
  39. static void __stdcall EventNotify( LPVOID lpParam, 
  40. const unsigned long &ulnEventType );
  41. void _EventNotify( const unsigned long &ulnEventType );
  42. enum enumServerLoginErrorCode
  43. {
  44. enumConnectFailed = 0xA1,
  45. enumUsrNamePswdErr,
  46. enumIPPortErr,
  47. enumException
  48. };
  49. private:
  50. typedef HRESULT ( __stdcall * pfnCreateClientInterface )( REFIID riid, void **ppv );
  51. static OnlineGameLib::Win32::CLibrary m_theRainbowLib;
  52. OnlineGameLib::Win32::CCriticalSection m_csHistroyList;
  53. typedef std::list< OnlineGameLib::Win32::CBuffer * > stdList;
  54. stdList m_theHistroyList;
  55. HANDLE m_hWorkingThread;
  56. bool m_bReLink; //使用本类需要自动重连功能
  57. /*
  58.  * Config
  59.  */
  60. IClient *m_pClient;
  61. OnlineGameLib::Win32::_tstring m_sUserName;
  62. OnlineGameLib::Win32::_tstring m_sUserPwd;
  63. OnlineGameLib::Win32::_tstring m_sSvrIP;
  64. unsigned short m_nSvrPort;
  65. OnlineGameLib::Win32::CBuffer::Allocator m_theAllocator;
  66. OnlineGameLib::Win32::CCriticalSection m_csStatus;
  67. enum enumClientStatus
  68. {
  69. enumClientNormal = 0x00,
  70. enumClientConnect,
  71. enumClientVerify,
  72. enumClientError,
  73. };
  74. enumClientStatus m_ClientStatus;
  75. bool IsNormal();
  76. bool IsConnecting();
  77. bool IsVerifying();
  78. bool IsError();
  79. void SwitchToConnecting(); //切换状态到重连,如有必要会启动重连线程
  80. bool SetClientStatus(enumClientStatus nNewS);
  81. };
  82. extern CSmartClient g_theSmartClient;
  83. #endif // __INCLUDE_SMARTCLIENT__