Commit 88afe087 by guanghui.cui

肚子里有料 异步socket

parent f5e69428
......@@ -59,11 +59,54 @@ void FMPVipServer::onReadyRead()
QByteArray rspData;
dispatcher->doTask(recvData, rspData);
if(socket->isValid()){
rspData;
Write(rspData);
}
else{
//向23771发送数据
SendToMonitor(rspData);
}
socket->waitForDisconnected();
socket->deleteLater();
socket = nullptr;
}
void FMPVipServer::SendToMonitor(const QByteArray &data)
{
QTcpSocket client;
int jsonLength=data.length();
char* m_pFmPackage = new char[jsonLength + sizeof(FMSOCKEHEADER)];
FMSOCKEHEADER header = { 0, 0, 0 };
header.flag = 0x4d46;
header.len = jsonLength;
header.ver = 0x1;
memcpy(m_pFmPackage, &header, sizeof(FMSOCKEHEADER));
memcpy(m_pFmPackage+sizeof(FMSOCKEHEADER), data, jsonLength);
int toSendLength = jsonLength + sizeof(FMSOCKEHEADER);
int curSendLength = 0;
client.connectToHost("127.0.0.1", 23771);
if (client.waitForConnected()){
while(curSendLength < toSendLength)
{
int rlt= client.write(m_pFmPackage+curSendLength,toSendLength - curSendLength);
client.waitForBytesWritten();
if(rlt==-1){
FMP_DEBUG() << "发送数据失败:" << data;
break;
}
curSendLength += rlt;
}
}
else{
FMP_DEBUG() << "socket 连接失败!";
}
client.close();
}
......@@ -28,7 +28,7 @@ private slots:
void onNewConnection();
void onDisconnected();
void onReadyRead();
void SendToMonitor(const QByteArray &data);
private:
explicit FMPVipServer();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment