[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [vidalia/alpha] Wrapper class for socket access in ControlSocket class



commit 49c2f16a4ac4776544f076f954e701b2c8081a17
Author: Sebastian Baginski <sebthestampede@xxxxxxxxx>
Date:   Sun May 27 10:51:33 2012 +0200

    Wrapper class for socket access in ControlSocket class
---
 changes/bug5519.2                |    3 +
 src/torcontrol/ControlSocket.cpp |  133 +++++++++++++++++++++++++++----------
 src/torcontrol/ControlSocket.h   |   19 +++---
 3 files changed, 110 insertions(+), 45 deletions(-)

diff --git a/changes/bug5519.2 b/changes/bug5519.2
new file mode 100644
index 0000000..e619fc7
--- /dev/null
+++ b/changes/bug5519.2
@@ -0,0 +1,3 @@
+  Internal cleanups and improvements:
+  o Wrapper class for socket access in ControlSocket
+    class. Fixes ticket 5519.
diff --git a/src/torcontrol/ControlSocket.cpp b/src/torcontrol/ControlSocket.cpp
index fb5daa7..fd5f31e 100644
--- a/src/torcontrol/ControlSocket.cpp
+++ b/src/torcontrol/ControlSocket.cpp
@@ -19,26 +19,92 @@
 
 #include "stringutil.h"
 
+#include <QLocalSocket>
+#include <QTcpSocket>
+
 /** Timeout reads in 250ms. We can set this to a short value because if there
 * isn't any data to read, we want to return anyway. */
 #define READ_TIMEOUT  250
 
 
+/** A simple helper class to wrap the sockets access code. */
+class ControlSocket::SocketWrapper{
+
+public:
+  SocketWrapper(ControlMethod::Method method)
+    :_method(method)
+  {
+    _tcpSocket = new QTcpSocket();
+    _localSocket = new QLocalSocket();
+  }
+
+  ~SocketWrapper()
+  {
+    delete _localSocket;
+    delete _tcpSocket;
+  }
+
+  ControlMethod::Method getMethod() const
+  {
+    return _method;
+  }
+
+  QIODevice * socket() const
+  {
+    if (_method == ControlMethod::Socket)
+      return _localSocket;
+    else
+      return _tcpSocket;
+  }
+
+  bool isConnected() const
+  {
+    if (_method==ControlMethod::Socket)
+      return (_localSocket->isValid() && _localSocket->state() == QLocalSocket::ConnectedState);
+    else
+      return (_tcpSocket->isValid() && _tcpSocket->state() == QAbstractSocket::ConnectedState);
+  }
+
+  void flush()
+  {
+    if (_method==ControlMethod::Socket)
+      _localSocket->flush();
+    else
+      _tcpSocket->flush();
+  }
+
+  void connectToHost(const QHostAddress &address, quint16 port)
+  {
+    _tcpSocket->connectToHost(address, port);
+  }
+
+  void disconnectFromHost()
+  {
+    _tcpSocket->disconnectFromHost();
+  }
+
+  void connectToServer(const QString &name)
+  {
+    _localSocket->connectToServer(name);
+  }
+
+  void disconnectFromServer()
+  {
+    _localSocket->disconnectFromServer();
+  }
+
+private:
+  QTcpSocket *_tcpSocket; /**< Socket used in the connection */
+  QLocalSocket *_localSocket; /**< Socket used in the connection */
+  ControlMethod::Method _method; /**< Connection method. */
+};
+
+
 /** Default constructor. */
 ControlSocket::ControlSocket(ControlMethod::Method method)
 {
-  _tcpSocket = new QTcpSocket();
-  _localSocket = new QLocalSocket();
-  _method = method;
-  switch(_method) {
-    case ControlMethod::Port:
-      _socket = _tcpSocket;
-      break;
-
-    case ControlMethod::Socket:
-      _socket = _localSocket;
-      break;
-  }
+  _sock = new SocketWrapper(method);
+  _socket = _sock->socket();
 
   QObject::connect(_socket, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
   QObject::connect(_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
@@ -47,54 +113,57 @@ ControlSocket::ControlSocket(ControlMethod::Method method)
                    this, SIGNAL(error(QAbstractSocket::SocketError)));
 }
 
+/** Destructor. */
+ControlSocket::~ControlSocket()
+{
+  delete _sock;
+}
+
 /** Returns true if the control socket is connected and ready to send or
  * receive. */
 bool
-ControlSocket::isConnected()
+ControlSocket::isConnected() const
 {
-  switch(_method) {
-    case ControlMethod::Port:
-      return (_tcpSocket->isValid() && _tcpSocket->state() == QAbstractSocket::ConnectedState);
-      break;
+  return _sock->isConnected();
+}
 
-    default:
-    case ControlMethod::Socket:
-      return (_localSocket->isValid() && _localSocket->state() == QLocalSocket::ConnectedState);
-      break;
-  }
+ControlMethod::Method
+ControlSocket::getMethod() const
+{
+  return _sock->getMethod();
 }
 
 /** Connects to address:port */
 void
 ControlSocket::connectToHost(const QHostAddress &address, quint16 port)
 {
-  _tcpSocket->connectToHost(address, port);
+  _sock->connectToHost(address, port);
 }
 
 /** Disconnects from host */
 void
 ControlSocket::disconnectFromHost()
 {
-  _tcpSocket->disconnectFromHost();
+  _sock->disconnectFromHost();
 }
 
 /** Connects to a unix socket file */
 void
 ControlSocket::connectToServer(const QString &name)
 {
-  _localSocket->connectToServer(name);
+  _sock->connectToServer(name);
 }
 
 /** Disconnects from the socket */
 void
 ControlSocket::disconnectFromServer()
 {
-  _localSocket->disconnectFromServer();
+  _sock->disconnectFromServer();
 }
 
 /** Interface to QTcpSocket::canReadLine */
 bool
-ControlSocket::canReadLine()
+ControlSocket::canReadLine() const
 {
   return _socket->canReadLine();
 }
@@ -174,15 +243,7 @@ ControlSocket::sendCommand(ControlCommand cmd, QString *errmsg)
     return err(errmsg, tr("Error sending control command. [%1]")
                                             .arg(_socket->errorString()));
   }
-  switch(_method) {
-    case ControlMethod::Port:
-      _tcpSocket->flush();
-      break;
-
-    case ControlMethod::Socket:
-      _localSocket->flush();
-      break;
-  }
+  _sock->flush();
   return true;
 }
 
