SockIt
|
00001 /* 00002 * Tcp.h 00003 * 00004 * Created on: Jun 8, 2011 00005 * Author: jtedesco 00006 */ 00007 00008 #ifndef TCP_H_ 00009 #define TCP_H_ 00010 00011 #include <boost/asio.hpp> 00012 #include <boost/thread.hpp> 00013 #include <boost/make_shared.hpp> 00014 #include <boost/lexical_cast.hpp> 00015 #include <boost/optional.hpp> 00016 #include <iostream> 00017 #include <set> 00018 #include <map> 00019 00020 #if defined(__UNIX__) 00021 00022 #include <netinet/tcp.h> 00023 #include <sys/types.h> 00024 #include <sys/socket.h> 00025 #include <errno.h> 00026 00027 #elif defined(__OSX__) 00028 00029 #include <netinet/tcp.h> 00030 #include <sys/types.h> 00031 #include <sys/socket.h> 00032 #include <errno.h> 00033 00034 #else 00035 00036 #define WIN32_LEAN_AND_MEAN 00037 #include <winsock2.h> 00038 #include <Windows.h> 00039 #include <MSTcpIP.h> 00040 00041 #endif 00042 00043 00044 class Tcp; 00045 00046 #include "TcpEvent.h" 00047 #include "Logger.h" 00048 00049 using boost::optional; 00050 using boost::asio::ip::tcp; 00051 using boost::asio::buffer_size; 00052 using std::map; 00053 00060 class Tcp 00061 { 00062 public: 00063 00074 Tcp(string host, int port, boost::asio::io_service & io_service); 00075 00080 virtual ~Tcp() {} 00081 00082 friend class TcpEvent; 00083 00084 protected: 00085 00092 bool set_tcp_keepalive(boost::shared_ptr<tcp::socket> socket); 00093 00101 inline string bool_option_to_string(optional<bool> &arg, string iftrue, string iffalse); 00102 00108 template <class T> inline string option_to_string(optional<T> &arg); 00109 00117 void inline parse_string_bool_arg(map<string, string> &options, string arg, optional<bool> &arg_value); 00118 00126 void inline parse_string_int_arg(map<string, string> &options, string arg, optional<int> &arg_value); 00127 00138 void parse_args(map<string, string> options); 00139 00143 void log_options(); 00144 00149 virtual void close() = 0; 00150 00162 virtual void send_handler(const boost::system::error_code & error_code, std::size_t bytes_transferred, 00163 string data, string host, int port, boost::shared_ptr<tcp::socket> connection); 00164 00175 virtual void receive_handler(const boost::system::error_code & error_code, std::size_t bytes_transferred, 00176 boost::shared_ptr<tcp::socket> connection, string host, int port); 00177 00183 virtual void fire_error_event(const string & message) = 0; 00184 00190 virtual void fire_disconnect_event(const string & message) = 0; 00191 00198 virtual void fire_data_event(const string data, boost::shared_ptr<tcp::socket> connection) = 0; 00199 00203 static const int BUFFER_SIZE = 4096; 00204 00209 static const int MAX_DATA_SIZE = 65536; 00210 00214 boost::array<char, BUFFER_SIZE> receive_buffer; 00215 00219 boost::asio::io_service & io_service; 00220 00224 bool waiting_to_shutdown; 00225 00229 optional<bool> using_ipv6; 00230 00234 optional<bool> no_delay; 00235 00239 optional<bool> do_not_route; 00240 00244 optional<bool> keep_alive; 00245 00249 optional<int> keep_alive_timeout; 00250 00254 int active_jobs; 00255 00259 boost::mutex active_jobs_mutex; 00260 00264 string host; 00265 00269 int port; 00270 00276 std::set<boost::system::error_code> disconnect_errors; 00277 00281 bool failed; 00282 }; 00283 00284 #endif /* TCP_H_ */