IServerImpl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************
  2. /* Copyright (C), 2014-2020, lyfz. Co., Ltd.
  3. /* 文件名: IServerImpl.h
  4. /* 作者: Jeff.w
  5. /* 创建日期: 2014-08-07
  6. /* 版本号: V1.0
  7. /* 描述: 核心层底,客户端与服务端通信接口
  8. /* 其它:
  9. /* 主要类模块:
  10. /* 历史修改记录:
  11. /* 作者 时间 版本 描述
  12. /* Jeff.w 14/08/07 1.0 创建这个模块
  13. ***************************************************************/
  14. #ifndef __ISERVER_IMPL_HEADER__
  15. #define __ISERVER_IMPL_HEADER__
  16. #pragma once
  17. #include <afxwin.h>
  18. #include <atlbase.h>
  19. //#pragma warning(push)
  20. //#pragma warning(disable:4995)
  21. #include <list>
  22. //#pragma warning(pop)
  23. #include "CritSection.h"
  24. #include "SocketHandle.h"
  25. #include "SocketServerImpl.h"
  26. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  27. #include "AsyncSocketServerImpl.h"
  28. #endif
  29. #include "Global.h"
  30. // 宏定义:计算数据包总长度;
  31. #define CALCDATALEN(x) (x+sizeof(STProtocolheader)+sizeof(STProtocoltail))
  32. // 使用通信这一块,完全分离出来;
  33. namespace ServerSocketImpl
  34. {
  35. typedef std::list<SOCKET> CSocketList;
  36. class IServerImpl :public ISocketServerHandler
  37. {
  38. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  39. typedef ASocketServerImpl<ISocketServerHandler> CSocketServer;
  40. #else
  41. typedef SocketServerImpl<ISocketServerHandler> CSocketServer;
  42. #endif
  43. friend CSocketServer;
  44. public:
  45. IServerImpl();
  46. ~IServerImpl();
  47. virtual void OnThreadBegin(CSocketHandle* pSH);
  48. virtual void OnThreadExit(CSocketHandle* pSH);
  49. virtual void OnAddConnection(CSocketHandle* pSH, SOCKET newSocket);
  50. virtual void OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr);
  51. virtual void OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket);
  52. virtual void OnConnectionDropped(CSocketHandle* pSH);
  53. virtual void OnConnectionError(CSocketHandle* pSH, DWORD dwError);
  54. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  55. virtual void OnRemoveConnection(CSocketHandle* pSH , SOCKET dropSocket);
  56. //virtual void OnThreadLoopEnter(CSocketHandle* pSH);
  57. //virtual void OnThreadLoopLeave(CSocketHandle* pSH);
  58. #endif
  59. protected:
  60. CString m_strPort;
  61. int m_nPort;
  62. int m_nMode;
  63. int m_nSockType;
  64. CSocketServer m_SocketServer;
  65. void _cdecl AppendText(LPCTSTR lpszFormat, ...);
  66. void GetAddress(const SockAddrIn& addrIn, CString& rString) const;
  67. bool GetDestination(SockAddrIn& addrIn) const;
  68. bool SetupMCAST();
  69. // Jeff.最新研究出来的处理方案;
  70. // 对异常包需要计数处理,当1分钟内出现多少异常包时,需要重启服务函数;
  71. //unsigned int VerityIntegrityPacket(IN void *pIntegrityPacket,unsigned int nPacketSize);
  72. //void ToprocessRecivebuf(IN CSocketHandle &sockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize);
  73. void ToprocessRecivebuf(IN SocketIOBuffer &sockIOBuffer, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize);
  74. //int OnIntegrityPacket(IN CSocketHandle &sockHandle, IN void *pIntegrityPacket);
  75. int OnIntegrityPacket(IN SocketIOBuffer &sockHandle, IN void *pIntegrityPacket);
  76. public:
  77. BOOL m_bStopbeat;
  78. int m_nSocketIndex;
  79. CRITICAL_SECTION m_csProcessData;
  80. DWORD GetClientConnectCount();
  81. BOOL Initialize();
  82. void Start(IN LPCTSTR strPort,IN const int &nMode);
  83. void Stop();
  84. void Send();
  85. void SendAll(CSocketHandle &sockHandle, unsigned char *pMsg, int nLength);
  86. HANDLE m_hClearInvalidateSocketThread;
  87. HANDLE m_hRunObject;
  88. void StartClearInvalidateSocketThread();
  89. static DWORD WINAPI ClearInvalidateSocketThread(void *pInstance);
  90. private:
  91. CSocketHandle m_SocketFile;
  92. };
  93. extern IServerImpl* g_pServerSocket[20];
  94. };
  95. #endif