[email protected] | d01c6bd | 2011-05-31 23:20:59 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 5 | #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
| 6 | #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 7 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 14 | #include "net/base/address_list.h" |
| 15 | #include "net/base/completion_callback.h" |
[email protected] | 659fd67c | 2013-09-18 21:50:26 | [diff] [blame] | 16 | #include "net/base/net_export.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 17 | #include "net/log/net_log.h" |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 18 | #include "net/socket/connection_attempts.h" |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 19 | #include "net/socket/stream_socket.h" |
| 20 | #include "net/socket/tcp_socket.h" |
| 21 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | namespace net { |
| 23 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 24 | class SocketPerformanceWatcher; |
| 25 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 26 | // A client socket that uses TCP as the transport layer. |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 27 | class NET_EXPORT TCPClientSocket : public StreamSocket { |
| 28 | public: |
| 29 | // The IP address(es) and port number to connect to. The TCP socket will try |
| 30 | // each IP address in the list until it succeeds in establishing a |
| 31 | // connection. |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 32 | TCPClientSocket( |
| 33 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 34 | std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 35 | net::NetLog* net_log, |
| 36 | const net::NetLog::Source& source); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 37 | |
| 38 | // Adopts the given, connected socket and then acts as if Connect() had been |
| 39 | // called. This function is used by TCPServerSocket and for testing. |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 40 | TCPClientSocket(std::unique_ptr<TCPSocket> connected_socket, |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 41 | const IPEndPoint& peer_address); |
| 42 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | ~TCPClientSocket() override; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 44 | |
| 45 | // Binds the socket to a local IP address and port. |
| 46 | int Bind(const IPEndPoint& address); |
| 47 | |
| 48 | // StreamSocket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 49 | int Connect(const CompletionCallback& callback) override; |
| 50 | void Disconnect() override; |
| 51 | bool IsConnected() const override; |
| 52 | bool IsConnectedAndIdle() const override; |
| 53 | int GetPeerAddress(IPEndPoint* address) const override; |
| 54 | int GetLocalAddress(IPEndPoint* address) const override; |
| 55 | const BoundNetLog& NetLog() const override; |
| 56 | void SetSubresourceSpeculation() override; |
| 57 | void SetOmniboxSpeculation() override; |
| 58 | bool WasEverUsed() const override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 59 | void EnableTCPFastOpenIfSupported() override; |
| 60 | bool WasNpnNegotiated() const override; |
| 61 | NextProto GetNegotiatedProtocol() const override; |
| 62 | bool GetSSLInfo(SSLInfo* ssl_info) override; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 63 | |
| 64 | // Socket implementation. |
| 65 | // Multiple outstanding requests are not supported. |
| 66 | // Full duplex mode (reading and writing at the same time) is supported. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 67 | int Read(IOBuffer* buf, |
| 68 | int buf_len, |
| 69 | const CompletionCallback& callback) override; |
| 70 | int Write(IOBuffer* buf, |
| 71 | int buf_len, |
| 72 | const CompletionCallback& callback) override; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 73 | int SetReceiveBufferSize(int32_t size) override; |
| 74 | int SetSendBufferSize(int32_t size) override; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 75 | |
| 76 | virtual bool SetKeepAlive(bool enable, int delay); |
| 77 | virtual bool SetNoDelay(bool no_delay); |
| 78 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 79 | void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 80 | void ClearConnectionAttempts() override; |
| 81 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 82 | int64_t GetTotalReceivedBytes() const override; |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 83 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 84 | private: |
| 85 | // State machine for connecting the socket. |
| 86 | enum ConnectState { |
| 87 | CONNECT_STATE_CONNECT, |
| 88 | CONNECT_STATE_CONNECT_COMPLETE, |
| 89 | CONNECT_STATE_NONE, |
| 90 | }; |
| 91 | |
| 92 | // State machine used by Connect(). |
| 93 | int DoConnectLoop(int result); |
| 94 | int DoConnect(); |
| 95 | int DoConnectComplete(int result); |
| 96 | |
| 97 | // Helper used by Disconnect(), which disconnects minus resetting |
| 98 | // current_address_index_ and bind_address_. |
| 99 | void DoDisconnect(); |
| 100 | |
| 101 | void DidCompleteConnect(int result); |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 102 | void DidCompleteRead(const CompletionCallback& callback, int result); |
| 103 | void DidCompleteWrite(const CompletionCallback& callback, int result); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 104 | void DidCompleteReadWrite(const CompletionCallback& callback, int result); |
| 105 | |
| 106 | int OpenSocket(AddressFamily family); |
| 107 | |
bmcquade | 8b62f47 | 2015-07-08 16:03:54 | [diff] [blame] | 108 | // Emits histograms for TCP metrics, at the time the socket is |
| 109 | // disconnected. |
| 110 | void EmitTCPMetricsHistogramsOnDisconnect(); |
| 111 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 112 | // Socket performance statistics (such as RTT) are reported to the |
| 113 | // |socket_performance_watcher_|. May be nullptr. |
| 114 | // |socket_performance_watcher_| is owned by |socket_|. If non-null, |
| 115 | // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_| |
| 116 | // is destroyed. |
| 117 | SocketPerformanceWatcher* socket_performance_watcher_; |
| 118 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 119 | std::unique_ptr<TCPSocket> socket_; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 120 | |
| 121 | // Local IP address and port we are bound to. Set to NULL if Bind() |
| 122 | // wasn't called (in that case OS chooses address/port). |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 123 | std::unique_ptr<IPEndPoint> bind_address_; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 124 | |
| 125 | // The list of addresses we should try in order to establish a connection. |
| 126 | AddressList addresses_; |
| 127 | |
| 128 | // Where we are in above list. Set to -1 if uninitialized. |
| 129 | int current_address_index_; |
| 130 | |
| 131 | // External callback; called when connect is complete. |
| 132 | CompletionCallback connect_callback_; |
| 133 | |
| 134 | // The next state for the Connect() state machine. |
| 135 | ConnectState next_connect_state_; |
| 136 | |
| 137 | // This socket was previously disconnected and has not been re-connected. |
| 138 | bool previously_disconnected_; |
| 139 | |
| 140 | // Record of connectivity and transmissions, for use in speculative connection |
| 141 | // histograms. |
| 142 | UseHistory use_history_; |
| 143 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 144 | // Failed connection attempts made while trying to connect this socket. |
| 145 | ConnectionAttempts connection_attempts_; |
| 146 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 147 | // Total number of bytes received by the socket. |
| 148 | int64_t total_received_bytes_; |
| 149 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); |
| 151 | }; |
| 152 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 153 | } // namespace net |
| 154 | |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 155 | #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ |