-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetclient.cpp
More file actions
68 lines (59 loc) · 1.92 KB
/
netclient.cpp
File metadata and controls
68 lines (59 loc) · 1.92 KB
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
#include "netclient.hpp"
#include <cassert>
netClient::netClient(std::shared_ptr<commandExecutor> cmd_exec_ptr)
:
m_cmd_exec(cmd_exec_ptr),
m_socket(std::make_unique<QTcpSocket>())
{
connect(m_socket.get(), SIGNAL(readyRead()),this, SLOT(onTcpReceive()));
}
void netClient::startConnect(const QHostAddress &address, uint16_t port) {
m_socket->connectToHost(address, port);
// we need to wait...
if(!m_socket->waitForConnected(5000)) {
qDebug() << "Error: " << m_socket->errorString();
}
}
bool netClient::is_connected() {
if(m_socket == nullptr) {
qDebug()<<"Socket is not defined (nullptr)";
return false;
}
else if (m_socket->state() != QAbstractSocket::ConnectedState) {
qDebug()<<"Socket is not connected";
return false;
} else {
return true;
}
}
void netClient::send_msg(const std::string &msg) {
if (!is_connected()) return;
QByteArray packet = serialize_msg(msg);
size_t send_bytes = m_socket->write(packet);
if (send_bytes != packet.size())
throw std::runtime_error("send packet error");
}
void netClient::onTcpReceive() {
std::cout << "onTcpReceive\n";
QByteArray data_array = m_socket->readAll();
std::string arr(data_array.data(), static_cast<size_t>(data_array.size()));
m_data_eater.eat(arr);
m_data_eater.process();
std::string last_cmd = m_data_eater.getLastCommand();
qDebug() << "last command " << QString::fromStdString(last_cmd);
if (!last_cmd.empty()) {
auto cmd_exec_ptr = m_cmd_exec.lock();
cmd_exec_ptr->parseAndExecMsg(last_cmd);
}
}
QByteArray netClient::serialize_msg(const std::string &msg) {
assert(msg.size() <= std::numeric_limits<uint16_t>::max() && "Too big message");
uint16_t msg_size = static_cast<uint16_t>(msg.size());
QByteArray packet(msg_size + 2, 0); // 2 is bytes for size
packet[0] = static_cast<char>(msg_size >> 8);
packet[1] = static_cast<char>(msg_size & 0xFF);
for (unsigned int i = 0; i < msg_size; ++i) {
packet[i + 2] = msg.at(i);
}
return packet;
}