mail[Wesnoth-commits] r44092 - in /trunk/src: network_ana.cpp network_manager_ana.cpp network_manager_ana.hpp


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

Header


Content

Posted by billybiset on July 10, 2010 - 21:53:
Author: billynux
Date: Sat Jul 10 21:53:45 2010
New Revision: 44092

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44092&view=rev
Log:
Fixed a sending bug that enables to connect to the add-on server and cleaned 
up the read_from mthod in ana.

Modified:
    trunk/src/network_ana.cpp
    trunk/src/network_manager_ana.cpp
    trunk/src/network_manager_ana.hpp

Modified: trunk/src/network_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_ana.cpp?rev=44092&r1=44091&r2=44092&view=diff
==============================================================================
--- trunk/src/network_ana.cpp (original)
+++ trunk/src/network_ana.cpp Sat Jul 10 21:53:45 2010
@@ -274,23 +274,13 @@
     {
         // comment next debug msg: too much output
 //         std::cout << "DEBUG: Trying to read from connection in " << 
timeout << " milliseconds.\n";
-        ana::detail::read_buffer buffer;
-
-        network::connection read_id = ana_manager.read_from( connection_num, 
buffer, timeout );
-
-        if ( buffer == NULL || read_id == 0) // check timeout and return 0, 
or throw if error occured
+        network::connection read_id = ana_manager.read_from( connection_num, 
cfg, timeout );
+
+        if ( read_id == 0 ) // TODO: check timeout and return 0, or throw if 
error occured
             return 0;
         else
         {
-            std::cout << "DEBUG: Read a buffer of size " << buffer->size() 
<< "\n";
-
-            std::istringstream input( buffer->string() );
-
-            read_gz(cfg, input);
-
             std::cout << cfg;
-
-            cfg.debug();
 
             if (cfg.empty())
                 std::cout << "Empty buffer.\n";

Modified: trunk/src/network_manager_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.cpp?rev=44092&r1=44091&r2=44092&view=diff
==============================================================================
--- trunk/src/network_manager_ana.cpp (original)
+++ trunk/src/network_manager_ana.cpp Sat Jul 10 21:53:45 2010
@@ -86,6 +86,8 @@
 {
     timeout_called_mutex_.lock();
     timeout_called_mutex_.unlock();
+    handler_mutex_.lock();
+    handler_mutex_.unlock();
 }
 
 void ana_receive_handler::wait_completion(ana::detail::timed_sender* 
component, size_t timeout_ms )
@@ -684,17 +686,28 @@
     return components_.size(); // TODO:check if this is the intention, 
guessing not
 }
 
+void ana_network_manager::compress_config( const config& cfg, 
std::ostringstream& out)
+{
+    boost::iostreams::filtering_stream<boost::iostreams::output> filter;
+    filter.push(boost::iostreams::gzip_compressor());
+    filter.push(out);
+    write(filter, cfg);
+}
+
+void ana_network_manager::read_config( const ana::detail::read_buffer& 
buffer, config& cfg)
+{
+    std::istringstream input( buffer->string() );
+
+    read_gz(cfg, input);
+}
+
 size_t ana_network_manager::send_all( const config& cfg, bool zipped )
 {
     std::cout << "DEBUG: Sending to everybody. " << (zipped ? 
"Zipped":"Raw") << "\n";
 
+
     std::ostringstream out;
-    {
-        boost::iostreams::filtering_stream<boost::iostreams::output> filter;
-        filter.push(boost::iostreams::gzip_compressor());
-        filter.push(out);
-        write(filter, cfg);
-    }
+    compress_config(cfg,out);
 
     std::set<ana_component*>::iterator it;
 
@@ -720,28 +733,36 @@
     return out.str().size();
 }
 
-size_t ana_network_manager::send( network::connection connection_num , const 
config& cfg, bool zipped )
+size_t ana_network_manager::send( network::connection connection_num , const 
config& cfg, bool /*zipped*/ )
 {
     std::cout << "DEBUG: Single send...\n";
     ana::net_id id( connection_num );
 
-    std::stringstream out;
-    config_writer cfg_writer(out, zipped);
-    cfg_writer.write(cfg);
-
-    std::set<ana_component*>::iterator it;
-
-    for (it = components_.begin(); it != components_.end(); ++it)
-    {
-        if ( (*it)->is_server() )
-        {
-            ana_send_handler handler( *it, out.str().size() );
-            (*it)->server()->send_one( id, ana::buffer( out.str() ), 
&handler, ana::ZERO_COPY);
-
-            handler.wait_completion();
-        }
-    }
-    return out.str().size();
+    std::ostringstream out;
+    compress_config(cfg, out );
+
+    ana_component_set::iterator it;
+
+    it = std::find_if( components_.begin(), components_.end(),
+                    boost::bind(&ana_component::get_id, _1) == id );
+
+    if ( it != components_.end())
+    {
+        ana_send_handler handler( *it, out.str().size() );
+
+        if ( (*it)->is_client() )
+            (*it)->client()->send( ana::buffer( out.str() ), &handler, 
ana::ZERO_COPY);
+        else
+            (*it)->server()->send_all( ana::buffer( out.str() ), &handler, 
ana::ZERO_COPY);
+        handler.wait_completion();
+
+        if ( handler.error() )
+            return 0;
+        else
+            return out.str().size();
+    }
+    else
+        return 0;
 }
 
 void ana_network_manager::send_all_except(const config& cfg, 
network::connection connection_num)
@@ -770,60 +791,61 @@
         }
     }
 }
