mail[Wesnoth-commits] r44160 - in /trunk/src: ./ ana/src/


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

Header


Content

Posted by billybiset on July 14, 2010 - 09:02:
Author: billynux
Date: Wed Jul 14 09:02:50 2010
New Revision: 44160

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44160&view=rev
Log:
Fixed a bug in the ana implementation of the server. I was able to run a 
server with ana and play a game between two old clients (with SDLnet). Still 
some bugs present when trying to connect a ana client.

Modified:
    trunk/src/ana/src/asio_sender.cpp
    trunk/src/ana/src/asio_sender.hpp
    trunk/src/ana/src/asio_server.cpp
    trunk/src/ana/src/asio_server.hpp
    trunk/src/network_ana.cpp
    trunk/src/network_manager_ana.cpp
    trunk/src/network_manager_ana.hpp

Modified: trunk/src/ana/src/asio_sender.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_sender.cpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/ana/src/asio_sender.cpp (original)
+++ trunk/src/ana/src/asio_sender.cpp Wed Jul 14 09:02:50 2010
@@ -65,10 +65,15 @@
             (*output_stream) << size;
 
             //write the header first in a separate operation, then send the 
full buffer
-            boost::asio::async_write(socket, boost::asio::buffer( 
output_stream->str() ),
+            socket.async_write_some( boost::asio::buffer( 
output_stream->str() ),
+                                     
boost::bind(&asio_sender::handle_sent_header,this,
+                                                 
boost::asio::placeholders::error, output_stream,
+                                                 &socket, buffer, handler, 
running_timer, _2 ));
+
+/*            boost::asio::async_write(socket, boost::asio::buffer( 
output_stream->str() ),
                                         
boost::bind(&asio_sender::handle_sent_header,this,
                                                     
boost::asio::placeholders::error, output_stream,
-                                                    &socket, buffer, 
handler, running_timer));
+                                                    &socket, buffer, 
handler, running_timer));*/
         }
     }
     catch(std::exception& e)
@@ -84,9 +89,13 @@
                                      tcp::socket*                     socket,
                                      ana::detail::shared_buffer       buffer,
                                      ana::send_handler*               
handler,
-                                     ana::timer*                      
running_timer)
+                                     ana::timer*                      
running_timer,
+                                     size_t                           
bytes_sent)
 {
     delete bos;
+
+    if (bytes_sent != sizeof(uint32_t) )
+        throw std::runtime_error("Couldn't send header.");
 
     if ( ec )
         handle_send(ec, handler, running_timer);

Modified: trunk/src/ana/src/asio_sender.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_sender.hpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/ana/src/asio_sender.hpp (original)
+++ trunk/src/ana/src/asio_sender.hpp Wed Jul 14 09:02:50 2010
@@ -51,15 +51,16 @@
                                 tcp::socket*,
                                 ana::detail::shared_buffer,
                                 ana::send_handler*,
-                                ana::timer*);
+                                ana::timer*,
+                                size_t);
 
         void handle_partial_send( ana::detail::shared_buffer,
                                   const boost::system::error_code&,
                                   tcp::socket*,
                                   ana::send_handler*,
                                   ana::timer*,
-                                  size_t accumulated,
-                                  size_t last_msg_size);
+                                  size_t,
+                                  size_t);
 
         void handle_send(const boost::system::error_code&,
                          ana::send_handler*,

Modified: trunk/src/ana/src/asio_server.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.cpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.cpp (original)
+++ trunk/src/ana/src/asio_server.cpp Wed Jul 14 09:02:50 2010
@@ -45,6 +45,7 @@
 
 asio_server::asio_server() :
     io_service_(),
+    work_( io_service_ ),
     io_thread_(),
     acceptor_( NULL ),
     client_proxies_(),
@@ -171,6 +172,8 @@
 {
     listening_ = true;
     listener_  = listener;
+    for (std::list<client_proxy*>::iterator it( client_proxies_.begin() ); 
it != client_proxies_.end(); ++it)
+        (*it)->set_listener_handler( listener_ );
 }
 
 void asio_server::send_one(net_id id,boost::asio::const_buffer buffer, 
send_handler* handler, send_type copy_buffer)

Modified: trunk/src/ana/src/asio_server.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.hpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.hpp (original)
+++ trunk/src/ana/src/asio_server.hpp Wed Jul 14 09:02:50 2010
@@ -143,16 +143,17 @@
 
         void async_accept( ana::connection_handler* );
 
-        boost::asio::io_service      io_service_;
-        boost::thread                io_thread_;
-        std::auto_ptr<tcp::acceptor> acceptor_;
-        std::list<client_proxy*>     client_proxies_;
-        bool                         listening_;
-        ana::listener_handler*       listener_;
-        ana::connection_handler*     connection_handler_;
-        asio_client_proxy*           last_client_proxy_;
+        boost::asio::io_service       io_service_;
+        boost::asio::io_service::work work_;
+        boost::thread                 io_thread_;
+        std::auto_ptr<tcp::acceptor>  acceptor_;
+        std::list<client_proxy*>      client_proxies_;
+        bool                          listening_;
+        ana::listener_handler*        listener_;
+        ana::connection_handler*      connection_handler_;
+        asio_client_proxy*            last_client_proxy_;
 
-        ana::stats_collector*        stats_collector_;
+        ana::stats_collector*         stats_collector_;
 };
 
 #endif

