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