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
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/address_list.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/net_export.h"
14 #include "net/log/net_log.h"
15 #include "net/socket/connection_attempts.h"
16 #include "net/socket/stream_socket.h"
17 #include "net/socket/tcp_socket.h"
21 // A client socket that uses TCP as the transport layer.
22 class NET_EXPORT TCPClientSocket
: public StreamSocket
{
24 // The IP address(es) and port number to connect to. The TCP socket will try
25 // each IP address in the list until it succeeds in establishing a
27 TCPClientSocket(const AddressList
& addresses
,
29 const net::NetLog::Source
& source
);
31 // Adopts the given, connected socket and then acts as if Connect() had been
32 // called. This function is used by TCPServerSocket and for testing.
33 TCPClientSocket(scoped_ptr
<TCPSocket
> connected_socket
,
34 const IPEndPoint
& peer_address
);
36 ~TCPClientSocket() override
;
38 // Binds the socket to a local IP address and port.
39 int Bind(const IPEndPoint
& address
);
41 // StreamSocket implementation.
42 int Connect(const CompletionCallback
& callback
) override
;
43 void Disconnect() override
;
44 bool IsConnected() const override
;
45 bool IsConnectedAndIdle() const override
;
46 int GetPeerAddress(IPEndPoint
* address
) const override
;
47 int GetLocalAddress(IPEndPoint
* address
) const override
;
48 const BoundNetLog
& NetLog() const override
;
49 void SetSubresourceSpeculation() override
;
50 void SetOmniboxSpeculation() override
;
51 bool WasEverUsed() const override
;
52 bool UsingTCPFastOpen() const override
;
53 void EnableTCPFastOpenIfSupported() override
;
54 bool WasNpnNegotiated() const override
;
55 NextProto
GetNegotiatedProtocol() const override
;
56 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
58 // Socket implementation.
59 // Multiple outstanding requests are not supported.
60 // Full duplex mode (reading and writing at the same time) is supported.
61 int Read(IOBuffer
* buf
,
63 const CompletionCallback
& callback
) override
;
64 int Write(IOBuffer
* buf
,
66 const CompletionCallback
& callback
) override
;
67 int SetReceiveBufferSize(int32 size
) override
;
68 int SetSendBufferSize(int32 size
) override
;
70 virtual bool SetKeepAlive(bool enable
, int delay
);
71 virtual bool SetNoDelay(bool no_delay
);
73 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
74 void ClearConnectionAttempts() override
;
75 void AddConnectionAttempts(const ConnectionAttempts
& attempts
) override
;
78 // State machine for connecting the socket.
80 CONNECT_STATE_CONNECT
,
81 CONNECT_STATE_CONNECT_COMPLETE
,
85 // State machine used by Connect().
86 int DoConnectLoop(int result
);
88 int DoConnectComplete(int result
);
90 // Helper used by Disconnect(), which disconnects minus resetting
91 // current_address_index_ and bind_address_.
94 void DidCompleteConnect(int result
);
95 void DidCompleteReadWrite(const CompletionCallback
& callback
, int result
);
97 int OpenSocket(AddressFamily family
);
99 // Emits histograms for TCP metrics, at the time the socket is
101 void EmitTCPMetricsHistogramsOnDisconnect();
103 scoped_ptr
<TCPSocket
> socket_
;
105 // Local IP address and port we are bound to. Set to NULL if Bind()
106 // wasn't called (in that case OS chooses address/port).
107 scoped_ptr
<IPEndPoint
> bind_address_
;
109 // The list of addresses we should try in order to establish a connection.
110 AddressList addresses_
;
112 // Where we are in above list. Set to -1 if uninitialized.
113 int current_address_index_
;
115 // External callback; called when connect is complete.
116 CompletionCallback connect_callback_
;
118 // The next state for the Connect() state machine.
119 ConnectState next_connect_state_
;
121 // This socket was previously disconnected and has not been re-connected.
122 bool previously_disconnected_
;
124 // Record of connectivity and transmissions, for use in speculative connection
126 UseHistory use_history_
;
128 // Failed connection attempts made while trying to connect this socket.
129 ConnectionAttempts connection_attempts_
;
131 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket
);
136 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_