ServerSocket.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include <afxwin.h>
  3. //#include <atlbase.h>
  4. #pragma warning(push)
  5. #pragma warning(disable:4995)
  6. #include <list>
  7. #pragma warning(pop)
  8. #include "CritSection.h"
  9. #include "SocketHandle.h"
  10. #include "SocketServerImpl.h"
  11. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  12. #include "AsyncSocketServerImpl.h"
  13. #endif
  14. #include "Global.h"
  15. #include "BaseVar.h"
  16. namespace ServerSocket
  17. {
  18. typedef std::list<SOCKET> CSocketList;
  19. class CServerSocket :
  20. public CDialog,
  21. public ISocketServerHandler
  22. {
  23. #if defined(SOCKHANDLE_USE_OVERLAPPED)
  24. typedef ASocketServerImpl<ISocketServerHandler> CSocketServer;
  25. #else
  26. typedef SocketServerImpl<ISocketServerHandler> CSocketServer;
  27. #endif
  28. friend CSocketServer; // 模板类;
  29. protected:
  30. // ISocketServerHandler
  31. virtual void OnThreadBegin(CSocketHandle* pSH);
  32. virtual void OnThreadExit(CSocketHandle* pSH);
  33. virtual void OnAddConnection(CSocketHandle* pSH, SOCKET newSocket);
  34. virtual void OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr);
  35. virtual void OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket);
  36. virtual void OnConnectionDropped(CSocketHandle* pSH);
  37. virtual void OnConnectionError(CSocketHandle* pSH, DWORD dwError);
  38. protected:
  39. void GetAddress(const SockAddrIn& addrIn, CString& rString) const;
  40. bool GetDestination(SockAddrIn& addrIn) const;
  41. public:
  42. CServerSocket(void);
  43. ~CServerSocket(void);
  44. public:
  45. int SvrStart(CString strPort);
  46. void SocketStop();
  47. void SocketSend(CSocketHandle &sockHandle, unsigned char *pMsg, int iLength);
  48. protected:
  49. int m_nSockType;
  50. int m_nMode;
  51. void ProcessData(CSocketHandle &sockHandle, const BYTE* pData, DWORD nLen);
  52. int OnCmdProcess(CSocketHandle &sockHandle, void *pData);
  53. int ProcessResDevOnline(CSocketHandle &sockHandle, void *pBuffer);
  54. int ProcessResSingleVarOnline(CSocketHandle &sockHandle, void *pBuffer);
  55. int ProcessResMulVarOnline(CSocketHandle &sockHandle, void *pBuffer);
  56. int ProcessResReadSingleVarData(CSocketHandle &sockHandle, void *pBuffer);
  57. int ProcessResReadMulVarData(CSocketHandle &sockHandle, void *pBuffer);
  58. int ProcessResWriteSingleVarData(CSocketHandle &sockHandle, void *pBuffer);
  59. int PrcocessResWriteMulVarData(CSocketHandle &sockHandle, void *pBuffer);
  60. int ProcessResSoftDog(CSocketHandle &sockHandle, void *pBuffer);
  61. int ProcessResAlarmConfirm(CSocketHandle &sockHandle, void *pBuffer);
  62. int ProcessResReadSpecSingleVarData(CSocketHandle &sockHandle, void *pBuffer);
  63. int ProcessResReadSpecMulVarData(CSocketHandle &sockHandle, void *pBuffer);
  64. #if 0
  65. int ProcessHeart(CSocketHandle &sockHandle, void *pBuffer);
  66. #endif
  67. //重新读取数据库通知
  68. int ProcessNoticeReadDBVar(CSocketHandle &sockHandle, void *pBuffer);
  69. int ProcessNoticeReadDBDev(CSocketHandle &sockHandle, void *pBuffer);
  70. int ProcessNoticeReadDBUser(CSocketHandle &sockHandle, void *pBuffer);
  71. int ProcessNoticeTestAlarm(CSocketHandle &sockHandle, void *pBuffer);
  72. //主动发变量的值给客户端 没有简单变量,特殊变量之分
  73. int ProcessAutoSendVarDataToClient(CSocketHandle &sockHandle, CBaseVar *pBaseVar );
  74. //主支发报警信息给客户端
  75. int ProcessAutoSendAlarmDataToClient( CSocketHandle &sockHandle,CHAR *pDevUid, int nVarID, int iStatus,CHAR *pContent
  76. ,double dbParaValue, int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex );
  77. public:
  78. void AutoSendDataToAllClient( CBaseVar *pBaseVar );
  79. void AutoSendAlarmDataToClient( CHAR *pDevUid, int iVarID, int iStatus,CHAR *pContent
  80. ,double dbParaValue, int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex );
  81. CSocketServer m_SocketServer; // 模板类对象;
  82. int m_nSocketIndex;
  83. CRITICAL_SECTION m_csProcessData;
  84. };
  85. extern CServerSocket* g_pServerSocket[MAX_SERVER_LISTEN];
  86. };