SocketManager.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef ADCHPP_SOCKETMANAGER_H
00020 #define ADCHPP_SOCKETMANAGER_H
00021
00022 #include "common.h"
00023
00024 #include "forward.h"
00025 #include "ServerInfo.h"
00026 #include "Singleton.h"
00027 #include "Thread.h"
00028
00029 #include <boost/asio.hpp>
00030
00031 namespace adchpp {
00032
00033 class SocketManager : public Singleton<SocketManager>, public Thread {
00034 public:
00035 typedef std::function<void()> Callback;
00037 ADCHPP_DLL void addJob(const Callback& callback) throw();
00041 ADCHPP_DLL void addJob(const long msec, const Callback& callback);
00045 ADCHPP_DLL void addJob(const std::string& time, const Callback& callback);
00050 ADCHPP_DLL Callback addTimedJob(const long msec, const Callback& callback);
00055 ADCHPP_DLL Callback addTimedJob(const std::string& time, const Callback& callback);
00056
00057 void startup() throw(ThreadException);
00058 void shutdown();
00059
00060 void setServers(const ServerInfoList& servers_) { servers = servers_; }
00061
00062 typedef std::function<void (const ManagedSocketPtr&)> IncomingHandler;
00063 void setIncomingHandler(const IncomingHandler& handler) { incomingHandler = handler; }
00064
00065 std::map<std::string, int> errors;
00066
00067 private:
00068 friend class ManagedSocket;
00069 friend class SocketFactory;
00070
00071 virtual int run();
00072
00073 void closeFactories();
00074
00075 boost::asio::io_service io;
00076 std::unique_ptr<boost::asio::io_service::work> work;
00077
00078 ServerInfoList servers;
00079 std::vector<SocketFactoryPtr> factories;
00080
00081 IncomingHandler incomingHandler;
00082
00083 static const std::string className;
00084
00085 friend class Singleton<SocketManager>;
00086 ADCHPP_DLL static SocketManager* instance;
00087
00088 typedef shared_ptr<boost::asio::deadline_timer> timer_ptr;
00089 void addJob(const boost::asio::deadline_timer::duration_type& duration, const Callback& callback);
00090 Callback addTimedJob(const boost::asio::deadline_timer::duration_type& duration, const Callback& callback);
00091 void setTimer(timer_ptr timer, const boost::asio::deadline_timer::duration_type& duration, Callback* callback);
00092 void handleWait(timer_ptr timer, const boost::asio::deadline_timer::duration_type& duration, const boost::system::error_code& error,
00093 Callback* callback);
00094 void cancelTimer(timer_ptr timer, Callback* callback);
00095
00096 void onLoad(const SimpleXML& xml) throw();
00097
00098 SocketManager();
00099 virtual ~SocketManager();
00100 };
00101
00102 }
00103
00104 #endif // SOCKETMANAGER_H