OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
7 | 7 |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 19 matching lines...) Expand all Loading... |
30 bool ConnectUnix(const std::string& path); | 30 bool ConnectUnix(const std::string& path); |
31 bool ConnectTcp(const std::string& host, int port); | 31 bool ConnectTcp(const std::string& host, int port); |
32 | 32 |
33 // Just a wrapper around unix socket shutdown(), see man 2 shutdown. | 33 // Just a wrapper around unix socket shutdown(), see man 2 shutdown. |
34 void Shutdown(); | 34 void Shutdown(); |
35 | 35 |
36 // Just a wrapper around unix socket close(), see man 2 close. | 36 // Just a wrapper around unix socket close(), see man 2 close. |
37 void Close(); | 37 void Close(); |
38 bool IsClosed() const { return socket_ < 0; } | 38 bool IsClosed() const { return socket_ < 0; } |
39 | 39 |
| 40 int fd() const { return socket_; } |
| 41 |
40 bool Accept(Socket* new_socket); | 42 bool Accept(Socket* new_socket); |
41 | 43 |
42 // Returns the port allocated to this socket or zero on error. | 44 // Returns the port allocated to this socket or zero on error. |
43 int GetPort(); | 45 int GetPort(); |
44 | 46 |
45 bool IsFdInSet(const fd_set& fds) const; | |
46 bool AddFdToSet(fd_set* fds) const; | |
47 | |
48 // Just a wrapper around unix read() function. | 47 // Just a wrapper around unix read() function. |
49 // Reads up to buffer_size, but may read less then buffer_size. | 48 // Reads up to buffer_size, but may read less then buffer_size. |
50 // Returns the number of bytes read. | 49 // Returns the number of bytes read. |
51 int Read(void* buffer, size_t buffer_size); | 50 int Read(void* buffer, size_t buffer_size); |
52 | 51 |
53 // Non-blocking version of Read() above. This must be called after a | 52 // Non-blocking version of Read() above. This must be called after a |
54 // successful call to select(). The socket must also be in non-blocking mode | 53 // successful call to select(). The socket must also be in non-blocking mode |
55 // before calling this method. | 54 // before calling this method. |
56 int NonBlockingRead(void* buffer, size_t buffer_size); | 55 int NonBlockingRead(void* buffer, size_t buffer_size); |
57 | 56 |
(...skipping 24 matching lines...) Expand all Loading... |
82 // PipeNotifier and must live (not be closed) at least as long as this socket | 81 // PipeNotifier and must live (not be closed) at least as long as this socket |
83 // is alive. | 82 // is alive. |
84 void AddEventFd(int event_fd); | 83 void AddEventFd(int event_fd); |
85 | 84 |
86 // Returns whether Accept() or Connect() was interrupted because the socket | 85 // Returns whether Accept() or Connect() was interrupted because the socket |
87 // received an external event fired through the provided fd. | 86 // received an external event fired through the provided fd. |
88 bool DidReceiveEventOnFd(int fd) const; | 87 bool DidReceiveEventOnFd(int fd) const; |
89 | 88 |
90 bool DidReceiveEvent() const; | 89 bool DidReceiveEvent() const; |
91 | 90 |
92 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | |
93 | |
94 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); | 91 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); |
95 | 92 |
96 private: | 93 private: |
97 enum EventType { | 94 enum EventType { |
98 READ, | 95 READ, |
99 WRITE | 96 WRITE |
100 }; | 97 }; |
101 | 98 |
102 union SockAddr { | 99 union SockAddr { |
103 // IPv4 sockaddr | 100 // IPv4 sockaddr |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 // Used to listen for external events (e.g. process received a SIGTERM) while | 143 // Used to listen for external events (e.g. process received a SIGTERM) while |
147 // blocking on I/O operations. | 144 // blocking on I/O operations. |
148 std::vector<Event> events_; | 145 std::vector<Event> events_; |
149 | 146 |
150 DISALLOW_COPY_AND_ASSIGN(Socket); | 147 DISALLOW_COPY_AND_ASSIGN(Socket); |
151 }; | 148 }; |
152 | 149 |
153 } // namespace forwarder | 150 } // namespace forwarder |
154 | 151 |
155 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 152 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
OLD | NEW |