mail[Wesnoth-commits] r44774 - in /trunk/src/ana: api/ src/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by billybiset on July 27, 2010 - 22:06:
Author: billynux
Date: Tue Jul 27 22:06:34 2010
New Revision: 44774

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44774&view=rev
Log:
Added a connection attempt timeout feature to ANA.

Modified:
    trunk/src/ana/api/ana.hpp
    trunk/src/ana/api/timers.hpp
    trunk/src/ana/src/asio_client.cpp
    trunk/src/ana/src/asio_client.hpp
    trunk/src/ana/src/asio_proxy_connection.cpp
    trunk/src/ana/src/asio_proxy_connection.hpp

Modified: trunk/src/ana/api/ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/api/ana.hpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/api/ana.hpp (original)
+++ trunk/src/ana/api/ana.hpp Tue Jul 27 22:06:34 2010
@@ -589,7 +589,7 @@
          *
          * \sa connection_handler
          */
-        virtual operation_id connect( connection_handler* ) = 0;
+        virtual void connect( connection_handler* ) = 0;
 
         /**
          * Attempt a connection to the server through a proxy.
@@ -600,11 +600,11 @@
          *
          * \sa connection_handler
          */
-        virtual operation_id connect_through_proxy(std::string proxy_address,
-                                                   std::string proxy_port,
-                                                   connection_handler* 
handler,
-                                                   std::string user_name = 
"",
-                                                   std::string password = 
"") = 0;
+        virtual void connect_through_proxy(std::string proxy_address,
+                                           std::string proxy_port,
+                                           connection_handler* handler,
+                                           std::string user_name = "",
+                                           std::string password = "") = 0;
 
         /** Run the client listener, starts listening for incoming messages. 
*/
         virtual void run() = 0;
@@ -641,6 +641,18 @@
          */
         virtual void cancel_pending( )                             = 0;
 
+        /**
+         * Set a timeout value for connection attempts. If attempting to 
connect through a proxy
+         * this value will be used once for the whole connection attempt.
+         *
+         * You should use the time lapse constructors in namespace ana::time:
+         *      - ana::time::seconds(10)
+         *      - ana::time::minutes(1)
+         *
+         * @sa ana::time
+         */
+        virtual void set_connect_timeout( size_t ms ) = 0;
+
         /** Standard destructor. */
         virtual ~client() {}
     };