Modified: trunk/src/network_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_ana.cpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/network_ana.cpp (original)
+++ trunk/src/network_ana.cpp Wed Jul 14 09:02:50 2010
@@ -269,10 +269,11 @@
     connection receive_data(config&           cfg,
                             connection        connection_num,
                             unsigned int      timeout,
-                            bandwidth_in_ptr* /*bandwidth_in*/)
-    {
-        // comment next debug msg: too much output
-//         std::cout << "DEBUG: Trying to read from connection in " << 
timeout << " milliseconds.\n";
+                            bandwidth_in_ptr* bandwidth_in)
+    {
+        //TODO: temporary fix
+        *bandwidth_in = bandwidth_in_ptr( new network::bandwidth_in(4) );
+
         network::connection read_id = ana_manager.read_from( connection_num, 
cfg, timeout );
 
         if ( read_id != 0 )
@@ -286,14 +287,17 @@
     connection receive_data(config&           cfg,
                             connection        connection_num,
                             bool*             /*gzipped*/,
-                            bandwidth_in_ptr* /*bandwidth_in*/) // TODO: use 
this pointer
-    {
-        return receive_data(cfg,connection_num, static_cast<unsigned>(0), 
NULL); // <- just call the previous version without timeouts
-    }
-
-    connection receive_data(std::vector<char>& buf, bandwidth_in_ptr* 
/*bandwidth_in*/)
-    {
-//         std::cout << "DEBUG: Trying to read to a vector<char>.\n";
+                            bandwidth_in_ptr* bandwidth_in) // TODO: use 
this pointer
+    {
+        // <- just call the previous version without timeouts
+        return receive_data(cfg,connection_num, static_cast<unsigned>(0), 
bandwidth_in);
+    }
+
+    connection receive_data(std::vector<char>& buf, bandwidth_in_ptr* 
bandwidth_in)
+    {
+        //TODO: temporary fix
+        *bandwidth_in = bandwidth_in_ptr( new network::bandwidth_in(4) );
+
         return ana_manager.read_from_all( buf );
     }
 

Modified: trunk/src/network_manager_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.cpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/network_manager_ana.cpp (original)
+++ trunk/src/network_manager_ana.cpp Wed Jul 14 09:02:50 2010
@@ -46,6 +46,8 @@
     std::cout << "DEBUG: Terminating an ana_send_handler...\n";
     if ( target_calls_ > 0 )
         throw std::runtime_error("Handler wasn't called enough times.");
+    mutex_.lock();
+    mutex_.unlock();
 }
 
 void ana_send_handler::handle_send(ana::error_code error_code, ana::net_id 
/*client*/)
@@ -566,6 +568,11 @@
     return pending_handshakes_.find( id ) != pending_handshakes_.end();
 }
 
+bool clients_manager::is_a_client( ana::net_id id ) const
+{
+    return ids_.find( id ) != ids_.end();
+}
+
 network::connection clients_manager::get_pending_connection_id()
 {
     const network::connection result = *pending_ids_.begin();
@@ -856,7 +863,7 @@
             throw std::runtime_error("Can't send to the server itself.");
 
         ana_send_handler handler( size );
-        (*it)->client()->send( ana::buffer( base_char, size ), &handler, 
ana::ZERO_COPY);
+        (*it)->client()->send( ana::buffer( base_char, size ), &handler); 
//, ana::ZERO_COPY);
         handler.wait_completion();
 
         if ( handler.error() )
@@ -873,11 +880,11 @@
                 // TODO: The next lines shouldn't be commented, no way the 
handler isn't called
                 ana_send_handler handler( size );
 //                 (*it)->server()->send_one( id, ana::buffer( base_char, 
size ), this, ana::ZERO_COPY);
-                (*it)->server()->send_one( id, ana::buffer( base_char, size 
), &handler, ana::ZERO_COPY);
+                (*it)->server()->send_one( id, ana::buffer( base_char, size 
), &handler); //, ana::ZERO_COPY);
                 handler.wait_completion();
