blob: bf41545d180e3139783aaf60f722cdfc4848aa15 [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"
eroman87c53d62015-04-02 06:51:0717#include "net/log/net_log.h"
ttuttle23fdb7b2015-05-15 01:28:0318#include "net/socket/connection_attempts.h"
[email protected]c9080d82013-09-15 15:14:1619#include "net/socket/stream_socket.h"
20#include "net/socket/tcp_socket.h"
21
initial.commit586acc5fe2008-07-26 22:42:5222namespace net {
23
tbansal7b403bcc2016-04-13 22:33:2124class SocketPerformanceWatcher;
25
[email protected]c9080d82013-09-15 15:14:1626// A client socket that uses TCP as the transport layer.
[email protected]c9080d82013-09-15 15:14:1627class 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.
tbansal7b403bcc2016-04-13 22:33:2132 TCPClientSocket(
33 const AddressList& addresses,
danakj655b66c2016-04-16 00:51:3834 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
tbansal7b403bcc2016-04-13 22:33:2135 net::NetLog* net_log,
36 const net::NetLog::Source& source);
[email protected]c9080d82013-09-15 15:14:1637
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.
danakj655b66c2016-04-16 00:51:3840 TCPClientSocket(std::unique_ptr<TCPSocket> connected_socket,
[email protected]c9080d82013-09-15 15:14:1641 const IPEndPoint& peer_address);
42
dchengb03027d2014-10-21 12:00:2043 ~TCPClientSocket() override;
[email protected]c9080d82013-09-15 15:14:1644
45 // Binds the socket to a local IP address and port.
46 int Bind(const IPEndPoint& address);
47
48 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2049 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;
dchengb03027d2014-10-21 12:00:2059 void EnableTCPFastOpenIfSupported() override;
60 bool WasNpnNegotiated() const override;
61 NextProto GetNegotiatedProtocol() const override;
62 bool GetSSLInfo(SSLInfo* ssl_info) override;
[email protected]c9080d82013-09-15 15:14:1663
64 // Socket implementation.
65 // Multiple outstanding requests are not supported.
66 // Full duplex mode (reading and writing at the same time) is supported.
dchengb03027d2014-10-21 12:00:2067 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 Drissman13fc8932015-12-20 04:40:4673 int SetReceiveBufferSize(int32_t size) override;
74 int SetSendBufferSize(int32_t size) override;
[email protected]c9080d82013-09-15 15:14:1675
76 virtual bool SetKeepAlive(bool enable, int delay);
77 virtual bool SetNoDelay(bool no_delay);
78
ttuttle23fdb7b2015-05-15 01:28:0379 void GetConnectionAttempts(ConnectionAttempts* out) const override;
80 void ClearConnectionAttempts() override;
81 void AddConnectionAttempts(const ConnectionAttempts& attempts) override;
tbansalf82cc8e2015-10-14 20:05:4982 int64_t GetTotalReceivedBytes() const override;
ttuttle23fdb7b2015-05-15 01:28:0383
[email protected]c9080d82013-09-15 15:14:1684 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);
tbansalf82cc8e2015-10-14 20:05:49102 void DidCompleteRead(const CompletionCallback& callback, int result);
103 void DidCompleteWrite(const CompletionCallback& callback, int result);
[email protected]c9080d82013-09-15 15:14:16104 void DidCompleteReadWrite(const CompletionCallback& callback, int result);
105
106 int OpenSocket(AddressFamily family);
107
bmcquade8b62f472015-07-08 16:03:54108 // Emits histograms for TCP metrics, at the time the socket is
109 // disconnected.
110 void EmitTCPMetricsHistogramsOnDisconnect();
111
tbansal7b403bcc2016-04-13 22:33:21112 // 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
danakj655b66c2016-04-16 00:51:38119 std::unique_ptr<TCPSocket> socket_;
[email protected]c9080d82013-09-15 15:14:16120
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).
danakj655b66c2016-04-16 00:51:38123 std::unique_ptr<IPEndPoint> bind_address_;
[email protected]c9080d82013-09-15 15:14:16124
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
ttuttle23fdb7b2015-05-15 01:28:03144 // Failed connection attempts made while trying to connect this socket.
145 ConnectionAttempts connection_attempts_;
146
tbansalf82cc8e2015-10-14 20:05:49147 // Total number of bytes received by the socket.
148 int64_t total_received_bytes_;
149
[email protected]c9080d82013-09-15 15:14:16150 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket);
151};
152
initial.commit586acc5fe2008-07-26 22:42:52153} // namespace net
154
[email protected]f7984fc62009-06-22 23:26:44155#endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_