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