-//                 if ( handler.error() )
-//                     return 0;
-//                 else
+                if ( handler.error() )
+                    return 0;
+                else
                     return size;
             }
         }
@@ -1120,32 +1127,28 @@
             if (components_.empty() )
                 throw std::runtime_error("Received a message while no 
component was running.\n");
 
-            it = components_.begin();
-
-            if ( (*it)->is_client() )
-                throw std::runtime_error("Received a message from a client 
with a wrong id.\n");
-            else
+            std::map< ana::server*, clients_manager* >::iterator mgrs;
+
+            for ( mgrs = server_manager_.begin(); mgrs != 
server_manager_.end(); ++mgrs)
             {
-                // I received info from something not directly in 
components_, it must be the id of a client in a server
-                clients_manager* clients_mgr = server_manager_[ 
(*it)->server() ];
-
-                if ( clients_mgr->is_pending_handshake( client ) )
+                if (mgrs->second->is_a_client( client ) ) // Is this your 
client?
                 {
-                    if ( buffer->size() != sizeof(uint32_t) ) // all 
handshakes are 4 bytes long
-                        (*it)->server()->disconnect( client );
-
-                    uint32_t handshake;
+                    if ( mgrs->second->is_pending_handshake( client ) ) // 
Did he login already?
                     {
-                        ana::serializer::bistream bis( buffer->string() );
-
-                        bis >> handshake;
-                        ana::network_to_host_long( handshake ); //not 
necessary since I'm expecting a 0 anyway
-                    }
-
-                    if ( handshake != 0 )
-                        (*it)->server()->disconnect( client );
-                    else
-                    {
+                        if ( buffer->size() != sizeof(uint32_t) ) // all 
handshakes are 4 bytes long
+                            mgrs->first->disconnect( client );
+
+                        uint32_t handshake;
+                        {
+                            ana::serializer::bistream bis( buffer->string() 
);
+
+                            bis >> handshake;
+                            ana::network_to_host_long( handshake ); //not 
necessary since I'm expecting a 0 anyway
+                        }
+
+                        if ( handshake != 0 )
+                            mgrs->first->disconnect( client );
+
                         //send back it's id
                         ana::serializer::bostream bos;
 
@@ -1154,15 +1157,22 @@
                         ana::host_to_network_long( network_byte_order_id );
                         bos << network_byte_order_id;
 
-                        const size_t bytes_sent = send_raw_data( 
bos.str().c_str(), bos.str().size(), client );
-
-                        if ( bytes_sent != sizeof( uint32_t ) ) // couldn't 
send id to the client, disconnect
-                            (*it)->server()->disconnect( client );
-                        else
-                        {
-                            (*it)->server()->set_header_first_mode( client );
-                            clients_mgr->has_connected( client );
-                        }
+                        //TODO: I should check if this operation was 
successful
+                        mgrs->first->send_one(client, ana::buffer( bos.str() 
), this );
+                        mgrs->first->set_header_first_mode( client );
+                        mgrs->second->has_connected( client );
+                    }
+                    else // just add the buffer to the associated 
clients_manager
+                    {
+                        ana::net_id server_id = mgrs->first->id();
+
+                        it = std::find_if( components_.begin(), 
components_.end(),
+                                        boost::bind(&ana_component::get_id, 
_1) == server_id );
+
+                        if ( (*it)->is_client() )
+                            throw std::runtime_error("Wrong id to receive 
from.");
+
+                        (*it)->add_buffer( buffer, client );
                     }
                 }
             }

Modified: trunk/src/network_manager_ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.hpp?rev=44160&r1=44159&r2=44160&view=diff
==============================================================================
--- trunk/src/network_manager_ana.hpp (original)
+++ trunk/src/network_manager_ana.hpp Wed Jul 14 09:02:50 2010
@@ -341,6 +341,8 @@
 
         bool is_pending_handshake( ana::net_id ) const;
 
+        bool is_a_client( ana::net_id id ) const;
+
         network::connection get_pending_connection_id();
 
     private:




Related Messages


Powered by MHonArc, Updated Wed Jul 14 10:00:10 2010