📄 qtcpmsgclient.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "QTcpMsgClient.h"
#include "../QNetBase/QNetBase.h"
#include "../QStdlib/QStdlib.h"
#include <assert.h>
#pragma package(smart_init)
using namespace N_QTcpMsgClient;
//---------------------------------------------------------------------------
IQTcpMsgClient* __stdcall IQTcpMsgClient::Create(void)
{
return new QTcpMsgClient;
}
//---------------------------------------------------------------------------
__stdcall QTcpMsgClient::QTcpMsgClient()
: IQTcpMsgClient()
, m_Socket(0)
{
}
//---------------------------------------------------------------------------
__stdcall QTcpMsgClient::~QTcpMsgClient()
{
this->Stop();
}
//---------------------------------------------------------------------------
void __stdcall QTcpMsgClient::Release(void)
{
assert(this);
delete this;
}
//---------------------------------------------------------------------------
bool __stdcall QTcpMsgClient::Start(const std::string& Addr, unsigned int Port
, const std::string& EndSign, unsigned int MaxPackage)
{
if (this->IsPowerOn())
return false;
m_SendBuf.clear();
m_RecvBuf.clear();
m_Msg.clear();
m_EndSign = EndSign;
m_MaxPackage = MaxPackage;
m_Socket = _QCreateSocket();
if (-1 == m_Socket || 0 == m_Socket) {
m_Socket = 0;
return false;
}
std::string Ip = _QNameToIp(Addr);
if (Ip.empty())
Ip = Addr;
if (!_QConnect(m_Socket, Ip, Port, true)) {
_QClose(m_Socket);
m_Socket = 0;
return false;
}
_QSetBlock(m_Socket, false);
return true;
}
//---------------------------------------------------------------------------
bool __stdcall QTcpMsgClient::IsPowerOn(void)const
{
return 0 != m_Socket;
}
//---------------------------------------------------------------------------
void __stdcall QTcpMsgClient::Stop(void)
{
if (!this->IsPowerOn())
return;
_QClose(m_Socket);
m_Socket = 0;
m_SendBuf.clear();
m_RecvBuf.clear();
m_Msg.clear();
}
//---------------------------------------------------------------------------
bool __stdcall QTcpMsgClient::Send(const std::string& Msg)
{
if (!this->IsPowerOn())
return false;
m_SendBuf += Msg;
return true;
}
//---------------------------------------------------------------------------
bool __stdcall QTcpMsgClient::GetQueuedMsg(std::string* pMsg)
{
if (!pMsg)
return false;
return m_Msg.recv(pMsg);
}
//---------------------------------------------------------------------------
void __stdcall QTcpMsgClient::Process(void)
{
if (!this->IsPowerOn())
return;
this->ProcessRecv();
this->ProcessSend();
}
//---------------------------------------------------------------------------
void __stdcall QTcpMsgClient::ProcessRecv(void)
{
if (!this->IsPowerOn())
return;
if (!_QWaitIn(m_Socket, 0))
return;
std::string Data;
if (!_QRecv(m_Socket, &Data)) {
this->Stop();
return;
}
m_RecvBuf += Data;
while (std::string::npos != m_RecvBuf.find(m_EndSign)) {
m_Msg.send(GetNext(m_RecvBuf, m_EndSign));
}
if (m_RecvBuf.size() > m_MaxPackage)
this->Stop();
return;
}
//---------------------------------------------------------------------------
void __stdcall QTcpMsgClient::ProcessSend(void)
{
if (!this->IsPowerOn())
return;
if (m_SendBuf.empty())
return;
if (!_QWaitOut(m_Socket, 0))
return;
if (!_QSend(m_Socket, &m_SendBuf)) {
this->Stop();
return;
}
return;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -