blob: 7a20d3ebb0575494e7a177beb4a18d43cdfc9441 [file] [log] [blame]
Brian Silvermana9cbe302013-03-12 18:41:44 -07001#ifndef AOS_COMMON_NETWORK_SOCKET_H_
2#define AOS_COMMON_NETWORK_SOCKET_H_
brians343bc112013-02-10 01:53:46 +00003
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 Silvermana9cbe302013-03-12 18:41:44 -070011
Brian Silvermana9cbe302013-03-12 18:41:44 -070012#include "aos/common/time.h"
Brian Silverman66f079a2013-08-26 16:24:30 -070013#include "aos/common/network_port.h"
brians343bc112013-02-10 01:53:46 +000014
15namespace aos {
16
17class Socket {
18 public:
19 int LastStatus() const { return last_ret_; }
20
Brian Silverman3204dd82013-03-12 18:42:01 -070021 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
brians343bc112013-02-10 01:53:46 +000036 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 Silvermana9cbe302013-03-12 18:41:44 -070044 union {
brians343bc112013-02-10 01:53:46 +000045 sockaddr_in in;
46 sockaddr addr;
47 } addr_; // filled in by Connect
48
Brian Silvermana9cbe302013-03-12 18:41:44 -070049 int socket_;
brians343bc112013-02-10 01:53:46 +000050 int last_ret_;
51};
52
Brian Silvermana9cbe302013-03-12 18:41:44 -070053} // namespace aos
brians343bc112013-02-10 01:53:46 +000054
Brian Silvermana9cbe302013-03-12 18:41:44 -070055#endif // AOS_COMMON_NETWORK_SOCKET_H_