OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_CLIENT_SOCKET_H_ | 5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ | 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/power_monitor/power_observer.h" |
14 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
15 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
17 #include "net/socket/connection_attempts.h" | 19 #include "net/socket/connection_attempts.h" |
18 #include "net/socket/stream_socket.h" | 20 #include "net/socket/stream_socket.h" |
19 #include "net/socket/tcp_socket.h" | 21 #include "net/socket/tcp_socket.h" |
20 | 22 |
21 namespace net { | 23 namespace net { |
22 | 24 |
23 class NetLog; | 25 class NetLog; |
24 struct NetLogSource; | 26 struct NetLogSource; |
25 class SocketPerformanceWatcher; | 27 class SocketPerformanceWatcher; |
26 | 28 |
27 // A client socket that uses TCP as the transport layer. | 29 // A client socket that uses TCP as the transport layer. |
28 class NET_EXPORT TCPClientSocket : public StreamSocket { | 30 class NET_EXPORT TCPClientSocket : public StreamSocket, |
| 31 public base::PowerObserver { |
29 public: | 32 public: |
30 // The IP address(es) and port number to connect to. The TCP socket will try | 33 // The IP address(es) and port number to connect to. The TCP socket will try |
31 // each IP address in the list until it succeeds in establishing a | 34 // each IP address in the list until it succeeds in establishing a |
32 // connection. | 35 // connection. |
33 TCPClientSocket( | 36 TCPClientSocket( |
34 const AddressList& addresses, | 37 const AddressList& addresses, |
35 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 38 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
36 net::NetLog* net_log, | 39 net::NetLog* net_log, |
37 const net::NetLogSource& source); | 40 const net::NetLogSource& source); |
38 | 41 |
(...skipping 15 matching lines...) Expand all Loading... |
54 int GetPeerAddress(IPEndPoint* address) const override; | 57 int GetPeerAddress(IPEndPoint* address) const override; |
55 int GetLocalAddress(IPEndPoint* address) const override; | 58 int GetLocalAddress(IPEndPoint* address) const override; |
56 const NetLogWithSource& NetLog() const override; | 59 const NetLogWithSource& NetLog() const override; |
57 void SetSubresourceSpeculation() override; | 60 void SetSubresourceSpeculation() override; |
58 void SetOmniboxSpeculation() override; | 61 void SetOmniboxSpeculation() override; |
59 bool WasEverUsed() const override; | 62 bool WasEverUsed() const override; |
60 void EnableTCPFastOpenIfSupported() override; | 63 void EnableTCPFastOpenIfSupported() override; |
61 bool WasAlpnNegotiated() const override; | 64 bool WasAlpnNegotiated() const override; |
62 NextProto GetNegotiatedProtocol() const override; | 65 NextProto GetNegotiatedProtocol() const override; |
63 bool GetSSLInfo(SSLInfo* ssl_info) override; | 66 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 67 void SetFailOnSuspend(bool disconnect_on_suspend) override; |
64 | 68 |
65 // Socket implementation. | 69 // Socket implementation. |
66 // Multiple outstanding requests are not supported. | 70 // Multiple outstanding requests are not supported. |
67 // Full duplex mode (reading and writing at the same time) is supported. | 71 // Full duplex mode (reading and writing at the same time) is supported. |
68 int Read(IOBuffer* buf, | 72 int Read(IOBuffer* buf, |
69 int buf_len, | 73 int buf_len, |
70 const CompletionCallback& callback) override; | 74 const CompletionCallback& callback) override; |
71 int ReadIfReady(IOBuffer* buf, | 75 int ReadIfReady(IOBuffer* buf, |
72 int buf_len, | 76 int buf_len, |
73 const CompletionCallback& callback) override; | 77 const CompletionCallback& callback) override; |
74 int Write(IOBuffer* buf, | 78 int Write(IOBuffer* buf, |
75 int buf_len, | 79 int buf_len, |
76 const CompletionCallback& callback) override; | 80 const CompletionCallback& callback) override; |
77 int SetReceiveBufferSize(int32_t size) override; | 81 int SetReceiveBufferSize(int32_t size) override; |
78 int SetSendBufferSize(int32_t size) override; | 82 int SetSendBufferSize(int32_t size) override; |
79 | 83 |
80 virtual bool SetKeepAlive(bool enable, int delay); | 84 virtual bool SetKeepAlive(bool enable, int delay); |
81 virtual bool SetNoDelay(bool no_delay); | 85 virtual bool SetNoDelay(bool no_delay); |
82 | 86 |
83 void GetConnectionAttempts(ConnectionAttempts* out) const override; | 87 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
84 void ClearConnectionAttempts() override; | 88 void ClearConnectionAttempts() override; |
85 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; | 89 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
86 int64_t GetTotalReceivedBytes() const override; | 90 int64_t GetTotalReceivedBytes() const override; |
87 | 91 |
| 92 // base::PowerObserver methods: |
| 93 void OnSuspend() override; |
| 94 |
88 private: | 95 private: |
89 // State machine for connecting the socket. | 96 // State machine for connecting the socket. |
90 enum ConnectState { | 97 enum ConnectState { |
91 CONNECT_STATE_CONNECT, | 98 CONNECT_STATE_CONNECT, |
92 CONNECT_STATE_CONNECT_COMPLETE, | 99 CONNECT_STATE_CONNECT_COMPLETE, |
93 CONNECT_STATE_NONE, | 100 CONNECT_STATE_NONE, |
94 }; | 101 }; |
95 | 102 |
96 // A helper method shared by Read() and ReadIfReady(). If |read_if_ready| is | 103 // A helper method shared by Read() and ReadIfReady(). If |read_if_ready| is |
97 // set to true, ReadIfReady() will be used instead of Read(). | 104 // set to true, ReadIfReady() will be used instead of Read(). |
98 int ReadCommon(IOBuffer* buf, | 105 int ReadCommon(IOBuffer* buf, |
99 int buf_len, | 106 int buf_len, |
100 const CompletionCallback& callback, | 107 const CompletionCallback& callback, |
101 bool read_if_ready); | 108 bool read_if_ready); |
102 | 109 |
103 // State machine used by Connect(). | 110 // State machine used by Connect(). |
104 int DoConnectLoop(int result); | 111 int DoConnectLoop(int result); |
105 int DoConnect(); | 112 int DoConnect(); |
106 int DoConnectComplete(int result); | 113 int DoConnectComplete(int result); |
107 | 114 |
108 // Helper used by Disconnect(), which disconnects minus resetting | 115 // Helper used by Disconnect(), which disconnects minus resetting |
109 // current_address_index_ and bind_address_. | 116 // current_address_index_ and bind_address_. |
110 void DoDisconnect(); | 117 void DoDisconnect(); |
111 | 118 |
112 void DidCompleteConnect(int result); | 119 void DidCompleteConnect(int result); |
113 void DidCompleteRead(const CompletionCallback& callback, int result); | 120 void DidCompleteRead(int result); |
114 void DidCompleteWrite(const CompletionCallback& callback, int result); | 121 void DidCompleteWrite(int result); |
115 void DidCompleteReadWrite(const CompletionCallback& callback, int result); | 122 void DidCompleteReadWrite(const CompletionCallback& callback, int result); |
116 | 123 |
117 int OpenSocket(AddressFamily family); | 124 int OpenSocket(AddressFamily family); |
118 | 125 |
119 // Emits histograms for TCP metrics, at the time the socket is | 126 // Emits histograms for TCP metrics, at the time the socket is |
120 // disconnected. | 127 // disconnected. |
121 void EmitTCPMetricsHistogramsOnDisconnect(); | 128 void EmitTCPMetricsHistogramsOnDisconnect(); |
122 | 129 |
123 // Socket performance statistics (such as RTT) are reported to the | 130 // Socket performance statistics (such as RTT) are reported to the |
124 // |socket_performance_watcher_|. May be nullptr. | 131 // |socket_performance_watcher_|. May be nullptr. |
125 // |socket_performance_watcher_| is owned by |socket_|. If non-null, | 132 // |socket_performance_watcher_| is owned by |socket_|. If non-null, |
126 // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_| | 133 // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_| |
127 // is destroyed. | 134 // is destroyed. |
128 SocketPerformanceWatcher* socket_performance_watcher_; | 135 SocketPerformanceWatcher* socket_performance_watcher_; |
129 | 136 |
130 std::unique_ptr<TCPSocket> socket_; | 137 std::unique_ptr<TCPSocket> socket_; |
131 | 138 |
132 // Local IP address and port we are bound to. Set to NULL if Bind() | 139 // Local IP address and port we are bound to. Set to NULL if Bind() |
133 // wasn't called (in that case OS chooses address/port). | 140 // wasn't called (in that case OS chooses address/port). |
134 std::unique_ptr<IPEndPoint> bind_address_; | 141 std::unique_ptr<IPEndPoint> bind_address_; |
135 | 142 |
136 // The list of addresses we should try in order to establish a connection. | 143 // The list of addresses we should try in order to establish a connection. |
137 AddressList addresses_; | 144 AddressList addresses_; |
138 | 145 |
139 // Where we are in above list. Set to -1 if uninitialized. | 146 // Where we are in above list. Set to -1 if uninitialized. |
140 int current_address_index_; | 147 int current_address_index_; |
141 | 148 |
142 // External callback; called when connect is complete. | 149 // External callbacks; called when corresponding operations are complete. |
| 150 // Cleared when no such operation is pending. |
143 CompletionCallback connect_callback_; | 151 CompletionCallback connect_callback_; |
| 152 CompletionCallback read_callback_; |
| 153 CompletionCallback write_callback_; |
144 | 154 |
145 // The next state for the Connect() state machine. | 155 // The next state for the Connect() state machine. |
146 ConnectState next_connect_state_; | 156 ConnectState next_connect_state_; |
147 | 157 |
148 // This socket was previously disconnected and has not been re-connected. | 158 // This socket was previously disconnected and has not been re-connected. |
149 bool previously_disconnected_; | 159 bool previously_disconnected_; |
150 | 160 |
151 // Record of connectivity and transmissions, for use in speculative connection | 161 // Record of connectivity and transmissions, for use in speculative connection |
152 // histograms. | 162 // histograms. |
153 UseHistory use_history_; | 163 UseHistory use_history_; |
154 | 164 |
155 // Failed connection attempts made while trying to connect this socket. | 165 // Failed connection attempts made while trying to connect this socket. |
156 ConnectionAttempts connection_attempts_; | 166 ConnectionAttempts connection_attempts_; |
157 | 167 |
158 // Total number of bytes received by the socket. | 168 // Total number of bytes received by the socket. |
159 int64_t total_received_bytes_; | 169 int64_t total_received_bytes_; |
160 | 170 |
| 171 bool disconnect_on_suspend_; |
| 172 // Set to true if the socket was disconnected due to entering suspend mode. |
| 173 // Once set, read/write operations return ERR_NETWORK_IO_SUSPENDED, until |
| 174 // Connect() or Disconnect() is called. |
| 175 bool was_disconnected_on_suspend_; |
| 176 |
| 177 base::WeakPtrFactory<TCPClientSocket> weak_ptr_factory_; |
| 178 |
161 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); | 179 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); |
162 }; | 180 }; |
163 | 181 |
164 } // namespace net | 182 } // namespace net |
165 | 183 |
166 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ | 184 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
OLD | NEW |