Permalink
Browse files

Refs #10311. Port MythSocket code to ReferenceCounter.

  • Loading branch information...
daniel-kristjansson committed Jun 14, 2012
1 parent 0c88729 commit f2a0d748d234ba162b4cd6d2cf8f4bc8b1c4fcac
@@ -473,7 +473,7 @@ bool RemoteGetFileList(QString host, QString path, QStringList* list,
if (sock)
{
ok = sock->SendReceiveStringList(*list);
- sock->DownRef();
+ sock->DecrRef();
}
else
list->clear();
@@ -167,7 +167,7 @@ bool LCD::connectToHost(const QString &lhostname, unsigned int lport)
.arg(count));
if (socket)
- socket->DownRef();
+ socket->DecrRef();
socket = new MythSocket(-1, this);
if (socket->connect(hostname, port))
@@ -736,7 +736,8 @@ LCD::~LCD()
if (socket)
{
- socket->DownRef();
+ socket->DecrRef();
+ socket = NULL;
lcd_ready = false;
}
}
@@ -118,12 +118,12 @@ MythCoreContextPrivate::~MythCoreContextPrivate()
QMutexLocker locker(&m_sockLock);
if (m_serverSock)
{
- m_serverSock->DownRef();
+ m_serverSock->DecrRef();
m_serverSock = NULL;
}
if (m_eventSock)
{
- m_eventSock->DownRef();
+ m_eventSock->DecrRef();
m_eventSock = NULL;
}
@@ -322,7 +322,7 @@ bool MythCoreContext::ConnectToMasterServer(bool blockingClient,
if (!IsBackend() && !d->m_eventSock)
{
- d->m_serverSock->DownRef();
+ d->m_serverSock->DecrRef();
d->m_serverSock = NULL;
QCoreApplication::postEvent(
@@ -386,7 +386,7 @@ MythSocket *MythCoreContext::ConnectCommandSocket(
if (p_proto_mismatch)
*p_proto_mismatch = true;
- serverSock->DownRef();
+ serverSock->DecrRef();
serverSock = NULL;
break;
}
@@ -412,7 +412,7 @@ MythSocket *MythCoreContext::ConnectCommandSocket(
sleepms = WOLsleepTime * 1000;
}
- serverSock->DownRef();
+ serverSock->DecrRef();
serverSock = NULL;
if (!serverSock && (cnt == 1))
@@ -452,50 +452,59 @@ MythSocket *MythCoreContext::ConnectCommandSocket(
MythSocket *MythCoreContext::ConnectEventSocket(const QString &hostname,
int port)
{
- MythSocket *m_eventSock = new MythSocket();
+ MythSocket *eventSock = new MythSocket();
- while (m_eventSock->state() != MythSocket::Idle)
+ while (eventSock->state() != MythSocket::Idle)
{
usleep(5000);
}
// Assume that since we _just_ connected the command socket,
// this one won't need multiple retries to work...
- if (!m_eventSock->connect(hostname, port))
+ if (!eventSock->connect(hostname, port))
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect event "
"socket to master backend");
- m_eventSock->DownRef();
- m_eventSock = NULL;
+ eventSock->DecrRef();
return NULL;
}
- m_eventSock->Lock();
+ eventSock->Lock();
QString str = QString("ANN Monitor %1 %2")
.arg(d->m_localHostname).arg(true);
QStringList strlist(str);
- m_eventSock->writeStringList(strlist);
- if (!m_eventSock->readStringList(strlist) || strlist.empty() ||
+ eventSock->writeStringList(strlist);
+ bool ok = true;
+ if (!eventSock->readStringList(strlist) || strlist.empty() ||
(strlist[0] == "ERROR"))
{
if (!strlist.empty())
- LOG(VB_GENERAL, LOG_ERR, LOC + "Problem connecting "
- "event socket to master backend");
+ {
+ LOG(VB_GENERAL, LOG_ERR, LOC +
+ "Problem connecting event socket to master backend");
+ }
else
- LOG(VB_GENERAL, LOG_ERR, LOC + "Timeout connecting "
- "event socket to master backend");
-
- m_eventSock->DownRef();
- m_eventSock->Unlock();
- m_eventSock = NULL;
- return NULL;
+ {
+ LOG(VB_GENERAL, LOG_ERR, LOC +
+ "Timeout connecting event socket to master backend");
+ }
+ ok = false;
}
- m_eventSock->Unlock();
- m_eventSock->setCallbacks(this);
+ eventSock->Unlock();
+
+ if (ok)
+ {
+ eventSock->setCallbacks(this);
+ }
+ else
+ {
+ eventSock->DecrRef();
+ eventSock = NULL;
+ }
- return m_eventSock;
+ return eventSock;
}
bool MythCoreContext::IsConnectedToMaster(void)
@@ -958,12 +967,12 @@ bool MythCoreContext::SendReceiveStringList(QStringList &strlist,
{
LOG(VB_GENERAL, LOG_NOTICE,
QString("Connection to backend server lost"));
- d->m_serverSock->DownRef();
+ d->m_serverSock->DecrRef();
d->m_serverSock = NULL;
if (d->m_eventSock)
{
- d->m_eventSock->DownRef();
+ d->m_eventSock->DecrRef();
d->m_eventSock = NULL;
}
@@ -995,7 +1004,7 @@ bool MythCoreContext::SendReceiveStringList(QStringList &strlist,
{
if (d->m_serverSock)
{
- d->m_serverSock->DownRef();
+ d->m_serverSock->DecrRef();
d->m_serverSock = NULL;
}
@@ -1123,13 +1132,13 @@ void MythCoreContext::connectionClosed(MythSocket *sock)
QMutexLocker locker(&d->m_sockLock);
if (d->m_serverSock)
{
- d->m_serverSock->DownRef();
+ d->m_serverSock->DecrRef();
d->m_serverSock = NULL;
}
if (d->m_eventSock)
{
- d->m_eventSock->DownRef();
+ d->m_eventSock->DecrRef();
d->m_eventSock = NULL;
}
@@ -266,7 +266,7 @@ bool telnet(const QString &host, int port)
MythSocket *s = new MythSocket();
bool connected = s->connect(host, port);
- s->DownRef();
+ s->DecrRef();
return connected;
}
@@ -41,14 +41,17 @@ MythSocketThread *MythSocket::s_readyread_thread = NULL;
QMap<QString, QHostAddress::SpecialAddress> MythSocket::s_loopback_cache;
-MythSocket::MythSocket(int socket, MythSocketCBs *cb)
- : MSocketDevice(MSocketDevice::Stream), m_cb(cb),
- m_useReadyReadCallback(true),
- m_state(Idle), m_addr(), m_port(0),
- m_ref_count(0), m_notifyread(false), m_expectingreply(false),
- m_isValidated(false), m_isAnnounced(false)
+MythSocket::MythSocket(int socket, MythSocketCBs *cb) :
+ MSocketDevice(MSocketDevice::Stream),
+ ReferenceCounter(QString("MythSocket(%1)").arg(socket)),
+ m_cb(cb), m_useReadyReadCallback(true),
+ m_state(Idle),
+ m_addr(), m_port(0),
+ m_notifyread(false), m_expectingreply(false),
+ m_isValidated(false), m_isAnnounced(false)
{
LOG(VB_SOCKET, LOG_DEBUG, LOC + "new socket");
+
if (socket > -1)
{
setSocket(socket);
@@ -92,35 +95,18 @@ void MythSocket::setCallbacks(MythSocketCBs *cb)
s_readyread_thread->RemoveFromReadyRead(this);
}
-void MythSocket::UpRef(void)
+int MythSocket::DecrRef(void)
{
- QMutexLocker locker(&m_ref_lock);
- m_ref_count++;
- LOG(VB_SOCKET, LOG_DEBUG, LOC + QString("UpRef: %1").arg(m_ref_count));
-}
-
-bool MythSocket::DownRef(void)
-{
- m_ref_lock.lock();
- int ref = --m_ref_count;
- m_ref_lock.unlock();
-
- LOG(VB_SOCKET, LOG_DEBUG, LOC + QString("DownRef: %1").arg(ref));
+ int ref = ReferenceCounter::DecrRef();
- if (m_cb && ref == 0)
+ if (m_cb && ref == 1)
{
m_cb = NULL;
s_readyread_thread->RemoveFromReadyRead(this);
- // thread will downref & delete obj
- return true;
- }
- else if (ref < 0)
- {
- delete this;
- return true;
+ // ready read thread will call DecrRef() & delete obj
}
- return false;
+ return ref;
}
MythSocket::State MythSocket::state(void) const
@@ -5,6 +5,7 @@
#include <QStringList>
#include <QMutex>
+#include "referencecounter.h"
#include "msocketdevice.h"
#include "mythsocket_cb.h"
#include "mythbaseexp.h"
@@ -15,7 +16,8 @@ class QString;
class QHostAddress;
class MythSocketThread;
-class MBASE_PUBLIC MythSocket : public MSocketDevice
+class MBASE_PUBLIC MythSocket :
+ public MSocketDevice, public ReferenceCounter
{
friend class MythSocketThread;
friend class QList<MythSocket*>;
@@ -35,8 +37,7 @@ class MBASE_PUBLIC MythSocket : public MSocketDevice
bool closedByRemote(void);
void deleteLater(void);
- void UpRef(void);
- bool DownRef(void);
+ virtual int DecrRef(void); // ReferenceCounter
State state(void) const;
QString stateToString(void) const { return stateToString(state()); }
@@ -96,10 +97,8 @@ class MBASE_PUBLIC MythSocket : public MSocketDevice
State m_state;
QHostAddress m_addr;
quint16 m_port;
- int m_ref_count;
bool m_notifyread;
- QMutex m_ref_lock;
mutable QMutex m_lock; // externally accessible lock
bool m_expectingreply;
@@ -104,7 +104,7 @@ void MythSocketThread::AddToReadyRead(MythSocket *sock)
}
StartReadyReadThread();
- sock->UpRef();
+ sock->IncrRef();
{
QMutexLocker locker(&m_readyread_lock);
@@ -337,7 +337,7 @@ void MythSocketThread::run(void)
for (it = m_readyread_downref_list.begin();
it != m_readyread_downref_list.end(); ++it)
{
- (*it)->DownRef();
+ (*it)->DecrRef();
}
m_readyread_downref_list.clear();
downref_tm = tm.elapsed();
@@ -42,9 +42,15 @@ RemoteFile::~RemoteFile()
{
Close();
if (controlSock)
- controlSock->DownRef();
+ {
+ controlSock->DecrRef();
+ controlSock = NULL;
+ }
if (sock)
- sock->DownRef();
+ {
+ sock->DecrRef();
+ sock = NULL;
+ }
}
MythSocket *RemoteFile::openSocket(bool control)
@@ -83,7 +89,7 @@ MythSocket *RemoteFile::openSocket(bool control)
{
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Could not connect to server %1:%2") .arg(host).arg(port));
- lsock->DownRef();
+ lsock->DecrRef();
return NULL;
}
@@ -96,7 +102,7 @@ MythSocket *RemoteFile::openSocket(bool control)
{
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Failed validation to server %1:%2").arg(host).arg(port));
- lsock->DownRef();
+ lsock->DecrRef();
return NULL;
}
#endif
@@ -110,7 +116,7 @@ MythSocket *RemoteFile::openSocket(bool control)
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Could not read string list from server %1:%2")
.arg(host).arg(port));
- lsock->DownRef();
+ lsock->DecrRef();
return NULL;
}
}
@@ -159,7 +165,7 @@ MythSocket *RemoteFile::openSocket(bool control)
if (strlist.empty() || strlist[0] == "ERROR")
{
- lsock->DownRef();
+ lsock->DecrRef();
lsock = NULL;
if (strlist.empty())
{
@@ -244,12 +250,12 @@ void RemoteFile::Close(void)
if (sock)
{
- sock->DownRef();
+ sock->DecrRef();
sock = NULL;
}
if (controlSock)
{
- controlSock->DownRef();
+ controlSock->DecrRef();
controlSock = NULL;
}
Oops, something went wrong.

0 comments on commit f2a0d74

Please sign in to comment.