SpSecureClient.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // 使用集中配置项连接
  30. bool ConnectFromCentralSetting();
  31. bool IsConnectionOK();
  32. bool IsSecureConn();
  33. void Close();
  34. string SendPackage(const CSmartPointer<IPackage>& pSendPkg);
  35. CSmartPointer<IPackage> ReceivePackage(int nWaitSecond);
  36. CSmartPointer<IPackage> CreateNewPackage(const char *pServiceCode);
  37. CSmartPointer<IPackage> CreateReplyPackage(const CSmartPointer<IPackage>& pRecvPkg);
  38. protected:
  39. virtual ~SpSecureClient(); // 不应该直接delete,而用DecRef()
  40. //获取当前实体连接到的server, -1, unlink; 0, link to server 1; 1, link to server 2
  41. int getCurrentLink();
  42. // virtual function to be override by user
  43. virtual void OnErrorMsg(DWORD dwSysCode, DWORD dwUserCode, const CSimpleStringA &pErrMsg);
  44. virtual void OnDisconnect();
  45. virtual void OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg) =0;
  46. private:
  47. // override CSecureClientBase
  48. virtual void OnReceivePackage(CSmartPointer<IPackage> &pRecvPkg);
  49. virtual void OnClose();
  50. virtual void OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg);
  51. virtual void OnAuthPass();
  52. virtual bool OnAuthRequestSM(CConnAuthSMReq* pReq);
  53. virtual bool OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen);
  54. virtual bool OnSessionKeySMRet(CConnAuthSMRet* pRet, BYTE* pBuf, int* pBufLen);
  55. virtual bool OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen);
  56. ErrorCodeEnum ParseIPAddress(const char *str, CSimpleStringA &ip, int &port);
  57. DWORD GetEntityUserCode(DWORD code);
  58. bool findConnectInfo(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo);
  59. bool readConnectInfoFromCentralSetting(const char *pEntityName, ENTITY_CONNECT_INFO *connectInfo);
  60. bool setConnectInfo(const ENTITY_CONNECT_INFO *connectInfo);
  61. void connectClose(const char *pEntityName);
  62. //不支持双活
  63. bool Connect_Single(ENTITY_CONNECT_INFO *connectInfo, int option);
  64. //支持双活
  65. bool Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option);
  66. void OnReConnect();
  67. void DbgWithLink_DualActive(const char* formatStr, ...);
  68. protected:
  69. static void* logProducer;
  70. public:
  71. static CEntityBase* m_pEntity;
  72. static void SetSecureEntity(CEntityBase* t_entity) { m_pEntity = t_entity; };
  73. private:
  74. bool m_bClosed;
  75. friend struct OnPackageTask;
  76. friend struct OnDisconnectTask;
  77. friend struct OnErrorTask;
  78. };
  79. void getHttpToken(std::string& channelId, std::string& token, std::string& terminalNo, std::string& reserve1);
  80. #endif // SPSECURECALLBACK_H