ClientBase.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // ClientBase.cpp: implementation of the CClientBase class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "../../Framework/Common/ErrorCode.h"
  6. #include "ClientBase.h"
  7. #include "ClientComm.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CClientBase::CClientBase()
  12. {
  13. m_pClientCommFunc = new CClientComm(this);
  14. }
  15. CClientBase::~CClientBase()
  16. {
  17. delete m_pClientCommFunc;
  18. m_pClientCommFunc = NULL;
  19. }
  20. bool CClientBase::Connect(const char *pServerAddr, int nPort, int nOption)
  21. {
  22. return m_pClientCommFunc->Connect(pServerAddr, nPort, nOption);
  23. }
  24. void CClientBase::Close()
  25. {
  26. m_pClientCommFunc->Close();
  27. }
  28. string CClientBase::SendPackage(const IPackage *pSendPkg)
  29. {
  30. return m_pClientCommFunc->SendPackage(pSendPkg);
  31. }
  32. IPackage* CClientBase::ReceivePackage(int nWaitSecond)
  33. {
  34. return m_pClientCommFunc->ReceivePackage(nWaitSecond);
  35. }
  36. IPackage* CClientBase::CreateNewPackage(const char *pServiceCode)
  37. {
  38. return m_pClientCommFunc->CreateNewPackage(pServiceCode);
  39. }
  40. IPackage* CClientBase::CreateReplyPackage(IPackage *pRecvPkg)
  41. {
  42. return m_pClientCommFunc->CreateReplyPackage(pRecvPkg);
  43. }
  44. // 默认错误处理
  45. void CClientBase::OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg)
  46. {
  47. printf("[ERROR] [%d] [%d] %s\n", dwSysCode, dwUserCode, pErrMsg);
  48. }
  49. // 生成鉴权请求结构,需调用令牌管理实体取得令牌及加密后的设备信息
  50. bool CClientBase::OnAuthRequest(CConnAuthReq *pReq)
  51. {
  52. printf("OnAuthRequest....\n");
  53. // 取得令牌
  54. int nLen = sizeof(pReq->m_arrVerifyToken);
  55. if (!m_tokenEntity.GetToken(pReq->m_arrVerifyToken, &nLen))
  56. return false;
  57. // 生成设备信息
  58. CVerifyInfo info;
  59. memset(&info, 0, sizeof(info));
  60. strcpy(info.m_arrTerminalNo, "075512345678");
  61. info.m_arrIP[0] = 192;
  62. info.m_arrIP[1] = 168;
  63. info.m_arrIP[2] = 0;
  64. info.m_arrIP[3] = 2;
  65. strcpy(info.m_arrServiceType, "CMBRVC");
  66. // 使用临时私钥加密
  67. nLen = sizeof(pReq->m_arrVerifyInfo);
  68. if (!m_tokenEntity.EncWithClientPriKey((BYTE*)&info, sizeof(info), pReq->m_arrVerifyInfo, &nLen))
  69. return false;
  70. return true;
  71. }
  72. bool CClientBase::OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen)
  73. {
  74. printf("OnSessionKeyRet....\n");
  75. if (*pBufLen < 16)
  76. return false;
  77. if (!m_tokenEntity.DecWithClientPriKey(pRet->m_arrEncSessionKey, sizeof(pRet->m_arrEncSessionKey), pBuf, pBufLen))
  78. {
  79. OnError(Error_Unexpect, 0, "decrypt session key fail");
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool CClientBase::OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen)
  85. {
  86. printf("OnGetSharedSK...\n");
  87. // 终端号
  88. strcpy(pTerminalNo, "075512345678");
  89. *pTerminalNoLen = 12;
  90. // 从令牌管理实体中取出共享会话密钥,并返回
  91. return m_tokenEntity.GetSharedSessionKey(pBuf, pBufLen);
  92. }
  93. void CClientBase::OnAuthPass()
  94. {
  95. printf("OnAuthPass...\n");
  96. }