-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecvclient.cpp
96 lines (80 loc) · 2.75 KB
/
recvclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "recvclient.h"
#include <iostream>
extern QObject* rootObject;
BufferError::BufferError()throw() {}
const char* BufferError::what()const throw(){
return "recv length flag == 0";
}
JsonBuffer::JsonBuffer():expectMessageLength(0) {}
void JsonBuffer::append(QByteArray data) {
int lastBufLength = buffer.length();
buffer.append(data);
if(expectMessageLength <= 0) {
getMessageLength();
}
if(data.length() >= expectMessageLength) {
if(expectMessageLength <= 0) {
throw BufferError();
}
QByteArray message = buffer.left(expectMessageLength);
qDebug() << message;
QJsonDocument jd = QJsonDocument::fromJson(message.right(message.length()-4));
QJsonObject jsonObj = jd.object();
int wantRightLength = data.length() - (expectMessageLength - lastBufLength);
buffer = data.right(wantRightLength);
emit collected(jsonObj);
// will be reset
expectMessageLength = 0;
}
}
void JsonBuffer::getMessageLength() {
QByteArray lengthData = buffer.left(4);
const int *pInt = (int*)lengthData.data();
qDebug() << *pInt;
expectMessageLength = *pInt;
}
RecvServer::RecvServer(): QThread() {
QObject::connect(&buffer, &JsonBuffer::collected, this, &RecvServer::putJsonObj);
client.moveToThread(this);
buffer.moveToThread(this);
qRegisterMetaType<QAbstractSocket::SocketError>("SocketError");
}
RecvServer::~RecvServer() {
qDebug() << "client.close";
client.close();
this->exit();
}
void RecvServer::run() {
QObject::connect(&client, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &RecvServer::socketError);
QObject::connect(&client, &QTcpSocket::readyRead, this, &RecvServer::readData);
client.connectToHost(QHostAddress("127.0.0.1"), 9092);
bool isConnected = client.waitForConnected();
if(!isConnected) {
socketError(client.error());
return;
}
int ret = client.write(account.c_str(), strlen(account.c_str()));
if(ret < 0) {
qDebug() << "ret < 0";
socketError(client.error());
}
qDebug() << "after send:" << ret;
QThread::exec();
}
void RecvServer::readData() {
QByteArray data = client.readAll();
qDebug() << "readData is called";
qDebug() << data;
buffer.append(data);
}
void RecvServer::socketError(QAbstractSocket::SocketError socketError) {
QString msg = client.errorString();
QMetaObject::invokeMethod(rootObject, "netWorkError", Q_ARG(QVariant, msg));
//this->exit(-1);
}
void RecvServer::putJsonObj(QJsonObject jsonObj) {
QMetaObject::invokeMethod(rootObject, "recvJsonObj", Q_ARG(QVariant, jsonObj));
}
void RecvServer::setAccount(std::string account) {
this->account = account;
}