Vidalia  0.2.17
ControlSocket.h
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file ControlSocket.h
00013 ** \brief Socket used to connect to Tor's control interface
00014 */
00015 
00016 #ifndef _CONTROLSOCKET_H
00017 #define _CONTROLSOCKET_H
00018 
00019 #include "ControlCommand.h"
00020 #include "ControlReply.h"
00021 #include "ControlMethod.h"
00022 
00023 #include <QtCore>
00024 #include <QLocalSocket>
00025 #include <QTcpSocket>
00026 
00027 class ControlSocket : public QObject
00028 {
00029   Q_OBJECT
00030 
00031 public:
00032   /** Default constructor. */
00033   ControlSocket(ControlMethod::Method method = ControlMethod::Port);
00034 
00035   /** Send a command to Tor */
00036   bool sendCommand(ControlCommand cmd, QString *errmsg = 0);
00037   /** Read a response from Tor */
00038   bool readReply(ControlReply &reply, QString *errmsg = 0);
00039 
00040   /** Returns true if the control socket is connected and ready to send or
00041    * receive. */
00042   bool isConnected();
00043   /** Interface to each socket's canReadLine implementation */
00044   bool canReadLine();
00045   
00046   void connectToHost(const QHostAddress &address, quint16 port);
00047   /** Disconnects from host */
00048   void disconnectFromHost();
00049   /** Connects to a unix socket file */
00050   void connectToServer(const QString &name);
00051   /** Disconnects from the socket */
00052   void disconnectFromServer();
00053 
00054   ControlMethod::Method getMethod() { return _method; }
00055   
00056   /** Returns the string description of <b>error</b>. */
00057   static QString toString(const QAbstractSocket::SocketError error);
00058 
00059 signals:
00060   /** Interface to the signals from each socket used */
00061   void readyRead();
00062   void disconnected();
00063   void connected();
00064   void error(QAbstractSocket::SocketError);
00065 
00066 protected:
00067   /** Processes custom events sent to this object (e.g. SendCommandEvents)
00068    * from other threads. */
00069   void customEvent(QEvent *event);
00070   /** Reads line data off the socket in chunks. */
00071   bool readLineData(QString &line, QString *errmsg = 0);
00072   /** Reads a line of data from the socket (blocking) */
00073   bool readLine(QString &line, QString *errmsg = 0);
00074 
00075 private:
00076   QTcpSocket *_tcpSocket; /**< Socket used in the connection */
00077   QLocalSocket *_localSocket; /**< Socket used in the connection */
00078   QIODevice *_socket; /**< Abstract pointer to transparently use both sockets */
00079   ControlMethod::Method _method;
00080 };
00081 
00082 #endif
00083