-
Notifications
You must be signed in to change notification settings - Fork 15
/
connection.cpp
46 lines (43 loc) · 1.22 KB
/
connection.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
#include <QHostAddress>
#include <QsLog.h>
#include "connection.h"
Connection::Connection(QTcpSocket *socket, QObject *parent):
QObject(parent),
mSocket(socket),
mLength(-1)
{
connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), SLOT(disconnected()));
}
void Connection::readyRead()
{
QByteArray tcpReq = mSocket->readAll();
foreach(char byte, tcpReq) {
mData.append(byte);
if (mData.count() == 6) {
mLength = toUInt16(mData, 4) + 6;
mData.reserve(mLength);
}
if (mData.count() == mLength) {
QLOG_DEBUG() << QString("[Server] request from: %1:%2").
arg(mSocket->peerAddress().toString()).
arg(mSocket->peerPort());
QLOG_TRACE() << "[Server] request data " << tcpReq.toHex().toUpper();
ADU *request = new ADU(mSocket, mData);
QLOG_TRACE() << "[Server] Request:" << request->aduToString();
mLength = -1;
mData.resize(0);
mData.reserve(6);
emit modbusRequest(request);
}
}
}
void Connection::disconnected()
{
QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
QLOG_TRACE() << QString("[Server] Disconnected: %1:%2").
arg(socket->peerAddress().toString()).
arg(socket->peerPort());
socket->deleteLater();
deleteLater();
}