[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r2580: Take out a workaround we had for Qt 4.0 that ended up breaki (in vidalia: . trunk/src/torcontrol)
Author: edmanm
Date: 2008-05-19 18:46:14 -0400 (Mon, 19 May 2008)
New Revision: 2580
Modified:
vidalia/
vidalia/trunk/src/torcontrol/controlconnection.cpp
vidalia/trunk/src/torcontrol/controlconnection.h
Log:
r319@thebe: edmanm | 2008-05-19 18:41:42 -0400
Take out a workaround we had for Qt 4.0 that ended up breaking on Qt 4.4. We
no longer support anything older than Qt 4.2 anyway. Also simplify the
threading a little bit.
Property changes on: vidalia
___________________________________________________________________
svk:merge ticket from /local/vidalia [r319] on 45a62a8a-8088-484c-baad-c7b3e776dd32
Modified: vidalia/trunk/src/torcontrol/controlconnection.cpp
===================================================================
--- vidalia/trunk/src/torcontrol/controlconnection.cpp 2008-05-19 17:16:57 UTC (rev 2579)
+++ vidalia/trunk/src/torcontrol/controlconnection.cpp 2008-05-19 22:46:14 UTC (rev 2580)
@@ -198,67 +198,39 @@
bool
ControlConnection::send(ControlCommand cmd, ControlReply &reply, QString *errmsg)
{
+ ReceiveWaiter w;
bool result = false;
QString errstr;
-
- _recvMutex.lock();
- if (send(cmd, &errstr)) {
+
+ _connMutex.lock();
+ if (_sock->sendCommand(cmd, &errstr)) {
/* Create and enqueue a new receive waiter */
- ReceiveWaiter *w = new ReceiveWaiter();
- _recvQueue.enqueue(w);
- _recvMutex.unlock();
-
- /* Wait for and get the result, clean up, and return */
- result = w->getResult(&reply, &errstr);
- if (!result)
- tc::error("Failed to receive control reply: %1").arg(errstr);
- delete w;
+ _recvQueue.enqueue(&w);
+ _connMutex.unlock();
} else {
+ _connMutex.unlock();
tc::error("Failed to send control command (%1): %2").arg(cmd.keyword())
.arg(errstr);
- _recvMutex.unlock();
+ return err(errmsg, errstr);
}
- if (!result && errmsg)
- *errmsg = errstr;
+ /* Wait for and get the result, clean up, and return */
+ result = w.getResult(&reply, &errstr);
+ if (!result) {
+ tc::error("Failed to receive control reply: %1").arg(errstr);
+ if (errmsg)
+ *errmsg = errstr;
+ }
return result;
}
-/** Sends a control command to Tor. If the current thread is not the thread
- * that actually owns the socket, the command will be pushed to the correct
- * thread before sending. */
+/** Sends a control command to Tor and returns without waiting for the
+ * response. */
bool
ControlConnection::send(ControlCommand cmd, QString *errmsg)
{
- QThread *socketThread;
- bool result;
-
- /* Check for a valid and connected socket */
- _connMutex.lock();
- if (!_sock || _status != Connected) {
- tc::warn("Unable to send control command '%1' when socket status is '%2'")
- .arg(cmd.keyword()).arg(_status);
- _connMutex.unlock();
- return err(errmsg, tr("Control socket is not connected."));
- }
- socketThread = _sock->thread();
- _connMutex.unlock();
-
- if (socketThread != QThread::currentThread()) {
- /* Push the message to the correct thread before sending */
- SendWaiter *w = new SendWaiter();
- QCoreApplication::postEvent(_sock, new SendCommandEvent(cmd, w));
-
- /* Wait for the result, clean up, and return */
- result = w->getResult(errmsg);
- delete w;
- } else {
- /* Send the command directly on the socket */
- _connMutex.lock();
- result = _sock->sendCommand(cmd, errmsg);
- _connMutex.unlock();
- }
- return result;
+ QMutexLocker locker(&_connMutex);
+ return _sock->sendCommand(cmd, errmsg);
}
/** Called when there is data on the control socket. */
@@ -283,12 +255,10 @@
/* Response to a previous command */
tc::debug("Control Reply: %1").arg(reply.toString());
- _recvMutex.lock();
if (!_recvQueue.isEmpty()) {
waiter = _recvQueue.dequeue();
waiter->setResult(true, reply);
}
- _recvMutex.unlock();
}
} else {
tc::error("Unable to read control reply: %1").arg(errmsg);
@@ -296,40 +266,6 @@
}
}
-/** Catches events for the control socket. */
-bool
-ControlConnection::eventFilter(QObject *obj, QEvent *event)
-{
- if (event->type() == CustomEventType::SendCommandEvent) {
- /* Get the control command and waiter from the event */
- SendCommandEvent *sce = (SendCommandEvent *)event;
- SendWaiter *w = sce->waiter();
- QString errmsg;
- bool result;
-
- /* Send the command, if the socket exists. */
- _connMutex.lock();
- if (_sock) {
- result = _sock->sendCommand(sce->command(), &errmsg);
- } else {
- result = false;
- errmsg = tr("Control socket is not connected");
- }
- _connMutex.unlock();
-
- /* If there is someone waiting for a result, give them one. */
- if (w) {
- w->setResult(result, errmsg);
- }
-
- /* Stop processing this event */
- sce->accept();
- return true;
- }
- /* Pass other events on */
- return QObject::eventFilter(obj, event);
-}
-
/** Main thread implementation. Creates and connects a control socket, then
* spins up an event loop. */
void
@@ -353,7 +289,6 @@
QObject::connect(_connectTimer, SIGNAL(timeout()), this, SLOT(connect()),
Qt::DirectConnection);
- _sock->installEventFilter(this);
_connMutex.unlock();
/* Attempt to connect to Tor */
@@ -364,21 +299,18 @@
/* Clean up the socket */
_connMutex.lock();
- _sock->removeEventFilter(this);
_sock->disconnect(this);
delete _sock;
delete _connectTimer;
_sock = 0;
- _connMutex.unlock();
/* If there are any messages waiting for a response, clear them. */
- _recvMutex.lock();
while (!_recvQueue.isEmpty()) {
ReceiveWaiter *w = _recvQueue.dequeue();
w->setResult(false, ControlReply(),
tr("Control socket is not connected."));
}
- _recvMutex.unlock();
+ _connMutex.unlock();
}
@@ -421,38 +353,3 @@
_waitCond.wakeAll();
}
-
-/*
- * ControlConnection::SendWaiter
- */
-/** Sets the result of the send operation. */
-void
-ControlConnection::SendWaiter::setResult(bool success, QString errmsg)
-{
- _mutex.lock();
- _status = (success ? Success : Failed);
- _errmsg = errmsg;
- _mutex.unlock();
- _waitCond.wakeAll();
-}
-
-/** Waits for and gets the result of the send operation. */
-bool
-ControlConnection::SendWaiter::getResult(QString *errmsg)
-{
- forever {
- _mutex.lock();
- if (_status == Waiting) {
- _waitCond.wait(&_mutex);
- _mutex.unlock();
- } else {
- _mutex.unlock();
- break;
- }
- }
- if (errmsg) {
- *errmsg = _errmsg;
- }
- return (_status == Success);
-}
-
Modified: vidalia/trunk/src/torcontrol/controlconnection.h
===================================================================
--- vidalia/trunk/src/torcontrol/controlconnection.h 2008-05-19 17:16:57 UTC (rev 2579)
+++ vidalia/trunk/src/torcontrol/controlconnection.h 2008-05-19 22:46:14 UTC (rev 2580)
@@ -72,10 +72,6 @@
/** Emitted when a control connection fails. */
void connectFailed(QString errmsg);
-protected:
- /** Catches events for the control socket. */
- bool eventFilter(QObject *obj, QEvent *event);
-
private slots:
/** Connects to Tor's control interface. */
void connect();
@@ -102,7 +98,6 @@
QHostAddress _addr; /**< Address of Tor's control interface. */
quint16 _port; /**< Port of Tor's control interface. */
QMutex _connMutex; /**< Mutex around the control socket. */
- QMutex _recvMutex; /**< Mutex around the queue of ReceiveWaiters. */
QMutex _statusMutex; /**< Mutex around the connection status value. */
int _connectAttempt; /**< How many times we've tried to connect to Tor while
waiting for Tor to start. */
@@ -127,39 +122,6 @@
QString _errmsg; /**< Error message if the reply fails. */
};
QQueue<ReceiveWaiter *> _recvQueue; /**< Objects waiting for a reply. */
-
- /** Object used to wait for the result of a send operation. */
- class SendWaiter {
- public:
- /** Default constructor. */
- SendWaiter() { _status = Waiting; }
- /** Sets the result of the send operation. */
- void setResult(bool success, QString errmsg = QString());
- /** Waits for and gets the result of the send operation. */
- bool getResult(QString *errmsg = 0);
- private:
- /** Status of the send waiter. */
- enum SenderStatus { Waiting, Failed, Success } _status;
- QMutex _mutex; /**< Mutex around the wait condition. */
- QWaitCondition _waitCond; /**< Waits for the send to complete. */
- QString _errmsg; /**< Error message if the send fails. */
- };
-
- /** Private event used to push a control command to the socket's thread */
- class SendCommandEvent : public QEvent {
- public:
- /** Constructor. */
- SendCommandEvent(ControlCommand cmd, SendWaiter *waiter = 0)
- : QEvent((QEvent::Type)CustomEventType::SendCommandEvent)
- { _cmd = cmd; _waiter = waiter; }
- /** Returns the control command to send to Tor. */
- ControlCommand command() { return _cmd; }
- /** Returns a waiter (if any) for the result of this send. */
- SendWaiter* waiter() { return _waiter; }
- private:
- ControlCommand _cmd; /**< Command to send to Tor. */
- SendWaiter* _waiter; /**< Waiter for the result of this event. */
- };
};
#endif