ana/src/asio_server.hpp

Go to the documentation of this file.
00001 /* $Id: asio_server.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 
00003 /**
00004  * @file
00005  * @brief Header file of the server side for 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_SERVER_HPP
00034 #define ASIO_SERVER_HPP
00035 
00036 #include <list>
00037 
00038 #include <boost/asio.hpp>
00039 #include <boost/interprocess/smart_ptr/unique_ptr.hpp>
00040 #include <boost/function.hpp>
00041 
00042 #include "../api/ana.hpp"
00043 #include "asio_listener.hpp"
00044 #include "asio_sender.hpp"
00045 
00046 using boost::asio::ip::tcp;
00047 
00048 struct asio_proxy_manager
00049 {
00050     virtual ~asio_proxy_manager() {}
00051 
00052     virtual void deregister_client(ana::server::client_proxy* client) = 0;
00053 };
00054 
00055 class asio_server : public  ana::server,
00056                     private asio_proxy_manager
00057 {
00058     private:
00059         class asio_client_proxy : public virtual ana::server::client_proxy,
00060                                   public asio_listener,
00061                                   private asio_sender
00062         {
00063             public:
00064                 asio_client_proxy(boost::asio::io_service& io_service, asio_proxy_manager* mgr);
00065 
00066                 virtual tcp::socket& socket();
00067 
00068                 virtual ~asio_client_proxy();
00069             private:
00070                 virtual void disconnect() { disconnect_listener(); }
00071 
00072                 virtual void disconnect_listener();
00073 
00074                 virtual void cancel_pending();
00075 
00076                 virtual void send(ana::detail::shared_buffer,
00077                                   ana::send_handler*,
00078                                   ana::detail::sender*,
00079                                   ana::operation_id);
00080 
00081                 virtual std::string ip_address( ) const;
00082 
00083                 virtual ana::stats_collector& stats_collector();
00084 
00085                 virtual ana::timer* create_timer();
00086 
00087                 virtual const ana::stats* get_stats( ana::stat_type type ) const;
00088 
00089                 virtual void expecting_message( size_t ms_until_timeout );
00090 
00091                 tcp::socket           socket_;
00092                 asio_proxy_manager*   manager_;
00093                 ana::stats_collector  stats_collector_;
00094         };
00095 
00096     public:
00097         asio_server();
00098 
00099         virtual ~asio_server();
00100 
00101     private:
00102         virtual void set_connection_handler( ana::connection_handler* );
00103 
00104         virtual ana::operation_id send_all(boost::asio::const_buffer,
00105                                            ana::send_handler*,
00106                                            ana::send_type );
00107 
00108         virtual ana::operation_id send_if (boost::asio::const_buffer,
00109                                            ana::send_handler*,
00110                                            const ana::client_predicate&,
00111                                            ana::send_type );
00112 
00113         virtual ana::operation_id send_one(ana::net_id,
00114                                            boost::asio::const_buffer,
00115                                            ana::send_handler*,
00116                                            ana::send_type );
00117 
00118         virtual ana::operation_id send_all_except(ana::net_id,
00119                                                   boost::asio::const_buffer,
00120                                                   ana::send_handler*,
00121                                                   ana::send_type );
00122 
00123         virtual void set_listener_handler( ana::listener_handler* );
00124         virtual void run_listener();
00125         virtual void run(ana::port pt);
00126 
00127         virtual void deregister_client(client_proxy* client);
00128 
00129         virtual std::string ip_address( ana::net_id ) const;
00130 
00131         virtual const ana::stats* get_client_stats( ana::net_id, ana::stat_type ) const;
00132 
00133         virtual void log_receive( ana::read_buffer buffer );
00134 
00135         virtual const ana::stats* get_stats( ana::stat_type type ) const;
00136 
00137         virtual void cancel_pending( );
00138         virtual void cancel_pending( ana::net_id client_id );
00139 
00140         virtual void disconnect( ana::net_id );
00141 
00142         virtual void disconnect();
00143         virtual void set_raw_buffer_max_size( size_t );
00144 
00145         // TODO: implement this, or improve OO design with 2nd param (vote for 2)
00146         virtual void wait_raw_object(ana::serializer::bistream& , size_t ) {}
00147 
00148         virtual ana::stats_collector& stats_collector();
00149 
00150         virtual ana::timer* create_timer();
00151 
00152         virtual void set_header_first_mode( ana::net_id id );
00153 
00154         virtual void set_raw_data_mode( ana::net_id id );
00155 
00156         virtual void expecting_message( ana::net_id, size_t ms_until_timeout );
00157 
00158         void handle_accept (const boost::system::error_code& ec,
00159                             asio_client_proxy*               client,
00160                             ana::connection_handler*         handler);
00161 
00162         void register_client(client_proxy* client);
00163 
00164         void run_acceptor_thread(asio_server* obj, ana::connection_handler* );
00165 
00166         void async_accept( ana::connection_handler* );
00167 
00168         boost::asio::io_service       io_service_;
00169         boost::asio::io_service::work work_;
00170         std::list<boost::thread*>     io_threads_;
00171         std::auto_ptr<tcp::acceptor>  acceptor_;
00172         std::list<client_proxy*>      client_proxies_;
00173         bool                          listening_;
00174         ana::listener_handler*        listener_;
00175         ana::connection_handler*      connection_handler_;
00176         asio_client_proxy*            last_client_proxy_;
00177 
00178         ana::stats_collector          stats_collector_;
00179 
00180         ana::operation_id             last_valid_operation_id_; //0 means no operation performed
00181 };
00182 
00183 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:33 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs