Vidalia  0.3.1
ControlSocket.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file ControlSocket.h
13 ** \brief Socket used to connect to Tor's control interface
14 */
15 
16 #ifndef _CONTROLSOCKET_H
17 #define _CONTROLSOCKET_H
18 
19 #include "ControlCommand.h"
20 #include "ControlReply.h"
21 #include "ControlMethod.h"
22 
23 #include <QtCore>
24 #include <QLocalSocket>
25 #include <QTcpSocket>
26 
27 class ControlSocket : public QObject
28 {
29  Q_OBJECT
30 
31 public:
32  /** Default constructor. */
34 
35  /** Send a command to Tor */
36  bool sendCommand(ControlCommand cmd, QString *errmsg = 0);
37  /** Read a response from Tor */
38  bool readReply(ControlReply &reply, QString *errmsg = 0);
39 
40  /** Returns true if the control socket is connected and ready to send or
41  * receive. */
42  bool isConnected();
43  /** Interface to each socket's canReadLine implementation */
44  bool canReadLine();
45 
46  void connectToHost(const QHostAddress &address, quint16 port);
47  /** Disconnects from host */
48  void disconnectFromHost();
49  /** Connects to a unix socket file */
50  void connectToServer(const QString &name);
51  /** Disconnects from the socket */
52  void disconnectFromServer();
53 
55 
56  /** Returns the string description of <b>error</b>. */
57  static QString toString(const QAbstractSocket::SocketError error);
58 
59 signals:
60  /** Interface to the signals from each socket used */
61  void readyRead();
62  void disconnected();
63  void connected();
64  void error(QAbstractSocket::SocketError);
65 
66 protected:
67  /** Processes custom events sent to this object (e.g. SendCommandEvents)
68  * from other threads. */
69  void customEvent(QEvent *event);
70  /** Reads line data off the socket in chunks. */
71  bool readLineData(QString &line, QString *errmsg = 0);
72  /** Reads a line of data from the socket (blocking) */
73  bool readLine(QString &line, QString *errmsg = 0);
74 
75 private:
76  QTcpSocket *_tcpSocket; /**< Socket used in the connection */
77  QLocalSocket *_localSocket; /**< Socket used in the connection */
78  QIODevice *_socket; /**< Abstract pointer to transparently use both sockets */
80 };
81 
82 #endif
83 
ControlSocket::connected
void connected()
ControlSocket::sendCommand
bool sendCommand(ControlCommand cmd, QString *errmsg=0)
Definition: ControlSocket.cpp:162
ControlSocket::readLineData
bool readLineData(QString &line, QString *errmsg=0)
Definition: ControlSocket.cpp:253
ControlSocket::error
void error(QAbstractSocket::SocketError)
ControlSocket::ControlSocket
ControlSocket(ControlMethod::Method method=ControlMethod::Port)
Definition: ControlSocket.cpp:28
ControlSocket::canReadLine
bool canReadLine()
Definition: ControlSocket.cpp:97
ControlSocket::connectToServer
void connectToServer(const QString &name)
Definition: ControlSocket.cpp:83
ControlSocket::_tcpSocket
QTcpSocket * _tcpSocket
Definition: ControlSocket.h:76
ControlMethod.h
ControlCommand
Definition: ControlCommand.h:22
ControlSocket::disconnectFromHost
void disconnectFromHost()
Definition: ControlSocket.cpp:76
ControlReply
Definition: ControlReply.h:24
ControlSocket::customEvent
void customEvent(QEvent *event)
Definition: ControlSocket.cpp:139
ControlSocket::_localSocket
QLocalSocket * _localSocket
Definition: ControlSocket.h:77
ControlMethod::Port
@ Port
Definition: ControlMethod.h:24
ControlSocket
Definition: ControlSocket.h:27
ControlSocket::getMethod
ControlMethod::Method getMethod()
Definition: ControlSocket.h:54
ControlSocket::disconnectFromServer
void disconnectFromServer()
Definition: ControlSocket.cpp:90
ControlCommand.h
ControlSocket::readReply
bool readReply(ControlReply &reply, QString *errmsg=0)
Definition: ControlSocket.cpp:203
ControlMethod::Method
Method
Definition: ControlMethod.h:24
ControlSocket::isConnected
bool isConnected()
Definition: ControlSocket.cpp:53
ControlSocket::_socket
QIODevice * _socket
Definition: ControlSocket.h:78
ControlSocket::readyRead
void readyRead()
ControlSocket::connectToHost
void connectToHost(const QHostAddress &address, quint16 port)
Definition: ControlSocket.cpp:69
ControlReply.h
ControlSocket::disconnected
void disconnected()
ControlSocket::readLine
bool readLine(QString &line, QString *errmsg=0)
Definition: ControlSocket.cpp:274
ControlSocket::_method
ControlMethod::Method _method
Definition: ControlSocket.h:79
ControlSocket::toString
static QString toString(const QAbstractSocket::SocketError error)
Definition: ControlSocket.cpp:104