blob: 295e8b68113950afda1d7f0379b840a4bfdf4c1b [file] [log] [blame]
[email protected]d01c6bd2011-05-31 23:20:591// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]f7984fc62009-06-22 23:26:445#ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_
6#define NET_SOCKET_TCP_CLIENT_SOCKET_H_
initial.commit586acc5fe2008-07-26 22:42:527
tbansalf82cc8e2015-10-14 20:05:498#include <stdint.h>
9
danakj655b66c2016-04-16 00:51:3810#include <memory>
11
[email protected]c9080d82013-09-15 15:14:1612#include "base/compiler_specific.h"
Avi Drissman13fc8932015-12-20 04:40:4613#include "base/macros.h"
[email protected]c9080d82013-09-15 15:14:1614#include "net/base/address_list.h"
15#include "net/base/completion_callback.h"
[email protected]659fd67c2013-09-18 21:50:2616#include "net/base/net_export.h"
ttuttle23fdb7b2015-05-15 01:28:0317#include "net/socket/connection_attempts.h"
[email protected]c9080d82013-09-15 15:14:1618#include "net/socket/stream_socket.h"
19#include "net/socket/tcp_socket.h"
20
initial.commit586acc5fe2008-07-26 22:42:5221namespace net {
22
mikecironef22f9812016-10-04 03:40:1923class NetLog;
24struct NetLogSource;
tbansal7b403bcc2016-04-13 22:33:2125class SocketPerformanceWatcher;
26
[email protected]c9080d82013-09-15 15:14:1627// A client socket that uses TCP as the transport layer.
[email protected]c9080d82013-09-15 15:14:1628class NET_EXPORT TCPClientSocket : public StreamSocket {
29 public:
30 // 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
32 // connection.
tbansal7b403bcc2016-04-13 22:33:2133 TCPClientSocket(
34 const AddressList& addresses,
danakj655b66c2016-04-16 00:51:3835 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
tbansal7b403bcc2016-04-13 22:33:2136 net::NetLog* net_log,
mikecironef22f9812016-10-04 03:40:1937 const net::NetLogSource& source);
[email protected]c9080d82013-09-15 15:14:1638
39 // Adopts the given, connected socket and then acts as if Connect() had been
40 // called. This function is used by TCPServerSocket and for testing.
danakj655b66c2016-04-16 00:51:3841 TCPClientSocket(std::unique_ptr<TCPSocket> connected_socket,
[email protected]c9080d82013-09-15 15:14:1642 const IPEndPoint& peer_address);
43
dchengb03027d2014-10-21 12:00:2044 ~TCPClientSocket() override;
[email protected]c9080d82013-09-15 15:14:1645
46 // Binds the socket to a local IP address and port.
47 int Bind(const IPEndPoint& address);
48
49 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2050 int Connect(const CompletionCallback& callback) override;
51 void Disconnect() override;
52 bool IsConnected() const override;
53 bool IsConnectedAndIdle() const override;
54 int GetPeerAddress(IPEndPoint* address) const override;
55 int GetLocalAddress(IPEndPoint* address) const override;
tfarina428341112016-09-22 13:38:2056 const NetLogWithSource& NetLog() const override;
dchengb03027d2014-10-21 12:00:2057 void SetSubresourceSpeculation() override;
58 void SetOmniboxSpeculation() override;
59 bool WasEverUsed() const override;
dchengb03027d2014-10-21 12:00:2060 void EnableTCPFastOpenIfSupported() override;
61 bool WasNpnNegotiated() const override;
62 NextProto GetNegotiatedProtocol() const override;
63 bool GetSSLInfo(SSLInfo* ssl_info) override;
[email protected]c9080d82013-09-15 15:14:1664
65 // Socket implementation.
66 // Multiple outstanding requests are not supported.
67 // Full duplex mode (reading and writing at the same time) is supported.
dchengb03027d2014-10-21 12:00:2068 int Read(IOBuffer* buf,
69 int buf_len,
70 const CompletionCallback& callback) override;
71 int Write(IOBuffer* buf,
72 int buf_len,
73 const CompletionCallback& callback) override;
Avi Drissman13fc8932015-12-20 04:40:4674 int SetReceiveBufferSize(int32_t size) override;
75 int SetSendBufferSize(int32_t size) override;
[email protected]c9080d82013-09-15 15:14:1676
77 virtual bool SetKeepAlive(bool enable, int delay);
78 virtual bool SetNoDelay(bool no_delay);
79
ttuttle23fdb7b2015-05-15 01:28:0380 void GetConnectionAttempts(ConnectionAttempts* out) const override;
81 void ClearConnectionAttempts() override;
82 void AddConnectionAttempts(const ConnectionAttempts& attempts) override;
tbansalf82cc8e2015-10-14 20:05:4983 int64_t GetTotalReceivedBytes() const override;
ttuttle23fdb7b2015-05-15 01:28:0384
[email protected]c9080d82013-09-15 15:14:1685 private:
86 // State machine for connecting the socket.
87 enum ConnectState {
88 CONNECT_STATE_CONNECT,
89 CONNECT_STATE_CONNECT_COMPLETE,
90 CONNECT_STATE_NONE,
91 };
92
93 // State machine used by Connect().
94 int DoConnectLoop(int result);
95 int DoConnect();
96 int DoConnectComplete(int result);
97
98 // Helper used by Disconnect(), which disconnects minus resetting
99 // current_address_index_ and bind_address_.
100 void DoDisconnect();
101
102 void DidCompleteConnect(int result);
tbansalf82cc8e2015-10-14 20:05:49103 void DidCompleteRead(const CompletionCallback& callback, int result);
104 void DidCompleteWrite(const CompletionCallback& callback, int result);
[email protected]c9080d82013-09-15 15:14:16105 void DidCompleteReadWrite(const CompletionCallback& callback, int result);
106
107 int OpenSocket(AddressFamily family);
108
bmcquade8b62f472015-07-08 16:03:54109 // Emits histograms for TCP metrics, at the time the socket is
110 // disconnected.
111 void EmitTCPMetricsHistogramsOnDisconnect();
112
tbansal7b403bcc2016-04-13 22:33:21113 // Socket performance statistics (such as RTT) are reported to the
114 // |socket_performance_watcher_|. May be nullptr.
115 // |socket_performance_watcher_| is owned by |socket_|. If non-null,
116 // |socket_performance_watcher_| is guaranteed to be destroyed when |socket_|
117 // is destroyed.
118 SocketPerformanceWatcher* socket_performance_watcher_;
119
danakj655b66c2016-04-16 00:51:38120 std::unique_ptr<TCPSocket> socket_;
[email protected]c9080d82013-09-15 15:14:16121
122 // 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).
danakj655b66c2016-04-16 00:51:38124 std::unique_ptr<IPEndPoint> bind_address_;
[email protected]c9080d82013-09-15 15:14:16125
126 // The list of addresses we should try in order to establish a connection.
127 AddressList addresses_;
128
129 // Where we are in above list. Set to -1 if uninitialized.
130 int current_address_index_;
131
132 // External callback; called when connect is complete.
133 CompletionCallback connect_callback_;
134
135 // The next state for the Connect() state machine.
136 ConnectState next_connect_state_;
137
138 // This socket was previously disconnected and has not been re-connected.
139 bool previously_disconnected_;
140
141 // Record of connectivity and transmissions, for use in speculative connection
142 // histograms.
143 UseHistory use_history_;
144
ttuttle23fdb7b2015-05-15 01:28:03145 // Failed connection attempts made while trying to connect this socket.
146 ConnectionAttempts connection_attempts_;
147
tbansalf82cc8e2015-10-14 20:05:49148 // Total number of bytes received by the socket.
149 int64_t total_received_bytes_;
150
[email protected]c9080d82013-09-15 15:14:16151 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket);
152};
153
initial.commit586acc5fe2008-07-26 22:42:52154} // namespace net
155
[email protected]f7984fc62009-06-22 23:26:44156#endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_