#include <controlconnection.h>
Public Types | |
enum | Status { Unset, Disconnected, Disconnecting, Connecting, Connected } |
Signals | |
void | connected () |
void | disconnected () |
void | connectFailed (QString errmsg) |
Public Member Functions | |
ControlConnection (TorEvents *events=0) | |
~ControlConnection () | |
void | connect (const QHostAddress &addr, quint16 port) |
void | cancelConnect () |
void | disconnect () |
bool | isConnected () |
Status | status () |
bool | send (const ControlCommand &cmd, ControlReply &reply, QString *errmsg=0) |
bool | send (const ControlCommand &cmd, QString *errmsg=0) |
Private Slots | |
void | connect () |
void | onReadyRead () |
void | onConnected () |
void | onDisconnected () |
void | onError (QAbstractSocket::SocketError error) |
Private Member Functions | |
void | setStatus (Status status) |
QString | statusString (Status status) |
void | run () |
Private Attributes | |
ControlSocket * | _sock |
TorEvents * | _events |
Status | _status |
QHostAddress | _addr |
quint16 | _port |
QMutex | _connMutex |
QMutex | _recvMutex |
QMutex | _statusMutex |
int | _connectAttempt |
QTimer * | _connectTimer |
QQueue< ReceiveWaiter * > | _recvQueue |
SendCommandEvent::SendWaiter * | _sendWaiter |
Classes | |
class | ReceiveWaiter |
Definition at line 34 of file controlconnection.h.
Control connection status
Definition at line 40 of file controlconnection.h.
ControlConnection::ControlConnection | ( | TorEvents * | events = 0 |
) |
Default constructor.
Definition at line 32 of file controlconnection.cpp.
References _events, _sendWaiter, _sock, _status, and Unset.
ControlConnection::~ControlConnection | ( | ) |
void ControlConnection::connect | ( | const QHostAddress & | addr, | |
quint16 | port | |||
) |
Connect to the specified Tor control interface.
Definition at line 53 of file controlconnection.cpp.
References _addr, _connectAttempt, _port, _sock, Connecting, tc::error(), and setStatus().
Referenced by TorControl::connect().
void ControlConnection::cancelConnect | ( | ) |
Cancels a pending control connection to Tor.
Definition at line 151 of file controlconnection.cpp.
References Disconnected, setStatus(), and tc::warn().
Referenced by TorControl::onStopped().
void ControlConnection::disconnect | ( | ) |
Disconnect from Tor's control interface.
Definition at line 89 of file controlconnection.cpp.
References _connMutex, _sock, Disconnecting, and setStatus().
Referenced by TorControl::disconnect().
bool ControlConnection::isConnected | ( | ) |
Returns true if the control socket is connected to Tor.
Definition at line 160 of file controlconnection.cpp.
References Connected, and status().
Referenced by TorControl::isConnected(), TorControl::isRunning(), and TorControl::stop().
ControlConnection::Status ControlConnection::status | ( | ) |
Returns the status of the control connection.
Definition at line 167 of file controlconnection.cpp.
References _status, and _statusMutex.
Referenced by isConnected(), onError(), and TorControl::onStopped().
bool ControlConnection::send | ( | const ControlCommand & | cmd, | |
ControlReply & | reply, | |||
QString * | errmsg = 0 | |||
) |
Sends a control command to Tor and waits for the reply.
Definition at line 203 of file controlconnection.cpp.
References _recvMutex, _recvQueue, tc::error(), ControlConnection::ReceiveWaiter::getResult(), and ControlCommand::keyword().
Referenced by TorControl::send(), and TorControl::signal().
bool ControlConnection::send | ( | const ControlCommand & | cmd, | |
QString * | errmsg = 0 | |||
) |
Sends a control command to Tor and does not wait for a reply.
Sends a control command to Tor and returns true if the command was sent successfully. Otherwise, returns false and *errmsg (if supplied) will be set.
Definition at line 236 of file controlconnection.cpp.
References _connMutex, _sendWaiter, _sock, err(), SendCommandEvent::SendWaiter::getResult(), ControlSocket::isConnected(), and CustomEventType::SendCommandEvent.
void ControlConnection::connected | ( | ) | [signal] |
Emitted when a control connection has been established.
Referenced by onConnected(), and run().
void ControlConnection::disconnected | ( | ) | [signal] |
Emitted when a control connection has been closed.
Referenced by onDisconnected(), and run().
void ControlConnection::connectFailed | ( | QString | errmsg | ) | [signal] |
Emitted when a control connection fails.
Referenced by onError().
void ControlConnection::connect | ( | ) | [private, slot] |
Connects to Tor's control interface.
Attempt to establish a connection to Tor's control interface. We will try a maximum of MAX_CONNECT_ATTEMPTS, waiting CONNECT_RETRY_DELAY between each attempt, to give slow Tors a chance to finish binding their control port.
Definition at line 76 of file controlconnection.cpp.
References _addr, _connectAttempt, _connMutex, _port, _sock, tc::debug(), and MAX_CONNECT_ATTEMPTS.
Referenced by run().
void ControlConnection::onReadyRead | ( | ) | [private, slot] |
Called when there is data on the control socket.
Definition at line 251 of file controlconnection.cpp.
References _connMutex, _events, _recvMutex, _recvQueue, _sock, tc::debug(), tc::error(), ControlReply::getStatus(), TorEvents::handleEvent(), ControlSocket::readReply(), ControlConnection::ReceiveWaiter::setResult(), and ControlReply::toString().
Referenced by run().
void ControlConnection::onConnected | ( | ) | [private, slot] |
Called when the control socket is connected.
Called when the control socket is connected. This method checks that the control protocol version of the Tor we connected to is at least V1.
Definition at line 100 of file controlconnection.cpp.
References connected(), Connected, and setStatus().
Referenced by run().
void ControlConnection::onDisconnected | ( | ) | [private, slot] |
Called when the control socket is disconnected.
Called when the control socket is disconnected and stops the control thread's event loop.
Definition at line 109 of file controlconnection.cpp.
References disconnected(), Disconnected, and setStatus().
Referenced by run().
void ControlConnection::onError | ( | QAbstractSocket::SocketError | error | ) | [private, slot] |
Called when the control socket encounters an error.
Called when the control socket encounters error.
Definition at line 118 of file controlconnection.cpp.
References _connectAttempt, _connectTimer, CONNECT_RETRY_DELAY, connectFailed(), Connecting, tc::debug(), Disconnected, tc::error(), MAX_CONNECT_ATTEMPTS, setStatus(), status(), ControlSocket::toString(), and tc::warn().
Referenced by run().
void ControlConnection::setStatus | ( | Status | status | ) | [private] |
Sets the control connection status.
Definition at line 192 of file controlconnection.cpp.
References _status, _statusMutex, tc::debug(), and statusString().
Referenced by cancelConnect(), connect(), disconnect(), onConnected(), onDisconnected(), and onError().
QString ControlConnection::statusString | ( | Status | status | ) | [private] |
Returns the string description of status.
Returns a string description of the control Status value status.
Definition at line 176 of file controlconnection.cpp.
References Connected, Connecting, Disconnected, Disconnecting, and Unset.
Referenced by setStatus().
void ControlConnection::run | ( | ) | [private] |
Main thread implementation.
Main thread implementation. Creates and connects a control socket, then spins up an event loop.
Definition at line 287 of file controlconnection.cpp.
References _connectTimer, _connMutex, _recvMutex, _recvQueue, _sendWaiter, _sock, connect(), connected(), tc::debug(), disconnected(), tc::error(), onConnected(), onDisconnected(), onError(), onReadyRead(), ControlConnection::ReceiveWaiter::setResult(), SendCommandEvent::SendWaiter::setResult(), SendCommandEvent::SendWaiter::status(), and SendCommandEvent::SendWaiter::Waiting.
ControlSocket* ControlConnection::_sock [private] |
Socket used to communicate with Tor.
Definition at line 96 of file controlconnection.h.
Referenced by connect(), ControlConnection(), disconnect(), onReadyRead(), run(), and send().
TorEvents* ControlConnection::_events [private] |
Dispatches asynchronous events from Tor.
Definition at line 97 of file controlconnection.h.
Referenced by ControlConnection(), and onReadyRead().
Status ControlConnection::_status [private] |
Status of the control connection.
Definition at line 98 of file controlconnection.h.
Referenced by ControlConnection(), setStatus(), and status().
QHostAddress ControlConnection::_addr [private] |
Address of Tor's control interface.
Definition at line 99 of file controlconnection.h.
Referenced by connect().
quint16 ControlConnection::_port [private] |
Port of Tor's control interface.
Definition at line 100 of file controlconnection.h.
Referenced by connect().
QMutex ControlConnection::_connMutex [private] |
Mutex around the control socket.
Definition at line 101 of file controlconnection.h.
Referenced by connect(), disconnect(), onReadyRead(), run(), and send().
QMutex ControlConnection::_recvMutex [private] |
Mutex around the queue of ReceiveWaiters.
Definition at line 102 of file controlconnection.h.
Referenced by onReadyRead(), run(), and send().
QMutex ControlConnection::_statusMutex [private] |
Mutex around the connection status value.
Definition at line 103 of file controlconnection.h.
Referenced by setStatus(), and status().
int ControlConnection::_connectAttempt [private] |
How many times we've tried to connect to Tor while waiting for Tor to start.
Definition at line 104 of file controlconnection.h.
QTimer* ControlConnection::_connectTimer [private] |
Timer used to delay connect attempts.
Definition at line 106 of file controlconnection.h.
QQueue<ReceiveWaiter *> ControlConnection::_recvQueue [private] |
Objects waiting for a reply.
Definition at line 126 of file controlconnection.h.
Referenced by onReadyRead(), run(), and send().
Definition at line 127 of file controlconnection.h.
Referenced by ControlConnection(), run(), send(), and ~ControlConnection().