Re-use landevicelink objects instead of re-creating one each time

This commit is contained in:
Albert Vaca 2015-12-10 17:11:48 -08:00
parent 77facdbcfd
commit 2e4c3762e9
4 changed files with 27 additions and 19 deletions

View file

@ -29,17 +29,26 @@
LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource) LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource)
: DeviceLink(deviceId, parent, connectionSource) : DeviceLink(deviceId, parent, connectionSource)
, mSocketLineReader(new SocketLineReader(socket)) , mSocketLineReader(nullptr)
{ {
connect(mSocketLineReader, SIGNAL(readyRead()), reset(socket, connectionSource);
this, SLOT(dataReceived())); }
void LanDeviceLink::reset(QSslSocket* socket, DeviceLink::ConnectionStarted connectionSource)
{
//We take ownership of the socket. //We take ownership of the socket.
//When the link provider destroys us, //When the link provider destroys us,
//the socket (and the reader) will be //the socket (and the reader) will be
//destroyed as well //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); mSocketLineReader->setParent(this);
socket->setParent(this); socket->setParent(this);
} }

View file

@ -37,6 +37,7 @@ class LanDeviceLink
public: public:
LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource); LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource);
void reset(QSslSocket* socket, ConnectionStarted connectionSource);
virtual QString name() Q_DECL_OVERRIDE; virtual QString name() Q_DECL_OVERRIDE;
bool sendPackage(NetworkPackage& np) override; bool sendPackage(NetworkPackage& np) override;

View file

@ -433,24 +433,22 @@ void LanLinkProvider::configureSocket(QSslSocket* socket)
void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin) 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 // Socket disconnection will now be handled by LanDeviceLink
disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
//We kill any possible link from this same device DeviceLink* deviceLink;
QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceLink->deviceId()); //Do we have a link for this device already?
if (oldLinkIterator != mLinks.end()) { QMap< QString, LanDeviceLink* >::iterator linkIterator = mLinks.find(deviceLink->deviceId());
DeviceLink* oldLink = oldLinkIterator.value(); if (linkIterator != mLinks.end()) {
disconnect(oldLink, SIGNAL(destroyed(QObject*)), deviceLink = linkIterator.value();
this, SLOT(deviceLinkDestroyed(QObject*))); deviceLink->reset(socket, connectionOrigin);
oldLink->deleteLater(); } else {
mLinks.erase(oldLinkIterator); deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin);
connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*)));
mLinks[deviceId] = deviceLink;
} }
mLinks[deviceId] = deviceLink;
refreshPairingHandler(deviceId); refreshPairingHandler(deviceId);
Q_EMIT onConnectionReceived(*receivedPackage, deviceLink); Q_EMIT onConnectionReceived(*receivedPackage, deviceLink);

View file

@ -74,7 +74,7 @@ private:
const static quint16 port = 1714; const static quint16 port = 1714;
quint16 mTcpPort; quint16 mTcpPort;
QMap<QString, DeviceLink*> mLinks; QMap<QString, LanDeviceLink*> mLinks;
QMap<QString, LanPairingHandler*> mPairingHandlers; QMap<QString, LanPairingHandler*> mPairingHandlers;
struct PendingConnect { struct PendingConnect {