udp_socket.h
Go to the documentation of this file.
1 
26 #ifndef ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H
27 #define ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H
28 
29 #include <string>
30 #include <boost/asio.hpp>
31 
33 
34 using std::size_t;
35 using std::string;
36 using namespace boost::asio;
37 using boost::asio::ip::udp;
38 
39 namespace eip {
40 namespace socket {
41 
45 class UDPSocket : public Socket
46 {
47 public:
48 
49  UDPSocket(io_service& io_serv, unsigned short local_port, std::string local_ip="0.0.0.0")
50  : socket_(io_serv), local_endpoint_(boost::asio::ip::address::from_string(local_ip), local_port) { }
51 
57  virtual void open(string hostname, string port)
58  {
59  udp::resolver resolver(socket_.get_io_service());
60  udp::resolver::query query(udp::v4(), hostname, port);
61  remote_endpoint_ = *resolver.resolve(query);
62  socket_.open(udp::v4());
63  socket_.bind(local_endpoint_);
64  }
65 
69  virtual inline void close()
70  {
71  socket_.close();
72  }
73 
79  virtual size_t send(const_buffer buf)
80  {
81  return socket_.send_to(const_buffers_1(buf), remote_endpoint_);
82  }
83 
89  virtual size_t receive(mutable_buffer buf)
90  {
91  // have to make this into a sequence or the templating engine throws a fit
92  udp::endpoint ep;
93  return socket_.receive_from(mutable_buffers_1(buf), ep);
94  }
95 
96 private:
97  udp::socket socket_;
98  udp::endpoint local_endpoint_;
99  udp::endpoint remote_endpoint_;
100 };
101 
102 } // namespace socket
103 } // namespace eip
104 #endif // ODVA_ETHERNETIP_SOCKET_UDP_SOCKET_H
virtual size_t receive(mutable_buffer buf)
Definition: udp_socket.h:89
UDPSocket(io_service &io_serv, unsigned short local_port, std::string local_ip="0.0.0.0")
Definition: udp_socket.h:49
virtual void open(string hostname, string port)
Definition: udp_socket.h:57
Definition: connection.h:41
virtual void close()
Definition: udp_socket.h:69
udp::endpoint local_endpoint_
Definition: udp_socket.h:98
virtual size_t send(const_buffer buf)
Definition: udp_socket.h:79
udp::endpoint remote_endpoint_
Definition: udp_socket.h:99
udp::socket socket_
Definition: udp_socket.h:97


odva_ethernetip
Author(s): Kareem Shehata
autogenerated on Mon Jun 10 2019 14:00:16