Modified: trunk/src/ana/api/timers.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/api/timers.hpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/api/timers.hpp (original)
+++ trunk/src/ana/api/timers.hpp Tue Jul 27 22:06:34 2010
@@ -105,10 +105,17 @@
      */
     enum timeout_policy
     {
-        NoTimeouts       /** Don't use timers in any operation.              
  */,
-        FixedTime        /** Use timers with a fixed time for every 
operation. */,
-        TimePerKilobyte  /** Use timers, calculating the necessary time from
-        the size of the buffer that is to be sent.        */
+        /** Don't use timers in any operation.                */
+        NoTimeouts,
+
+        /** Use timers with a fixed time for every operation. */
+        FixedTime,
+
+        /**
+         * Use timers, calculating the necessary time from
+         * the size of the buffer that is to be sent.
+         */
+        TimePerKilobyte
     };
 
     /** @name Timers

Modified: trunk/src/ana/src/asio_client.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.cpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.cpp (original)
+++ trunk/src/ana/src/asio_client.cpp Tue Jul 27 22:06:34 2010
@@ -49,6 +49,7 @@
     socket_(io_service_),
     address_(address),
     port_(pt),
+    connect_timeout_ms_( 0 ),
     proxy_( NULL ),
     use_proxy_( false ),
     stats_collector_( NULL ),
@@ -74,8 +75,11 @@
 }
 
 void asio_client::handle_proxy_connection(const boost::system::error_code& 
ec,
-                                          ana::connection_handler* handler)
-{
+                                          ana::connection_handler*         
handler,
+                                          ana::timer*                      
timer)
+{
+    delete timer;
+
     handler->handle_connect( ec, 0 );
 
     if ( ! ec )
@@ -91,10 +95,13 @@
 
 void asio_client::handle_connect(const boost::system::error_code& ec,
                                  tcp::resolver::iterator          
endpoint_iterator,
-                                 ana::connection_handler*         handler )
+                                 ana::connection_handler*         handler,
+                                 ana::timer*                      timer)
 {
     if ( ! ec )
     {
+        delete timer; // will call handle_timeout with ana::operation_aborted
+
         handler->handle_connect( ec, 0 );
 
         if ( ana::client::header_mode() )
@@ -113,13 +120,46 @@
             socket_.async_connect(endpoint,
                                   boost::bind(&asio_client::handle_connect, 
this,
                                               
boost::asio::placeholders::error, ++endpoint_iterator,
-                                              handler));
+                                              handler, timer));
         }
     }
 }
 
-ana::operation_id asio_client::connect( ana::connection_handler* handler )
-{
+void asio_client::handle_timeout(const boost::system::error_code& ec,
+                                 ana::connection_handler*         handler,
+                                 ana::timer*                      timer)
+{
+    if ( ec != ana::operation_aborted ) // Timed out before canceling
+    {
+        handler->handle_connect( ana::timeout_error, 0 );
+        cancel_pending();
+    }
+}
+
+ana::timer* asio_client::start_connection_timer(ana::connection_handler* 
handler)
+{
+    if ( connect_timeout_ms_ == 0 )
+        return NULL;
+    else
+    {
+        ana::timer* running_timer( NULL );
+
+        ana::client* sender = static_cast<ana::client*>(this);
+
+        running_timer = sender->create_timer();
+
+        running_timer->wait( connect_timeout_ms_,
+                            boost::bind(&asio_client::handle_timeout, this,
+                                        boost::asio::placeholders::error, 
handler,
+                                        running_timer ) );
+        return running_timer;
+    }
+}
+
+void asio_client::connect( ana::connection_handler* handler )
+{
+    ana::timer* running_timer = start_connection_timer(handler);
+
     try
     {
         tcp::resolver resolver(io_service_);
@@ -130,22 +170,22 @@
         socket_.async_connect(endpoint,
                               boost::bind(&asio_client::handle_connect, this,
                                           boost::asio::placeholders::error, 
++endpoint_iterator,
-                                          handler));
+                                          handler, running_timer));
     }
     catch (const std::exception& e)
     {
         handler->handle_connect( 
boost::system::error_code(1,boost::system::system_category ), 0 );
     }
-
-    return ++last_valid_operation_id_;
-}
-
-ana::operation_id asio_client::connect_through_proxy(std::string             
 proxy_address,
-                                                     std::string             
 proxy_port,
-                                                     
ana::connection_handler* handler,
-                                                     std::string             
 user_name,
-                                                     std::string             
 password)
-{
+}
+
+void asio_client::connect_through_proxy(std::string              
proxy_address,
+                                        std::string              proxy_port,
+                                        ana::connection_handler* handler,
+                                        std::string              user_name,
+                                        std::string              password)
+{
+    ana::timer* running_timer = start_connection_timer(handler);
+
     use_proxy_ = true;
 
     proxy_information proxy_info;
@@ -155,11 +195,9 @@
     proxy_info.user_name     = user_name;
     proxy_info.password      = password;
 
-    proxy_ = new proxy_connection( socket_, proxy_info, address_, port_);
+    proxy_ = new proxy_connection( socket_, proxy_info, address_, port_, 
running_timer);
 
     proxy_->connect( this, handler );
-
-    return ++last_valid_operation_id_;
 }
 
 ana::operation_id asio_client::send(boost::asio::const_buffer buffer,
@@ -208,6 +246,10 @@
     socket_.cancel();
 }
 
+void asio_client::set_connect_timeout( size_t ms )
+{
+    connect_timeout_ms_ = ms;
+}
 
 void asio_client::disconnect_listener()
 {

Modified: trunk/src/ana/src/asio_client.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.hpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.hpp (original)
+++ trunk/src/ana/src/asio_client.hpp Tue Jul 27 22:06:34 2010
@@ -63,13 +63,13 @@
 
     private:
 
-        virtual ana::operation_id connect( ana::connection_handler* );
+        virtual void connect( ana::connection_handler* );
 
-        virtual ana::operation_id connect_through_proxy(std::string          
    proxy_address,
-                                                        std::string          
    proxy_port,
-                                                        
ana::connection_handler* handler,
-                                                        std::string          
    user_name = "",
-                                                        std::string          
    password = "");
+        virtual void connect_through_proxy(std::string              
proxy_address,
+                                           std::string              
proxy_port,
+                                           ana::connection_handler* handler,
+                                           std::string              
user_name = "",
+                                           std::string              password 
 = "");
 
         virtual void run();
 
@@ -82,7 +82,8 @@
         virtual void disconnect_listener();
 
         virtual void handle_proxy_connection(const 
boost::system::error_code&,
-                                             ana::connection_handler*);
+                                             ana::connection_handler*,
+                                             ana::timer*);
 
         virtual tcp::socket& socket();
 
@@ -99,10 +100,18 @@
 
         virtual void cancel_pending( );
 
+        virtual void set_connect_timeout( size_t ms );
 
         void handle_connect(const boost::system::error_code& ec,
                             tcp::resolver::iterator endpoint_iterator,
-                            ana::connection_handler* );
+                            ana::connection_handler*,
+                            ana::timer*);
+
+        void handle_timeout(const boost::system::error_code& ec,
+                            ana::connection_handler*,
+                            ana::timer*);
+
+        ana::timer* start_connection_timer(ana::connection_handler*);
 
         /* Override, as per -Weffc++ */
         asio_client(const asio_client& other);
