OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
7 | |
8 #include <fcntl.h> | |
9 #include <netinet/in.h> | |
10 #include <sys/socket.h> | |
11 #include <sys/un.h> | |
12 | |
13 #include <string> | |
Satish
2012/09/06 15:53:12
move up to line 10
felipeg
2012/09/06 16:19:06
But linter tool says C++ headers should come in a
| |
14 | |
15 #include "base/basictypes.h" | |
16 | |
17 using std::string; | |
18 | |
19 namespace forwarder { | |
20 | |
21 class Socket { | |
Satish
2012/09/06 15:53:12
add a comment explaining what this class does
felipeg
2012/09/06 16:19:06
Done.
| |
22 public: | |
23 Socket(); | |
24 ~Socket(); | |
25 | |
26 bool BindUnix(const string& path, bool abstract); | |
27 bool BindTcp(const string& host, int port); | |
28 bool ConnectUnix(const string& path, bool abstract); | |
29 bool ConnectTcp(const string& host, int port); | |
30 | |
31 void Shutdown(); | |
Satish
2012/09/06 15:53:12
please add comments explaining what these do, and
felipeg
2012/09/06 16:19:06
This is a wrapper around unix sockets.
The explana
| |
32 void Close(); | |
33 bool IsClosed() const { return socket_ < 0; } | |
34 | |
35 bool Accept(Socket* new_socket); | |
36 | |
37 bool IsFdInSet(const fd_set& fds) const; | |
38 bool AddFdToSet(fd_set* fds) const; | |
39 | |
40 // Just a wrapper around unix read() function. | |
41 // Reads up to buffer_size, but may read less then buffer_size. | |
42 // Returns the number of bytes read. | |
43 int Read(char* buffer, size_t buffer_size); | |
44 // Same as Read(), just a wrapper around write(). | |
Satish
2012/09/06 15:53:12
leave an empty line above full length comments lik
felipeg
2012/09/06 16:19:06
Done.
| |
45 int Write(const char* buffer, size_t count); | |
46 | |
47 // Calls Read() multiple times until num_bytes is written to the provided | |
48 // buffer. No bounds checking is performed. | |
49 // Returns number of bytes read, which can be different from num_bytes in case | |
50 // of errror. | |
51 int ReadNumBytes(char* buffer, size_t num_bytes); | |
52 | |
53 // Calls Write() multiple times until num_bytes is written. No bounds checking | |
54 // is performed. Returns number of bytes written, which can be different from | |
55 // num_bytes in case of errror. | |
56 int WriteNumBytes(const char* buffer, size_t num_bytes); | |
57 | |
58 // Calls WriteNumBytes for the given string. | |
59 int WriteString(const string& buffer) { | |
60 return WriteNumBytes(buffer.c_str(), buffer.size()); | |
61 } | |
62 | |
63 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | |
64 | |
65 bool HasError() const { return socket_error_; } | |
66 | |
67 // |notifier_fd| must be a valid pipe file descriptor created from the | |
68 // PipeNotifier and must live (not be closed) at least as long as this socket | |
69 // is alive. | |
70 void set_notifier_fd(int notifier_fd) { exit_notifier_fd_ = notifier_fd; } | |
71 // Unset the |exit_notifier_fd_| so that it will not receive notifications | |
72 // anymore. | |
73 void unset_notifier_fd() { exit_notifier_fd_ = -1; } | |
Satish
2012/09/06 15:53:12
unset -> reset
felipeg
2012/09/06 16:19:06
Done.
| |
74 | |
75 private: | |
76 // If |host| is empty, use localhost. | |
77 bool InitTcpSocket(const string& host, int port); | |
78 bool InitUnixSocket(const string& path, bool abstract); | |
79 bool BindAndListen(); | |
80 bool Connect(); | |
81 | |
82 bool Resolve(const string& host); | |
83 bool InitSocketInternal(); | |
84 void SetSocketError(); | |
85 | |
86 // Waits until either the Socket or the |exit_notifier_fd_| has received a | |
87 // read event (accept or read). Returns false iff an exit notification was | |
88 // received. | |
89 bool WaitForEvent() const; | |
90 | |
91 int socket_; | |
92 int port_; | |
93 bool socket_error_; | |
94 | |
95 // Family of the socket (IF_INET, IF_INET6 or PF_UNIX). | |
96 int family_; | |
97 | |
98 // True if this is an abstract unix domain socket. | |
99 bool abstract_; | |
100 | |
101 typedef union { | |
102 // IPv4 sockaddr | |
103 sockaddr_in addr4; | |
104 // IPv6 sockaddr | |
105 sockaddr_in6 addr6; | |
106 // Unix Domain sockaddr | |
107 sockaddr_un addr_un; | |
108 } SockAddr; | |
109 | |
110 SockAddr addr_; | |
111 | |
112 // Points to one of the members of the above union depending on the family. | |
113 sockaddr* addr_ptr_; | |
114 // Length of one of the members of the above union depending on the family. | |
115 socklen_t addr_len_; | |
116 | |
117 int exit_notifier_fd_; | |
Satish
2012/09/06 15:53:12
comment please
felipeg
2012/09/06 16:19:06
Done.
| |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(Socket); | |
120 }; | |
121 | |
122 } // namespace forwarder | |
123 | |
124 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
OLD | NEW |