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/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 ~TCPClientSocket() override
;
37 // Binds the socket to a local IP address and port.
38 int Bind(const IPEndPoint
& address
);
40 // StreamSocket implementation.
41 int Connect(const CompletionCallback
& callback
) override
;
42 void Disconnect() override
;
43 bool IsConnected() const override
;
44 bool IsConnectedAndIdle() const override
;
45 int GetPeerAddress(IPEndPoint
* address
) const override
;
46 int GetLocalAddress(IPEndPoint
* address
) const override
;
47 const BoundNetLog
& NetLog() const override
;
48 void SetSubresourceSpeculation() override
;
49 void SetOmniboxSpeculation() override
;
50 bool WasEverUsed() const override
;
51 bool UsingTCPFastOpen() const override
;
52 void EnableTCPFastOpenIfSupported() override
;
53 bool WasNpnNegotiated() const override
;
54 NextProto
GetNegotiatedProtocol() const override
;
55 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
57 // Socket implementation.
58 // Multiple outstanding requests are not supported.
59 // Full duplex mode (reading and writing at the same time) is supported.
60 int Read(IOBuffer
* buf
,
62 const CompletionCallback
& callback
) override
;
63 int Write(IOBuffer
* buf
,
65 const CompletionCallback
& callback
) override
;
66 int SetReceiveBufferSize(int32 size
) override
;
67 int SetSendBufferSize(int32 size
) override
;
69 virtual bool SetKeepAlive(bool enable
, int delay
);
70 virtual bool SetNoDelay(bool no_delay
);
73 // State machine for connecting the socket.
75 CONNECT_STATE_CONNECT
,
76 CONNECT_STATE_CONNECT_COMPLETE
,
80 // State machine used by Connect().
81 int DoConnectLoop(int result
);
83 int DoConnectComplete(int result
);
85 // Helper used by Disconnect(), which disconnects minus resetting
86 // current_address_index_ and bind_address_.
89 void DidCompleteConnect(int result
);
90 void DidCompleteReadWrite(const CompletionCallback
& callback
, int result
);
92 int OpenSocket(AddressFamily family
);
94 scoped_ptr
<TCPSocket
> socket_
;
96 // Local IP address and port we are bound to. Set to NULL if Bind()
97 // wasn't called (in that case OS chooses address/port).
98 scoped_ptr
<IPEndPoint
> bind_address_
;
100 // The list of addresses we should try in order to establish a connection.
101 AddressList addresses_
;
103 // Where we are in above list. Set to -1 if uninitialized.
104 int current_address_index_
;
106 // External callback; called when connect is complete.
107 CompletionCallback connect_callback_
;
109 // The next state for the Connect() state machine.
110 ConnectState next_connect_state_
;
112 // This socket was previously disconnected and has not been re-connected.
113 bool previously_disconnected_
;
115 // Record of connectivity and transmissions, for use in speculative connection
117 UseHistory use_history_
;
119 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket
);
124 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_