SockIt

UdpClient.h

Go to the documentation of this file.
00001 #ifndef UDPCLIENT_H
00002 #define UDPCLIENT_H
00003 
00004 #include <algorithm>
00005 #include <map>
00006 #include <string>
00007 #include <queue>
00008 
00009 #include <boost/asio.hpp>
00010 #include <boost/bind.hpp>
00011 #include <boost/thread.hpp>
00012 
00013 #include "Client.h"
00014 #include "Logger.h"
00015 #include "Event.h"
00016 #include "Udp.h"
00017 
00018 using boost::asio::ip::udp;
00019 using std::vector;
00020 using std::string;
00021 
00022 class UdpClient: public Udp, public Client
00023 {
00024     public:
00025 
00034         UdpClient(const string &host, int port, boost::asio::io_service & io_service);
00035 
00045         UdpClient(const string &host, int port, boost::asio::io_service & io_service, map<string, string> options);
00046 
00051         virtual ~UdpClient();
00052 
00058         virtual void send(const string & data);
00059 
00065         virtual void send_bytes(const vector<byte> & bytes);
00066 
00071         virtual void shutdown();
00072 
00076         virtual int get_port();
00077 
00081         virtual string get_host();
00082 
00083     protected:
00084 
00088         void init_socket();
00089 
00095         virtual void fire_error_event(const string & message);
00096 
00104         virtual void fire_data_event(const string data, boost::shared_ptr<udp::socket> socket,
00105                 boost::shared_ptr<udp::endpoint> endpoint);
00106 
00110         virtual void close();
00111 
00112     private:
00113 
00117         UdpClient(const UdpClient &other);
00118 
00123         virtual void listen();
00124 
00134         void resolve_handler(const boost::system::error_code &err, udp::resolver::iterator endpoint_iterator);
00135 
00139         void flush();
00140 
00144         boost::shared_ptr<udp::resolver> resolver;
00145 
00149         boost::shared_ptr<udp::socket> socket;
00150 
00154         boost::mutex queue_mtx;
00155 
00159         std::queue<string> msgs_not_sent;
00160 
00164         bool resolved_endpoint;
00165 };
00166 
00167 #endif
 All Classes Files Functions Variables Typedefs Friends Defines