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 scoped_ptr
<TCPSocket
> socket_
;
101 // Local IP address and port we are bound to. Set to NULL if Bind()
102 // wasn't called (in that case OS chooses address/port).
103 scoped_ptr
<IPEndPoint
> bind_address_
;
105 // The list of addresses we should try in order to establish a connection.
106 AddressList addresses_
;
108 // Where we are in above list. Set to -1 if uninitialized.
109 int current_address_index_
;
111 // External callback; called when connect is complete.
112 CompletionCallback connect_callback_
;
114 // The next state for the Connect() state machine.
115 ConnectState next_connect_state_
;
117 // This socket was previously disconnected and has not been re-connected.
118 bool previously_disconnected_
;
120 // Record of connectivity and transmissions, for use in speculative connection
122 UseHistory use_history_
;
124 // Failed connection attempts made while trying to connect this socket.
125 ConnectionAttempts connection_attempts_
;
127 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket
);
132 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_