| 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 WasNpnNegotiated() const override; | 64 bool WasNpnNegotiated() 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 Write(IOBuffer* buf, | 75 int Write(IOBuffer* buf, |
| 72 int buf_len, | 76 int buf_len, |
| 73 const CompletionCallback& callback) override; | 77 const CompletionCallback& callback) override; |
| 74 int SetReceiveBufferSize(int32_t size) override; | 78 int SetReceiveBufferSize(int32_t size) override; |
| 75 int SetSendBufferSize(int32_t size) override; | 79 int SetSendBufferSize(int32_t size) override; |
| 76 | 80 |
| 77 virtual bool SetKeepAlive(bool enable, int delay); | 81 virtual bool SetKeepAlive(bool enable, int delay); |
| 78 virtual bool SetNoDelay(bool no_delay); | 82 virtual bool SetNoDelay(bool no_delay); |
| 79 | 83 |
| 80 void GetConnectionAttempts(ConnectionAttempts* out) const override; | 84 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 81 void ClearConnectionAttempts() override; | 85 void ClearConnectionAttempts() override; |
| 82 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; | 86 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
| 83 int64_t GetTotalReceivedBytes() const override; | 87 int64_t GetTotalReceivedBytes() const override; |
| 84 | 88 |
| 89 // base::PowerObserver methods: |
| 90 void OnSuspend() override; |
| 91 |
| 85 private: | 92 private: |
| 86 // State machine for connecting the socket. | 93 // State machine for connecting the socket. |
| 87 enum ConnectState { | 94 enum ConnectState { |
| 88 CONNECT_STATE_CONNECT, | 95 CONNECT_STATE_CONNECT, |
| 89 CONNECT_STATE_CONNECT_COMPLETE, | 96 CONNECT_STATE_CONNECT_COMPLETE, |
| 90 CONNECT_STATE_NONE, | 97 CONNECT_STATE_NONE, |
| 91 }; | 98 }; |
| 92 | 99 |
| 93 // State machine used by Connect(). | 100 // State machine used by Connect(). |
| 94 int DoConnectLoop(int result); | 101 int DoConnectLoop(int result); |
| 95 int DoConnect(); | 102 int DoConnect(); |
| 96 int DoConnectComplete(int result); | 103 int DoConnectComplete(int result); |
| 97 | 104 |
| 98 // Helper used by Disconnect(), which disconnects minus resetting | 105 // Helper used by Disconnect(), which disconnects minus resetting |
| 99 // current_address_index_ and bind_address_. | 106 // current_address_index_ and bind_address_. |
| 100 void DoDisconnect(); | 107 void DoDisconnect(); |
| 101 | 108 |
| 102 void DidCompleteConnect(int result); | 109 void DidCompleteConnect(int result); |
| 103 void DidCompleteRead(const CompletionCallback& callback, int result); | 110 void DidCompleteRead(int result); |
| 104 void DidCompleteWrite(const CompletionCallback& callback, int result); | 111 void DidCompleteWrite(int result); |
| 105 void DidCompleteReadWrite(const CompletionCallback& callback, int result); | 112 void DidCompleteReadWrite(const CompletionCallback& callback, int result); |
| 106 | 113 |
| 107 int OpenSocket(AddressFamily family); | 114 int OpenSocket(AddressFamily family); |
| 108 | 115 |
| 109 // Emits histograms for TCP metrics, at the time the socket is | 116 // Emits histograms for TCP metrics, at the time the socket is |
| 110 // disconnected. | 117 // disconnected. |
| 111 void EmitTCPMetricsHistogramsOnDisconnect(); | 118 void EmitTCPMetricsHistogramsOnDisconnect(); |
| 112 | 119 |
| 113 // Socket performance statistics (such as RTT) are reported to the | 120 // Socket performance statistics (such as RTT) are reported to the |
| 114 // |socket_performance_watcher_|. May be nullptr. | 121 // |socket_performance_watcher_|. May be nullptr. |
| 115 // |socket_performance_watcher_| is owned by |socket_|. If non-null, | 122 // |socket_performance_watcher_| is owned by |socket_|. If non-null, |
| 116 // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_| | 123 // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_| |
| 117 // is destroyed. | 124 // is destroyed. |
| 118 SocketPerformanceWatcher* socket_performance_watcher_; | 125 SocketPerformanceWatcher* socket_performance_watcher_; |
| 119 | 126 |
| 120 std::unique_ptr<TCPSocket> socket_; | 127 std::unique_ptr<TCPSocket> socket_; |
| 121 | 128 |
| 122 // Local IP address and port we are bound to. Set to NULL if Bind() | 129 // Local IP address and port we are bound to. Set to NULL if Bind() |
| 123 // wasn't called (in that case OS chooses address/port). | 130 // wasn't called (in that case OS chooses address/port). |
| 124 std::unique_ptr<IPEndPoint> bind_address_; | 131 std::unique_ptr<IPEndPoint> bind_address_; |
| 125 | 132 |
| 126 // The list of addresses we should try in order to establish a connection. | 133 // The list of addresses we should try in order to establish a connection. |
| 127 AddressList addresses_; | 134 AddressList addresses_; |
| 128 | 135 |
| 129 // Where we are in above list. Set to -1 if uninitialized. | 136 // Where we are in above list. Set to -1 if uninitialized. |
| 130 int current_address_index_; | 137 int current_address_index_; |
| 131 | 138 |
| 132 // External callback; called when connect is complete. | 139 // External callbacks; called when corresponding operations are complete. |
| 140 // Cleared when no such operation is pending. |
| 133 CompletionCallback connect_callback_; | 141 CompletionCallback connect_callback_; |
| 142 CompletionCallback read_callback_; |
| 143 CompletionCallback write_callback_; |
| 134 | 144 |
| 135 // The next state for the Connect() state machine. | 145 // The next state for the Connect() state machine. |
| 136 ConnectState next_connect_state_; | 146 ConnectState next_connect_state_; |
| 137 | 147 |
| 138 // This socket was previously disconnected and has not been re-connected. | 148 // This socket was previously disconnected and has not been re-connected. |
| 139 bool previously_disconnected_; | 149 bool previously_disconnected_; |
| 140 | 150 |
| 141 // Record of connectivity and transmissions, for use in speculative connection | 151 // Record of connectivity and transmissions, for use in speculative connection |
| 142 // histograms. | 152 // histograms. |
| 143 UseHistory use_history_; | 153 UseHistory use_history_; |
| 144 | 154 |
| 145 // Failed connection attempts made while trying to connect this socket. | 155 // Failed connection attempts made while trying to connect this socket. |
| 146 ConnectionAttempts connection_attempts_; | 156 ConnectionAttempts connection_attempts_; |
| 147 | 157 |
| 148 // Total number of bytes received by the socket. | 158 // Total number of bytes received by the socket. |
| 149 int64_t total_received_bytes_; | 159 int64_t total_received_bytes_; |
| 150 | 160 |
| 161 bool disconnect_on_suspend_; |
| 162 // Set to true if the socket was disconnected due to entering suspend mode. |
| 163 // Once set, read/write operations return ERR_NETWORK_IO_SUSPENDED, until |
| 164 // Connect() or Disconnect() is called. |
| 165 bool was_disconnected_on_suspend_; |
| 166 |
| 167 base::WeakPtrFactory<TCPClientSocket> weak_ptr_factory_; |
| 168 |
| 151 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); | 169 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); |
| 152 }; | 170 }; |
| 153 | 171 |
| 154 } // namespace net | 172 } // namespace net |
| 155 | 173 |
| 156 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ | 174 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
| OLD | NEW |