SockIt

Udp.h

Go to the documentation of this file.
00001 /*
00002  * Udp.h
00003  *
00004  *  Created on: Jun 8, 2011
00005  *      Author: jtedesco
00006  */
00007 
00008 #ifndef UDP_H_
00009 #define UDP_H_
00010 
00011 #include <boost/asio.hpp>
00012 #include <boost/asio/buffer.hpp>
00013 #include <boost/lexical_cast.hpp>
00014 #include <boost/make_shared.hpp>
00015 #include <boost/optional.hpp>
00016 #include <boost/thread.hpp>
00017 #include <map>
00018 
00019 class Udp;
00020 
00021 #include "UdpEvent.h"
00022 #include "Logger.h"
00023 
00024 using boost::optional;
00025 using boost::asio::buffer_size;
00026 using boost::asio::buffer_cast;
00027 using boost::asio::ip::udp;
00028 using std::string;
00029 using std::map;
00030 
00031 typedef boost::asio::const_buffers_1 boost_buffer;
00032 
00039 class Udp
00040 {
00041     public:
00042 
00053         Udp(string host, int port, boost::asio::io_service & io_service);
00054 
00059         virtual ~Udp() {}
00060 
00061         friend class UdpEvent;
00062 
00063     protected:
00064 
00072         inline string bool_option_to_string(optional<bool> &arg, string iftrue, string iffalse);
00073 
00079         template<class T> inline string option_to_string(optional<T> &arg);
00080 
00088         void inline parse_string_bool_arg(map<string, string> &options, string arg, optional<bool> &arg_value);
00089 
00103         void parse_args(map<string, string> options);
00104 
00108         void log_options();
00109 
00113         virtual void listen() = 0;
00114 
00119         virtual void close() = 0;
00120 
00131         void send_handler(const boost::system::error_code &err, std::size_t bytes_transferred,
00132                 string msg, string host, int port);
00133 
00145         void receive_handler(const boost::system::error_code &err, std::size_t bytes_transferred,
00146                         boost::shared_ptr<udp::socket> socket, boost::shared_ptr<udp::endpoint> endpoint, string host,
00147                         int port);
00148 
00154         virtual void fire_error_event(const string & message) = 0;
00155 
00163         virtual void fire_data_event(const string data, boost::shared_ptr<udp::socket> socket,
00164                 boost::shared_ptr<udp::endpoint> endpoint) = 0;
00165 
00167         boost::asio::io_service & io_service;
00168 
00171         static const int BUFFER_SIZE = 2048;
00172 
00174         boost::array<char, BUFFER_SIZE> receive_buffer;
00175 
00177         int pending_sends;
00178 
00180         boost::mutex pending_sends_mutex;
00181 
00183         bool should_close;
00184 
00186         optional<bool> using_ipv6;
00187 
00189         optional<bool> multicast;
00190 
00192         optional<string> multicast_group;
00193 
00195         optional<int> multicast_ttl;
00196 
00198         optional<bool> do_not_route;
00199 
00201         optional<bool> reuse_address;
00202 
00204         string host;
00205 
00207         int port;
00208 
00210         boost::shared_ptr<udp::endpoint> remote_endpoint;
00211 
00213         bool failed;
00214 };
00215 
00216 #endif /* UDP_H_ */
 All Classes Files Functions Variables Typedefs Friends Defines