diff --git a/src/torcontrol/ControlSocket.h b/src/torcontrol/ControlSocket.h
index 27e65b7..77ed282 100644
--- a/src/torcontrol/ControlSocket.h
+++ b/src/torcontrol/ControlSocket.h
@@ -20,9 +20,8 @@
 #include "ControlReply.h"
 #include "ControlMethod.h"
 
-#include <QtCore>
-#include <QLocalSocket>
-#include <QTcpSocket>
+#include <QObject>
+#include <QAbstractSocket>
 
 class ControlSocket : public QObject
 {
@@ -31,6 +30,8 @@ class ControlSocket : public QObject
 public:
   /** Default constructor. */
   ControlSocket(ControlMethod::Method method = ControlMethod::Port);
+  /** Destructor. */
+  ~ControlSocket();
 
   /** Send a command to Tor */
   bool sendCommand(ControlCommand cmd, QString *errmsg = 0);
@@ -39,9 +40,9 @@ public:
 
   /** Returns true if the control socket is connected and ready to send or
    * receive. */
-  bool isConnected();
+  bool isConnected() const;
   /** Interface to each socket's canReadLine implementation */
-  bool canReadLine();
+  bool canReadLine() const;
 
   void connectToHost(const QHostAddress &address, quint16 port);
   /** Disconnects from host */
@@ -51,7 +52,7 @@ public:
   /** Disconnects from the socket */
   void disconnectFromServer();
 
-  ControlMethod::Method getMethod() { return _method; }
+  ControlMethod::Method getMethod() const;
 
   /** Returns the string description of <b>error</b>. */
   static QString toString(const QAbstractSocket::SocketError error);
@@ -73,10 +74,10 @@ protected:
   bool readLine(QString &line, QString *errmsg = 0);
 
 private:
-  QTcpSocket *_tcpSocket; /**< Socket used in the connection */
-  QLocalSocket *_localSocket; /**< Socket used in the connection */
+  class SocketWrapper;
+
+  SocketWrapper * _sock;  /**< Helper object to convenient use both sockets */
   QIODevice *_socket; /**< Abstract pointer to transparently use both sockets */
-  ControlMethod::Method _method;
 };
 
 #endif



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits