blob: ed4a8e7c3a1b9f954715ff52faba9a407edd059c [file] [log] [blame]
Parker Schuh2cd173d2017-01-28 00:12:01 -08001#ifndef AOS_VISION_EVENTS_UDP_H_
2#define AOS_VISION_EVENTS_UDP_H_
Brian Silverman4acae812016-03-14 13:16:19 -04003
4#include <arpa/inet.h>
Brian Silverman4acae812016-03-14 13:16:19 -04005#include <math.h>
Parker Schuh2cd173d2017-01-28 00:12:01 -08006#include <sys/socket.h>
Brian Silverman4acae812016-03-14 13:16:19 -04007#include <unistd.h>
Parker Schuh2cd173d2017-01-28 00:12:01 -08008#include <string>
Brian Silverman4acae812016-03-14 13:16:19 -04009#include <vector>
10
11#include "aos/common/macros.h"
12#include "aos/common/scoped_fd.h"
13
14namespace aos {
Parker Schuh2cd173d2017-01-28 00:12:01 -080015namespace events {
Brian Silverman4acae812016-03-14 13:16:19 -040016
17// Simple wrapper around a transmitting UDP socket.
18//
19// LOG(FATAL)s for all errors, including from Send.
20class TXUdpSocket {
21 public:
Parker Schuh2cd173d2017-01-28 00:12:01 -080022 TXUdpSocket(const std::string &ip_addr, int port);
Brian Silverman4acae812016-03-14 13:16:19 -040023
24 // Returns the number of bytes actually sent.
Parker Schuh2cd173d2017-01-28 00:12:01 -080025 int Send(const char *data, int size);
Brian Silverman4acae812016-03-14 13:16:19 -040026
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.
36class 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 Schuh2cd173d2017-01-28 00:12:01 -080049} // namespace events
Brian Silverman4acae812016-03-14 13:16:19 -040050} // namespace aos
51
Parker Schuh2cd173d2017-01-28 00:12:01 -080052#endif // AOS_VISION_EVENTS_UDP_H_