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. 4 5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_H_ 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_H_ 7 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" 17 18 namespace net { 19 20 // A client socket that uses TCP as the transport layer. 21 class NET_EXPORT TCPClientSocket : public StreamSocket { 22 public: 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 25 // connection. 26 TCPClientSocket(const AddressList& addresses, 27 net::NetLog* net_log, 28 const net::NetLog::Source& source); 29 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); 34 35 virtual ~TCPClientSocket(); 36 37 // Binds the socket to a local IP address and port. 38 int Bind(const IPEndPoint& address); 39 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 void EnableTCPFastOpenIfSupported() OVERRIDE; 53 virtual bool WasNpnNegotiated() const OVERRIDE; 54 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; 55 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 56 57 // Socket implementation. 58 // Multiple outstanding requests are not supported. 59 // Full duplex mode (reading and writing at the same time) is supported. 60 virtual int Read(IOBuffer* buf, int buf_len, 61 const CompletionCallback& callback) OVERRIDE; 62 virtual int Write(IOBuffer* buf, int buf_len, 63 const CompletionCallback& callback) OVERRIDE; 64 virtual int SetReceiveBufferSize(int32 size) OVERRIDE; 65 virtual int SetSendBufferSize(int32 size) OVERRIDE; 66 67 virtual bool SetKeepAlive(bool enable, int delay); 68 virtual bool SetNoDelay(bool no_delay); 69 70 private: 71 // State machine for connecting the socket. 72 enum ConnectState { 73 CONNECT_STATE_CONNECT, 74 CONNECT_STATE_CONNECT_COMPLETE, 75 CONNECT_STATE_NONE, 76 }; 77 78 // State machine used by Connect(). 79 int DoConnectLoop(int result); 80 int DoConnect(); 81 int DoConnectComplete(int result); 82 83 // Helper used by Disconnect(), which disconnects minus resetting 84 // current_address_index_ and bind_address_. 85 void DoDisconnect(); 86 87 void DidCompleteConnect(int result); 88 void DidCompleteReadWrite(const CompletionCallback& callback, int result); 89 90 int OpenSocket(AddressFamily family); 91 92 scoped_ptr<TCPSocket> socket_; 93 94 // Local IP address and port we are bound to. Set to NULL if Bind() 95 // wasn't called (in that case OS chooses address/port). 96 scoped_ptr<IPEndPoint> bind_address_; 97 98 // The list of addresses we should try in order to establish a connection. 99 AddressList addresses_; 100 101 // Where we are in above list. Set to -1 if uninitialized. 102 int current_address_index_; 103 104 // External callback; called when connect is complete. 105 CompletionCallback connect_callback_; 106 107 // The next state for the Connect() state machine. 108 ConnectState next_connect_state_; 109 110 // This socket was previously disconnected and has not been re-connected. 111 bool previously_disconnected_; 112 113 // Record of connectivity and transmissions, for use in speculative connection 114 // histograms. 115 UseHistory use_history_; 116 117 DISALLOW_COPY_AND_ASSIGN(TCPClientSocket); 118 }; 119 120 } // namespace net 121 122 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_H_ 123