Re-use landevicelink objects instead of re-creating one each time
This commit is contained in:
parent
77facdbcfd
commit
2e4c3762e9
4 changed files with 27 additions and 19 deletions
|
@ -29,17 +29,26 @@
|
|||
|
||||
LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource)
|
||||
: DeviceLink(deviceId, parent, connectionSource)
|
||||
, mSocketLineReader(new SocketLineReader(socket))
|
||||
, mSocketLineReader(nullptr)
|
||||
{
|
||||
connect(mSocketLineReader, SIGNAL(readyRead()),
|
||||
this, SLOT(dataReceived()));
|
||||
reset(socket, connectionSource);
|
||||
}
|
||||
|
||||
void LanDeviceLink::reset(QSslSocket* socket, DeviceLink::ConnectionStarted connectionSource)
|
||||
{
|
||||
//We take ownership of the socket.
|
||||
//When the link provider destroys us,
|
||||
//the socket (and the reader) will be
|
||||
//destroyed as well
|
||||
connect(socket, SIGNAL(disconnected()),
|
||||
this, SLOT(deleteLater()));
|
||||
|
||||
if (mSocketLineReader) {
|
||||
delete mSocketLineReader;
|
||||
}
|
||||
|
||||
mSocketLineReader = new SocketLineReader(socket);
|
||||
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
|
||||
connect(mSocketLineReader, SIGNAL(readyRead()), this, SLOT(dataReceived()));
|
||||
mSocketLineReader->setParent(this);
|
||||
socket->setParent(this);
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ class LanDeviceLink
|
|||
|
||||
public:
|
||||
LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource);
|
||||
void reset(QSslSocket* socket, ConnectionStarted connectionSource);
|
||||
|
||||
virtual QString name() Q_DECL_OVERRIDE;
|
||||
bool sendPackage(NetworkPackage& np) override;
|
||||
|
|
|
@ -433,24 +433,22 @@ void LanLinkProvider::configureSocket(QSslSocket* socket)
|
|||
|
||||
void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin)
|
||||
{
|
||||
|
||||
LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin);
|
||||
connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*)));
|
||||
|
||||
// Socket disconnection will now be handled by LanDeviceLink
|
||||
disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
|
||||
|
||||
//We kill any possible link from this same device
|
||||
QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceLink->deviceId());
|
||||
if (oldLinkIterator != mLinks.end()) {
|
||||
DeviceLink* oldLink = oldLinkIterator.value();
|
||||
disconnect(oldLink, SIGNAL(destroyed(QObject*)),
|
||||
this, SLOT(deviceLinkDestroyed(QObject*)));
|
||||
oldLink->deleteLater();
|
||||
mLinks.erase(oldLinkIterator);
|
||||
DeviceLink* deviceLink;
|
||||
//Do we have a link for this device already?
|
||||
QMap< QString, LanDeviceLink* >::iterator linkIterator = mLinks.find(deviceLink->deviceId());
|
||||
if (linkIterator != mLinks.end()) {
|
||||
deviceLink = linkIterator.value();
|
||||
deviceLink->reset(socket, connectionOrigin);
|
||||
} else {
|
||||
deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin);
|
||||
connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*)));
|
||||
mLinks[deviceId] = deviceLink;
|
||||
}
|
||||
|
||||
mLinks[deviceId] = deviceLink;
|
||||
|
||||
refreshPairingHandler(deviceId);
|
||||
Q_EMIT onConnectionReceived(*receivedPackage, deviceLink);
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ private:
|
|||
const static quint16 port = 1714;
|
||||
quint16 mTcpPort;
|
||||
|
||||
QMap<QString, DeviceLink*> mLinks;
|
||||
QMap<QString, LanDeviceLink*> mLinks;
|
||||
QMap<QString, LanPairingHandler*> mPairingHandlers;
|
||||
|
||||
struct PendingConnect {
|
||||
|
|
Loading…
Reference in a new issue