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 | |
| 21 | int Send(const void *buf, int length); |
| 22 | int Recv(void *buf, int length); |
| 23 | int Recv(void *buf, int length, long usec); // returns 0 if timed out |
| 24 | protected: |
| 25 | int Connect(NetworkPort port, const char *address, int type = SOCK_DGRAM); |
| 26 | Socket(); |
| 27 | ~Socket(); |
| 28 | |
| 29 | // Resets socket_ and last_ret_. |
| 30 | void Reset(); |
| 31 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame^] | 32 | union { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | sockaddr_in in; |
| 34 | sockaddr addr; |
| 35 | } addr_; // filled in by Connect |
| 36 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame^] | 37 | int socket_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | int last_ret_; |
| 39 | }; |
| 40 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame^] | 41 | } // namespace aos |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 42 | |
Brian Silverman | a9cbe30 | 2013-03-12 18:41:44 -0700 | [diff] [blame^] | 43 | #endif // AOS_COMMON_NETWORK_SOCKET_H_ |