00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef PEEKABOT_CLIENT_SERVER_CONNECTION_HH_INCLUDED
00010 #define PEEKABOT_CLIENT_SERVER_CONNECTION_HH_INCLUDED
00011
00012
00013 #include <string>
00014 #include <queue>
00015 #include <stdexcept>
00016 #include <boost/thread/condition.hpp>
00017 #include <boost/thread/thread.hpp>
00018 #include <boost/thread/recursive_mutex.hpp>
00019 #include <boost/thread/mutex.hpp>
00020 #include <boost/utility.hpp>
00021 #include <boost/date_time/posix_time/posix_time.hpp>
00022
00023 #include "../Types.hh"
00024 #include "../Sockets.hh"
00025 #include "Status.hh"
00026 #include "Result.hh"
00027 #include "Transport.hh"
00028
00029
00030 namespace peekabot
00031 {
00032 class Action;
00033
00034 namespace client
00035 {
00036 class ClientImpl;
00037
00051 class ServerConnection : public Transport,
00052 public boost::noncopyable
00053 {
00054 public:
00055 ServerConnection(boost::shared_ptr<ClientImpl> client);
00056
00057 virtual ~ServerConnection();
00058
00067 void connect(const std::string &hostname,
00068 unsigned int port,
00069 bool low_latency_mode);
00070
00074 void disconnect();
00075
00086 virtual void flush();
00087
00093 bool is_connected() const;
00094
00101 virtual void dispatch_action(boost::shared_ptr<Action> action);
00102
00108 boost::posix_time::time_duration get_uptime() const throw();
00109
00110 private:
00111 void _connect(
00112 const std::string &hostname,
00113 unsigned int port,
00114 bool low_latency_mode);
00115
00116 void perform_authentication(bool low_latency_mode)
00117 throw(std::exception);
00118
00119
00123 void tx_thread();
00124
00128 void rx_thread();
00129
00130
00131 void serialize_and_send(boost::shared_ptr<Action> action);
00132
00133 void blocking_send(const void *buf, size_t n);
00134
00135 size_t timed_send(const void *buf, size_t n, uint32_t timeout_ms);
00136
00137 size_t timed_recv(void *buf, size_t n, uint32_t timeout_ms);
00138
00139 void timed_connect(
00140 const std::string &hostname, int port, size_t timeout_ms);
00141
00142 void discard_unsent() throw();
00143
00144 private:
00145 boost::thread * volatile m_tx_thread;
00146 boost::thread * volatile m_rx_thread;
00147
00148 volatile bool m_stop_signal;
00149
00150 boost::recursive_mutex m_sockfd_mutex;
00151
00152 sockets::SocketType m_sockfd;
00153
00154 bool m_server_is_big_endian;
00155
00156 boost::posix_time::ptime m_up_since;
00157
00158 mutable boost::recursive_mutex m_outbound_mutex;
00159 std::queue<boost::shared_ptr<Action> > m_outbound;
00160 boost::condition m_outbound_push_cond;
00161 boost::condition m_outbound_pop_cond;
00162 };
00163 }
00164 }
00165
00166
00167 #endif // PEEKABOT_CLIENT_SERVER_CONNECTION_HH_INCLUDED