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/base/net_log.h"
15 #include "net/socket/stream_socket.h"
16 #include "net/socket/tcp_socket.h"
20 // A client socket that uses TCP as the transport layer.
21 class NET_EXPORT TCPClientSocket
: public StreamSocket
{
23 // The IP address(es) and port number to connect to. The TCP socket will try
24 // each IP address in the list until it succeeds in establishing a
26 TCPClientSocket(const AddressList
& addresses
,
28 const net::NetLog::Source
& source
);
30 // Adopts the given, connected socket and then acts as if Connect() had been
31 // called. This function is used by TCPServerSocket and for testing.
32 TCPClientSocket(scoped_ptr
<TCPSocket
> connected_socket
,
33 const IPEndPoint
& peer_address
);
35 virtual ~TCPClientSocket();
37 // Binds the socket to a local IP address and port.
38 int Bind(const IPEndPoint
& address
);
40 // StreamSocket implementation.
41 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
42 virtual void Disconnect() OVERRIDE
;
43 virtual bool IsConnected() const OVERRIDE
;
44 virtual bool IsConnectedAndIdle() const OVERRIDE
;
45 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
46 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
47 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
48 virtual void SetSubresourceSpeculation() OVERRIDE
;
49 virtual void SetOmniboxSpeculation() OVERRIDE
;
50 virtual bool WasEverUsed() const OVERRIDE
;
51 virtual bool UsingTCPFastOpen() const OVERRIDE
;
52 virtual bool WasNpnNegotiated() const OVERRIDE
;
53 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
54 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
56 // Socket implementation.
57 // Multiple outstanding requests are not supported.
58 // Full duplex mode (reading and writing at the same time) is supported.
59 virtual int Read(IOBuffer
* buf
, int buf_len
,
60 const CompletionCallback
& callback
) OVERRIDE
;
61 virtual int Write(IOBuffer
* buf
, int buf_len
,
62 const CompletionCallback
& callback
) OVERRIDE
;
63 virtual int SetReceiveBufferSize(int32 size
) OVERRIDE
;
64 virtual int SetSendBufferSize(int32 size
) OVERRIDE
;
66 virtual bool SetKeepAlive(bool enable
, int delay
);
67 virtual bool SetNoDelay(bool no_delay
);
70 // State machine for connecting the socket.
72 CONNECT_STATE_CONNECT
,
73 CONNECT_STATE_CONNECT_COMPLETE
,
77 // State machine used by Connect().
78 int DoConnectLoop(int result
);
80 int DoConnectComplete(int result
);
82 // Helper used by Disconnect(), which disconnects minus resetting
83 // current_address_index_ and bind_address_.
86 void DidCompleteConnect(int result
);
87 void DidCompleteReadWrite(const CompletionCallback
& callback
, int result
);
89 int OpenSocket(AddressFamily family
);
91 scoped_ptr
<TCPSocket
> socket_
;
93 // Local IP address and port we are bound to. Set to NULL if Bind()
94 // wasn't called (in that case OS chooses address/port).
95 scoped_ptr
<IPEndPoint
> bind_address_
;
97 // The list of addresses we should try in order to establish a connection.
98 AddressList addresses_
;
100 // Where we are in above list. Set to -1 if uninitialized.
101 int current_address_index_
;
103 // External callback; called when connect is complete.
104 CompletionCallback connect_callback_
;
106 // The next state for the Connect() state machine.
107 ConnectState next_connect_state_
;
109 // This socket was previously disconnected and has not been re-connected.
110 bool previously_disconnected_
;
112 // Record of connectivity and transmissions, for use in speculative connection
114 UseHistory use_history_
;
116 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket
);
121 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_