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

模拟服务器

开发平台:

C/C++

  1. /********************************************************************
  2. created: 2003/02/18
  3. file base: SocketClient
  4. file ext: h
  5. author: liupeng
  6. purpose:
  7. *********************************************************************/
  8. #ifndef __INCLUDE_SOCKETCLIENT_H__
  9. #define __INCLUDE_SOCKETCLIENT_H__
  10. #if defined (_MSC_VER) && (_MSC_VER >= 1020)
  11. #pragma once
  12. #endif
  13. #include "UsesWinsock.h"
  14. #include "Thread.h"
  15. #include "CriticalSection.h"
  16. #include "IOBuffer.h"
  17. #include "ManualResetEvent.h"
  18. #include "NodeList.h"
  19. #include "OpaqueUserData.h"
  20. #include "EventSelect.h"
  21. #include "Cipher.h"
  22. /*
  23.  * namespace OnlineGameLib::Win32
  24.  */
  25. namespace OnlineGameLib {
  26. namespace Win32 {
  27. class CSocketClient : 
  28. protected CThread, 
  29. private CUsesWinsock, 
  30. protected CIOBuffer::Allocator
  31. {
  32. public:
  33. virtual ~CSocketClient();
  34. using CThread::Start;
  35. void Connect(
  36. const _tstring &addressToConnectServer,
  37. unsigned short portToConnectServer
  38. );
  39. bool StartConnections();
  40. void StopConnections();
  41. void InitiateShutdown();
  42. void WaitForShutdownToComplete();
  43. void Write( const char *pData, size_t dataLength );
  44. void Write( CIOBuffer *pBuffer );
  45. protected:
  46. CSocketClient(
  47. const _tstring &addressToConnectServer,
  48. unsigned short portToConnectServer,
  49. size_t maxFreeBuffers,
  50. size_t bufferSize /* = 1024 */
  51. );
  52. CSocketClient(
  53. size_t maxFreeBuffers,
  54. size_t bufferSize /* = 1024 */
  55. );
  56. void ReleaseBuffers();
  57. bool WaitAndVerifyCipher();
  58. /*
  59.  * Virtual function has different access specifier to base class
  60.  */
  61. virtual int Run();
  62.     unsigned m_uKeyMode;
  63.     unsigned m_uServerKey;
  64.     unsigned m_uClientKey;
  65.     #pragma pack( 1 )
  66.     struct 
  67. {
  68. WORD wLen;
  69. ACCOUNT_BEGIN AccountBegin;
  70. }m_theSendAccountBegin;
  71.     #pragma pack()
  72. private:
  73. /*
  74.  * Override this to create the listening socket of your choice
  75.  */
  76. virtual SOCKET CreateConnectionSocket( 
  77. const _tstring &addressToConnectServer,
  78. unsigned short port);
  79. /*
  80.  * Interface for derived classes to receive state change notifications...
  81.  */
  82. virtual void OnStartConnections() {};
  83. virtual void OnStopConnections() {};
  84. virtual void OnShutdownInitiated() {};
  85. virtual void OnShutdownComplete() {};
  86. virtual void OnConnect() {};
  87. virtual void OnClose() {};
  88. void OnRead( CIOBuffer *pBuffer );
  89. virtual void ReadCompleted( CIOBuffer *pBuffer ) = 0;
  90. virtual void OnWrite(){};
  91. virtual void OnError( const _tstring &message );
  92. SOCKET m_connectSocket;
  93. CEventSelect m_eventSelect;
  94. CManualResetEvent m_shutdownEvent;
  95. CManualResetEvent m_successConnectionsEvent;
  96.     CCriticalSection m_criticalSection;
  97. _tstring m_address;
  98. unsigned short m_port;
  99. };
  100. } // End of namespace OnlineGameLib
  101. } // End of namespace Win32
  102. #endif //__INCLUDE_SOCKETCLIENT_H__