00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef PEEKABOT_SERVER_HH_INCLUDED
00023 #define PEEKABOT_SERVER_HH_INCLUDED
00024
00025
00026 #include <queue>
00027 #include <string>
00028 #include <boost/function.hpp>
00029 #include <boost/utility.hpp>
00030 #include <boost/asio.hpp>
00031 #include <boost/thread/mutex.hpp>
00032 #include <boost/thread/condition.hpp>
00033 #include <boost/thread/thread.hpp>
00034
00035
00036 namespace peekabot
00037 {
00038 class Config;
00039 class ClientConnection;
00040 class Action;
00041 class SceneTree;
00042
00043 struct ServerData
00044 {
00045 SceneTree *m_scene;
00046 };
00047
00054 class Server : public boost::noncopyable
00055 {
00056 public:
00057 Server(const Config &config);
00058
00059 ~Server();
00060
00061 const Config &get_config() const;
00062
00063 void start();
00064
00065 void stop();
00066
00067 bool is_stopped() const;
00068
00069 void post(boost::function<void (ServerData &)> handler);
00070
00071
00072
00073
00074 void listen_ipv4(unsigned int port);
00075
00076
00077
00078 private:
00079 void start_accept(
00080 boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor);
00081
00082 void handle_accept(
00083 const boost::system::error_code &e,
00084 boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor,
00085 boost::shared_ptr<ClientConnection> c);
00086
00087 void server_thread();
00088
00089 private:
00090 const Config &m_config;
00091
00092 ServerData m_data;
00093
00094 typedef std::queue<
00095 boost::function<void (ServerData &)> > RequestQueue;
00096 RequestQueue m_requests;
00097 boost::mutex m_requests_mutex;
00098 boost::condition m_requests_cond;
00099
00100 boost::asio::io_service m_io_service;
00101 boost::asio::io_service::work *m_work;
00102
00103 boost::thread *m_asio_thread;
00104 boost::thread *m_server_thread;
00105 volatile bool m_stop_signal;
00106 };
00107 }
00108
00109
00110 #endif // PEEKABOT_SERVER_HH_INCLUDED