+network::connection ana_network_manager::read_from_ready_buffer( const 
ana_component_set::iterator& it, config& cfg)
+{
+    read_config( (*it)->wait_for_element(), cfg);
+
+    return (*it)->get_wesnoth_id();
+}
+
+network::connection ana_network_manager::read_from( const 
ana_component_set::iterator& it,
+                                                    config&             cfg,
+                                                    size_t              
timeout_ms)
+{
+    if (  (*it)->new_buffer_ready() )
+        return read_from_ready_buffer( it, cfg );
+    else if (timeout_ms == 0 )
+        return 0;
+    else
+    {
+        ana::client* const client = (*it)->client(); // TODO: server support
+
+        ana_receive_handler handler;
+        client->set_listener_handler( &handler );
+
+        handler.wait_completion( client, timeout_ms );
+
+        client->set_listener_handler( this );
+
+        if ( handler.error() )
+            return 0;
+        else
+        {
+            read_config( handler.buffer(), cfg);
+            return (*it)->get_wesnoth_id();
+        }
+    }
+}
 
 network::connection ana_network_manager::read_from( network::connection 
connection_num,
-                                                    
ana::detail::read_buffer& buffer,
-                                                    size_t timeout_ms)
-{
-    std::set<ana_component*>::iterator it;
+                                                    config&             cfg,
+                                                    size_t              
timeout_ms)
+{
+    if ( components_.empty() )
+        throw std::runtime_error("Trying to read but nothing was running.");
+
+    ana_component_set::iterator it;
 
     if ( connection_num == 0 )
     {
-        if ( components_.empty() )
-            throw std::runtime_error("Trying to read but nothing was 
running.");
-
         if ( components_.size() == 1 )
-        {
-            it = components_.begin();
-
-            if (  (*it)->new_buffer_ready() )
-            {
-                buffer = (*it)->wait_for_element();
-                return (*it)->get_wesnoth_id();
-            }
-            else if (timeout_ms == 0 )
-                return 0;
-            else
-            {
-                ana::client* const client = (*it)->client();
-
-                ana_receive_handler handler;
-                client->set_listener_handler( &handler );
-
-                handler.wait_completion( client, timeout_ms );
-
-                client->set_listener_handler( this );
-
-                if ( handler.error() )
-                    return 0;
-                else
-                {
-                    buffer = handler.buffer();
-                    return (*it)->get_wesnoth_id();
-                }
-            }
-        }
+            return read_from( components_.begin(), cfg, timeout_ms );
         else
         {
             //Check first if there is an available buffer
             for (it = components_.begin(); it != components_.end(); ++it)
-            {
                 if (  (*it)->new_buffer_ready() )
-                {
-                    buffer = (*it)->wait_for_element();
-                    return (*it)->get_wesnoth_id();
-                }
-            }
+                    return read_from_ready_buffer( it, cfg );
 
             // If no timeout was requested, return
             if (timeout_ms == 0 )
@@ -846,21 +868,17 @@
             {
                 // For concurrency reasons, this checks if the old handler 
was used before the wait operation
                 for (it = components_.begin(); it != components_.end(); ++it)
-                {
                     if (  (*it)->new_buffer_ready() )
-                    {
-                        buffer = (*it)->wait_for_element();
-                        return (*it)->get_wesnoth_id();
-                    }
-                }
+                        return read_from_ready_buffer( it, cfg );
+
+                // So nothing was read:
                 return 0;
             }
             else
             {
-                buffer = handler.buffer();
+                read_config( handler.buffer(), cfg);
                 return handler.get_wesnoth_id();
             }
-//             throw std::runtime_error("Global Buffer Queue here?");
         }
     }
     else
@@ -874,10 +892,7 @@
                         boost::bind(&ana_component::get_id, _1) == id );
 
         if ( it != components_.end())
-        {
-            buffer = (*it)->wait_for_element();
-            return (*it)->get_wesnoth_id();
-        }
+            return read_from(it, cfg, timeout_ms);
         else
             throw std::runtime_error("Trying a network read from an invalid 
component id.");
     }

Modified: trunk/src/network_manager_ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.hpp?rev=44092&r1=44091&r2=44092&view=diff
==============================================================================
--- trunk/src/network_manager_ana.hpp (original)
+++ trunk/src/network_manager_ana.hpp Sat Jul 10 21:53:45 2010
@@ -404,10 +404,41 @@
 
         void send_all_except(const config& cfg, network::connection 
connection_num);
 
-        /** Read a message from a given component. */
+        /**
+         * Read a message from a given component or from every one.
+         *
+         * @param connection_num : The id of the network component, 0 to 
read from every component.
+         * @param cfg : The config input to place the read data.
+         * @param timeout_ms : Amount of milliseconds to wait for the data.
+         *
+         * @returns The network::connection number of the component that 
read the data or 0 if an error occurred.
+         */
         network::connection read_from( network::connection connection_num,
-                                       ana::detail::read_buffer& buffer,
-                                       size_t timeout_ms = 0 );
+                                       config&             cfg,
+                                       size_t              timeout_ms = 0 );
+
+        /**
+         * Read a message from a given component or from every one.
+         *
+         * @param it : The ana component to read from.
+         * @param cfg : The config input to place the read data.
+         * @param timeout_ms : Amount of milliseconds to wait for the data.
+         *
+         * @returns The network::connection number of the component that 
read the data or 0 if an error occurred.
+         */
+        network::connection read_from( const ana_component_set::iterator& it,
+                                       config&             cfg,
+                                       size_t              timeout_ms = 0 );
+
+        /**
+         * Read a message from a given component or from every one.
+         *
+         * @param it : The ana component to read from.
+         * @param cfg : The config input to place the read data.
+         *
+         * @returns The network::connection number of the component that 
read the data or 0 if an error occurred.
+         */
+        network::connection read_from_ready_buffer( const 
ana_component_set::iterator& it, config& cfg);
 
         /** Retrieve upload statistics on a given component. */
         network::statistics get_send_stats(network::connection handle);
@@ -424,6 +455,23 @@
 
         virtual void handle_disconnect(ana::error_code /*error_code*/, 
ana::net_id client);
 
+        /**
+         * Pack a config object to an outpt stream using compression.
+         *
+         * @param cfg : The config object as input.
+         * @param out : The output stream as output.
+         */
+        void compress_config( const config& cfg, std::ostringstream& out);
+
+        /**
+         * Read a config object from an input buffer.
+         *
+         * @param buffer : The buffer with the compressed stream as input.
+         * @param cfg : The config object as output.
+         */
+        void read_config( const ana::detail::read_buffer& buffer, config& 
cfg);
+
+        // Attributes
         ana::timer*                connect_timer_;
         ana_component_set          components_;
 




Related Messages


Powered by MHonArc, Updated Sat Jul 10 23:20:08 2010