ana/src/asio_client.hpp

Go to the documentation of this file.
00001 /* $Id: asio_client.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 
00003 /**
00004  * @file
00005  * @brief Header file of the client side of the ana project.
00006  *
00007  * ana: Asynchronous Network API.
00008  * Copyright (C) 2010 - 2012 Guillermo Biset.
00009  *
00010  * This file is part of the ana project.
00011  *
00012  * System:         ana
00013  * Language:       C++
00014  *
00015  * Author:         Guillermo Biset
00016  * E-Mail:         billybiset AT gmail DOT com
00017  *
00018  * ana is free software: you can redistribute it and/or modify
00019  * it under the terms of the GNU General Public License as published by
00020  * the Free Software Foundation, either version 2 of the License, or
00021  * (at your option) any later version.
00022  *
00023  * ana is distributed in the hope that it will be useful,
00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  * GNU General Public License for more details.
00027  *
00028  * You should have received a copy of the GNU General Public License
00029  * along with ana.  If not, see <http://www.gnu.org/licenses/>.
00030  *
00031  */
00032 
00033 #ifndef ASIO_CLIENT_HPP
00034 #define ASIO_CLIENT_HPP
00035 
00036 #include <list>
00037 
00038 #include <boost/asio.hpp>
00039 #include <memory>
00040 
00041 using boost::asio::ip::tcp;
00042 
00043 #include "../api/ana.hpp"
00044 
00045 #include "asio_proxy_connection.hpp"
00046 #include "asio_listener.hpp"
00047 #include "asio_sender.hpp"
00048 
00049 
00050 class asio_client : public ana::client,
00051                     public asio_listener,
00052                     private proxy_connection_manager,
00053                     private asio_sender
00054 {
00055     public:
00056         /**
00057          * Standard constructor.
00058          *
00059          * @param address : Address to try to connect to. The server should be there.
00060          * @param port : port to use for the connection. The server should have opened it.
00061          */
00062         asio_client(std::string address, ana::port port);
00063 
00064         virtual ~asio_client();
00065 
00066     private:
00067 
00068         virtual void connect( ana::connection_handler* );
00069 
00070         virtual void connect_through_proxy(std::string              proxy_address,
00071                                            std::string              proxy_port,
00072                                            ana::connection_handler* handler,
00073                                            std::string              user_name = "",
00074                                            std::string              password  = "");
00075 
00076         virtual void run();
00077 
00078         virtual ana::operation_id send( boost::asio::const_buffer,
00079                                         ana::send_handler*,
00080                                         ana::send_type );
00081 
00082         virtual void disconnect() { disconnect_listener(); }
00083 
00084         virtual void disconnect_listener();
00085 
00086         virtual void handle_proxy_connection(const boost::system::error_code&,
00087                                              ana::connection_handler*,
00088                                              ana::timer*);
00089 
00090         virtual tcp::socket& socket();
00091 
00092         virtual void log_receive( ana::read_buffer buffer );
00093 
00094         virtual const ana::stats* get_stats( ana::stat_type type ) const;
00095 
00096         virtual ana::stats_collector& stats_collector();
00097 
00098         virtual ana::timer* create_timer() { return new ana::timer( io_service_); }
00099 
00100         virtual void cancel_pending( );
00101 
00102         virtual void set_connect_timeout( size_t ms );
00103 
00104         virtual void expecting_message( size_t ms_until_timeout );
00105 
00106         virtual std::string ip_address() const;
00107 
00108         void handle_connect(const boost::system::error_code& ec,
00109                             tcp::resolver::iterator endpoint_iterator,
00110                             ana::connection_handler*,
00111                             ana::timer*);
00112 
00113         void inform_connection_result( ana::connection_handler*, ana::error_code);
00114 
00115         void handle_timeout(const boost::system::error_code& ec,
00116                             ana::connection_handler*,
00117                             ana::timer*);
00118 
00119         ana::timer* start_connection_timer(ana::connection_handler*);
00120 
00121         /* Override, as per -Weffc++ */
00122         asio_client(const asio_client& other);
00123         asio_client& operator= (const asio_client& other);
00124 
00125         /*attr*/
00126         boost::asio::io_service       io_service_;
00127         std::list<boost::thread*>     io_threads_;
00128         boost::asio::io_service::work work_;
00129 
00130         tcp::socket               socket_;
00131 
00132         std::string               address_;
00133         ana::port                 port_;
00134 
00135         size_t                    connect_timeout_ms_;
00136 
00137         proxy_connection*         proxy_;
00138         bool                      use_proxy_;
00139 
00140         ana::stats_collector      stats_collector_;
00141         ana::operation_id         last_valid_operation_id_;
00142 
00143         boost::mutex              connection_informed_mutex_;
00144         bool                      connection_informed_;
00145 };
00146 
00147 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:39 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs