forked from OFFIS-Automation/qt-remote-signals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteSignals.cpp
106 lines (99 loc) · 3.22 KB
/
RemoteSignals.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
97
98
99
100
101
102
103
104
105
106
// OFFIS Automation Framework
// Copyright (C) 2013 OFFIS e.V.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http:#www.gnu.org/licenses/>.
#include "RemoteSignals.h"
#include <QtEndian>
#include <QDebug>
#include <QCoreApplication>
RemoteSignals::RemoteSignals(quint64 gid1, quint64 gid2, QIODevice *readDevice, QIODevice *writeDevice, bool init) :
mGlobalId1(gid1),
mGlobalId2(gid2),
mReadDevice(readDevice),
mWriteDevice(writeDevice)
{
mReadSize = 0;
mCallUid = 0;
connect(this, SIGNAL(transmitSignal(QByteArray)), SLOT(transmitSignalAsync(QByteArray)));
if(init)
initialize();
}
void RemoteSignals::initialize()
{
if(mReadDevice)
{
connect(mReadDevice, SIGNAL(readyRead()), SLOT(onReadyRead()), Qt::DirectConnection);
if(mReadDevice->bytesAvailable())
onReadyRead();
}
}
void RemoteSignals::transmitSignalAsync(const QByteArray &msgData)
{
QByteArray sizeData(4,0);
qToLittleEndian<int>(msgData.size(), (uchar*)sizeData.data());
QMutexLocker lock(&mMutex);
if(!mWriteDevice)
throw std::runtime_error("Could not send signal: no worte device was specified!");
mWriteDevice->write(sizeData);
mWriteDevice->write(msgData);
}
quint64 RemoteSignals::nextCallUid()
{
QMutexLocker lock(&mUidMutex);
return ++mCallUid;
}
const QByteArray RemoteSignals::waitForResponse(quint64 callUid)
{
while(true)
{
QCoreApplication::processEvents();
QMutexLocker lock(&mUidMutex);
if(mResponses.contains(callUid))
return mResponses.take(callUid);
}
}
void RemoteSignals::onReadyRead()
{
while(true)
{
qint64 bytes = mReadDevice->bytesAvailable();
if(mReadSize)
{
if(bytes < mReadSize)
return;
QByteArray data = mReadDevice->read(mReadSize);
processRemoteInputs(data);
mReadSize = 0;
}
else if(bytes >= 4)
{
QByteArray sizeArray = mReadDevice->read(4);
mReadSize = qFromLittleEndian<int>((uchar*)sizeArray.data());
}
else
return;
}
}
void RemoteSignals::handleError(int id)
{
qCritical() << "the signal with the id " << id << " was not found!";
qFatal("A signal id was not found, this is a critical failure! Maybe Client and Server are out of sync?");
}
void RemoteSignals::checkId(int versionId, quint64 id1, quint64 id2)
{
if(versionId != version())
qFatal("The version the sender did not match the version of the receiver. Client and Server are out of sync.");
if(id1 != gid1() || id2 != gid2())
qFatal("The global id of the sender did not match the id of the receiver. Client and Server are out of sync.");
}