Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / socket / tcp_client_socket.h
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 bool WasNpnNegotiated() const OVERRIDE;
53   virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
54   virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
55
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;
65
66   virtual bool SetKeepAlive(bool enable, int delay);
67   virtual bool SetNoDelay(bool no_delay);
68
69  private:
70   // State machine for connecting the socket.
71   enum ConnectState {
72     CONNECT_STATE_CONNECT,
73     CONNECT_STATE_CONNECT_COMPLETE,
74     CONNECT_STATE_NONE,
75   };
76
77   // State machine used by Connect().
78   int DoConnectLoop(int result);
79   int DoConnect();
80   int DoConnectComplete(int result);
81
82   // Helper used by Disconnect(), which disconnects minus resetting
83   // current_address_index_ and bind_address_.
84   void DoDisconnect();
85
86   void DidCompleteConnect(int result);
87   void DidCompleteReadWrite(const CompletionCallback& callback, int result);
88
89   int OpenSocket(AddressFamily family);
90
91   scoped_ptr<TCPSocket> socket_;
92
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_;
96
97   // The list of addresses we should try in order to establish a connection.
98   AddressList addresses_;
99
100   // Where we are in above list. Set to -1 if uninitialized.
101   int current_address_index_;
102
103   // External callback; called when connect is complete.
104   CompletionCallback connect_callback_;
105
106   // The next state for the Connect() state machine.
107   ConnectState next_connect_state_;
108
109   // This socket was previously disconnected and has not been re-connected.
110   bool previously_disconnected_;
111
112   // Record of connectivity and transmissions, for use in speculative connection
113   // histograms.
114   UseHistory use_history_;
115
116   DISALLOW_COPY_AND_ASSIGN(TCPClientSocket);
117 };
118
119 }  // namespace net
120
121 #endif  // NET_SOCKET_TCP_CLIENT_SOCKET_H_