@@ -118,6 +127,8 @@
         std::string               address_;
         ana::port                 port_;
 
+        size_t                    connect_timeout_ms_;
+
         proxy_connection*         proxy_;
         bool                      use_proxy_;
 

Modified: trunk/src/ana/src/asio_proxy_connection.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_proxy_connection.cpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/src/asio_proxy_connection.cpp (original)
+++ trunk/src/ana/src/asio_proxy_connection.cpp Tue Jul 27 22:06:34 2010
@@ -34,16 +34,19 @@
 
 #include "asio_proxy_connection.hpp"
 
-proxy_connection::proxy_connection(tcp::socket& socket,
+proxy_connection::proxy_connection(tcp::socket&      socket,
                                    proxy_information pi,
-                                   ana::address address,
-                                   ana::port port) :
+                                   ana::address      address,
+                                   ana::port         port,
+                                   ana::timer*       timer) :
     socket_(socket),
     proxy_info_(pi),
     address_(address),
     port_(port),
     manager_( NULL ),
-    conn_handler_( NULL )
+    conn_handler_( NULL ),
+    authenticating_( false ),
+    timer_( timer )
 {
 }
 
@@ -133,11 +136,11 @@
     delete buf;
 
     if ( ec )
-        manager_->handle_proxy_connection( ec, conn_handler_ );
+        manager_->handle_proxy_connection( ec, conn_handler_, timer_ );
     else
     {
         if ( finds( ss.str(), "200 Connection established" ) )
-            manager_->handle_proxy_connection( ec, conn_handler_ );
+            manager_->handle_proxy_connection( ec, conn_handler_, timer_ );
         else
         {
             if ( ( ! authenticating_ ) && finds( ss.str(), "407 Proxy 
Authentication Required" ) )
@@ -151,14 +154,14 @@
                 }
                 else //TODO: digest authentication support here
                     manager_->handle_proxy_connection(
-                        
boost::system::error_code(1,boost::system::system_category ),
-                        conn_handler_);
+                              
boost::system::error_code(1,boost::system::system_category ),
+                                                      conn_handler_, timer_);
 
             }
             else //Can't connect, wrong password or wasn't offered the 
possibility to authenticate
                 manager_->handle_proxy_connection(
                     
boost::system::error_code(1,boost::system::system_category ),
-                    conn_handler_);
+                    conn_handler_, timer_);
         }
     }
 }
@@ -196,7 +199,7 @@
     else
     {
         if ( endpoint_iterator == tcp::resolver::iterator() ) // could not 
connect to proxy
-            manager_->handle_proxy_connection( ec, conn_handler_ );
+            manager_->handle_proxy_connection( ec, conn_handler_, timer_ );
         else
         {
             //retry
@@ -229,7 +232,7 @@
     {
         manager_->handle_proxy_connection(
             boost::system::error_code(1,boost::system::system_category),
-            conn_handler_ );
+            conn_handler_, timer_ );
     }
 }
 

Modified: trunk/src/ana/src/asio_proxy_connection.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_proxy_connection.hpp?rev=44774&r1=44773&r2=44774&view=diff
==============================================================================
--- trunk/src/ana/src/asio_proxy_connection.hpp (original)
+++ trunk/src/ana/src/asio_proxy_connection.hpp Tue Jul 27 22:06:34 2010
@@ -46,7 +46,8 @@
        virtual ~proxy_connection_manager() {}
 
     virtual void handle_proxy_connection(const boost::system::error_code&,
-                                         ana::connection_handler* )        = 
0;
+                                         ana::connection_handler*,
+                                         ana::timer*)                      = 
0;
 };
 
 struct proxy_information
@@ -71,7 +72,8 @@
         proxy_connection(tcp::socket&      socket,
                          proxy_information pi,
                          ana::address      address,
-                         ana::port         port);
+                         ana::port         port,
+                         ana::timer*       timer);
 
         void connect( proxy_connection_manager* manager, 
ana::connection_handler* handler );
 
@@ -106,6 +108,7 @@
         ana::connection_handler*  conn_handler_;
 
         bool                      authenticating_;
+        ana::timer*               timer_;
 };
 
 #endif




Related Messages


Powered by MHonArc, Updated Tue Jul 27 22:20:18 2010