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 | |
| 5 | #include "aos/common/logging/logging.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace vision { |
| 9 | |
| 10 | TXUdpSocket::TXUdpSocket(const char *ip_addr, int port) |
| 11 | : fd_(PCHECK(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))) { |
| 12 | sockaddr_in destination_in{}; |
| 13 | destination_in.sin_family = AF_INET; |
| 14 | destination_in.sin_port = htons(port); |
| 15 | if (inet_aton(ip_addr, &destination_in.sin_addr) == 0) { |
| 16 | LOG(FATAL, "invalid IP address %s\n", ip_addr); |
| 17 | } |
| 18 | |
| 19 | PCHECK(connect(fd_.get(), reinterpret_cast<sockaddr *>(&destination_in), |
| 20 | sizeof(destination_in))); |
| 21 | } |
| 22 | |
| 23 | int TXUdpSocket::Send(const void *data, int size) { |
| 24 | return PCHECK(send(fd_.get(), static_cast<const char *>(data), size, 0)); |
| 25 | } |
| 26 | |
| 27 | RXUdpSocket::RXUdpSocket(int port) |
| 28 | : fd_(PCHECK(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))) { |
| 29 | sockaddr_in bind_address{}; |
| 30 | |
| 31 | bind_address.sin_family = AF_INET; |
| 32 | bind_address.sin_port = htons(port); |
| 33 | bind_address.sin_addr.s_addr = htonl(INADDR_ANY); |
| 34 | |
| 35 | PCHECK(bind(fd_.get(), reinterpret_cast<sockaddr *>(&bind_address), |
| 36 | sizeof(bind_address))); |
| 37 | } |
| 38 | |
| 39 | int RXUdpSocket::Recv(void *data, int size) { |
| 40 | return PCHECK(recv(fd_.get(), static_cast<char *>(data), size, 0)); |
| 41 | } |
| 42 | |
| 43 | } // namespace vision |
| 44 | } // namespace aos |