SpSecureClient.h 3.8 KB

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