00001
00002 #ifndef BASESERVER_V1_04_01_H_
00003 #define BASESERVER_V1_04_01_H_
00004
00005 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00006 # pragma once
00007 #endif
00008
00009 #include <boost/asio.hpp>
00010 #include <boost/chrono.hpp>
00011 #include <boost/cstdint.hpp>
00012 #include <boost/detail/atomic_count.hpp>
00013 #include <boost/shared_ptr.hpp>
00014 #include <boost/asio/system_timer.hpp>
00015 #include <boost/thread.hpp>
00016 #include <boost/noncopyable.hpp>
00017
00018 #include <string>
00019 #include <stdexcept>
00020 #include <set>
00021
00022 namespace tecgraf { namespace ftc { namespace v1_04_01
00023 {
00024
00025 class AccessKey;
00026 class BaseSession;
00027 class ServerConfig;
00028 class Connection;
00029 class ServerExceptionHandler;
00030 typedef boost::shared_ptr<Connection> Connection_ptr;
00031
00037 class BaseServer : private boost::noncopyable
00038 {
00039 public:
00044 BaseServer(ServerConfig& config);
00045
00049 virtual ~BaseServer();
00050
00054 const ServerConfig& config() const;
00055
00064 virtual void dispatch();
00065
00073 void wait_ready();
00074
00083 void timed_wait_ready(uint32_t timeout_ms);
00084
00091 virtual void stop();
00092
00098 bool stopped() const;
00099
00104 void exception_handler(ServerExceptionHandler* exception_handler);
00105
00110 ServerExceptionHandler* exception_handler() const;
00111
00116 virtual BaseSession * create_session(Connection& conn) = 0;
00117
00122 void on_connection_stop(Connection_ptr conn);
00123
00125
00129 void exception_raised(const std::string& msg);
00130
00136 void exception_raised(const std::string& msg, const std::string& data_id);
00137
00142 void exception_raised(const std::exception& e);
00143
00149 void exception_raised(const std::exception& e, const std::string& data_id);
00151 protected:
00155 void start_timeout_timer();
00159 virtual void check_timeout(const uint64_t& timeout_ms);
00160 private:
00161 void connect();
00162
00163 void start_accept();
00164 void handle_accept(const boost::system::error_code& e, Connection_ptr conn);
00165
00166 void handle_timeout(const boost::system::error_code& ec);
00167
00168 void handle_stop();
00169
00170 void run();
00171
00172 ServerConfig& m_config;
00173
00174 boost::asio::io_service io_service;
00175
00177 boost::asio::system_timer timeout_timer;
00178
00180 boost::asio::ip::tcp::acceptor acceptor;
00181
00183 ServerExceptionHandler* m_exception_handler;
00184
00186 boost::shared_ptr<boost::detail::atomic_count> num_connections;
00187
00189 boost::mutex connections_mutex;
00190
00192 std::set<Connection_ptr> connections;
00193
00195 boost::mutex acceptor_mutex;
00196
00198 boost::condition_variable acceptor_notifier;
00199 };
00200
00201 }}}
00202
00203 #endif
00204