Go to the documentation of this file.00001
00026 #ifndef ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H
00027 #define ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H
00028
00029 #include <string>
00030 #include <boost/asio.hpp>
00031
00032 #include "odva_ethernetip/socket/socket.h"
00033
00034 using std::size_t;
00035 using std::string;
00036 using namespace boost::asio;
00037 using boost::asio::ip::udp;
00038
00039 namespace eip {
00040 namespace socket {
00041
00045 class UDPSocket : public Socket
00046 {
00047 public:
00048
00049 UDPSocket(io_service& io_serv, unsigned short local_port, std::string local_ip="0.0.0.0")
00050 : socket_(io_serv), local_endpoint_(boost::asio::ip::address::from_string(local_ip), local_port) { }
00051
00057 virtual void open(string hostname, string port)
00058 {
00059 udp::resolver resolver(socket_.get_io_service());
00060 udp::resolver::query query(udp::v4(), hostname, port);
00061 remote_endpoint_ = *resolver.resolve(query);
00062 socket_.open(udp::v4());
00063 socket_.bind(local_endpoint_);
00064 }
00065
00069 virtual inline void close()
00070 {
00071 socket_.close();
00072 }
00073
00079 virtual size_t send(const_buffer buf)
00080 {
00081 return socket_.send_to(const_buffers_1(buf), remote_endpoint_);
00082 }
00083
00089 virtual size_t receive(mutable_buffer buf)
00090 {
00091
00092 udp::endpoint ep;
00093 return socket_.receive_from(mutable_buffers_1(buf), ep);
00094 }
00095
00096 private:
00097 udp::socket socket_;
00098 udp::endpoint local_endpoint_;
00099 udp::endpoint remote_endpoint_;
00100 };
00101
00102 }
00103 }
00104 #endif // ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H