Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 1 | #include "aos/vision/events/udp.h" |
| 2 | |
| 3 | #include <string.h> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 5 | #include "glog/logging.h" |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 6 | |
| 7 | namespace aos { |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 8 | namespace events { |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 9 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 10 | TXUdpSocket::TXUdpSocket(const std::string &ip_addr, int port) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 11 | : fd_(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) { |
| 12 | PCHECK(fd_.get() != -1); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 13 | sockaddr_in destination_in; |
| 14 | memset(&destination_in, 0, sizeof(destination_in)); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 15 | destination_in.sin_family = AF_INET; |
| 16 | destination_in.sin_port = htons(port); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 17 | CHECK(inet_aton(ip_addr.c_str(), &destination_in.sin_addr) != 0) |
| 18 | << ": invalid IP address " << ip_addr; |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 19 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 20 | PCHECK(connect(fd_.get(), reinterpret_cast<sockaddr *>(&destination_in), |
| 21 | sizeof(destination_in)) == 0); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 22 | } |
| 23 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 24 | int TXUdpSocket::Send(const char *data, int size) { |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 25 | // Don't fail on send. If no one is connected that is fine. |
| 26 | return send(fd_.get(), data, size, 0); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 29 | int RXUdpSocket::SocketBindListenOnPort(int port) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 30 | int fd; |
| 31 | PCHECK((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 32 | sockaddr_in bind_address; |
| 33 | memset(&bind_address, 0, sizeof(bind_address)); |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 34 | |
| 35 | bind_address.sin_family = AF_INET; |
| 36 | bind_address.sin_port = htons(port); |
| 37 | bind_address.sin_addr.s_addr = htonl(INADDR_ANY); |
| 38 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 39 | PCHECK(bind(fd, reinterpret_cast<sockaddr *>(&bind_address), |
| 40 | sizeof(bind_address)) == 0); |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 41 | return fd; |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Austin Schuh | 8d5fff4 | 2018-05-30 20:44:12 -0700 | [diff] [blame] | 44 | RXUdpSocket::RXUdpSocket(int port) : fd_(SocketBindListenOnPort(port)) {} |
| 45 | |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 46 | int RXUdpSocket::Recv(void *data, int size) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 47 | int result; |
| 48 | PCHECK((result = recv(fd_.get(), static_cast<char *>(data), size, 0)) != -1); |
| 49 | return result; |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 52 | } // namespace events |
Brian Silverman | 4acae81 | 2016-03-14 13:16:19 -0400 | [diff] [blame] | 53 | } // namespace aos |