SockIt
|
00001 /* 00002 * File: TcpClient.h 00003 * Author: jtedesco 00004 * 00005 * Created on May 26, 2011, 12:32 PM 00006 */ 00007 00008 #ifndef TCPCLIENT_H 00009 #define TCPCLIENT_H 00010 00011 #include <boost/lexical_cast.hpp> 00012 #include <boost/thread.hpp> 00013 00014 #include <string.h> 00015 00016 #include "Client.h" 00017 #include "Logger.h" 00018 #include "Tcp.h" 00019 00020 using boost::asio::ip::tcp; 00021 00026 class TcpClient: public Tcp, public Client 00027 { 00028 00029 public: 00030 00039 TcpClient(const string & host, int port, boost::asio::io_service & io_service); 00040 00050 TcpClient(const string & host, int port, boost::asio::io_service & io_service, map<string, string> options); 00051 00056 virtual ~TcpClient(); 00057 00063 virtual void send(const string & data); 00064 00070 virtual void send_bytes(const vector<byte> & bytes); 00071 00076 virtual void shutdown(); 00077 00081 virtual int get_port(); 00082 00086 virtual string get_host(); 00087 00097 FB_JSAPI_EVENT(disconnect, 1, (const string &)); 00098 00102 FB_JSAPI_EVENT(connect, 0, ()); 00103 00104 protected: 00105 00111 virtual void fire_error_event(const string & message); 00112 00118 virtual void fire_disconnect_event(const string & message); 00119 00126 virtual void fire_data_event(const string data, boost::shared_ptr<tcp::socket> connection); 00127 00131 virtual void close(); 00132 00133 private: 00134 00138 TcpClient(const TcpClient &other); 00139 00143 void init(); 00144 00148 void init_socket(); 00149 00159 void resolve_handler(const boost::system::error_code & error_code, tcp::resolver::iterator endpoint_iterator); 00160 00170 void connect_handler(const boost::system::error_code & error_code, tcp::resolver::iterator endpoint_iterator); 00171 00176 void listen(); 00177 00181 void flush(); 00182 00186 boost::shared_ptr<tcp::socket> connection; 00187 00191 boost::shared_ptr<tcp::resolver> resolver; 00192 00197 bool connected; 00198 00202 boost::mutex connected_mutex; 00203 00207 queue<string> data_queue; 00208 00212 boost::mutex data_queue_mutex; 00213 }; 00214 00215 #endif /* TCPCLIENT_H */ 00216