| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // ClientBase.cpp: implementation of the CClientBase class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "../../Framework/Common/ErrorCode.h"
- #include "ClientBase.h"
- #include "ClientComm.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CClientBase::CClientBase()
- {
- m_pClientCommFunc = new CClientComm(this);
- }
- CClientBase::~CClientBase()
- {
- delete m_pClientCommFunc;
- m_pClientCommFunc = NULL;
- }
- bool CClientBase::Connect(const char *pServerAddr, int nPort, int nOption)
- {
- return m_pClientCommFunc->Connect(pServerAddr, nPort, nOption);
- }
- void CClientBase::Close()
- {
- m_pClientCommFunc->Close();
- }
- string CClientBase::SendPackage(const IPackage *pSendPkg)
- {
- return m_pClientCommFunc->SendPackage(pSendPkg);
- }
- IPackage* CClientBase::ReceivePackage(int nWaitSecond)
- {
- return m_pClientCommFunc->ReceivePackage(nWaitSecond);
- }
- IPackage* CClientBase::CreateNewPackage(const char *pServiceCode)
- {
- return m_pClientCommFunc->CreateNewPackage(pServiceCode);
- }
- IPackage* CClientBase::CreateReplyPackage(IPackage *pRecvPkg)
- {
- return m_pClientCommFunc->CreateReplyPackage(pRecvPkg);
- }
- // 默认错误处理
- void CClientBase::OnError(DWORD dwSysCode, DWORD dwUserCode, const char *pErrMsg)
- {
- printf("[ERROR] [%d] [%d] %s\n", dwSysCode, dwUserCode, pErrMsg);
- }
- // 生成鉴权请求结构,需调用令牌管理实体取得令牌及加密后的设备信息
- bool CClientBase::OnAuthRequest(CConnAuthReq *pReq)
- {
- printf("OnAuthRequest....\n");
- // 取得令牌
- int nLen = sizeof(pReq->m_arrVerifyToken);
- if (!m_tokenEntity.GetToken(pReq->m_arrVerifyToken, &nLen))
- return false;
-
- // 生成设备信息
- CVerifyInfo info;
- memset(&info, 0, sizeof(info));
- strcpy(info.m_arrTerminalNo, "075512345678");
-
- info.m_arrIP[0] = 192;
- info.m_arrIP[1] = 168;
- info.m_arrIP[2] = 0;
- info.m_arrIP[3] = 2;
- strcpy(info.m_arrServiceType, "CMBRVC");
- // 使用临时私钥加密
- nLen = sizeof(pReq->m_arrVerifyInfo);
-
- if (!m_tokenEntity.EncWithClientPriKey((BYTE*)&info, sizeof(info), pReq->m_arrVerifyInfo, &nLen))
- return false;
- return true;
- }
- bool CClientBase::OnSessionKeyRet(CConnAuthRet *pRet, BYTE *pBuf, int *pBufLen)
- {
- printf("OnSessionKeyRet....\n");
- if (*pBufLen < 16)
- return false;
- if (!m_tokenEntity.DecWithClientPriKey(pRet->m_arrEncSessionKey, sizeof(pRet->m_arrEncSessionKey), pBuf, pBufLen))
- {
- OnError(Error_Unexpect, 0, "decrypt session key fail");
- return false;
- }
- return true;
- }
- bool CClientBase::OnGetSharedSK(char *pTerminalNo, int *pTerminalNoLen, BYTE *pBuf, int *pBufLen)
- {
- printf("OnGetSharedSK...\n");
- // 终端号
- strcpy(pTerminalNo, "075512345678");
- *pTerminalNoLen = 12;
- // 从令牌管理实体中取出共享会话密钥,并返回
- return m_tokenEntity.GetSharedSessionKey(pBuf, pBufLen);
- }
- void CClientBase::OnAuthPass()
- {
- printf("OnAuthPass...\n");
- }
|