Chromium Code Reviews| 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> | |
| 14 | |
| 15 #include "base/basictypes.h" | |
| 16 | |
| 17 using std::string; | |
|
bulach
2012/09/07 08:30:28
nit: this is uncommon, we normally just have "std:
tfarina
2012/09/07 11:39:39
specially in a HEADER file! ;)
felipeg
2012/09/07 13:19:26
Done.
felipeg
2012/09/07 13:19:26
Done.
| |
| 18 | |
| 19 namespace forwarder { | |
|
bulach
2012/09/07 08:30:28
nit: I guess it'd be clearer as forwarder2
felipeg
2012/09/07 13:19:26
Done.
| |
| 20 | |
| 21 // Wrapper class around unix socket api. Can be used to create, bind or | |
| 22 // connect to both Unix domain sockets and TCP sockets. | |
| 23 class Socket { | |
| 24 public: | |
| 25 Socket(); | |
| 26 ~Socket(); | |
| 27 | |
| 28 bool BindUnix(const string& path, bool abstract); | |
| 29 bool BindTcp(const string& host, int port); | |
| 30 bool ConnectUnix(const string& path, bool abstract); | |
| 31 bool ConnectTcp(const string& host, int port); | |
| 32 | |
| 33 // Just a wrapper around unix socket shutdown(), see man 2 shutdown. | |
| 34 void Shutdown(); | |
| 35 | |
| 36 // Just a wrapper around unix socket close(), see man 2 close. | |
| 37 void Close(); | |
| 38 bool IsClosed() const { return socket_ < 0; } | |
| 39 | |
| 40 bool Accept(Socket* new_socket); | |
| 41 | |
| 42 bool IsFdInSet(const fd_set& fds) const; | |
| 43 bool AddFdToSet(fd_set* fds) const; | |
| 44 | |
| 45 // Just a wrapper around unix read() function. | |
| 46 // Reads up to buffer_size, but may read less then buffer_size. | |
| 47 // Returns the number of bytes read. | |
| 48 int Read(char* buffer, size_t buffer_size); | |
| 49 | |
| 50 // Same as Read(), just a wrapper around write(). | |
| 51 int Write(const char* buffer, size_t count); | |
| 52 | |
| 53 // Calls Read() multiple times until num_bytes is written to the provided | |
| 54 // buffer. No bounds checking is performed. | |
| 55 // Returns number of bytes read, which can be different from num_bytes in case | |
| 56 // of errror. | |
| 57 int ReadNumBytes(char* buffer, size_t num_bytes); | |
| 58 | |
| 59 // Calls Write() multiple times until num_bytes is written. No bounds checking | |
| 60 // is performed. Returns number of bytes written, which can be different from | |
| 61 // num_bytes in case of errror. | |
| 62 int WriteNumBytes(const char* buffer, size_t num_bytes); | |
| 63 | |
| 64 // Calls WriteNumBytes for the given string. | |
| 65 int WriteString(const string& buffer) { | |
| 66 return WriteNumBytes(buffer.c_str(), buffer.size()); | |
|
bulach
2012/09/07 08:30:28
nit: shouldn't be inlined, move to the .cc file..
felipeg
2012/09/07 13:19:26
Done.
| |
| 67 } | |
| 68 | |
| 69 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2); | |
|
bulach
2012/09/07 08:30:28
nit: move this one to 80
felipeg
2012/09/07 13:19:26
Done.
| |
| 70 | |
| 71 bool HasError() const { return socket_error_; } | |
|
bulach
2012/09/07 08:30:28
nit: "has_error", otherwise it can't be inlined..
felipeg
2012/09/07 13:19:26
Done.
digit
2012/09/07 13:47:12
The style guide says that lower_case() method shou
tfarina
2012/09/07 13:50:46
Like digit pointed out. I think it's fine as is. T
felipeg
2012/09/07 13:56:57
Done.
felipeg
2012/09/07 13:56:57
Done.
bulach
2012/09/07 14:03:32
it's a recommendation from:
http://dev.chromium.or
| |
| 72 | |
| 73 // |notifier_fd| must be a valid pipe file descriptor created from the | |
| 74 // PipeNotifier and must live (not be closed) at least as long as this socket | |
| 75 // is alive. | |
| 76 void set_notifier_fd(int notifier_fd) { exit_notifier_fd_ = notifier_fd; } | |
|
digit
2012/09/07 13:47:12
nit: this doesn't match the internal field name.
felipeg
2012/09/07 13:56:57
Done.
| |
| 77 // Unset the |exit_notifier_fd_| so that it will not receive notifications | |
| 78 // anymore. | |
| 79 void reset_notifier_fd() { exit_notifier_fd_ = -1; } | |
| 80 | |
| 81 private: | |
| 82 // If |host| is empty, use localhost. | |
| 83 bool InitTcpSocket(const string& host, int port); | |
| 84 bool InitUnixSocket(const string& path, bool abstract); | |
| 85 bool BindAndListen(); | |
| 86 bool Connect(); | |
| 87 | |
| 88 bool Resolve(const string& host); | |
| 89 bool InitSocketInternal(); | |
| 90 void SetSocketError(); | |
| 91 | |
| 92 // Waits until either the Socket or the |exit_notifier_fd_| has received a | |
| 93 // read event (accept or read). Returns false iff an exit notification was | |
| 94 // received. | |
| 95 bool WaitForEvent() const; | |
| 96 | |
| 97 int socket_; | |
| 98 int port_; | |
| 99 bool socket_error_; | |
| 100 | |
| 101 // Family of the socket (IF_INET, IF_INET6 or PF_UNIX). | |
| 102 int family_; | |
| 103 | |
| 104 // True if this is an abstract unix domain socket. | |
| 105 bool abstract_; | |
| 106 | |
| 107 typedef union { | |
|
tfarina
2012/09/07 11:39:39
Also move it to the beginning of private section.
tfarina
2012/09/07 11:39:39
does this needs to be typedefed? this is c++ land,
felipeg
2012/09/07 13:19:26
Done.
felipeg
2012/09/07 13:19:26
Done.
| |
| 108 // IPv4 sockaddr | |
| 109 sockaddr_in addr4; | |
| 110 // IPv6 sockaddr | |
| 111 sockaddr_in6 addr6; | |
| 112 // Unix Domain sockaddr | |
| 113 sockaddr_un addr_un; | |
| 114 } SockAddr; | |
| 115 | |
| 116 SockAddr addr_; | |
| 117 | |
| 118 // Points to one of the members of the above union depending on the family. | |
| 119 sockaddr* addr_ptr_; | |
| 120 // Length of one of the members of the above union depending on the family. | |
| 121 socklen_t addr_len_; | |
| 122 | |
| 123 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application | |
| 124 // exit notifications before calling socket blocking operations such as Read | |
| 125 // and Accept. | |
| 126 int exit_notifier_fd_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(Socket); | |
| 129 }; | |
| 130 | |
| 131 } // namespace forwarder | |
| 132 | |
| 133 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_ | |
| OLD | NEW |