udpSocket.h
Go to the documentation of this file.
1 
7 #include <iostream>
8 #include <string>
9 #include <boost/asio.hpp>
10 #include <boost/thread.hpp>
11 #include <boost/signals2.hpp>
12 
16 typedef boost::function<void(const boost::system::error_code& error, const char* msg_recvd, std::size_t msg_bytes_recvd)> F_LISTEN;
17 
21 typedef boost::function<void(const boost::system::error_code& error, const char* msg_sent, std::size_t msg_bytes_sent)> F_SEND;
22 
26 typedef std::vector<char> VC_DATA;
27 
33 class UdpSocket
34 {
35 private:
39  boost::asio::io_service mIo;
40 
44  boost::thread bt;
45 
49  boost::shared_ptr<boost::asio::ip::udp::socket> m_socket;
50 
54  bool m_socket_ready = false;
55 
59  boost::asio::ip::udp::endpoint m_local_endpoint;
60 
64  boost::asio::ip::udp::endpoint m_remote_endpoint;
65 
66  enum
67  {
68  max_length = 1024
69  };
70 
74  char m_data[max_length];
75 
79  VC_DATA m_vcDataSend;
80 
84  F_SEND m_fSend;
85 
89  F_LISTEN m_fListen;
90  void start_receive();
91  void handle_receive_from(const boost::system::error_code& error, std::size_t msg_bytes);
92  void handle_send_to(const boost::system::error_code& error, std::size_t msg_bytes);
93 public:
94  UdpSocket();
95  ~UdpSocket();
96  void open(const unsigned short LocalPort = 0);
97  bool isOpened() const;
98  void set_sender_handler(const F_SEND& f);
99  void set_listener_handler(const F_LISTEN& f);
100  bool send(boost::system::error_code& ec, std::string DestIp, unsigned short DestPort, const char* msg_content, size_t msg_bytes);
101  void close(boost::system::error_code& ec);
102 
103 };
std::vector< char > VC_DATA
Definition: udpSocket.h:26
boost::function< void(const boost::system::error_code &error, const char *msg_recvd, std::size_t msg_bytes_recvd)> F_LISTEN
Definition: udpSocket.h:16
boost::function< void(const boost::system::error_code &error, const char *msg_sent, std::size_t msg_bytes_sent)> F_SEND
Definition: udpSocket.h:21
UDP Socket Class that allows to create a udp socket with custom listener and timeout for response...
Definition: udpSocket.h:33