Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: tools/android/forwarder2/socket.h

Issue 10916112: Forwarder 2 implementation (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/android/forwarder2/host_forwarder_main.cc ('k') | tools/android/forwarder2/socket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace forwarder2 {
18
19 // Wrapper class around unix socket api. Can be used to create, bind or
20 // connect to both Unix domain sockets and TCP sockets.
21 class Socket {
22 public:
23 Socket();
24 ~Socket();
25
26 bool BindUnix(const std::string& path, bool abstract);
27 bool BindTcp(const std::string& host, int port);
28 bool ConnectUnix(const std::string& path, bool abstract);
29 bool ConnectTcp(const std::string& host, int port);
30
31 // Just a wrapper around unix socket shutdown(), see man 2 shutdown.
32 void Shutdown();
33
34 // Just a wrapper around unix socket close(), see man 2 close.
35 void Close();
36 bool IsClosed() const { return socket_ < 0; }
37
38 bool Accept(Socket* new_socket);
39
40 bool IsFdInSet(const fd_set& fds) const;
41 bool AddFdToSet(fd_set* fds) const;
42
43 // Just a wrapper around unix read() function.
44 // Reads up to buffer_size, but may read less then buffer_size.
45 // Returns the number of bytes read.
46 int Read(char* buffer, size_t buffer_size);
47
48 // Same as Read(), just a wrapper around write().
49 int Write(const char* buffer, size_t count);
50
51 // Calls Read() multiple times until num_bytes is written to the provided
52 // buffer. No bounds checking is performed.
53 // Returns number of bytes read, which can be different from num_bytes in case
54 // of errror.
55 int ReadNumBytes(char* buffer, size_t num_bytes);
56
57 // Calls Write() multiple times until num_bytes is written. No bounds checking
58 // is performed. Returns number of bytes written, which can be different from
59 // num_bytes in case of errror.
60 int WriteNumBytes(const char* buffer, size_t num_bytes);
61
62 // Calls WriteNumBytes for the given std::string.
63 int WriteString(const std::string& buffer);
64
65 bool has_error() const { return socket_error_; }
66
67 // |exit_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_exit_notifier_fd(int exit_notifier_fd) {
71 exit_notifier_fd_ = exit_notifier_fd;
72 }
73 // Unset the |exit_notifier_fd_| so that it will not receive notifications
74 // anymore.
75 void reset_exit_notifier_fd() { exit_notifier_fd_ = -1; }
76
77 static int GetHighestFileDescriptor(const Socket& s1, const Socket& s2);
78
79 private:
80 union SockAddr {
81 // IPv4 sockaddr
82 sockaddr_in addr4;
83 // IPv6 sockaddr
84 sockaddr_in6 addr6;
85 // Unix Domain sockaddr
86 sockaddr_un addr_un;
87 };
88
89 // If |host| is empty, use localhost.
90 bool InitTcpSocket(const std::string& host, int port);
91 bool InitUnixSocket(const std::string& path, bool abstract);
92 bool BindAndListen();
93 bool Connect();
94
95 bool Resolve(const std::string& host);
96 bool InitSocketInternal();
97 void SetSocketError();
98
99 // Waits until either the Socket or the |exit_notifier_fd_| has received a
100 // read event (accept or read). Returns false iff an exit notification was
101 // received.
102 bool WaitForEvent() const;
103
104 int socket_;
105 int port_;
106 bool socket_error_;
107
108 // Family of the socket (PF_INET, PF_INET6 or PF_UNIX).
109 int family_;
110
111 // True if this is an abstract unix domain socket.
112 bool abstract_;
113
114 SockAddr addr_;
115
116 // Points to one of the members of the above union depending on the family.
117 sockaddr* addr_ptr_;
118 // Length of one of the members of the above union depending on the family.
119 socklen_t addr_len_;
120
121 // File descriptor from PipeNotifier (see pipe_notifier.h) to send application
122 // exit notifications before calling socket blocking operations such as Read
123 // and Accept.
124 int exit_notifier_fd_;
125
126 DISALLOW_COPY_AND_ASSIGN(Socket);
127 };
128
129 } // namespace forwarder
130
131 #endif // TOOLS_ANDROID_FORWARDER2_SOCKET_H_
OLDNEW
« no previous file with comments | « tools/android/forwarder2/host_forwarder_main.cc ('k') | tools/android/forwarder2/socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698