Files
ict/client/ewsc/net/netlib/THNetCommCmdInter.cpp
2026-01-23 08:57:55 +08:00

111 lines
2.7 KiB
C++

// THNetCommCmdInter.cpp: implementation of the CTHNetCommCmdInter class.
//
//////////////////////////////////////////////////////////////////////
#include "THNetCommCmdInter.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTHNetCommCmdInter::CTHNetCommCmdInter()
{
m_pPara = NULL;
// m_szHostName[0] = 0;
// m_szSrvName[0] = 0;
}
CTHNetCommCmdInter::~CTHNetCommCmdInter()
{
m_oLink.Close();
m_pPara = NULL;
}
int CTHNetCommCmdInter::LinkByPara()
{
return m_oLink.LinkByPara(m_oLink.m_oServPara);
}
int CTHNetCommCmdInter::LinkTo(const char * szHost,int nPort)
{
m_oLink.m_oServPara.SetHostName(szHost,nPort);
return m_oLink.LinkByPara(m_oLink.m_oServPara);
}
int CTHNetCommCmdInter::LinkToSrv(const char * szServer,const int nAppNo,const int nCtxNo)
{
m_oLink.m_oServPara.SetServName(szServer,nAppNo,nCtxNo);
return m_oLink.LinkByPara(m_oLink.m_oServPara);
}
int CTHNetCommCmdInter::LinkToSrv(const char * szServer,const char * szApp,const char * szCtx)
{
m_oLink.m_oServPara.SetServName(szServer,szApp,szCtx);
return m_oLink.LinkByPara(m_oLink.m_oServPara);
}
void CTHNetCommCmdInter::SetPara(CNetCmdCommCmdPara * pPara)
{
m_pPara = pPara;
}
int CTHNetCommCmdInter::SendData(BYTE * pData,int nLen)
{
if(!m_oLink.IsWritable())
{
m_oLink.LinkByPara(m_oLink.m_oServPara);
}
XByteArray ba(1024 * 1024);
m_pPara->Write(&ba);
if(ba.m_nLength + nLen > ba.m_nMaxLength)
{
BYTE * pNewData = new BYTE[ba.m_nLength + nLen];
memcpy(pNewData,ba.m_pData,ba.m_nLength);
delete [] ba.m_pData;
ba.m_pData = pNewData;
ba.m_nMaxLength = ba.m_nLength + nLen;
}
ba.WriteData(pData,nLen);
NET_HEAD oHead;
oHead.src.n_reg_type = 3;
// oHead.cmdtype = NETDBIO_CMD_COMM_CMD;
oHead.len = ba.m_nLength;
if( !m_oLink.TxStream(&oHead,(void*)ba.m_pData))
{
printf("无法向服务 %s/%s 发送消息", m_oLink.m_oServPara.m_szHostName,
m_oLink.m_oServPara.m_szServName);
return 0;
}
return 1;
}
int CTHNetCommCmdInter::RecvData(XByteArray * pa)
{
m_szErrorCode[0] = 0;
if(m_pPara->m_nWriteBack == 0)
{
sprintf(m_szErrorCode,"无返回结果");
return 0;
}
NET_HEAD oHead;
if (!m_oLink.RxHead(&oHead))
{
sprintf(m_szErrorCode,"无法读取返回报文头");
return 0;
}
pa->Reset();
if(pa->m_nMaxLength < (int)oHead.len)
{
delete [] pa->m_pData;
pa->m_pData = new BYTE[(int)oHead.len];
pa->m_nMaxLength = (int)oHead.len;
}
if (!m_oLink.RxData(pa,(int)oHead.len))
{
sprintf(m_szErrorCode,"无法读取返回报文结果");
return 0;
}
int nSucc = pa->ReadInt();
if(nSucc >= 0)
{
sprintf(m_szErrorCode,"执行成功");
return 1;
}
pa->ReadString(m_szErrorCode,1024);
printf("%s\n",m_szErrorCode);
return 1;
}