OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 NET_SOCKET_TCP_SOCKET_POSIX_H_ | 5 #ifndef NET_SOCKET_TCP_SOCKET_POSIX_H_ |
6 #define NET_SOCKET_TCP_SOCKET_POSIX_H_ | 6 #define NET_SOCKET_TCP_SOCKET_POSIX_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
| 11 |
10 #include "base/callback.h" | 12 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "net/base/address_family.h" | 16 #include "net/base/address_family.h" |
16 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
18 #include "net/base/socket_performance_watcher.h" | 19 #include "net/base/socket_performance_watcher.h" |
19 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class TickClock; | 23 class TickClock; |
23 } | 24 } |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 | 27 |
27 class AddressList; | 28 class AddressList; |
28 class IOBuffer; | 29 class IOBuffer; |
29 class IPEndPoint; | 30 class IPEndPoint; |
30 class SocketPosix; | 31 class SocketPosix; |
31 | 32 |
32 class NET_EXPORT TCPSocketPosix { | 33 class NET_EXPORT TCPSocketPosix { |
33 public: | 34 public: |
34 // |socket_performance_watcher| is notified of the performance metrics related | 35 // |socket_performance_watcher| is notified of the performance metrics related |
35 // to this socket. |socket_performance_watcher| may be null. | 36 // to this socket. |socket_performance_watcher| may be null. |
36 TCPSocketPosix( | 37 TCPSocketPosix( |
37 scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 38 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
38 NetLog* net_log, | 39 NetLog* net_log, |
39 const NetLog::Source& source); | 40 const NetLog::Source& source); |
40 virtual ~TCPSocketPosix(); | 41 virtual ~TCPSocketPosix(); |
41 | 42 |
42 int Open(AddressFamily family); | 43 int Open(AddressFamily family); |
43 // Takes ownership of |socket_fd|. | 44 // Takes ownership of |socket_fd|. |
44 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address); | 45 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address); |
45 | 46 |
46 int Bind(const IPEndPoint& address); | 47 int Bind(const IPEndPoint& address); |
47 | 48 |
48 int Listen(int backlog); | 49 int Listen(int backlog); |
49 int Accept(scoped_ptr<TCPSocketPosix>* socket, | 50 int Accept(std::unique_ptr<TCPSocketPosix>* socket, |
50 IPEndPoint* address, | 51 IPEndPoint* address, |
51 const CompletionCallback& callback); | 52 const CompletionCallback& callback); |
52 | 53 |
53 int Connect(const IPEndPoint& address, const CompletionCallback& callback); | 54 int Connect(const IPEndPoint& address, const CompletionCallback& callback); |
54 bool IsConnected() const; | 55 bool IsConnected() const; |
55 bool IsConnectedAndIdle() const; | 56 bool IsConnectedAndIdle() const; |
56 | 57 |
57 // Multiple outstanding requests are not supported. | 58 // Multiple outstanding requests are not supported. |
58 // Full duplex mode (reading and writing at the same time) is supported. | 59 // Full duplex mode (reading and writing at the same time) is supported. |
59 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 60 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // succeeds in establishing a connection. The corresponding log will have | 99 // succeeds in establishing a connection. The corresponding log will have |
99 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a | 100 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a |
100 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of | 101 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of |
101 // NetLog::TYPE_TCP_CONNECT. | 102 // NetLog::TYPE_TCP_CONNECT. |
102 // | 103 // |
103 // TODO(yzshen): Change logging format and let TCPClientSocket log the | 104 // TODO(yzshen): Change logging format and let TCPClientSocket log the |
104 // start/end of a series of connect attempts itself. | 105 // start/end of a series of connect attempts itself. |
105 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); | 106 void StartLoggingMultipleConnectAttempts(const AddressList& addresses); |
106 void EndLoggingMultipleConnectAttempts(int net_error); | 107 void EndLoggingMultipleConnectAttempts(int net_error); |
107 | 108 |
108 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock); | 109 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock); |
109 | 110 |
110 const BoundNetLog& net_log() const { return net_log_; } | 111 const BoundNetLog& net_log() const { return net_log_; } |
111 | 112 |
112 private: | 113 private: |
113 // States that using a socket with TCP FastOpen can lead to. | 114 // States that using a socket with TCP FastOpen can lead to. |
114 enum TCPFastOpenStatus { | 115 enum TCPFastOpenStatus { |
115 TCP_FASTOPEN_STATUS_UNKNOWN, | 116 TCP_FASTOPEN_STATUS_UNKNOWN, |
116 | 117 |
117 // The initial FastOpen connect attempted returned synchronously, | 118 // The initial FastOpen connect attempted returned synchronously, |
118 // indicating that we had and sent a cookie along with the initial data. | 119 // indicating that we had and sent a cookie along with the initial data. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // is attempted, and may capture some cases where the status is registered | 173 // is attempted, and may capture some cases where the status is registered |
173 // but no connect is subsequently attempted. | 174 // but no connect is subsequently attempted. |
174 // TODO(jri): The expectation is that such cases are not the common case | 175 // TODO(jri): The expectation is that such cases are not the common case |
175 // with TCP FastOpen for SSL sockets however. Change code to be more | 176 // with TCP FastOpen for SSL sockets however. Change code to be more |
176 // accurate when TCP FastOpen is used for more than just SSL sockets. | 177 // accurate when TCP FastOpen is used for more than just SSL sockets. |
177 TCP_FASTOPEN_PREVIOUSLY_FAILED, | 178 TCP_FASTOPEN_PREVIOUSLY_FAILED, |
178 | 179 |
179 TCP_FASTOPEN_MAX_VALUE | 180 TCP_FASTOPEN_MAX_VALUE |
180 }; | 181 }; |
181 | 182 |
182 void AcceptCompleted(scoped_ptr<TCPSocketPosix>* tcp_socket, | 183 void AcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket, |
183 IPEndPoint* address, | 184 IPEndPoint* address, |
184 const CompletionCallback& callback, | 185 const CompletionCallback& callback, |
185 int rv); | 186 int rv); |
186 int HandleAcceptCompleted(scoped_ptr<TCPSocketPosix>* tcp_socket, | 187 int HandleAcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket, |
187 IPEndPoint* address, | 188 IPEndPoint* address, |
188 int rv); | 189 int rv); |
189 int BuildTcpSocketPosix(scoped_ptr<TCPSocketPosix>* tcp_socket, | 190 int BuildTcpSocketPosix(std::unique_ptr<TCPSocketPosix>* tcp_socket, |
190 IPEndPoint* address); | 191 IPEndPoint* address); |
191 | 192 |
192 void ConnectCompleted(const CompletionCallback& callback, int rv); | 193 void ConnectCompleted(const CompletionCallback& callback, int rv); |
193 int HandleConnectCompleted(int rv); | 194 int HandleConnectCompleted(int rv); |
194 void LogConnectBegin(const AddressList& addresses) const; | 195 void LogConnectBegin(const AddressList& addresses) const; |
195 void LogConnectEnd(int net_error) const; | 196 void LogConnectEnd(int net_error) const; |
196 | 197 |
197 void ReadCompleted(const scoped_refptr<IOBuffer>& buf, | 198 void ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
198 const CompletionCallback& callback, | 199 const CompletionCallback& callback, |
199 int rv); | 200 int rv); |
200 int HandleReadCompleted(IOBuffer* buf, int rv); | 201 int HandleReadCompleted(IOBuffer* buf, int rv); |
201 | 202 |
202 void WriteCompleted(const scoped_refptr<IOBuffer>& buf, | 203 void WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
203 const CompletionCallback& callback, | 204 const CompletionCallback& callback, |
204 int rv); | 205 int rv); |
205 int HandleWriteCompleted(IOBuffer* buf, int rv); | 206 int HandleWriteCompleted(IOBuffer* buf, int rv); |
206 int TcpFastOpenWrite(IOBuffer* buf, | 207 int TcpFastOpenWrite(IOBuffer* buf, |
207 int buf_len, | 208 int buf_len, |
208 const CompletionCallback& callback); | 209 const CompletionCallback& callback); |
209 | 210 |
210 // Notifies |socket_performance_watcher_| of the latest RTT estimate available | 211 // Notifies |socket_performance_watcher_| of the latest RTT estimate available |
211 // from the tcp_info struct for this TCP socket. | 212 // from the tcp_info struct for this TCP socket. |
212 void NotifySocketPerformanceWatcher(); | 213 void NotifySocketPerformanceWatcher(); |
213 | 214 |
214 // Called after the first read completes on a TCP FastOpen socket. | 215 // Called after the first read completes on a TCP FastOpen socket. |
215 void UpdateTCPFastOpenStatusAfterRead(); | 216 void UpdateTCPFastOpenStatusAfterRead(); |
216 | 217 |
217 scoped_ptr<SocketPosix> socket_; | 218 std::unique_ptr<SocketPosix> socket_; |
218 scoped_ptr<SocketPosix> accept_socket_; | 219 std::unique_ptr<SocketPosix> accept_socket_; |
219 | 220 |
220 // Socket performance statistics (such as RTT) are reported to the | 221 // Socket performance statistics (such as RTT) are reported to the |
221 // |socket_performance_watcher_|. May be nullptr. | 222 // |socket_performance_watcher_|. May be nullptr. |
222 scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher_; | 223 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_; |
223 | 224 |
224 scoped_ptr<base::TickClock> tick_clock_; | 225 std::unique_ptr<base::TickClock> tick_clock_; |
225 | 226 |
226 // Minimum interval betweeen consecutive notifications to | 227 // Minimum interval betweeen consecutive notifications to |
227 // |socket_performance_watcher_|. | 228 // |socket_performance_watcher_|. |
228 const base::TimeDelta rtt_notifications_minimum_interval_; | 229 const base::TimeDelta rtt_notifications_minimum_interval_; |
229 | 230 |
230 // Time when the |socket_performance_watcher_| was last notified of updated | 231 // Time when the |socket_performance_watcher_| was last notified of updated |
231 // RTT. | 232 // RTT. |
232 base::TimeTicks last_rtt_notification_; | 233 base::TimeTicks last_rtt_notification_; |
233 | 234 |
234 // Enables experimental TCP FastOpen option. | 235 // Enables experimental TCP FastOpen option. |
(...skipping 11 matching lines...) Expand all Loading... |
246 bool logging_multiple_connect_attempts_; | 247 bool logging_multiple_connect_attempts_; |
247 | 248 |
248 BoundNetLog net_log_; | 249 BoundNetLog net_log_; |
249 | 250 |
250 DISALLOW_COPY_AND_ASSIGN(TCPSocketPosix); | 251 DISALLOW_COPY_AND_ASSIGN(TCPSocketPosix); |
251 }; | 252 }; |
252 | 253 |
253 } // namespace net | 254 } // namespace net |
254 | 255 |
255 #endif // NET_SOCKET_TCP_SOCKET_POSIX_H_ | 256 #endif // NET_SOCKET_TCP_SOCKET_POSIX_H_ |
OLD | NEW |