Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_NETWORK_SOCKET_H_ |
| 2 | #define AOS_COMMON_NETWORK_SOCKET_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 4 | #include <arpa/inet.h> |
| 5 | #include <netinet/in.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include <sys/socket.h> |
| 9 | #include <sys/types.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include <unistd.h> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 11 | #include <chrono> |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 12 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 13 | #include "aos/common/network_port.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
Brian | c9f6455 | 2014-04-02 19:44:09 -0700 | [diff] [blame] | 16 | namespace network { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
| 18 | class Socket { |
| 19 | public: |
| 20 | int LastStatus() const { return last_ret_; } |
| 21 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 22 | int Send(const void *buf, int length); |
| 23 | |
| 24 | // buf is where to put the data and length is the maximum amount of data to |
| 25 | // put in for all overloads. |
| 26 | // All overloads return how many bytes were received or -1 for error. 0 is a |
| 27 | // valid return value for all overloads. |
| 28 | // No timeout. |
| 29 | int Receive(void *buf, int length); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 30 | // timeout is relative |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame^] | 31 | int Receive(void *buf, int length, ::std::chrono::microseconds timeout); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 32 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | protected: |
| 34 | int Connect(NetworkPort port, const char *address, int type = SOCK_DGRAM); |
| 35 | Socket(); |
| 36 | ~Socket(); |
| 37 | |
| 38 | // Resets socket_ and last_ret_. |
| 39 | void Reset(); |
| 40 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 41 | union { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 42 | sockaddr_in in; |
| 43 | sockaddr addr; |
| 44 | } addr_; // filled in by Connect |
| 45 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 46 | int socket_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 47 | int last_ret_; |
| 48 | }; |
| 49 | |
Brian | c9f6455 | 2014-04-02 19:44:09 -0700 | [diff] [blame] | 50 | } // namespace network |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 51 | } // namespace aos |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 52 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame] | 53 | #endif // AOS_COMMON_NETWORK_SOCKET_H_ |