Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 1 | #ifndef AOS_VISION_EVENTS_UDP_H_ |
| 2 | #define AOS_VISION_EVENTS_UDP_H_ |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 3 | |
| 4 | #include <arpa/inet.h> |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 5 | #include <math.h> |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 6 | #include <sys/socket.h> |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 7 | #include <unistd.h> |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 8 | #include <string> |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include "aos/common/macros.h" |
| 12 | #include "aos/common/scoped_fd.h" |
| 13 | |
| 14 | namespace aos { |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 15 | namespace events { |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 16 | |
| 17 | // Simple wrapper around a transmitting UDP socket. |
| 18 | // |
| 19 | // LOG(FATAL)s for all errors, including from Send. |
| 20 | class TXUdpSocket { |
| 21 | public: |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 22 | TXUdpSocket(const std::string &ip_addr, int port); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 23 | |
| 24 | // Returns the number of bytes actually sent. |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 25 | int Send(const char *data, int size); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 26 | |
| 27 | private: |
| 28 | ScopedFD fd_; |
| 29 | |
| 30 | DISALLOW_COPY_AND_ASSIGN(TXUdpSocket); |
| 31 | }; |
| 32 | |
| 33 | // Simple wrapper around a receiving UDP socket. |
| 34 | // |
| 35 | // LOG(FATAL)s for all errors, including from Recv. |
| 36 | class RXUdpSocket { |
| 37 | public: |
| 38 | RXUdpSocket(int port); |
| 39 | |
| 40 | // Returns the number of bytes received. |
| 41 | int Recv(void *data, int size); |
| 42 | |
| 43 | private: |
| 44 | ScopedFD fd_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(RXUdpSocket); |
| 47 | }; |
| 48 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 49 | } // namespace events |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 50 | } // namespace aos |
| 51 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame^] | 52 | #endif // AOS_VISION_EVENTS_UDP_H_ |