Changeset 2838 for vidalia/trunk/src
- Timestamp:
- 07/06/08 18:43:48 (5 months ago)
- Location:
- vidalia/trunk/src/torcontrol
- Files:
-
- 5 modified
-
CMakeLists.txt (modified) (1 diff)
-
controlconnection.cpp (modified) (6 diffs)
-
controlconnection.h (modified) (3 diffs)
-
controlsocket.cpp (modified) (4 diffs)
-
controlsocket.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
vidalia/trunk/src/torcontrol/CMakeLists.txt
r2780 r2838 29 29 routerdescriptor.cpp 30 30 routerstatus.cpp 31 sendcommandevent.cpp 31 32 serverstatusevent.cpp 32 33 stream.cpp -
vidalia/trunk/src/torcontrol/controlconnection.cpp
r2695 r2838 35 35 _status = Unset; 36 36 _sock = 0; 37 _sendWaiter = new SendCommandEvent::SendWaiter(); 37 38 } 38 39 … … 42 43 /* Exit the event loop */ 43 44 exit(); 44 /* Wait for the thread to finish .*/45 /* Wait for the thread to finish */ 45 46 wait(); 47 /* Clean up after the send waiter */ 48 delete _sendWaiter; 46 49 } 47 50 … … 201 204 ControlReply &reply, QString *errmsg) 202 205 { 203 ReceiveWaiter w;204 206 bool result = false; 205 207 QString errstr; 206 207 _connMutex.lock(); 208 if (!_sock) { 209 _connMutex.unlock(); 210 return err(errmsg, tr("Control socket is not connected.")); 211 } 212 if (_sock->sendCommand(cmd, &errstr)) { 208 209 _recvMutex.lock(); 210 if (send(cmd, &errstr)) { 213 211 /* Create and enqueue a new receive waiter */ 214 _recvQueue.enqueue(&w); 215 _connMutex.unlock(); 212 ReceiveWaiter *w = new ReceiveWaiter(); 213 _recvQueue.enqueue(w); 214 _recvMutex.unlock(); 215 216 /* Wait for and get the result, clean up, and return */ 217 result = w->getResult(&reply, &errstr); 218 if (!result) 219 tc::error("Failed to receive control reply: %1").arg(errstr); 220 delete w; 216 221 } else { 217 _connMutex.unlock();218 222 tc::error("Failed to send control command (%1): %2").arg(cmd.keyword()) 219 223 .arg(errstr); 220 return err(errmsg, errstr); 221 } 222 223 /* Wait for and get the result, clean up, and return */ 224 result = w.getResult(&reply, &errstr); 225 if (!result) { 226 tc::error("Failed to receive control reply: %1").arg(errstr); 227 if (errmsg) 228 *errmsg = errstr; 229 } 224 _recvMutex.unlock(); 225 } 226 227 if (!result && errmsg) 228 *errmsg = errstr; 230 229 return result; 231 230 } 232 231 233 /** Sends a control command to Tor and returns without waiting for the 234 * response. */ 232 /** Sends a control command to Tor and returns true if the command was sent 233 * successfully. Otherwise, returns false and <b>*errmsg</b> (if supplied) 234 * will be set. */ 235 235 bool 236 236 ControlConnection::send(const ControlCommand &cmd, QString *errmsg) 237 237 { 238 QMutexLocker locker(&_connMutex); 239 if (!_sock) 238 _connMutex.lock(); 239 if (!_sock || !_sock->isConnected()) { 240 _connMutex.unlock(); 240 241 return err(errmsg, tr("Control socket is not connected.")); 241 return _sock->sendCommand(cmd, errmsg); 242 } 243 QCoreApplication::postEvent(_sock, new SendCommandEvent(cmd, _sendWaiter)); 244 _connMutex.unlock(); 245 246 return _sendWaiter->getResult(errmsg); 242 247 } 243 248 … … 264 269 tc::debug("Control Reply: %1").arg(reply.toString()); 265 270 271 _recvMutex.lock(); 266 272 if (!_recvQueue.isEmpty()) { 267 273 waiter = _recvQueue.dequeue(); 268 274 waiter->setResult(true, reply); 269 275 } 276 _recvMutex.unlock(); 270 277 } 271 278 } else { … … 312 319 delete _connectTimer; 313 320 _sock = 0; 321 _connMutex.unlock(); 314 322 315 323 /* If there are any messages waiting for a response, clear them. */ 324 if (_sendWaiter->status() == SendCommandEvent::SendWaiter::Waiting) 325 _sendWaiter->setResult(false, tr("Control socket is not connected.")); 326 327 _recvMutex.lock(); 316 328 while (!_recvQueue.isEmpty()) { 317 329 ReceiveWaiter *w = _recvQueue.dequeue(); … … 319 331 tr("Control socket is not connected.")); 320 332 } 321 _ connMutex.unlock();333 _recvMutex.unlock(); 322 334 } 323 335 -
vidalia/trunk/src/torcontrol/controlconnection.h
r2581 r2838 29 29 #include "controlsocket.h" 30 30 #include "torevents.h" 31 #include "sendcommandevent.h" 31 32 32 33 … … 99 100 quint16 _port; /**< Port of Tor's control interface. */ 100 101 QMutex _connMutex; /**< Mutex around the control socket. */ 102 QMutex _recvMutex; /**< Mutex around the queue of ReceiveWaiters. */ 101 103 QMutex _statusMutex; /**< Mutex around the connection status value. */ 102 104 int _connectAttempt; /**< How many times we've tried to connect to Tor while … … 123 125 }; 124 126 QQueue<ReceiveWaiter *> _recvQueue; /**< Objects waiting for a reply. */ 127 SendCommandEvent::SendWaiter* _sendWaiter; 125 128 }; 126 129 -
vidalia/trunk/src/torcontrol/controlsocket.cpp
r2695 r2838 19 19 20 20 #include "controlsocket.h" 21 #include "sendcommandevent.h" 21 22 22 23 /** Timeout reads in 250ms. We can set this to a short value because if there … … 36 37 { 37 38 return (isValid() && state() == QAbstractSocket::ConnectedState); 39 } 40 41 /** Processes custom events sent to this object (e.g. SendCommandEvents) from 42 * other threads. */ 43 void 44 ControlSocket::customEvent(QEvent *event) 45 { 46 if (event->type() == CustomEventType::SendCommandEvent) { 47 SendCommandEvent *sce = dynamic_cast<SendCommandEvent *>(event); 48 if (! sce) 49 return; 50 51 QString errmsg; 52 bool result = sendCommand(sce->command(), &errmsg); 53 if (sce->waiter()) 54 sce->waiter()->setResult(result, errmsg); 55 sce->accept(); 56 } 38 57 } 39 58 … … 47 66 bool 48 67 ControlSocket::sendCommand(ControlCommand cmd, QString *errmsg) 49 { 68 { 50 69 if (!isConnected()) { 51 70 return err(errmsg, tr("Control socket is not connected.")); … … 126 145 } 127 146 128 /* The implementation below is (loosely) based on the Java control library from Tor */ 147 /* The implementation below is (loosely) based on the Java control library 148 * from Tor */ 129 149 do { 130 150 /* Read a line of the response */ -
vidalia/trunk/src/torcontrol/controlsocket.h
r2362 r2838 45 45 46 46 protected: 47 /** Processes custom events sent to this object (e.g. SendCommandEvents) 48 * from other threads. */ 49 void customEvent(QEvent *event); 47 50 /** Reads line data off the socket in chunks. */ 48 51 bool readLineData(QString &line, QString *errmsg = 0);
