| 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> |
| 11 #include <sys/un.h> | 11 #include <sys/un.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 16 | 17 |
| 17 namespace forwarder2 { | 18 namespace forwarder2 { |
| 18 | 19 |
| 19 // Wrapper class around unix socket api. Can be used to create, bind or | 20 // Wrapper class around unix socket api. Can be used to create, bind or |
| 20 // connect to both Unix domain sockets and TCP sockets. | 21 // connect to both Unix domain sockets and TCP sockets. |
| 21 // TODO(pliard): Split this class into TCPSocket and UnixDomainSocket. | 22 // TODO(pliard): Split this class into TCPSocket and UnixDomainSocket. |
| 22 class Socket { | 23 class Socket { |
| 23 public: | 24 public: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // is performed. Returns number of bytes written, which can be different from | 63 // is performed. Returns number of bytes written, which can be different from |
| 63 // num_bytes in case of errror. | 64 // num_bytes in case of errror. |
| 64 int WriteNumBytes(const void* buffer, size_t num_bytes); | 65 int WriteNumBytes(const void* buffer, size_t num_bytes); |
| 65 | 66 |
| 66 // Calls WriteNumBytes for the given std::string. Note that the null | 67 // Calls WriteNumBytes for the given std::string. Note that the null |
| 67 // terminator is not written to the socket. | 68 // terminator is not written to the socket. |
| 68 int WriteString(const std::string& buffer); | 69 int WriteString(const std::string& buffer); |
| 69 | 70 |
| 70 bool has_error() const { return socket_error_; } | 71 bool has_error() const { return socket_error_; } |
| 71 | 72 |
| 72 // |exit_notifier_fd| must be a valid pipe file descriptor created from the | 73 // |event_fd| must be a valid pipe file descriptor created from the |
| 73 // PipeNotifier and must live (not be closed) at least as long as this socket | 74 // PipeNotifier and must live (not be closed) at least as long as this socket |
| 74 // is alive. | 75 // is alive. |
| 75 void set_exit_notifier_fd(int exit_notifier_fd) { | 76 void AddEventFd(int event_fd); |
| 76 exit_notifier_fd_ = exit_notifier_fd; | |
| 77 } | |
| 78 // Unset the |exit_notifier_fd_| so that it will not receive notifications | |
| 79 // anymore. | |
| 80 void reset_exit_notifier_fd() { exit_notifier_fd_ = -1; } | |
| 81 | 77 |
| 82 // Returns whether Accept() or Connect() was interrupted because the socket | 78 // Returns whether Accept() or Connect() was interrupted because the socket |
| 83 // received an exit notification. | 79 // received an external event fired through the provided fd. |
| 84 bool exited() const { return exited_; } | 80 bool DidReceiveEventOnFd(int fd) const; |
| 81 |
| 82 bool DidReceiveEvent() const { return !fired_events_.empty(); }; |
| 85 | 83 |
| 86 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | 84 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); |
| 87 | 85 |
| 88 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); | 86 static pid_t GetUnixDomainSocketProcessOwner(const std::string& path); |
| 89 | 87 |
| 90 private: | 88 private: |
| 91 union SockAddr { | 89 union SockAddr { |
| 92 // IPv4 sockaddr | 90 // IPv4 sockaddr |
| 93 sockaddr_in addr4; | 91 sockaddr_in addr4; |
| 94 // IPv6 sockaddr | 92 // IPv6 sockaddr |
| (...skipping 28 matching lines...) Expand all Loading... |
| 123 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). | 121 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX). |
| 124 int family_; | 122 int family_; |
| 125 | 123 |
| 126 SockAddr addr_; | 124 SockAddr addr_; |
| 127 | 125 |
| 128 // Points to one of the members of the above union depending on the family. | 126 // Points to one of the members of the above union depending on the family. |
| 129 sockaddr* addr_ptr_; | 127 sockaddr* addr_ptr_; |
| 130 // Length of one of the members of the above union depending on the family. | 128 // Length of one of the members of the above union depending on the family. |
| 131 socklen_t addr_len_; | 129 socklen_t addr_len_; |
| 132 | 130 |
| 133 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application | 131 // Sorted file descriptors from PipeNotifier (see pipe_notifier.h) to send |
| 134 // exit notifications before calling socket blocking operations such as Read | 132 // application exit notifications before calling socket blocking operations |
| 135 // and Accept. | 133 // such as Read and Accept. |
| 136 int exit_notifier_fd_; | 134 std::vector<int> event_fds_; |
| 137 | 135 |
| 138 bool exited_; | 136 std::vector<int> fired_events_; |
| 139 | 137 |
| 140 DISALLOW_COPY_AND_ASSIGN(Socket); | 138 DISALLOW_COPY_AND_ASSIGN(Socket); |
| 141 }; | 139 }; |
| 142 | 140 |
| 143 } // namespace forwarder | 141 } // namespace forwarder |
| 144 | 142 |
| 145 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | 143 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ |
| OLD | NEW |