SpSecureClient.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef SPSECURECALLBACK_H
  2. #define SPSECURECALLBACK_H
  3. #pragma once
  4. //
  5. // thin wrapper over RVCComm client library, we provide security session for the terminal.
  6. //
  7. // note: use mod_tokenmgr for Token management and RSA encryption and decryption,
  8. // so this header rely on mod_tokenmgr module, more detail please refer to design doc.
  9. #include "RVCComm.h"
  10. #include "SpBase.h"
  11. #include <assert.h>
  12. #pragma comment(lib, "ws2_32.lib")
  13. struct ENTITY_CONNECT_INFO;
  14. enum RvcCommStateEnum : DWORD
  15. {
  16. CS_Stop = 0, // 停止状态
  17. CS_Connectting, // 连接中
  18. CS_Fail, // 连接失败
  19. CS_Connected, // 连接建立
  20. CS_Break, // 异常中断
  21. };
  22. class SPBASE_API SpSecureClient : public CSecureClientBase
  23. {
  24. public:
  25. SpSecureClient(CEntityBase *pEntityBase);
  26. // !!!!!!!! 此函数已降级,请使用ConnectFromCentralSetting
  27. // 手工指定通讯参数,@nOption:1、重新鉴权新建会话密钥;2、通过握手使用缓存会话密钥;3、不使用会话密钥,即非安全通道; 4、不协商,直接使用共享会话密钥
  28. bool Connect(const char *pServerAddr, int nPort, int nOption); // wait until all authentication operation has been finished
  29. // !!!!!!!! 此函数已降级,请使用ConnectFromCentralSetting
  30. // 使用实体配置项连接, 连接参数从实体配置文件中读取 [Main::Server]和[Main::Server_Backup]项
  31. /** 将接口已废弃 [Gifur@202316]*/
  32. bool ConnectFromConfig(int nOption);
  33. // 使用集中配置项连接
  34. bool ConnectFromCentralSetting();
  35. bool IsConnectionOK();
  36. bool IsSecureConn();
  37. void Close();
  38. string SendPackage(const CSmartPointer<IPackage>& pSendPkg);
  39. CSmartPointer<IPackage> ReceivePackage(int nWaitSecond);
  40. CSmartPointer<IPackage> CreateNewPackage(const char *pServiceCode);
  41. CSmartPointer<IPackage> CreateReplyPackage(const CSmartPointer<IPackage>& pRecvPkg);
  42. protected:
  43. virtual ~SpSecureClient(); // 不应该直接delete,而用DecRef()
  44. //获取当前实体连接到的server, -1, unlink; 0, link to server 1; 1, link to server 2
  45. int getCurrentLink();
  46. // virtual function to be override by user
  47. virtual void OnErrorMsg(DWORD dwSysCode, DWORD dwUserCode, const CSimpleStringA &pErrMsg);
  48. virtual void OnDisconnect();
  49. virtual void OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg) =0;
  50. private:
  51. // override CSecureClientBase
  52. virtual void OnReceivePackage(CSmartPointer<IPackage> &pRecvPkg);
  53. virtual void OnClose();
  54. virtual void OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg);
  55. virtual void OnAuthPass();
  56. virtual bool OnAuthRequest(CConnAuthReq *pReq);
  57. virtual bool OnAuthRequestSM(CConnAuthSMReq* pReq);
  58. virtual bool OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen);
  59. virtual bool OnSessionKeySMRet(CConnAuthSMRet* pRet, BYTE* pBuf, int* pBufLen);
  60. virtual bool OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen);
  61. ErrorCodeEnum ParseIPAddress(const char *str, CSimpleStringA &ip, int &port);
  62. DWORD GetEntityUserCode(DWORD code);
  63. bool findConnectInfo(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo);
  64. bool readConnectInfoFromCentralSetting(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo);
  65. bool readConnectInfoFromConfig(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo);
  66. bool setConnectInfo(const ENTITY_CONNECT_INFO *connectInfo);
  67. void connectClose(const char *pEntityName);
  68. //不支持双活
  69. bool Connect_Single(ENTITY_CONNECT_INFO *connectInfo, int option);
  70. //支持双活
  71. bool Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option);
  72. void OnReConnect();
  73. void DbgWithLink_DualActive(const char* formatStr, ...);
  74. protected:
  75. static void* logProducer;
  76. public:
  77. static CEntityBase* m_pEntity;
  78. static void SetSecureEntity(CEntityBase* t_entity) { m_pEntity = t_entity; };
  79. private:
  80. bool m_bClosed;
  81. friend struct OnPackageTask;
  82. friend struct OnDisconnectTask;
  83. friend struct OnErrorTask;
  84. };
  85. void getHttpToken(std::string& channelId, std::string& token, std::string& terminalNo, std::string& reserve1);
  86. #endif // SPSECURECALLBACK_H