changeset 241:d46111dcc510

Irccd: general cleanup
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2016 17:20:06 +0200
parents b25176b3bb80
children d7f02d0e7166
files lib/irccd/connection.cpp lib/irccd/connection.hpp
diffstat 2 files changed, 69 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/connection.cpp	Wed Aug 17 16:55:25 2016 +0200
+++ b/lib/irccd/connection.cpp	Wed Aug 17 17:20:06 2016 +0200
@@ -34,6 +34,8 @@
 
 class Connection::State {
 public:
+    State() = default;
+    virtual ~State() = default;
     virtual Status status() const noexcept = 0;
     virtual void prepare(Connection &cnt, fd_set &in, fd_set &out) = 0;
     virtual void sync(Connection &cnt, fd_set &in, fd_set &out) = 0;
@@ -46,13 +48,13 @@
 
 class Connection::DisconnectedState : public Connection::State {
 public:
-    Connection::Status status() const noexcept
+    Connection::Status status() const noexcept override
     {
         return Disconnected;
     }
 
-    void prepare(Connection &, fd_set &, fd_set &) {}
-    void sync(Connection &, fd_set &, fd_set &){}
+    void prepare(Connection &, fd_set &, fd_set &) override {}
+    void sync(Connection &, fd_set &, fd_set &) override {}
 };
 
 /*
@@ -75,12 +77,12 @@
         }
     }
 public:
-    Connection::Status status() const noexcept
+    Connection::Status status() const noexcept override
     {
         return Ready;
     }
 
-    void prepare(Connection &cnx, fd_set &in, fd_set &out)
+    void prepare(Connection &cnx, fd_set &in, fd_set &out) override
     {
         FD_SET(cnx.m_socket.handle(), &in);
 
@@ -88,13 +90,13 @@
             FD_SET(cnx.m_socket.handle(), &out);
     }
 
-    void sync(Connection &cnx, fd_set &in, fd_set &out)
+    void sync(Connection &cnx, fd_set &in, fd_set &out) override
     {
         if (FD_ISSET(cnx.m_socket.handle(), &out))
-            cnx.syncOutput();
+            cnx.send();
 
         if (FD_ISSET(cnx.m_socket.handle(), &in))
-            cnx.syncInput();
+            cnx.recv();
 
         std::string msg;
 
@@ -144,7 +146,7 @@
 
     void check(Connection &cnt) noexcept
     {
-        cnt.syncInput();
+        cnt.recv();
 
         auto msg = util::nextNetwork(cnt.m_input);
 
@@ -178,12 +180,12 @@
     }
 
 public:
-    Connection::Status status() const noexcept
+    Connection::Status status() const noexcept override
     {
         return Authenticating;
     }
 
-    void prepare(Connection &cnt, fd_set &in, fd_set &out)
+    void prepare(Connection &cnt, fd_set &in, fd_set &out) override
     {
         switch (m_auth) {
         case Created:
@@ -206,7 +208,7 @@
         }
     }
 
-    void sync(Connection &cnt, fd_set &in, fd_set &out)
+    void sync(Connection &cnt, fd_set &in, fd_set &out) override
     {
         switch (m_auth) {
         case Sending:
@@ -289,19 +291,19 @@
     }
 
 public:
-    Connection::Status status() const noexcept
+    Connection::Status status() const noexcept override
     {
         return Checking;
     }
 
-    void prepare(Connection &cnx, fd_set &in, fd_set &)
+    void prepare(Connection &cnx, fd_set &in, fd_set &) override
     {
         FD_SET(cnx.m_socket.handle(), &in);
     }
 
-    void sync(Connection &cnx, fd_set &, fd_set &)
+    void sync(Connection &cnx, fd_set &, fd_set &) override
     {
-        cnx.syncInput();
+        cnx.recv();
 
         verify(cnx);
     }
@@ -314,17 +316,17 @@
 
 class Connection::ConnectingState : public Connection::State {
 public:
-    Connection::Status status() const noexcept
+    Connection::Status status() const noexcept override
     {
         return Connecting;
     }
 
-    void prepare(Connection &cnx, fd_set &, fd_set &out)
+    void prepare(Connection &cnx, fd_set &, fd_set &out) override
     {
         FD_SET(cnx.m_socket.handle(), &out);
     }
 
-    void sync(Connection &cnx, fd_set &, fd_set &out)
+    void sync(Connection &cnx, fd_set &, fd_set &out) override
     {
         if (!FD_ISSET(cnx.m_socket.handle(), &out))
             return;
@@ -349,7 +351,17 @@
  * ------------------------------------------------------------------
  */
 
-void Connection::syncInput()
+unsigned Connection::recv(char *buffer, unsigned length)
+{
+    return m_socket.recv(buffer, length);
+}
+
+unsigned Connection::send(const char *buffer, unsigned length)
+{
+    return m_socket.send(buffer, length);
+}
+
+void Connection::recv()
 {
     try {
         std::string buffer;
@@ -367,7 +379,7 @@
     }
 }
 
-void Connection::syncOutput()
+void Connection::send()
 {
     try {
         auto ns = send(m_output.data(), m_output.length());
@@ -380,16 +392,6 @@
     }
 }
 
-unsigned Connection::recv(char *buffer, unsigned length)
-{
-    return m_socket.recv(buffer, length);
-}
-
-unsigned Connection::send(const char *buffer, unsigned length)
-{
-    return m_socket.send(buffer, length);
-}
-
 Connection::Connection()
     : m_state(std::make_unique<DisconnectedState>())
 {
--- a/lib/irccd/connection.hpp	Wed Aug 17 16:55:25 2016 +0200
+++ b/lib/irccd/connection.hpp	Wed Aug 17 17:20:06 2016 +0200
@@ -47,9 +47,9 @@
  *
  * Be aware that there are no namespaces for commands, if you plan to use
  * Irccdctl class and you also connect the onMessage signal, irccdctl will also
- * use it. Do not use irccdctl directly if this is a concern.
+ * use it. Do not use Irccdctl directly if this is a concern.
  *
- * The state may change as following.
+ * The state may change and is currently implementing as following:
  *
  *   [o]
  *    |       +----------------------------+
@@ -64,8 +64,6 @@
  *     |              +------------+   +-------+
  *     |                                   |
  *     ------------------------------------+
- *
- * Note: authenticating state is not implemented yet.
  */
 class Connection : public Service {
 public:
@@ -73,11 +71,11 @@
      * \brief The current connection state.
      */
     enum Status {
-        Disconnected,       //!< Socket is closed
-        Connecting,         //!< Connection is in progress
-        Checking,           //!< Connection is checking irccd daemon
-        Authenticating,     //!< Connection is authenticating
-        Ready               //!< Socket is ready for I/O
+        Disconnected,           //!< Socket is closed
+        Connecting,             //!< Connection is in progress
+        Checking,               //!< Connection is checking irccd daemon
+        Authenticating,         //!< Connection is authenticating
+        Ready                   //!< Socket is ready for I/O
     };
 
     /**
@@ -85,16 +83,16 @@
      */
     class Info {
     public:
-        unsigned short major;
-        unsigned short minor;
-        unsigned short patch;
+        unsigned short major;   //!< Major version number
+        unsigned short minor;   //!< Minor version number
+        unsigned short patch;   //!< Patch version
     };
 
     /**
      * onConnect
      * --------------------------------------------------------------
      *
-     * Connection was successfull.
+     * Connection was successful.
      */
     Signal<const Info &> onConnect;
 
@@ -102,7 +100,7 @@
      * onMessage
      * ---------------------------------------------------------------
      *
-     * Upon message.
+     * A message from irccd was received.
      */
     Signal<const nlohmann::json &> onMessage;
 
@@ -150,6 +148,20 @@
      */
     virtual unsigned send(const char *buffer, unsigned length);
 
+    /**
+     * Convenient wrapper around recv().
+     *
+     * Must be used in sync() function.
+     */
+    void recv();
+
+    /**
+     * Convenient wrapper around send().
+     *
+     * Must be used in sync() function.
+     */
+    void send();
+
 public:
     /**
      * Default constructor.
@@ -182,20 +194,6 @@
     }
 
     /**
-     * Convenient wrapper around recv().
-     *
-     * Must be used in sync() function.
-     */
-    void syncInput();
-
-    /**
-     * Convenient wrapper around send().
-     *
-     * Must be used in sync() function.
-     */
-    void syncOutput();
-
-    /**
      * Send an asynchronous request to irccd.
      *
      * \pre json.is_object
@@ -245,12 +243,20 @@
     virtual void connect(const net::Address &address);
 
     /**
-     * \copydoc Service::prepare
+     * Prepare the input and output set according to the current connection
+     * state.
+     *
+     * \param in the input set
+     * \param out the output set
+     * \param max the maximum file descriptor
      */
     void prepare(fd_set &in, fd_set &out, net::Handle &max) override;
 
     /**
-     * \copydoc Service::sync
+     * Do some I/O using the protected recv and send functions.
+     *
+     * \param in the input set
+     * \param out the output set
      */
     void sync(fd_set &in, fd_set &out) override;
 };