Replace socket_handler::fill_buffer() recursion with loop (#8389)
[libt2n] / src / socket_wrapper.cpp
index d153716..50ec75f 100644 (file)
@@ -1,21 +1,24 @@
-/***************************************************************************
- *   Copyright (C) 2008 by Gerd v. Egidy                                   *
- *   gve@intra2net.com                                                     *
- *                                                                         *
- *   This library is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU Lesser General Public License version   *
- *   2.1 as published by the Free Software Foundation.                     *
- *                                                                         *
- *   This library is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU Lesser General Public License for more details.                   *
- *                                                                         *
- *   You should have received a copy of the GNU Lesser General Public      *
- *   License along with this program; if not, write to the                 *
- *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
- ***************************************************************************/
+/*
+Copyright (C) 2008 by Intra2net AG - Gerd v. Egidy
+
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
 
 #include <functional>
 #include <string>
 namespace libt2n
 {
 
+/// set logging for coming and current connections
+void BasicSocketWrapper::set_logging(std::ostream *_logstream, log_level_values _log_level)
+{
+    ConnectionWrapper::set_logging(_logstream,_log_level);
+
+    if (connection_established())
+        get_connection()->set_logging(_logstream,_log_level);
+}
+
+
+/// return active connection, create new tcp or unix connection if not existing
 client_connection* BasicSocketWrapper::get_connection(void)
 {
     if (c.get() == NULL)
@@ -40,7 +54,9 @@ client_connection* BasicSocketWrapper::get_connection(void)
     return c.get();
 }
 
-void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+/// try to reconnect max_retries time if we encounter a t2n_communication_error 
+/// during execution of the command
+bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
 {
     int tries=0;
 
@@ -55,7 +71,7 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v
 
             f();
             // we were successful
-            return;
+            return true;
         }
         catch(t2n_connect_error &e)
         {
@@ -71,51 +87,59 @@ void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< v
 
             // otherwise ignore the exception and reconnect in the next iteration
         }
-        catch(...)
-        {
-            throw;
-        }
 
         tries++;
     }
+
+    return false;
 }
 
+/// return active connection, return a dummy-connection if we can't establish one
 client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
 {
-    client_connection* tmp;
+    client_connection* tmp=BasicSocketWrapper::get_connection();
 
-    try
-    {
-        tmp=BasicSocketWrapper::get_connection();
-    }
-    catch(t2n_connect_error &e)
+    if (tmp->is_closed())
     {
-        // TODO: return some kind of dummy connection
-    }
-    catch(...)
-    {
-        throw;
+        // throw away the closed connection...
+        c.reset();
+        // ...return the dummy one instead
+        tmp=&dc;
     }
 
     return tmp;
 }
 
-void ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
+/// try to execute the command, may ignore the command if server not available
+bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
 {
-    // TODO: check for dummy connection and try to establish a real one
-
-    try
-    {
-        ReconnectSocketWrapper::handle(stubBase,f);
-    }
-    catch(t2n_communication_error &e)
+    if (!connection_established())
     {
-        // ignore
+        // dummy connection is in place: try to establish a real one
+        client_connection* tmp=get_connection();
+
+        if (tmp != &dc)
+        {
+            // success! we've got a real connection
+            stubBase->replace_connection(tmp);
+        }
     }
-    catch(...)
+
+    // only try to handle the call if we've got a real connection
+    if (connection_established())
     {
-        throw;
+        try
+        {
+            ReconnectSocketWrapper::handle(stubBase,f);
+            return true;
+        }
+        catch(t2n_communication_error &e)
+        {
+            // ignore
+        }
     }
+
+    return false;
 }
 
 }